BASIC Interpreter Source Code 
Author Message
 BASIC Interpreter Source Code

I am wanting to write an interpreted langauge similar to BASIC with a
number of additional features found in some other languages.  But there
is a problem I am faced with in that certain parts of this project are
somewhat difficult for me to grasp.  Does anyone know where I can find
source code to a BASIC Interpreter, preferably written in BASIC itself,
however C would work also.  Documented source is always appreciated.

If not, could anyone help with the expression analysis code? For
instance, in a line such as:

X = (3-2)/4+Z*RND(1)

I understand that a stack is in order here, however I am unsure exactly
how to implement it.  If anyone has any good ideas for this project I
would certainly appreciate it.  Please send your response via Email.

Thanks



Wed, 29 Sep 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Scott,
  Check out ARDAF211.ZIP at ftp.global2000.net/pub/pbasic

 James

Quote:

>I am wanting to write an interpreted langauge similar to BASIC with a
>number of additional features found in some other languages.  But there
>is a problem I am faced with in that certain parts of this project are
>somewhat difficult for me to grasp.  Does anyone know where I can find
>source code to a BASIC Interpreter, preferably written in BASIC itself,
>however C would work also.  Documented source is always appreciated.
>If not, could anyone help with the expression analysis code? For
>instance, in a line such as:
>X = (3-2)/4+Z*RND(1)
>I understand that a stack is in order here, however I am unsure exactly
>how to implement it.  If anyone has any good ideas for this project I
>would certainly appreciate it.  Please send your response via Email.
>Thanks



Wed, 29 Sep 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Quote:

> somewhat difficult for me to grasp.  Does anyone know where I can find
> source code to a BASIC Interpreter, preferably written in BASIC itself,
> however C would work also.  Documented source is always appreciated.

I think Gunnar shows his GB alpha C source

http://www.hof.de/~gbasic/

--
http://www.geocities.com/SiliconValley/Lakes/7784



Wed, 29 Sep 1999 03:00:00 GMT  
 BASIC Interpreter Source Code


   >Newsgroups: comp.lang.basic.misc

   >If not, could anyone help with the expression analysis code? For
   >instance, in a line such as:
   >X = (3-2)/4+Z*RND(1)

   >I understand that a stack is in order here, however I am unsure
   >exactly how to implement it.  If anyone has any good ideas for this
   >project I would certainly appreciate it.  Please send your response
   >via Email.

   Jack Cranshaws compiler tutor:

   http:/iecc.com/compilers/crenshaw/

   He discusses "recursive descent" parsing, which is probably right up your
alley.

   There is also the simple-to-use binary tree method, but it is much less
extensible and begins making your compiler bloat quickly as you add more
and more operators or datatypes.


--: COMPUTERVREDEBREUK

Net-Tamer V 1.08 - Test Drive



Thu, 30 Sep 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Quote:

>   Jack Cranshaws compiler tutor:
>   http:/iecc.com/compilers/crenshaw/
>   He discusses "recursive descent" parsing, which is probably right up your
>alley.
>   There is also the simple-to-use binary tree method, but it is much less
>extensible and begins making your compiler bloat quickly as you add more
>and more operators or datatypes.

>--: COMPUTERVREDEBREUK

He that was a nice page. I downloaded the whole tuturial. Since it is
in Pascal I am going to port the code to QBASIC and ASM. Currently
I have done lesson 1. If someone else has done something like it
( could also be other basics as qb45 or pb )please let me, us, know
that. I will make my translattions available on my homepage, but here
is the first part:
DECLARE SUB INIT ()
DECLARE SUB GETCHAR ()
DECLARE SUB ERRORHANDLING (ERRS$)
DECLARE SUB ABORT (ABORTSTR$)
DECLARE SUB EXPECTED (EXPECTSTR$)
DECLARE FUNCTION ISALPHA% (CHAR$)
DECLARE FUNCTION ISDIGIT% (CHAR$)
DECLARE FUNCTION GETNAME$ ()
DECLARE FUNCTION GETNUMBER$ ()
DECLARE SUB EMIT (EMITSTR$)
DEFINT A-Z: CLS

'compiler-lesson 1 by Jack. Crenshaw translated to QBASIC by Rick.

'CONSTANTS

CONST TABS = 9
CONST BEL = 7

'GLOBAL VARIABLES
DIM SHARED INCHAR$

'MAIN PROGRAM:
CALL INIT

