Monday, January 04, 2016

Tiny BASIC part 2 - Adding new statements

This is part 2 of a series on extending tiny basic.bas which is an example that comes with Liberty BASIC v4.5.0.

In order to add some graphics capability I want to suggest two new commands just to start.

GRAPHICWIN width, height     and    PSET x, y, color$

These will enable us to open a window to draw in, and also to draw pixels of a specific color at a given x, y position.  This will be a crude start, but it will be instructive because it will show the reader how to extend Tiny BASIC to do what is desired.

The first thing to do is figure out where in the code Tiny BASIC parses commands so we can add some more.  The code below is the routine that does this.  If you look carefully you will see a SELECT CASE statement and then a CASE statement for each Tiny BASIC command.

More later.
[NextStatement]
 GOSUB [GetLabel]
 IF E$<>"" THEN [Ready]
 SELECT CASE D$
 CASE "if"
  GOSUB [GetExpression]
  IF E$<>"" THEN [Ready]
  IF N<1 font="" then="">
   B$=A$(L) : C=LEN(B$)+1
   GOTO [FinishStatement]
  END IF
  GOSUB [GetLabel]
  IF E$<>"" THEN [Ready]
  IF D$<>"then" THEN
   E$="'THEN' expected"
   GOTO [Ready]
  END IF
  GOTO [NextStatement]
 CASE "rem"
  B$=A$(L) : C=LEN(B$)+1
  GOTO [FinishStatement]
 CASE "input"
  GOSUB [GetVar]
  IF E$<>"" THEN [Ready]
  INPUT N : A(V)=N
  GOTO [FinishStatement]
 CASE "print"
[Print]
  GOSUB [SkipSpace]
  GOSUB [GetChar]
  IF C$=G$ THEN
   B$=""
[NextChar]
   C = C + 1 : C$=MID$(A$,C,1)
   IF C$="" THEN
    E$="Unterminated string"
    GOTO [Ready]
   ELSE
    IF C$<>G$ THEN
     B$=B$+C$
     GOTO [NextChar]
    END IF
   END IF
   C = C + 1 : C$=MID$(A$,C,1)
   IF C$=G$ THEN
    B$=B$+C$
    GOTO [NextChar]
   END IF
   PRINT B$;
  ELSE
   GOSUB [GetExpression]
   IF E$<>"" THEN [Ready]
   B=N1
   IF B=N THEN
    PRINT N;"*";
   ELSE
    PRINT N;
   END IF
  END IF
  GOSUB [SkipSpace]
  GOSUB [GetChar]
  IF C$="," THEN C = C + 1 : GOTO [Print]
  GOSUB [SkipSpace]
  GOSUB [GetChar]
  IF C$<>";" THEN
   PRINT
  ELSE
   C = C + 1
  END IF
  GOTO [FinishStatement]
 CASE "clear"
  FOR I=27 TO 52 : A(I)=0 : NEXT I
  GOTO [FinishStatement]
 CASE "run"
  FOR I=27 TO 52 : A(I)=0 : NEXT I
  L=27 : C=1
  GOTO [FinishStatement2]
 CASE "goto"
  GOSUB [GetExpression]
  IF E$<>"" THEN [Ready]
  IF E>=N THEN L=27
  C=1 : T=N
[NextGoto]
  IF L=126 THEN
   E$="Line not found"
   GOTO [Ready]
  END IF
  GOSUB [GetNumber]
  IF N=T THEN E=N : GOTO [NextStatement]
  L = L + 1 : C=1
  GOTO [NextGoto]
 CASE "new"
  FOR I=27 TO 125 : A$(I)="" : NEXT I
  FOR I=27 TO 52 : A(I)=0 : NEXT I
  IF E=0 THEN [FinishStatement]
  GOTO [Ready]
 CASE "cls"
  CLS : GOTO [FinishStatement]
 CASE "help"
  FOR I=9 TO 18
   B$=A$(I) : PRINT B$
  NEXT I
  GOTO [FinishStatement]
 CASE "mem"
  B=126
  FOR I=27 TO 125
   diffI = 152 - I  'Cheating here
   B$=A$(diffI) : IF B$="" THEN B=diffI
  NEXT I
  B=126-B : PRINT B;"*";
  PRINT " lines free"
  GOTO [FinishStatement]
 CASE "end"
  GOTO [Ready]
 CASE "bye"
  GOTO [ExitTinyBAS]
 CASE "list"
  GOSUB [GetNumber] : T=N : A=L : I=C
  IF T=0 THEN
   GOSUB [GetLabel]
   IF E$="" AND D$="pause" THEN I=C
   E$=""
  END IF
  FOR L=27 TO 125
   C=1 : GOSUB [GetNumber]
   B=(T=0) OR (N=T)
   IF B=TRUE THEN
    IF A$<>"" THEN
     PRINT A$
     IF D$="pause" THEN
      B = (L-26) mod 10
      IF B=0 THEN PRINT "Pause..."; : INPUT AAA$
     END IF
    END IF
   END IF
  NEXT L
  L=A : C=I
  GOTO [FinishStatement]
 CASE "save"
 PRINT "SAVE, TBD"
 CASE "load"
 PRINT "LOAD, TBD"
 CASE "let"
  GOSUB [GetLabel]
  IF E$<>"" THEN [Ready]
 END SELECT




No comments: