-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathutils.ts
37 lines (31 loc) · 999 Bytes
/
utils.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
import { Value, Type } from "./ast";
export function PyValue(typ: Type, result: number): Value {
switch (typ.tag) {
case "number":
return PyInt(result);
case "bool":
return PyBool(Boolean(result));
case "class":
return PyObj(typ.name, result);
case "none":
return PyNone();
}
}
export function PyInt(n: number): Value {
return { tag: "num", value: n };
}
export function PyBool(b: boolean): Value {
return { tag: "bool", value: b };
}
export function PyObj(name: string, address: number): Value {
if (address === 0) return PyNone();
else return { tag: "object", name, address };
}
export function PyNone(): Value {
return { tag: "none" };
}
export const NUM : Type = {tag: "number"};
export const BOOL : Type = {tag: "bool"};
export const NONE : Type = {tag: "none"};
export const TYPE_VAR : Type = {tag: "type-var"};
export function CLASS(name : string, genericArgs: Array<Type> = null) : Type {return {tag: "class", name, genericArgs}};