SUB ABORT (ABORTSTR$)
CALL ERRORHANDLING(ABORTSTR$)
END
END SUB

SUB EMIT (EMITSTR$)
PRINT CHR$(9); EMITSTR$
END SUB

SUB EMITLN (EMITLNSTR$)

CALL EMIT(EMITLNSTR$)
PRINT :

END SUB

SUB ERRORHANDLING (ERRS$)
PRINT :
PRINT CHR$(7); "ERROR: "; ERRS$
END SUB

SUB EXPECTED (EXPECTSTR$)
CALL ABORT(EXPECTSTR$ + " EXPECTED ")
END SUB

SUB GETCHAR

DO
  I$ = INKEY$
  INCHAR$ = LEFT$(I$, 1)
LOOP UNTIL I$ <> ""
END SUB

FUNCTION GETNAME$
IF ISALPHA(INCHAR$) THEN
  GETNAME$ = INCHAR$
  CALL GETCHAR
ELSE
  CALL EXPECTED("NAME")
END IF

END FUNCTION

FUNCTION GETNUMBER$
IF ISDIGIT(INCHAR$) THEN
  GETNUMBER$ = INCHAR$
  CALL GETCHAR
ELSE
  CALL EXPECTED("BYTE-INTEGER")
END IF

END FUNCTION

DEFSNG A-Z
SUB INIT
CALL GETCHAR
END SUB

DEFINT A-Z
FUNCTION ISALPHA (CHAR$)
CHECKIT$ = UCASE$(CHAR$)
IF CHECKIT$ >= "A" AND CHECKIT$ <= "Z" THEN ISALPHA = 1

END FUNCTION

FUNCTION ISDIGIT (CHAR$)

IF CHAR$ >= "0" AND CHAR$ <= "9" THEN ISDIGIT = 1

END FUNCTION

SUB MATCH (MATCHSTR$)
IF INCHAR$ = MATCHSTR$ THEN
  CALL GETCHAR
ELSE
  CALL EXPECTED("" + MATCHSTR$ + "")
END IF
END SUB

'Well that was easy..

RIck



Fri, 01 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

And lesson two with the expression parser too( I think he got me ;)
A few questions:
1) Any comments on my QBASIC/MASM translation of his
pascal\motorola statements ?
2)Does anybody knows the e-mail of this guy ? I wanna get permission
to upload this things on web
3)Anybody interested in getting together in this one ?
( still 10 lessons to go...). Might be in PB/QB or QBASIC...

Rick[Jesus this guy is making it seem so easy....after 10 years of
study!]

DECLARE FUNCTION ISADDOP% (CHAR$)
DECLARE SUB MULTIPLY ()
DECLARE SUB DIVIDE ()
DECLARE SUB FACTOR ()
DECLARE SUB EXPRESSION ()
DECLARE SUB SUBTRACT ()
DECLARE SUB MATCH (MATCHSTR$)
DECLARE SUB ADD ()
DECLARE SUB TERM ()
DECLARE SUB EMITLN (EMITLNSTR$)
DECLARE SUB INIT ()
DECLARE SUB GETCHAR ()
DECLARE SUB ERRORHANDLING (ERRS$)
DECLARE SUB ABORT (ABORTSTR$)
DECLARE SUB EXPECTED (EXPECTSTR$)
DECLARE FUNCTION ISALPHA% (CHAR$)
DECLARE FUNCTION ISDIGIT% (CHAR$)
DECLARE FUNCTION GETNAME$ ()
DECLARE FUNCTION GETNUMBER$ ()
DECLARE SUB EMIT (EMITSTR$)
DEFINT A-Z: CLS

'compiler-lesson 1 by Jack. Crenshaw translated to QBASIC by Rick.
'lesson 2 added expression

'CONSTANTS

CONST TABS = 9
CONST BEL = 7

'GLOBAL VARIABLES
DIM SHARED INCHAR$

'MAIN PROGRAM:
CALL INIT
CALL EXPRESSION       'AS OF LESSON 2

SUB ABORT (ABORTSTR$)
CALL ERRORHANDLING(ABORTSTR$)
END
END SUB

DEFSNG A-Z
SUB ADD
CALL MATCH("+")
CALL TERM
EMITLN ("POP AX")
EMITLN ("ADD AX, CX")

END SUB

SUB DIVIDE
CALL MATCH("/")
CALL FACTOR
EMITLN ("POP AX ")
EMITLN ("DIV CX ")

