MY START to a COMPILER

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

Post Reply
TRANERAECK
Coder
Posts: 29
Joined: Thu Mar 24, 2011 3:08 am

MY START to a COMPILER

Post by TRANERAECK »

Well I said I would post my compiler. It has been a while and I thought this is my start of one so why not?

Code: Select all

DECLARE SUB OBTAINCHARACTER (initIndex AS INTEGER)
DECLARE SUB KEYWORD.PARSE (initIndex AS INTEGER, NEWVARIABLE AS STRING_
                           REMCONDITION AS STRING, CASEWORD AS STRING)
DECLARE SUB VALUE.REM (REMCONDITION AS STRING, CASEWORD AS STRING)
DECLARE SUB VALUE.VARIABLE(CASEWORD AS STRING, VARERROR AS INTEGER_
                           NEWVARIABLE AS STRING)

REM SMALL MENU FOR CHOICE OF WHAT TO DO (FONT EDITOR)
REM CONNECT PATH TO FILE
REM NEED A WHILE STATEMENT
CALL OBTAINCHARACTER (initIndex)
CALL KEYWORD.PARSE (initIndex, NEWVARIABLE$, REMCONDITION$, CASEWORD$)
IF REMCONDITION$ = "True" THEN CALL VALUE.REM(REMCONDITION$, CASEWORD$)
IF CASEWORD$ = "VARIABLE" THEN CALL VALUE.VARIABLE(CASEWORD$,_
                                                     VARERROR%_
                                                     NEWVARIABLE$)
CALL ERRORHANDLER(VARERROR%)
REM WEND
REM ALGORITHM
REM CODE GENERATOR
REM COMPILER FOR HEX (done)
END

SUB OBTAINCHARACTER (tknIndex AS INTEGER)
   OPEN FileName$ FOR INPUT AS #1
      tknIndex = ASC(INPUT$(1, 1))
   CLOSE #1
END SUB

SUB KEYWORD.PARSE (tknIndex AS INTEGER, USERVARIABLE AS STRING_
                   REMSTATEMENT AS STRING, TOKEN AS STRING)
   CASECHARACTER$ = "NEARCASE"
   STRINGPOINTER% = 2
   DIM TKNNAME$(12)
   IF STR(tknIndex) = CHR$(10) THEN
      TOKEN$ = CHR$(10)
      EXIT SUB
   END IF
   IF STR(tknIndex) = "_" THEN
      CONTINUESTRING$ = "True"
      TOKEN$ = "_"
      EXIT SUB
    ELSE
      CONTINUESTRING$ = "False"
   END IF
   IF STR(tknIndex) = CHR$(13) THEN
      TOKEN$ = CHR$(13)
      EXIT SUB
   END IF
   IF STR(tknIndex) = "'" THEN
      REMSTATEMENT$ = "True"
    ELSE
      REMSTATEMENT$ = "False"
   END IF
   
   ' Define command as TOKEN$

   OPEN "FileName$" FOR INPUT AS #1
      FOR y% = 1 TO STRINGPOINTER%
         TKNNAME$(y%) = INPUT$(1, 1)
         IF TKNNAME$(y%) > CHR$(64) AND TKNNAME$(y%) <CHR> CHR$(96) AND TKNNAME$(y%) < CHR$(123) THEN
            INCR STRINGPOINTER%
            TOKEN$ = TOKEN$ + TKNNAME$(y%)
         END IF
	 IF TKNNAME$(y%) = CHR$(34) THEN
            INCR STRINGPOINTER%
            TOKEN$ = TOKEN$ + TKNNAME$(y%)
	 IF CHR$(13) OR CHR$(10) = TKNNAME$(y%) AND_
              CONTINUESTRING$ = "False" THEN EXIT FOR
      NEXT y%
      IF TKNNAME$(y%) = " " OR TKNNAME$(y%) = "(" THEN_
           CASECHARACTER$ = "FARCASE"
   CLOSE #1
   STYLECASE$ = LCASE$(TOKEN$)
   IF STYLECASE$ = "asm" THEN CALL KEYWORD.asm(tknIndex)
   IF STYLECASE$ = "call" THEN CALL KEYWORD.call
   IF BUDGET% = 5 THEN USERVARIABLE$ = TOKEN$
END SUB

