Post Reply 
Social Buttons
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A token-based interpreter...
02-09-2019, 09:08 AM
Post: #5
RE: A token-based interpreter...
this one is from Jimage interpreter
need connection with ANIscript because require tokenizer.

Code:
$ Filename "RDescent.exe" ' o2
include "RTL32.inc"
include "awinh037.inc"
#lookahead

'Globals..................................................................
string token :  int tc
int tokPLUS = 1, tokMINUS = 2 , tokMULTI = 3 ,tokDIVIDE = 4
int tokPOW = 5 , tokMOD = 7 , tokNUM = 8, tokVAR = 9, tokSTR = 10
int tokLPAREN = 11 , tokRPAREN = 12 , tokEOL = 13 , tokCOLON = 14

'open window.............................................................
INT win,wx=0,wy=0,ww=600,wh=400,wstyle = WS_MINMAXSIZE
INT button0,b0ID=100, lvControl,lvID=1000
win = SetWindow("RECURSIVE DESCENT EXPR-EVALUATOR",wx,wy,ww,wh,0,wstyle)

'token field - tokenized code-------------------------------------------
string tokList[] = {"2","+","3","*","4"}
   int tokType[] = { 8,  1 , 8 , 3 , 8 }


InterpretTokens()

'----------------------------------------------------------------------
'message loop
Wait()
'----------------------------------------------------------------------
'func main
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 DefWindowProc(hwnd,wMsg,wParam,lParam)
END FUNCTION

'---- user functions ............................................

Function getToken() as int
        tc++ ' token_count + 1
        token = tokList[tc] : INT tok = tokType[tc]
         print "TOKEN: " + token
     return tok
End function

'...............................................................

Function InterpretTokens()
int tok,nextTok
tok = getToken() : nextTok = tokType[tc+1]
    Select tok

        CASE tokNUM  '8-number
         If nextTok = tokPLUS Or nextTok = tokMINUS Or nextTok = tokMULTI Or nextTok = tokDIVIDE
             EvalExpr()
        End if

    End Select
End function

'------------------------------------------------------------

Function EvalExpr()
float result : string res
result = getExpression()
print "RESULT: " + str(result) 'show result
End Function
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
function getExpression() as float
        float value = getTerm()
        int ntok
        While 1
            ntok = tokType[tc+1]
            If ntok = tokPLUS      '"+"
                getToken()
                value = value + getTerm()
            ElseIf ntok = tokMINUS '"-"
                getToken()
                value = value - getTerm()
            Else
            Exit while
            End If
        Wend
        Return value
end function
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
function getTerm() as float
        float value = getFactor()
        int ntok
        ntok = tokType[tc+1]
            select ntok
                Case tokMULTI '"*"
                getToken(): value = value * getFactor()
                Case "/"
                getToken(): value = value / getFactor()
                'Case "%": getToken(): value = value % getFactor() '; break;
                'Case "&": getToken(): value = int(value) & int(getFactor())
                'Case "|": getToken(): value = int(value) | int(getFactor())
                Case "^"
                getToken(): value = value ^ int(getFactor())                   
            end select
        
        Return value
end function
',,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    function getFactor() as float {
        double value = 0.0;
        Variable v = null;
        getToken();
        switch (token) {
            Case NUMBER:
                value = tokenValue;
                Break;
            Case NUMERIC_FUNCTION:
                value = func.getFunctionValue(pgm.table[tokenAddress].type);
                Break;
            Case STRING_FUNCTION:
                String str = func.getStringFunction(pgm.table[tokenAddress].type);
                value = Tools.parseDouble(str);
                If ("NaN".equals(str))
                    value = Double.NaN;
                Else If (Double.isNaN(value))
                    error("Numeric value expected");
                Break;
            Case USER_FUNCTION:
                v = runUserFunction();
                If (v==null)
                    error("No return value");
                If (done)
                    value = 0;
                Else {
                    If (v.getString()!=null)
                        error("Numeric return value expected");
                    Else
                        value = v.getValue();
                }
                Break;
            Case TRUE: value = 1.0; break;
            Case FALSE: value = 0.0; break;
            Case PI: value = Math.PI; break;
            Case NaN: value = Double.NaN; break;
            Case WORD:
                v = lookupVariable();
                If (v==null)
                    Return 0.0;
                int Next = nextToken();
                If (Next=='[') {
                    v = getArrayElement(v);
                    value = v.getValue();
                    Next = nextToken();
                } Else If (Next=='.') {
                    value = getArrayLength(v);
                    Next = nextToken();
                } Else {
                    If (prefixValue!=0 && !checkingType) {
                        v.setValue(v.getValue()+prefixValue);
                        prefixValue = 0;
                    }
                    value = v.getValue();
                }
                If (!(Next==PLUS_PLUS || Next==MINUS_MINUS))
                    Break;
                getToken();
                If (token==PLUS_PLUS)
                    v.setValue(v.getValue()+(checkingType?0:1));
                Else
                    v.setValue(v.getValue()-(checkingType?0:1));
                Break;
            Case (int)'(':
                value = getLogicalExpression();
                getRightParen();
                Break;
            Case PLUS_PLUS:
                prefixValue = 1;
                value = getFactor();
                Break;
            Case MINUS_MINUS:
                prefixValue = -1;
                value = getFactor();
                Break;
            Case '!':
                value = getFactor();
                If (value==0.0 || value==1.0) {
                    value = value==0.0?1.0:0.0;
                } Else
                    error("Boolean expected");
                Break;
            Case '-':
                value = -getFactor();
                Break;
            Case '~':
                value = ~(int)getFactor();
                Break;
            Default:
                error("Number or numeric function expected");
        }
        '// IJ.log("getFactor: "+value+" "+pgm.decodeToken(preToken,0));
        Return value;
    }


Attached File(s) Image(s)
   
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
A token-based interpreter... - Aurel - 10-13-2018, 06:45 AM
RE: A token-based interpreter... - Aurel - 10-18-2018, 06:22 PM
RE: A token interpreter (N.A.F.I.) - Aurel - 10-23-2018, 04:47 AM
RE: A token-based interpreter... - Aurel - 11-15-2018, 08:23 AM
RE: A token-based interpreter... - Aurel - 02-09-2019 09:08 AM
RE: A token-based interpreter... - Aurel - 02-16-2019, 09:12 AM
RE: A token-based interpreter... - Aurel - 03-19-2019, 06:43 AM

Forum Jump:


User(s) browsing this thread: