source code for microB interpreter A01
Code:
'microB_Interpreter - with recursive descent token evaluator
' by Aurel - 26.3.2019 - last test 30.1.2020
include "awinh037.inc" ' awinh GUI api function
include "microAT.inc" 'tokenizer include
#lookahead
int tc=0 ' token count
string token ' def token
'*********************************************************************
'globals
INT win,x=0,y=0,w=400,h=300,wstyle = WS_MINMAXSIZE
INT button0,b0ID=100
' open window with message loop...
win = SetWindow("GUI_microB:",x,y,w,h,0,wstyle)
'button0 = SetButton(win,180,4,80,26,"Close (X)",0x50001000,0x200,b0ID)
'*********************************************************************
'test 1 - load source string from microB.inc Tokenizer
string code = "1-2*3-4" + crlf 'enter your expression / crlf=EOL
codeLen=len(code)
tn = run_tokenizer(code)
MsgBox str(tn) ,"Tokenizer Out" ' 1 means OK!
'---------------------------------------------------------------
' sintax / logic error block ?
'---------------------------------------------------------------
'if tokenization error=0 then OK!..execute
If tokerr = 0
exec()
End if
'================================================================
Wait() '/// without message loop ///
'===============================================================
Function WndProc (sys hwnd,wmsg,wparam,lparam) as sys callback
SELECT hwnd
CASE win
Select wmsg
CASE WM_CLOSE
CloseWindow(win)
EndProgram
End Select
END SELECT
RETURN Default
END FUNCTION
'================================================================
'-----------------------------------------------------
sub gettok()
tc++
token = tokList[tc]
'test
if tokList[tc+1] <> "" then return
end sub
'----------------------------------------------------
sub expr() as float
float v
If token = "-"
v = -(term())
else
v = term()
end if
while token = "+" or token = "-"
if token = "+": gettok() : v = v + term(): end if
if token = "-": gettok() : v = v - term(): end if
wend
return v
end sub
'---------------------------------------------------
sub term() as float
float v
v = factor()
while token = "*" or token = "/"
if token = "*": gettok() : v = v * factor(): end if
if token = "/": gettok() : v = v / factor(): end if
wend
return v
end sub
'-------------------------------------------------------
sub factor() as float
float v
if asc(token)>47 and asc(token)<58 'nums
v = val(token)
'print str(v)+ " factor"
gettok()
end if
if asc(token)=40 and asc(token)<>41 'match (...)
gettok() : v = expr() : gettok()
end if
return v
end sub
'execute-----------------------------------------------------
sub exec
gettok()'start
float res = expr()
print "RESULT=" + str(res)
end sub