summaryrefslogtreecommitdiff
path: root/src/interpreter/parser.ts
blob: ea077966f2e198a00b1db8206a1a5ecd6c4cdbfc (plain)
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
32
33
34
35
import peggyParser from "./PeggyParser.js";

export type Application = {
  application: {
    left: LambdaTerm;
    args: Array<LambdaTerm>;
  };
};

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 });
};