blob: 74801bf6c1f0889db0a5b7e979be29b4c6f05387 (
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
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
|
import { ASTNode, NodeType } from "./common";
import { Lex } from "./lexer";
import { Parse } from "./parser";
export type FnError<T extends string> = `Function execution error: ${T}`;
export type ToStringInner<T, Carry extends string = ""> = T extends
| string
| number
? `${T}`
: T extends readonly any[]
? T extends readonly [infer Head, ...infer Tail]
? `${ToStringInner<
Tail,
`${Carry extends "" ? "" : `${Carry}, `}${ToStringInner<Head>}`
>}`
: `[${Carry}]`
: FnError<`Can't stringify`>;
export type UnarrayIfOnlyHead<T extends readonly any[]> = T extends [
infer Head,
infer Next
]
? T
: T extends [infer Head]
? Head
: T;
export type BUILTIN_ARR<Args extends readonly ASTNode[]> = {
[Idx in keyof Args]: Args[Idx] extends ASTNode ? Evaluate<Args[Idx]> : never;
};
export type BUILTIN_ToString<Args extends readonly ASTNode[]> = ToStringInner<
UnarrayIfOnlyHead<{
[Idx in keyof Args]: Args[Idx] extends ASTNode
? ToStringInner<Evaluate<Args[Idx]>>
: never;
}>
>;
// export type BUILTIN_Print<Args extends readonly ASTNode[]>
export type SENTINEL_NO_BUILTIN = "__NO_BUILTIN__";
export type MapBuiltins<Node extends ASTNode> = Node["name"] extends "tostring"
? BUILTIN_ToString<Node["children"]>
: Node["name"] extends "arr"
? BUILTIN_ARR<Node["children"]>
: SENTINEL_NO_BUILTIN;
export type EvalError<T extends string> = `Eval error: ${T}`;
export type Evaluate<Node extends ASTNode> = Node["type"] extends NodeType.INT
? Node["value"]
: Node["type"] extends NodeType.EXT
? MapBuiltins<Node>
: EvalError<`Unhandled node type ${Node["type"]}`>;
const input = `5, arr(5000)` as const;
const lex_result = null as unknown as Lex<typeof input>;
const parse_result = null as unknown as Parse<typeof lex_result>;
const eval_result = null as unknown as Evaluate<typeof parse_result>;
|