-
Notifications
You must be signed in to change notification settings - Fork 1
/
stdlib.ts
120 lines (117 loc) · 3.24 KB
/
stdlib.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Value } from "./interpreter.ts";
import { Type } from "./typechecker.ts";
import { DiscriminateUnion, assertNever } from "./utils.ts";
type StdLibFun = {
tag: "TmStdlibFun";
type: DiscriminateUnion<Type, "tag", "TyArrow">;
// TODO
// impl: (...args: Value[]) => Value;
impl: (...args: any[]) => Value;
};
const STD_LIB: Record<string, () => StdLibFun> = {
"not": () => ({
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [{ tag: "TyBool" }],
returnType: { tag: "TyBool" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmBool">,
) => ({ tag: "TmBool", val: !(x.val) }),
}),
"+": () => ({
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [{ tag: "TyInt" }, { tag: "TyInt" }],
returnType: { tag: "TyInt" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmInt">,
y: DiscriminateUnion<Value, "tag", "TmInt">,
) => ({ tag: "TmInt", val: x.val + y.val }),
}),
"-": () => ({
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [{ tag: "TyInt" }, { tag: "TyInt" }],
returnType: { tag: "TyInt" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmInt">,
y: DiscriminateUnion<Value, "tag", "TmInt">,
) => ({ tag: "TmInt", val: x.val - y.val }),
}),
"*": () => ({
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [{ tag: "TyInt" }, { tag: "TyInt" }],
returnType: { tag: "TyInt" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmInt">,
y: DiscriminateUnion<Value, "tag", "TmInt">,
) => ({ tag: "TmInt", val: x.val * y.val }),
}),
"=": () => {
const paramType: Type = { tag: "TyInt" };
return {
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [paramType, paramType],
returnType: { tag: "TyBool" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmInt">,
y: DiscriminateUnion<Value, "tag", "TmInt">,
) => ({ tag: "TmBool", val: x.val == y.val }),
};
},
"string=?": () => {
const paramType: Type = { tag: "TyStr" };
return {
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [paramType, paramType],
returnType: { tag: "TyBool" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmStr">,
y: DiscriminateUnion<Value, "tag", "TmStr">,
) => ({ tag: "TmBool", val: x.val === y.val }),
};
},
"string-length": () => ({
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [{ tag: "TyStr" }],
returnType: { tag: "TyInt" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmStr">,
) => ({ tag: "TmInt", val: x.val.length }),
}),
"string-concat": () => ({
tag: "TmStdlibFun",
type: {
tag: "TyArrow",
paramTypes: [{ tag: "TyStr" }, { tag: "TyStr" }],
returnType: { tag: "TyStr" },
},
impl: (
x: DiscriminateUnion<Value, "tag", "TmStr">,
y: DiscriminateUnion<Value, "tag", "TmStr">,
) => ({ tag: "TmStr", val: x.val + y.val }),
}),
};
export function lookupInStdLib(
varName: string,
): (ReturnType<typeof STD_LIB[keyof typeof STD_LIB]>) | undefined {
return STD_LIB[varName]?.() || undefined;
}