SUB VALUE.REM(REMSTATEMENT AS STRING, TOKEN AS STRING)
   QUOTETH$ = TOKEN$
   OPEN FileName$ FOR INPUT AS #1
      TEMP$ = ":"
      WHILE TEMP$ <> CHR$(10)
         TEMP$ = INPUT$(1, 1)
         IF TEMP$ <> CHR$(10) THEN QUOTETH$ = QUOTETH$ + TEMP$
      WEND
   CLOSE #1
END SUB

SUB VALUE.VARIABLE(TOKEN AS STRING, ERRSTATEVAR AS INTEGER_
                   USERVARIABLE AS STRING)
   WHILE TOKEN$ = USERVARIABLE$
      OPEN FileName$ FOR INPUT AS #1
         GRABCHARACTER$ = INPUT$(1, 1)
         IF "=" OR "(" OR CHR$(13) OR "A" OR "a" <> GRABCHARACTER$ THEN
            COLOR 5, 0, 0
            PRINT "STOPPED COMPILING!!!!"
            ERRSTATEVAR = 03
         END IF
      CLOSE #1
      TOKEN$ = GRABCHARACTER$
   WEND
END SUB

SUB ERRORHANDLER(ERRSTATEVAR AS INTEGER)
   IF ERRSTATEVAR% = 03 THEN_
        PRINT "ERROR! USERVARIABLE NOT DEFINED!!!!";
END SUB

:roll: HOPE YOU like it. Could use a little help with an algorithm I have one not sure about the CODE generation either.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

What are you compiling? It looks like you have a LONG way to go...

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
TRANERAECK
Coder
Posts: 29
Joined: Thu Mar 24, 2011 3:08 am

Post by TRANERAECK »

I was posting my start to a compiler this is just a rough outline. I am following web information that is under education at colleges. They say there is four parts to a compiler on a four pass compiler. They are:
- Preprocessor
- Lexical Analyzer
- Parser
- Code generator

