From e2e74df94fcdd2f3165e035fc00c98573f0b40d8 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 3 Mar 2024 14:20:36 -0700 Subject: add parser --- src/interpreter/parser.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/interpreter/parser.ts (limited to 'src/interpreter/parser.ts') diff --git a/src/interpreter/parser.ts b/src/interpreter/parser.ts new file mode 100644 index 0000000..ea07796 --- /dev/null +++ b/src/interpreter/parser.ts @@ -0,0 +1,35 @@ +import peggyParser from "./PeggyParser.js"; + +export type Application = { + application: { + left: LambdaTerm; + args: Array; + }; +}; + +export type Abstraction = { + abstraction: { + param: Variable; + body: LambdaTerm; + }; +}; + +export type Variable = string; + +export type LambdaTerm = Application | Abstraction | Variable; + +export const isApplication = (term: LambdaTerm): term is Application => { + return (term as Application).application !== undefined; +}; + +export const isAbstraction = (term: LambdaTerm): term is Abstraction => { + return (term as Abstraction).abstraction !== undefined; +}; + +export const isVariable = (term: LambdaTerm): term is Variable => { + return typeof term === "string"; +}; + +export const parse = (term: string) => { + return peggyParser.parse(term, { library: true }); +}; -- cgit v1.2.3-70-g09d2