grammar FuncLang;
import ListLang; //Import all rules from ListLang grammar.
// New elements in the Grammar of this Programming Language
// – grammar rules start with lowercase
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; }
| lam=lambdaexp { $ast = $lam.ast; }
| call=callexp { $ast = $call.ast; }
| i=ifexp { $ast = $i.ast; }
| less=lessexp { $ast = $less.ast; }
| eq=equalexp { $ast = $eq.ast; }
| gt=greaterexp { $ast = $gt.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; }
| isnum=isNumExp { $ast = $isnum.ast; }
| isbool=isBoolExp { $ast = $isbool.ast; }
| isstring=isStringExp { $ast = $isstring.ast; }
| isproc=isProcedureExp { $ast = $isproc.ast; }
| islist=isListExp { $ast = $islist.ast; }
| ispair=isPairExp { $ast = $ispair.ast; }
| isunit=isUnitExp { $ast = $isunit.ast; }
;
lambdaexp returns [LambdaExp ast]
locals [ArrayList
@init { $formals = new ArrayList
‘(‘ Lambda
‘(‘ (id=Identifier { $formals.add($id.text); } )* ‘)’
body=exp
‘)’ { $ast = new LambdaExp($formals, $body.ast); }
;
callexp returns [CallExp ast]
locals [ArrayList
‘(‘ f=exp
( e=exp { $arguments.add($e.ast); } )*
‘)’ { $ast = new CallExp($f.ast,$arguments); }
;
ifexp returns [IfExp ast] :
‘(‘ If
e1=exp
e2=exp
e3=exp
‘)’ { $ast = new IfExp($e1.ast,$e2.ast,$e3.ast); }
;
lessexp returns [LessExp ast] :
‘(‘ Less
e1=exp
e2=exp
‘)’ { $ast = new LessExp($e1.ast,$e2.ast); }
;
equalexp returns [EqualExp ast] :
‘(‘ Equal
e1=exp
e2=exp
‘)’ { $ast = new EqualExp($e1.ast,$e2.ast); }
;
greaterexp returns [GreaterExp ast] :
‘(‘ Greater
e1=exp
e2=exp
‘)’ { $ast = new GreaterExp($e1.ast,$e2.ast); }
;
isNumExp returns [IsNumExp ast] :
‘(‘ ‘number?’
e=exp
‘)’ { $ast = new IsNumExp($e.ast); }
;
isBoolExp returns [IsBoolExp ast] :
‘(‘ ‘boolean?’
e=exp
‘)’ { $ast = new IsBoolExp($e.ast); }
;
isStringExp returns [IsStringExp ast] :
‘(‘ ‘string?’
e=exp
‘)’ { $ast = new IsStringExp($e.ast); }
;
isProcedureExp returns [IsProcedureExp ast] :
‘(‘ ‘procedure?’
e=exp
‘)’ { $ast = new IsProcedureExp($e.ast); }
;
isListExp returns [IsListExp ast] :
‘(‘ ‘list?’
e=exp
‘)’ { $ast = new IsListExp($e.ast); }
;
isPairExp returns [IsPairExp ast] :
‘(‘ ‘pair?’
e=exp
‘)’ { $ast = new IsPairExp($e.ast); }
;
isUnitExp returns [IsUnitExp ast] :
‘(‘ ‘unit?’
e=exp
‘)’ { $ast = new IsUnitExp($e.ast); }
;