-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed the grammar to match the language´s name and modified README.
- Loading branch information
Showing
4 changed files
with
567 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
parser: | ||
export CLASSPATH=/Users/tom/tom/FHR/MA/code/bin/antlr-3.3-complete.jar:$$CLASSPATH && java org.antlr.Tool Ex1Test.g Ex1Walker.g | ||
export CLASSPATH=/Users/tom/tom/FHR/MA/code/bin/antlr-3.3-complete.jar:$$CLASSPATH && java org.antlr.Tool Mocca.g MoccaWalker.g | ||
|
||
minify: | ||
java -jar tools/compiler-latest/compiler.jar \ | ||
--js=`pwd`/antlr3-all.js \ | ||
--js=`pwd`/Ex1TestLexer.js \ | ||
--js=`pwd`/Ex1TestParser.js\ | ||
--js=`pwd`/Ex1Walker.js\ | ||
--js=`pwd`/MoccaLexer.js \ | ||
--js=`pwd`/MoccaParser.js\ | ||
--js=`pwd`/MoccaWalker.js\ | ||
--js=`pwd`/jquery-1.8.3.min.js\ | ||
--js=`pwd`/main.js \ | ||
--js_output_file=`pwd`/mocca-lang.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
grammar Mocca; | ||
|
||
options { | ||
language=JavaScript; | ||
output=AST; | ||
} | ||
|
||
tokens { | ||
FN_CALL; | ||
FN_DEF; | ||
FN_DEF_INLINE; | ||
EX; | ||
} | ||
|
||
@members { | ||
|
||
} | ||
|
||
prog: exprStmt* EOF -> exprStmt* | ||
; | ||
|
||
exprStmt: ID ':' expr ';' -> ^(':' ID expr) | ||
| name=ID ':' '{' (arg+=ID)? (',' arg+=ID)* '=>' body+=exprStmt* '}' ';' -> ^(FN_DEF $name $arg* $body*) | ||
| name=ID ':' '=>' inline=exprStmt -> ^(FN_DEF_INLINE $name $inline) | ||
| expr ';' -> ^(EX expr) | ||
; | ||
|
||
expr: (exprAnd -> exprAnd) | ||
( | ||
('||' e=exprAnd -> ^('||' $expr $e)) | ||
)* | ||
; | ||
|
||
exprAnd: (exprCompare -> exprCompare) | ||
( | ||
('&&' e=exprCompare -> ^('&&' $exprAnd $e)) | ||
)* | ||
; | ||
|
||
exprCompare: (exprAdd -> exprAdd) | ||
( | ||
('==' e=exprAdd -> ^('==' $exprCompare $e)) | ||
| | ||
('!=' e=exprAdd -> ^('!=' $exprCompare $e)) | ||
| | ||
('>' e=exprAdd -> ^('>' $exprCompare $e)) | ||
| | ||
('<' e=exprAdd -> ^('<' $exprCompare $e)) | ||
| | ||
('>=' e=exprAdd -> ^('>=' $exprCompare $e)) | ||
| | ||
('<=' e=exprAdd -> ^('<=' $exprCompare $e)) | ||
)? | ||
; | ||
|
||
exprAdd: (exprMult -> exprMult) | ||
( | ||
('+' e=exprMult -> ^('+' $exprAdd $e)) | ||
| | ||
('-' e=exprMult -> ^('-' $exprAdd $e)) | ||
)* | ||
; | ||
|
||
exprMult:(unary -> unary) | ||
( | ||
('*' e=unary -> ^('*' $exprMult $e)) | ||
| | ||
('/' e=unary -> ^('/' $exprMult $e)) | ||
)* | ||
; | ||
|
||
unary: atom | ||
| '!' unary -> ^('!' unary) // points again to unary as one can chain multiple negations like !!!true; | ||
; | ||
|
||
// matching for BOOLEAN in atom means one can syntactically use TRUE in an arithmetic expression which | ||
// doesn´t make much sense. Cases like this need to be catched in the semantic analysis phase. | ||
atom: INTEGER | ||
| ID | ||
| BOOLEAN | ||
| '(' expr ')' -> expr | ||
| fn | ||
| 'if' cond=expr ':' body=exprStmt -> ^('if' $cond $body) | ||
; | ||
|
||
fn: ID '(' e+=expr? (',' e+=expr)* ')' -> ^(FN_CALL ID $e*) | ||
// | (ID '(' expr? (',' expr)* ')' '{')=>ID '(' x+=expr? (',' x+=expr)* ')' '{' body=exprStmt '}' -> ^(FN_CALL_LAZY ID $x* $body) | ||
; | ||
|
||
BOOLEAN: ('T'|'F'); | ||
ID: CHAR (CHAR|'0'..'9'|'.')*; | ||
INTEGER: '0'..'9'+; | ||
STRING: '"' (.)* '"'; | ||
WS: (' '|'\t'|'\r'|'\n') { this.skip(); }; | ||
|
||
fragment CHAR: 'a'..'z'|'A'..'Z'; | ||
|
||
|
Oops, something went wrong.