Skip to content

Commit

Permalink
Renamed the grammar to match the language´s name and modified README.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomTriple committed Jan 4, 2013
1 parent 0874855 commit 11041ad
Show file tree
Hide file tree
Showing 4 changed files with 567 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Makefile
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
98 changes: 98 additions & 0 deletions Mocca.g
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';


Loading

0 comments on commit 11041ad

Please sign in to comment.