-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_grammar.pegjs
More file actions
31 lines (23 loc) · 863 Bytes
/
lambda_grammar.pegjs
File metadata and controls
31 lines (23 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// no associativity of bracketing : expressions have to be fully parenthesised
LTerm
= v:LVar {return {ctor : "LVar", var :v};}
/ n:LInt {return {ctor : "LInt", val :n};}
/ l:LLambda {return {ctor : "LLambda", var :l.var, exp : l.exp};}
/ a:LApp {return {ctor : "LApp", lhs :a.lhs, rhs : a.rhs};}
/ c:LBinOp {return c;}
/ "(" _ t:LTerm _ ")" {return t;}
LVar
= id:LIdentifier {return id;}
LInt
= _ [0-9]+ { return parseInt(text(), 10); }
LLambda
= _ "λ" v:LVar _ "." _ e:LTerm {return {var:v, exp:e};}
LApp
= _ "(" _ l:LTerm _ r:LTerm _ ")" {return {lhs :l, rhs : r};}
LIdentifier
= _ id:[a-z]+ {return id.join('');}
LBinOp
= _ "(" _ l:LTerm _ "+" _ r:LTerm ")" {return {ctor : "PLUS", lhs :l, rhs : r};}
/ _ "(" _ l:LTerm _ "*" _ r:LTerm ")" {return {ctor : "TIMES", lhs :l, rhs : r};}
_ "whitespace"
= [ \t\n\r]*