END SUB

DEFINT A-Z
SUB EMIT (EMITSTR$)
PRINT CHR$(9); EMITSTR$
END SUB

SUB EMITLN (EMITLNSTR$)

CALL EMIT(EMITLNSTR$)
PRINT :

END SUB

SUB ERRORHANDLING (ERRS$)
PRINT :
PRINT CHR$(7); "ERROR: "; ERRS$
END SUB

SUB EXPECTED (EXPECTSTR$)
CALL ABORT(EXPECTSTR$ + " EXPECTED ")
END SUB

SUB EXPRESSION

IF ISADDOP(INCHAR$) THEN
  EMITLN ("XOR CX, CX ")
ELSE
  CALL TERM
END IF

DO WHILE ISADDOP(INCHAR$)

  EMITLN ("PUSH CX")

  SELECT CASE INCHAR$

    CASE "-"
    CALL SUBTRACT

    CASE "+"
    CALL ADD

    CASE ELSE
    CALL EXPECTED("ADDOP")

  END SELECT

LOOP

DEFSNG A-Z
SUB FACTOR

IF INCHAR$ = "(" THEN
  CALL MATCH("(")
  CALL EXPRESSION
  CALL MATCH(")")
ELSE
  EMITLN ("MOV CX, " + GETNUMBER$)
END IF

END SUB

DEFINT A-Z
SUB GETCHAR

DO
  I$ = INKEY$
  INCHAR$ = LEFT$(I$, 1)
LOOP UNTIL I$ <> ""
END SUB

FUNCTION GETNAME$
IF ISALPHA(INCHAR$) THEN
  GETNAME$ = INCHAR$
  CALL GETCHAR
ELSE
  CALL EXPECTED("NAME")
END IF

END FUNCTION

FUNCTION GETNUMBER$
IF ISDIGIT(INCHAR$) THEN
  GETNUMBER$ = INCHAR$
  CALL GETCHAR
ELSE
  CALL EXPECTED("BYTE-INTEGER")
END IF

END FUNCTION

DEFSNG A-Z
SUB INIT
CALL GETCHAR
END SUB

DEFINT A-Z
FUNCTION ISADDOP (CHAR$)

IF CHAR$ = "-" OR CHAR$ = "+" THEN ISADDOP = 1

END FUNCTION

FUNCTION ISALPHA (CHAR$)
CHECKIT$ = UCASE$(CHAR$)
IF CHECKIT$ >= "A" AND CHECKIT$ <= "Z" THEN ISALPHA = 1

END FUNCTION

FUNCTION ISDIGIT (CHAR$)

IF CHAR$ >= "0" AND CHAR$ <= "9" THEN ISDIGIT = 1

END FUNCTION

SUB MATCH (MATCHSTR$)
IF INCHAR$ = MATCHSTR$ THEN
  CALL GETCHAR
ELSE
  CALL EXPECTED("" + MATCHSTR$ + "")
END IF
END SUB

DEFSNG A-Z
SUB MULTIPLY
CALL MATCH("*")
CALL FACTOR
EMITLN ("POP AX ")
EMITLN ("MUL CX ")
END SUB

SUB SUBTRACT

CALL MATCH("-")
CALL TERM
CALL EMITLN("POP AX ")
CALL EMITLN("SUB AX,CX ")
END SUB

SUB TERM

CALL FACTOR

DO WHILE INCHAR$ = "*" OR INCHAR$ = "/"

  EMITLN ("PUSH CX")

  SELECT CASE INCHAR$

  CASE "*"
  CALL MULTIPLY

  CASE "/"
  CALL DIVIDE

  CASE ELSE
  CALL EXPECTED("MULOP")

  END SELECT

LOOP

END SUB



Sat, 02 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

And part 3 which finishes indeed the expression parser !
I hope this is enough so that everybody interested can
translate pascal and motorola from the tutorial to qbasic /masm.
I think I have really do the rest on my homepage( I can hear them
already complaining!)
Have fun,
RIck

