Forums
micro(A) interpreter - Printable Version

+- Forums (http://euradioboard.createmybb3.com)
+-- Forum: Personal (/forum-11.html)
+--- Forum: code things.... (/forum-12.html)
+--- Thread: micro(A) interpreter (/thread-58.html)



micro(A) interpreter - Aurel - 01-30-2020 09:57 PM

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



RE: micro(A) interpreter - Aurel - 03-07-2020 05:02 AM

Testing micro(A) execution time
tokenization time show 0 ...which is ok for a such a small code
(just a simple math expression)
"1+2.3*4 +(10.6/2.43+0.6)*0.7/1.44-0.7*(2.3+0.6)"
and funny thing execution time shows no mather what size of
expession is similar results
between 0,7 - 1,9 seconds
Big Grin

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 +(10.6/2.43+0.6)*0.7/1.44-0.7*(2.3+0.6)" + 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
startTime = GetTickCount()
exec()
   endTime = GetTickCount()                  ' stop tick counter
   procTime = (EndTime - startTime)/1000     ' in seconds
   MsgBox "Exec Time: " + str(procTime) + " s", "Execution Time"
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