-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.ts
22 lines (21 loc) · 1.03 KB
/
builtins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Exp, Fn, List, makeBoolean, makeNumber, Number } from "./ast.ts";
export const builtins: ReadonlyMap<string, Function> = new Map<
string,
Function
>(
[
["+", (val1: Exp, val2: Exp) => makeNumber(val1.val + val2.val)],
["-", (val1: Exp, val2: Exp) => makeNumber(val1.val - val2.val)],
["*", (val1: Exp, val2: Exp) => makeNumber(val1.val * val2.val)],
["/", (val1: Exp, val2: Exp) => makeNumber(val1.val / val2.val)],
[">", (val1: Exp, val2: Exp) => makeBoolean(val1.val > val2)],
[">=", (val1: Exp, val2: Exp) => makeBoolean(val1.val >= val2.val)],
["<", (val1: Exp, val2: Exp) => makeBoolean(val1.val < val2.val)],
["<=", (val1: Exp, val2: Exp) => makeBoolean(val1.val <= val2.val)],
["==", (val1: Exp, val2: Exp) => makeBoolean(val1.val === val2.val)],
["abs", (val: Number) => makeNumber(Math.abs(val.val))],
["append", (l: List, val: Exp) => l.val.concat(val)],
["apply", (proc: Exp, args: Exp) => (proc as Fn).val(...args)],
["begin", (...vals: Exp[]) => vals.slice(-1)],
],
);