DECLARE SUB SKIPWHITE ()
DECLARE FUNCTION ISWHITE% (CHAR$)
DECLARE FUNCTION ISALNUM% (CHAR$)
DECLARE SUB ASSIGNMENT ()
DECLARE SUB IDENT ()
DECLARE FUNCTION ISADDOP% (CHAR$)
DECLARE SUB MULTIPLY ()
DECLARE SUB DIVIDE ()
DECLARE SUB FACTOR ()
DECLARE SUB EXPRESSION ()
DECLARE SUB SUBTRACT ()
DECLARE SUB MATCH (MATCHSTR$)
DECLARE SUB ADD ()
DECLARE SUB TERM ()
DECLARE SUB EMITLN (EMITLNSTR$)
DECLARE SUB INIT ()
DECLARE SUB GETCHAR ()
DECLARE SUB ERRORHANDLING (ERRS$)
DECLARE SUB ABORT (ABORTSTR$)
DECLARE SUB EXPECTED (EXPECTSTR$)
DECLARE FUNCTION ISALPHA% (CHAR$)
DECLARE FUNCTION ISDIGIT% (CHAR$)
DECLARE FUNCTION GETNAME$ ()
DECLARE FUNCTION GETNUMBER$ ()
DECLARE SUB EMIT (EMITSTR$)
DEFINT A-Z: CLS

'compiler-lesson 1 by Jack. Crenshaw translated to QBASIC by Rick.
'lesson 2 added expression
'lesson 3 added assignment! (already)

'CONSTANTS

CONST TABS = 9
CONST BEL = 7
CONST CR = 13
'GLOBAL VARIABLES
DIM SHARED INCHAR$

'MAIN PROGRAM:
CALL INIT
CALL ASSIGNMENT       'AS OF LESSON 2
IF INCHAR$ <> CHR$(CR) THEN CALL EXPECTED("NEWLINE ")    'AS OF LESSON
3

SUB ABORT (ABORTSTR$)
CALL ERRORHANDLING(ABORTSTR$)
END
END SUB

DEFSNG A-Z
SUB ADD
CALL MATCH("+")
CALL TERM
EMITLN ("POP AX")
EMITLN ("ADD AX, CX")

END SUB

DEFINT A-Z
SUB ASSIGNMENT
NAME$ = GETNAME$
CALL MATCH("=")
CALL EXPRESSION
CALL EMITLN("LEA BX, " + NAME$)
CALL EMITLN("MOV [BX], AX")

END SUB

DEFSNG A-Z
SUB DIVIDE
CALL MATCH("/")
CALL FACTOR
EMITLN ("POP AX ")
EMITLN ("DIV CX ")

END SUB

DEFINT A-Z
SUB EMIT (EMITSTR$)
PRINT CHR$(9); EMITSTR$
END SUB

SUB EMITLN (EMITLNSTR$)

CALL EMIT(EMITLNSTR$)
PRINT :

END SUB

SUB ERRORHANDLING (ERRS$)
PRINT :
PRINT CHR$(7); "ERROR: "; ERRS$
END SUB

SUB EXPECTED (EXPECTSTR$)
CALL ABORT(EXPECTSTR$ + " EXPECTED ")
END SUB

SUB EXPRESSION

IF ISADDOP(INCHAR$) THEN
  EMITLN ("XOR CX, CX ")
ELSE
  CALL TERM
END IF

DO WHILE ISADDOP(INCHAR$)

  EMITLN ("PUSH CX")

  SELECT CASE INCHAR$

    CASE "-"
    CALL SUBTRACT

    CASE "+"
    CALL ADD

    CASE ELSE
    CALL EXPECTED("ADDOP")

  END SELECT

LOOP
END SUB

DEFSNG A-Z
SUB FACTOR

IF INCHAR$ = "(" THEN
  CALL MATCH("(")
  CALL EXPRESSION
  CALL MATCH(")")
ELSEIF ISALPHA(INCHAR$) THEN
  CALL IDENT
ELSE
  EMITLN ("MOV CX, " + GETNUMBER$)
END IF

END SUB

DEFINT A-Z
SUB GETCHAR

DO
  I$ = INKEY$
  INCHAR$ = LEFT$(I$, 1)
LOOP UNTIL I$ <> ""
END SUB

FUNCTION GETNAME$

TOKEN$ = ""
IF ISALPHA(INCHAR$) = 0 THEN CALL EXPECTED("NAME")
DO WHILE ISALNUM(INCHAR$)
  TOKEN$ = TOKEN$ + UCASE$(INCHAR$)
  CALL GETCHAR
LOOP
GETNAME$ = TOKEN$
CALL SKIPWHITE

END FUNCTION

FUNCTION GETNUMBER$

