This project is a personal learning project in compiler construction. It is an attempt to port the interpreter (written in C#) that was created by my mentor and friend Praseed Pai K.T (a.k.a Pai) as part of his teaching endeavours.
The code is wired based on the e-Book, written by the author himself, available in the Downloads section from http://slangfordotnet.codeplex.com/
The whole exercise is achieved in 6 Steps (Step 5 from the original 7 steps have been ignored thereby skipping compilation) as followed in the e-Book.
The code leverages Node.JS in step 7 for reading a valid SLANG program file (some samples have been provided in the programs folder). There is no dependency with Node.JS till Step 6. But it would be good to use Node.JS throughout to debug and see the JS console logs.
The interpreter have been bundled with a simple HTML code editor for anyone to write and run SLANG programs. Here's the Slang Interpreter.
The Grammar (EBNF) for SLANG is as listed below:
- Module ::= (Procedure)+
- Procedure ::= FUNCTION type func_name '('arglist')' stmts END
- type ::= NUMERIC | STRING | BOOLEAN
- arglist ::= '(' (type arg_name | ',')+ ')'
- stmts ::= (stmt)+
- stmt ::= vardeclstmt | printstmt | assignmentstmt | callstmt | ifstmt | whilestmt | returnstmt
- vardeclstmt ::= type variable ';'
- printstmt ::= PRINT expr ';'
- assignmentstmt ::= variable '=' value ';'
- ifstmt ::= IF '('expr')' THEN stmts (| (ELSE stmts)) ENDIF
- whilestmt ::= WHILE '('expr')' stmts WEND
- returnstmt ::= Return expr ';'
- expr ::= BExpr
- BExpr ::= LExpr LOGIC_OP BExpr
- LExpr ::= RExpr REL_OP LExpr
- RExpr ::= Term ADD_OP RExpr
- Term ::= Factor MUL_OP Term
- Factor ::= NUMERIC | STRING | TRUE | FALSE | variable | '(' expr ')' | ('+'|'-'|'!') Factor | callexpr
- callexpr ::= funcname '(' actuals ')'
- LOGIC_OP ::= '&&' | '||'
- REL_OP ::= '>' | '<' | '>=' | '<=' | '<>' | '=='
- MUL_OP ::= '*' |'/'
- ADD_OP ::= '+' |'-'
Here is the Syntax Diagram corresponding to the Grammar above.