summaryrefslogtreecommitdiff
path: root/src/interpreter/grammar.pegjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/interpreter/grammar.pegjs')
-rw-r--r--src/interpreter/grammar.pegjs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/interpreter/grammar.pegjs b/src/interpreter/grammar.pegjs
new file mode 100644
index 0000000..520d6c8
--- /dev/null
+++ b/src/interpreter/grammar.pegjs
@@ -0,0 +1,17 @@
+LambdaTerm = Application / Abstraction / Variable
+
+Application = LPAREN _? left:LambdaTerm _ argList:(LambdaTerm _)* lastArg:LambdaTerm _? RPAREN {
+ const args = lastArg || argList.length ? [...argList.map(x => x[0]), lastArg] : [];
+ return { application: { left, args } };
+}
+
+Abstraction = LPAREN _? LAMBDA _? LPAREN _? param:Variable _? RPAREN _? DOT _? body:LambdaTerm _? RPAREN { return { abstraction: { param, body } }; }
+
+Variable = param:[a-zA-Z0-9]+ { return param.join(""); }
+
+LPAREN = "("
+RPAREN = ")"
+DOT = "."
+LAMBDA = "λ" / "\\"
+
+_ = ("\n" / " " / "\t" / "\r\n")+ \ No newline at end of file