VALUE$ = ""
IF ISDIGIT(INCHAR$) = 0 THEN CALL EXPECTED("BYTE-INTEGER")
DO WHILE ISDIGIT(INCHAR$)
  VALUE$ = VALUE$ + INCHAR$
  CALL GETCHAR
LOOP

GETNUMBER$ = VALUE$
CALL SKIPWHITE

END FUNCTION

SUB IDENT

NAME$ = GETNAME$                   'THIS LIMITED TO 8 CHARS

IF INCHAR$ = "(" THEN
  CALL MATCH("(")
  CALL MATCH(")")
  EMITLN ("CALL " + NAME$)
ELSE
  EMITLN ("MOV CX," + NAME$)
END IF

END SUB

DEFSNG A-Z
SUB INIT

CALL GETCHAR
CALL SKIPWHITE

END SUB

DEFINT A-Z
FUNCTION ISADDOP (CHAR$)

IF CHAR$ = "-" OR CHAR$ = "+" THEN ISADDOP = 1

END FUNCTION

FUNCTION ISALNUM (CHAR$)
IF ISDIGIT(CHAR$) OR ISALPHA(CHAR$) THEN ISALNUM = 1

END FUNCTION

FUNCTION ISALPHA (CHAR$)
CHECKIT$ = UCASE$(CHAR$)
IF CHECKIT$ >= "A" AND CHECKIT$ <= "Z" THEN ISALPHA = 1

END FUNCTION

FUNCTION ISDIGIT (CHAR$)

IF CHAR$ >= "0" AND CHAR$ <= "9" THEN ISDIGIT = 1

END FUNCTION

FUNCTION ISWHITE (CHAR$)
IF CHAR$ = CHR$(32) OR CHAR$ = CHR$(9) THEN ISWHITE = 1
END FUNCTION

SUB MATCH (MATCHSTR$)
IF INCHAR$ <> MATCHSTR$ THEN
  CALL EXPECTED("" + MATCHSTR$ + "")
ELSE
  CALL GETCHAR
  CALL SKIPWHITE
END IF
END SUB

DEFSNG A-Z
SUB MULTIPLY
CALL MATCH("*")
CALL FACTOR
EMITLN ("POP AX ")
EMITLN ("MUL CX ")
END SUB

DEFINT A-Z
SUB SKIPWHITE
DO WHILE ISWHITE(INCHAR$)
  CALL GETCHAR
LOOP

END SUB

DEFSNG A-Z
SUB SUBTRACT

CALL MATCH("-")
CALL TERM
CALL EMITLN("POP AX ")
CALL EMITLN("SUB AX,CX ")
END SUB

SUB TERM

CALL FACTOR

DO WHILE INCHAR$ = "*" OR INCHAR$ = "/"

  EMITLN ("PUSH AX")

  SELECT CASE INCHAR$

  CASE "*"
  CALL MULTIPLY

  CASE "/"
  CALL DIVIDE

  CASE ELSE
  CALL EXPECTED("MULOP")

  END SELECT

LOOP

END SUB



Sat, 02 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

As usual I included buggie ;). But it is only in masm translation.
I flirted with CX, but it turned out I had to use XCHG.
I corrected it in the files you can download from
GeoCities/SV/H/7052/les1.zip
and les2.zip and les3.zip

Rick{still no comments ? {*filter*} !)



Sat, 02 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Quote:


> >   Jack Cranshaws compiler tutor:

> >   http:/iecc.com/compilers/crenshaw/

> >   He discusses "recursive descent" parsing, which is probably right up your
> >alley.

> >   There is also the simple-to-use binary tree method, but it is much less
> >extensible and begins making your compiler bloat quickly as you add more
> >and more operators or datatypes.


> >--: COMPUTERVREDEBREUK

> He that was a nice page. I downloaded the whole tuturial. Since it is
> in pascal I am going to port the code to QBASIC and ASM. Currently
> I have done lesson 1. If someone else has done something like it
> ( could also be other basics as qb45 or pb )please let me, us, know
> that. I will make my translattions available on my homepage, but here
> is the first part:

Where is your home page? Have you considered translating it
with QBasic and ArrowASM? That way, it would be freely available
for everyone. Then, the VAL linker could be used to link the
assembled code. Voila! A free QBasic compiler! <grin>

--
Kenneth W. Melvin

http://www.geocities.com/SiliconValley/8823/index.html
LinuX IS user friendly. It's just choosey about who its friends are.



Sun, 03 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Quote:


