Skip to content

Instantly share code, notes, and snippets.

@tristan
Created September 30, 2010 13:05
Show Gist options
  • Save tristan/604540 to your computer and use it in GitHub Desktop.
Save tristan/604540 to your computer and use it in GitHub Desktop.
tree grammar SimpleCalcWalker;
options {
language=JavaScript;
tokenVocab=SimpleCalc;
ASTLabelType=CommonTree;
}
eval returns [var result]
: e=math EOF { result = e; }
;
math returns [var result]
: ^('+' op1=math op2=math) { result = op1 + op2; }
| ^('-' op1=math op2=math) { result = op1 - op2; }
| ^('*' op1=math op2=math) { result = op1 * op2; }
| ^('/' op1=math op2=math) { result = op1 / op2; }
| ^('%' op1=math op2=math) { result = op1 \% op2; }
| ^(NEGATION op1=math) { result = -op1; }
| FLOAT { result = parseFloat($FLOAT.text); }
| INT { result = parseInt($INT.text); }
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment