Permalink
Browse files

Renamed the grammar to match the language´s name and modified README.

  • Loading branch information...
1 parent 0874855 commit 945f86276d2382da679f5c0930c600fa4b75d959 @thoefer2 thoefer2 committed Jan 4, 2013
Showing with 557 additions and 10 deletions.
  1. +4 −4 Makefile
  2. +98 −0 Mocca.g
  3. +451 −0 MoccaWalker.g
  4. +4 −6 README.md
View
@@ -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
View
98 Mocca.g
@@ -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.

0 comments on commit 945f862

Please sign in to comment.