>As usual I included buggie ;). But it is only in masm translation.
>I flirted with CX, but it turned out I had to use XCHG.
>I corrected it in the files you can download from
>GeoCities/SV/H/7052/les1.zip
>and les2.zip and les3.zip
>Rick{still no comments ? {*filter*} !)

DECLARE FUNCTION ISALNUM% (CHAR$)
DECLARE SUB SKIPWHITE ()
DECLARE FUNCTION ISWHITE% (CHAR$)
DECLARE SUB USERINPUT ()
DECLARE SUB USEROUTPUT ()
DECLARE SUB ASSIGNMENT ()
DECLARE SUB NEWLINE ()
DECLARE SUB INITTABLE ()
DECLARE FUNCTION FACTOR% ()
DECLARE FUNCTION ISADDOP% (CHAR$)
DECLARE FUNCTION TERM% ()
DECLARE SUB MATCH (MATCHSTR$)
DECLARE FUNCTION EXPRESSION% ()
DECLARE FUNCTION GETNUMBER% ()
DECLARE SUB EMITLN (EMITLNSTR$)
DECLARE SUB INIT ()
DECLARE SUB GETCHAR ()
DECLARE SUB ERRORHANDLING (ERRS$)
DECLARE SUB ABORT (ABORTSTR$)
DECLARE SUB EXPECTED (EXPECTSTR$)
DECLARE FUNCTION ISALPHA% (CHAR$)
DECLARE FUNCTION ISDIGIT% (CHAR$)
DECLARE FUNCTION GETNAME$ ()
DECLARE SUB EMIT (EMITSTR$)
DEFINT A-Z: CLS

'compiler-lesson 4 by Jack. Crenshaw translated to QBASIC/ MASM by
Rick.

'In this version B I have added both the skipwhite space routines
'which are already there in lesson 3, and also added a few lines so
'that you can use multiple character variables. It turns out to be a
'really horibble future that QBASIC will not allow some array like
'a(b$(26)), so that will be added for sure in the intrepreter!(g). But
'there is a workaround using two arrays. I also added a little
'sentence making the input/output more understandable. As it stands
'you can use 26 variable names which are remembered when you ask for
'the output. In expressions you can abbreviate the variables by there
'first character.  The intrepreter up to what Jack made is available
on my homepage as les4.zip( part 4a)

'CONSTANTS

CONST TABS = 9
CONST BEL = 7

'GLOBAL VARIABLES
DIM SHARED INCHAR$
DIM SHARED NAME$(&H41 TO &H5A)               'ALLOW 26 NAMES
DIM SHARED TABLE(&H41 TO &H5A)               'AS OFF LES 4

'MAIN PROGRAM:

CALL INIT
DO
  SELECT CASE INCHAR$

    CASE "?"
    CALL USERINPUT

    CASE "!"
    CALL USEROUTPUT

    CASE ELSE
    CALL ASSIGNMENT

  END SELECT

  CALL NEWLINE

LOOP UNTIL INCHAR$ = "."           'AS OFF LES 4

'GOOD BYE
'RIck

SUB ABORT (ABORTSTR$)
CALL ERRORHANDLING(ABORTSTR$)
END
END SUB

SUB ASSIGNMENT

NAMES = ASC(UCASE$(LEFT$(GETNAME$, 1)))
CALL MATCH("=")
TABLE(NAMES) = EXPRESSION

END SUB

SUB EMIT (EMITSTR$)
PRINT CHR$(9); EMITSTR$
END SUB

SUB EMITLN (EMITLNSTR$)

CALL EMIT(EMITLNSTR$)
PRINT :

END SUB

SUB ERRORHANDLING (ERRS$)
PRINT :
PRINT CHR$(7); "ERROR: "; ERRS$
END SUB

SUB EXPECTED (EXPECTSTR$)
CALL ABORT(EXPECTSTR$ + " EXPECTED ")
END SUB

FUNCTION EXPRESSION

VALUE = 0

IF ISADDOP(INCHAR$) THEN VALUE = 0 ELSE VALUE = TERM
DO WHILE ISADDOP(INCHAR$)

  SELECT CASE INCHAR$

  CASE "+"
  CALL MATCH("+")
  VALUE = VALUE + TERM

  CASE "-"
  CALL MATCH("-")
  VALUE = VALUE - TERM

  END SELECT

LOOP

EXPRESSION = VALUE

END FUNCTION

FUNCTION FACTOR