This allows you to create a compiler. I am TRYING to make a BASIC compiler in Quick BASIC. I am using the Quick BASIC found at QBCAFE under rpg games the first listing is a text dungeon and that has Quick BASIC in it. I like that Quick BASIC because it seems to be accurate. It does have multiple module compilation. I am going to make my compiler compile itself. The commands are listed in the KEYWORD.PARSE subroutine with subroutines flowing from that. I have the purple dragon ebook and hope to gain knowledge on the code generator; I also have the red dragon book. The HEX code THING I mention is translating a HEX in four digits that would be made by CVI 2 byte string to integer and then I use HEX on the integer and come up with a four digit HEX. I then take 2 digits and use 16 * 16 and come up with 256 characters or the ASCII amount for a character I also take the last 2 digits and do the same. When I do this it is 2 characters I made with CVI type of compiler. It used to work on DOS 2.10 on my TANDY 1000 and now with the registry I need to be a little more complex about how the compiler compiles a BASIC code so it runs from machine language. I sure hope that code generation is possible I only have a start to a parser that should read all the code that BASIC has. I will include statements to discuss any error in the subroutines and then have the error handler explain the error. It will have a WHILE statement for length of file of the source PROGRAM in the main body of the compiler; where I make the rem while. I also plan on using an algorithm as I found multiple ebooks on that subject for a compiler. One of the ebooks is ALGORITHMS FOR COMPILERS. I plan on inserting into the compiler a command called $MEMORY some type of meta command that uses xms; extended memory written out in QUICKBASIC or the BASIC I am making. I found BLUNTAXEBASIC downloads and in them is some assembly in assembly I have the tool that converts assembly to QUICKBASIC. There is also the question of some way to do the screen and a font how to make that appear to the code to be there. I can't just say SCREEN and the mode I have to program the SCREEN in along with a font. So I came up with the idea that I will make a menu and have a FONT EDITOR as a choice to the menu. This will also allow another FONT maybe another COUNTRIES language to be expressed if they can devise a way to program the compiler with it. I don't know how that happens because right now all the cource code for any compiler you find is wrong with some kind of error attached in it that chewed the source up. For example the compilers I am looking at are on qb45.com I downloaded J something under miscellaneous and it is a small compiler. The rest of languages on the web had to have come from BASIC well I made a compiler and other languages on my TANDY problem there is no hard drive or 3.5 floppy I formatted my 5.25 floppy real big I had some type of modem from it being internal because I ran some software and was on the billboards self made software worked. It is ok that I needed an access telephone number but it was an internal modem. There is also the problem that I saved my things in 1990 system had real standard drives on the EDUCATION and other COUNTRY computers and I transferred my work. I see that this windows computer would not have ever read a 720KB 3.5 floppy and how are you supposed to use QUICKBASIC when the free memory for any type of file is limited. The QUICKBASIC everyone uses only has 32000 free memory bytes for variables. If they made the QUICK BASIC the way I did on my TANDY the compiler isn't worth doohicky. It is limited by memory I sure as had no knowledge about extended memory or how to make the memory bigger. ERASE is to clear the variable and that is what I used so as not to use memory compiling the whole source program. Also the TANDY 1000 original I had had 1024 KILOBYTES of memory RAM. If we would go back and make the compilers again then we would have awesome BASIC. In BASIC you can make any size window you want so you can define the pixel. Also the TANDY 1000 had 73 characters across and 25 down. I designed my 2.11 DOS with BASIC to contain 80 characters and I couldn't figure out how many down it was supposed to be because when looking at the screen choices they are 320x200 or times 2, 640x400 that is 25 down at a pixel font 16 I didn't know make the pixel smaller then one pixel other then window and view commands. I hope that clarifies the situation. I posted all my stuff you can see some of what it is by seeing if you can download busstop os it is a busstop and there is a hobo that was my OS I had on my tandy the problem is I saved in Germany they won't let me have the full copy because I cannot install it all the way because they translated some of my source and it comes out with WEIRD characters. Or the source has an error because I used an internal modem. Also if the compiler is to work correctly it needs to operate in windows boundaries. I know that if you open an exe in WINDOWS notepad it says cannot be run in dos mode. This is strange the whole computer has DOS either an MSDOS or DOS which is command and there is A COMMANDPROMPT; under accesories. At the least cheesecake basic is making an attempt at a compiler that compiles itself in QUICKBASIC. Check it out look how there is no way for the file to include anything but the main file. The parser is referenced in the main file but not included. If you include another module in the main module it cannot have subs or functions and it will run where included. To pass a parameter to the module just define the variable however you want then use the same variable in the other module. My approach to a compiler is unusual and I hope that it would work. I have a compiler in VBASIC 6.0 it is an assembly compiler and can be compiled then compiles assembly I checked it out and it includes some file not used in the program. It is like constant standards or something. I also remembered I didn't think to make a font for use when I tried programming my DOS for my TANDY I did make a font but not for the DOS it was hard enough to make the DOS to begin with because I checked the DOS 2.10 for anything unusual and it had )> as a command then type anything I wanted my DOS to have )> and managed to type in that machine language access. Hope I cleared up some kind of wondering and I hope you understand this compiler I make is not a joke it is listed in a bunch of educational colleges for modern compiler design that this is how it is done. No I am not in college for this or do I have the source code for any compiler from a college to follow that says it is complete. They won't let you just jump on there FTP site and download it. I find that these web sites are secured. I need a password or to belong to the server for the college. I currently have sql 2005 server and am trying to get it to work but found to make a web page you have to go through xml to post it to the web. I can't include some of my files here as I currently would like I have anything and everything on my computer. I am on a DC7700 HP-COMPAQ and have two SATA hard drive one that was bought and the other was included with the computer 80 GB and 500 GB. I am happy with that I had a hard time installing the second drive but managed to do so. I have also reinstalled windows xp sp3 pro edition and wound up installing the driver system that is deleted only looking for a way to install a gamepad to check it out with this joystick ideas. I found some joystick ideas on an xbox web site and it could be the answer to all the buttons, digital joystick units, and everything. I found that of all the program I downloaded at qbcafe NFL.BAS works as is. Like it but I had NFL on my TANDY when I programmed it and am happy I still have my handwritten WAY to play from when I was 7-8 years old. My older brother always left to go play strat football and I was mad so at 7-8 years old made my own strat football. With the NFL.BAS file I can finish my NFL game. It figures out percents or values to 100 for players. I also made a BASEBALL game that looked like nintendo BASEBALL NES in basic. I also made KABOOM and a DONKEYKONG. In PASCAL I made a BURGERTIME and a DIGDUG myway same game didn't know all the boards, or screens. Well that wraps it up somewhere out there is some of my stuff maybe unless it got wiped. No matter the stuff is meant for DOS 2.10/2.11! I plan on making all new stuff on this windows xp. IF you need to see some of the information I collected over the last five years and would like to get it I think just make a post I can see if some of the stuff was never deleted so I can give you the website where I found it. :?
Post Reply