-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtIns.ts
38 lines (32 loc) · 1.38 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Token } from "./token.ts"
import { Lexer } from "./lexer.ts"
import { removeLast, dictToArray } from "./utils.ts"
import { TopLevel } from "./ast.ts"
const BUILT_IN_STRINGS: Record<string, string> = {
println: "\\x -> x;",
toStr: "\\x -> \"\";"
}
const BUILT_IN_TOKENS: Token[] = dictToArray(BUILT_IN_STRINGS).map(
([name, source]) => removeLast(new Lexer(name + " = " + source).lex())
).flat(1)
const BUILT_IN_NAMES: Set<string> = new Set(Object.keys(BUILT_IN_STRINGS))
const intorduceBuiltIns = (toks: Token[] = []): Token[] => [...BUILT_IN_TOKENS, ...toks]
const removeBuiltIns = (topLevels: TopLevel[]): TopLevel[] => {
const newTopLevels: TopLevel[] = []
for(const topLevel of topLevels) {
if(topLevel.type == "Expr") newTopLevels.push(topLevel)
else if(topLevel.type == "LetDef") {
const [n, _] = topLevel.val
if(!(BUILT_IN_STRINGS.hasOwnProperty(n))) newTopLevels.push(topLevel)
} else if(topLevel.type == "LetRecDef") {
const vals = topLevel.val
const newTopLevel: TopLevel = {
type: "LetRecDef",
val: vals.filter(([[n, _1], _2]) => !(BUILT_IN_STRINGS.hasOwnProperty(n)))
}
newTopLevels.push(newTopLevel)
}
}
return newTopLevels
}
export { BUILT_IN_STRINGS, BUILT_IN_NAMES, BUILT_IN_TOKENS, intorduceBuiltIns, removeBuiltIns }