IF INCHAR$ = "(" THEN
  CALL MATCH("(")
  FACTOR = EXPRESSION
  CALL MATCH(")")
ELSEIF ISALPHA(INCHAR$) THEN
  FACTOR = TABLE(ASC(UCASE$(LEFT$(GETNAME$, 1)))) 'FOR MULTIPLE CHARS
ELSE
  FACTOR = GETNUMBER
END IF

END FUNCTION

SUB GETCHAR

DO
  I$ = INKEY$
  INCHAR$ = LEFT$(I$, 1)
LOOP UNTIL I$ <> ""
END SUB

FUNCTION GETNAME$

TOKEN$ = ""
IF ISALPHA(INCHAR$) = 0 THEN CALL EXPECTED("NAME")

DO WHILE ISALNUM(INCHAR$)
  TOKEN$ = TOKEN$ + INCHAR$     'ADDED TO THE INTERPRETER
  CALL GETCHAR
LOOP
GETNAME$ = TOKEN$

CALL SKIPWHITE 'ADDED TO INTREPRETER

END FUNCTION

FUNCTION GETNUMBER

VALUE = 0
IF ISDIGIT(INCHAR$) = 0 THEN CALL EXPECTED("BYTE-INTEGER")
DO WHILE ISDIGIT(INCHAR$)
  VALUE = 10 * VALUE + ASC(INCHAR$) - ASC("0")
  CALL GETCHAR
LOOP
GETNUMBER = VALUE

CALL SKIPWHITE        'ADDED TO INTREPRETER

END FUNCTION

DEFSNG A-Z
SUB INIT

CALL INITTABLE
CALL GETCHAR

END SUB

DEFINT A-Z
SUB INITTABLE

FOR I = &H41 TO &H5A
  TABLE(I) = 0
NEXT
END SUB

FUNCTION ISADDOP (CHAR$)

IF CHAR$ = "-" OR CHAR$ = "+" THEN ISADDOP = 1

END FUNCTION

FUNCTION ISALNUM (CHAR$) 'ADDED TO THE INTREPRETER MATCHING THE
COMPILER

IF ISDIGIT(CHAR$) OR ISALPHA(CHAR$) THEN ISALNUM = 1

END FUNCTION

FUNCTION ISALPHA (CHAR$)

CHECKIT$ = UCASE$(CHAR$)
IF CHECKIT$ >= "A" AND CHECKIT$ <= "Z" THEN ISALPHA = 1

END FUNCTION

FUNCTION ISDIGIT (CHAR$)

IF CHAR$ >= "0" AND CHAR$ <= "9" THEN ISDIGIT = 1

END FUNCTION

FUNCTION ISWHITE (CHAR$) 'ADDED TO INTREPRETER MATCHING THE COMPILER

IF CHAR$ = CHR$(32) OR CHAR$ = CHR$(9) THEN ISWHITE = 1
END FUNCTION

SUB MATCH (MATCHSTR$)

IF INCHAR$ <> MATCHSTR$ THEN
  CALL EXPECTED("" + MATCHSTR$ + "")
ELSE
  CALL GETCHAR
  CALL SKIPWHITE
END IF

END SUB

SUB NEWLINE

'[RECOGNIZE AND SKIP OVER NEWLINE ]

IF INCHAR$ = CHR$(13) THEN
  CALL GETCHAR
  IF INCHAR$ = CHR$(10) THEN
    CALL GETCHAR
  END IF
END IF

END SUB

SUB SKIPWHITE    'ADDED TO THE INTREPRETER MATCHING THE COMPILER

DO WHILE ISWHITE(INCHAR$)
  CALL GETCHAR
LOOP

END SUB

FUNCTION TERM

VALUE = FACTOR

DO WHILE INCHAR$ = "*" OR INCHAR$ = "/"

  SELECT CASE INCHAR$

  CASE "*"
  CALL MATCH("*")
  VALUE = VALUE * FACTOR

  CASE "/"
  CALL MATCH("/")
  VALUE = VALUE \ FACTOR         'INTEGER DIVIDE..

  END SELECT

LOOP

TERM = VALUE

END FUNCTION

SUB USERINPUT
'EXPECTS A MORE CHARACTER INPUT LIKE !VAR AFTER WHICH PROMPTED FOR
VALUE
'THIS IS ALLOWING FOR 26 VARIABLES, ONE FOR EVERY STARTCHARACTER IN
'ALPHABET. SO ASM,BASM,CHASM ETC..

