-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.ts
85 lines (74 loc) · 1.97 KB
/
ast.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
// TODO: returning in main body
export type Program<A> = {
a?: A;
vars: VarDef<A>[];
funcs: FunDef<A>[];
classes: ClassDef<A>[];
body: Stmt<A>[];
};
export type ClassDef<A> = {
name: string;
fields: VarDef<A>[];
methods: FunDef<A>[];
};
export type VarDef<A> = { a?: A; typedVar: TypedVar<A>; value: Literal<A> };
export type TypedVar<A> = { a?: A; name: string; type: Type };
// TODO: functions that return none
export type FunDef<A> = {
a?: A;
name: string;
params: TypedVar<A>[];
ret: Type;
inits: VarDef<A>[];
body: Stmt<A>[];
};
export type Stmt<A> =
| { a?: A; tag: "assign"; lhs: LValue<A>; value: Expr<A> }
| {
a?: A;
tag: "if";
cond: Expr<A>;
body: Stmt<A>[];
elif?: { cond: Expr<A>; body: Stmt<A>[] };
else?: Stmt<A>[];
}
| { a?: A; tag: "while"; cond: Expr<A>; body: Stmt<A>[] }
| { a?: A; tag: "pass" }
| { a?: A; tag: "return"; expr?: Expr<A> }
| { a?: A; tag: "expr"; expr: Expr<A> };
export type LValue<A> =
| string
| { a?: A; tag: "field"; obj: Expr<A>; field: string };
export type Expr<A> =
| { a?: A; tag: "literal"; value: Literal<A> }
| { a?: A; tag: "id"; name: string }
| { a?: A; tag: "uniop"; op: UniOp; arg: Expr<A> }
| { a?: A; tag: "binop"; op: BinOp; left: Expr<A>; right: Expr<A> }
| { a?: A; tag: "parenthesis"; expr: Expr<A> }
| { a?: A; tag: "call"; name: string; args: Expr<A>[] }
| { a?: A; tag: "getfield"; obj: Expr<A>; field: string }
| { a?: A; tag: "method"; obj: Expr<A>; method: string; args: Expr<A>[] };
export enum UniOp {
NOT = "not",
NEG = "-",
}
// TODO: is
export enum BinOp {
ADD = "+",
SUB = "-",
MUL = "*",
DIV = "//",
MOD = "%",
EQ = "==",
NE = "!=",
LT = "<",
LE = "<=",
GT = ">",
GE = ">=",
IS = "is",
}
export type Literal<A> =
| { a?: A; tag: "none" }
| { a?: A; tag: "bool"; value: boolean }
| { a?: A; tag: "num"; value: number };
export type Type = "int" | "bool" | "none" | { tag: "object"; class: string };