grammar ListLang;
import DefineLang; //Import all rules from Definelang grammar.
exp returns [Exp ast]:
va=varexp { $ast = $va.ast; }
| num=numexp { $ast = $num.ast; }
| str=strexp { $ast = $str.ast; }
| bl=boolexp { $ast = $bl.ast; }
| add=addexp { $ast = $add.ast; }
| sub=subexp { $ast = $sub.ast; }
| mul=multexp { $ast = $mul.ast; }
| div=divexp { $ast = $div.ast; }
| let=letexp { $ast = $let.ast; }
| car=carexp { $ast = $car.ast; }
| cdr=cdrexp { $ast = $cdr.ast; }
| cons=consexp { $ast = $cons.ast; }
| list=listexp { $ast = $list.ast; }
| nl=nullexp { $ast = $nl.ast; }
;
carexp returns [CarExp ast] :
‘(‘ Car
e=exp
‘)’ { $ast = new CarExp($e.ast); }
;
cdrexp returns [CdrExp ast] :
‘(‘ Cdr
e=exp
‘)’ { $ast = new CdrExp($e.ast); }
;
consexp returns [ConsExp ast] :
‘(‘ Cons
e1=exp
e2=exp
‘)’ { $ast = new ConsExp($e1.ast,$e2.ast); }
;
listexp returns [ListExp ast]
locals [ArrayList
@init { $list = new ArrayList
‘(‘ List
( e=exp { $list.add($e.ast); } )*
‘)’ { $ast = new ListExp($list); }
;
nullexp returns [NullExp ast] :
‘(‘ Null
e=exp
‘)’ { $ast = new NullExp($e.ast); }
;
strexp returns [StrExp ast] :
s=StrLiteral { $ast = new StrExp($s.text); }
;
boolexp returns [BoolExp ast] :
TrueLiteral { $ast = new BoolExp(true); }
| FalseLiteral { $ast = new BoolExp(false); }
;