CALL MATCH("?")
NAMES$ = GETNAME$
NR = ASC(UCASE$(LEFT$(NAMES$, 1)))
NAME$(NR) = NAMES$               'FOR MULTIPLE CHARINPUT
PRINT NAMES$; : INPUT TABLE(NR)

END SUB

SUB USEROUTPUT
'[ASSUMES MORE CHARACTER INPUT LIKE !VARNAME FOR
'OUTPUTTING CURRENT VALUE OF VAR]
'ALLOWS FOR ONLY 1 VARNAME FOR EVERY START CHARACTER

CALL MATCH("!")
NAMES$ = GETNAME$
NR = ASC(UCASE$(LEFT$(NAMES$, 1)))
PRINT NAME$(NR); " := "; TABLE(NR)

END SUB



Sun, 03 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

!!LES5!!done!!

Les5 is translated too. This is really nice. All control statements
done!. There are however a lot of discutable choices made by
Jack which I copied just like Ranx Xerox. Additions and translations
will be added later. Also this program is already so large with so
many nestings possible that I have not had the chance to test *all of
it*. Every routine "an sich" is tested however. If anyone finds buggie
then It would be kind if they mail me...

Quote:
>>GeoCities/SV/H/7052/les5.zip
>>Rick{still no comments ?)



Mon, 04 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code



Quote:
>I am wanting to write an interpreted langauge similar to BASIC ...

I have pointers to source code for about a dozen different version of
Basic interpreters on my web page: <http://www.nicholson.com/rhn/basic/>
Might be useful for several examples of how to parse expressions.

Regards,

---

#include <canonical.disclaimer>   // only my own opinions, etc.



Wed, 06 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Quote:



>>I am wanting to write an interpreted langauge similar to BASIC ...
>I have pointers to source code for about a dozen different version of
>Basic interpreters on my web page: <http://www.nicholson.com/rhn/basic/>
>Might be useful for several examples of how to parse expressions.
>Regards,
>---

>#include <canonical.disclaimer>       // only my own opinions, etc.

He Ron, is this a reaction to some source code I posted which is in
QBASIC and is available( in more then one sense) for everybody who
cares to read Jack's tutorials and who knows QBASIC/ASM ? Then you
have to agree that none of all the basic intrepreters you offer, gives
that much( mostly C, sometimes no source available). Besides,
ironically , les 4 of Jack's( did you worked through it ?), has shown
that intrepreters<> compilers. Especially for expression parsing.

When someone wants asm output from a QBASIC like language without
source available then BASM might do.

RIck



Thu, 07 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

All QBASIC assemblers,

Les 6 is done. Finally solved the motorola registers D0 to CX , A0 to
BX and for temporaraly comparison of two factors or terms I can use
AX. All seems to work okay. MOre like a compiler yes.

Rick
[btw uploaded errors.zip to my homepage too. Might be interesting for
asm qbasicers]

su



Thu, 07 Oct 1999 03:00:00 GMT  
 BASIC Interpreter Source Code

Quote:


>All QBASIC assemblers,
>Les 7 for the lexical parser is done. Both for single chars and multiple chars.
> will be merged with the parser soon.
> I am having some questions upon the MASM syntax I used however for the parser
> There is some trouble. The way I did it until now requires that you translates output
> from mul and div to CX to ( another statement like MOV CX, AX), but I am thinking of another
> way to go. That would be to push assignments from CX but the outcomes from AX.
> I have to think how that would change J. Crenshaws structure, and any help is welcome.
> Also it would be most helpful if someone quickly writes some full debugscript. With my screen redirction
> functions it would be very easy to test the asm code with debug......I would like some help there
>( its easy but I do not have time for everything,...)

RIck
Quote:
>Rick



Mon, 11 Oct 1999 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. BASIC interpreter source code

2. BASIC CGI scripts/ source code for basic interpreter

3. Looking for Public Domain source code for Basic Interpreter

4. Visual Basic source-code to C++ source-code

5. Visual Basic source-code to C++ source-code

6. Visual Basic source-code to C++ source-code

7. Interpreter Source Code Wanted

8. Wanted: C -source code for a QBasic -like interpreter

9. BASIC Interpreter example source

10. Source for Basic interpreter/compiler

11. Searching for Sources for a simple basic-interpreter

12. search basic interpreter incl. cource-code

 

 
Powered by phpBB® Forum Software