This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExecutionTree.ts
86 lines (78 loc) · 1.72 KB
/
ExecutionTree.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
export const enum ENodeType {
"constant" = "constant",
"variable" = "variable",
"if" = "if",
"for" = "for",
"while" = "while",
"assign" = "assign",
"operation" = "operation",
}
export const enum EOperationType {
function = "func",
Neg = "!",
Plus = "+",
Minus = "-",
Mul = "*",
Div = "/",
Mod = "%",
Eq = "==",
EqNot = "!=",
Lt = "<",
Gt = ">",
Lte = "<=",
Gte = ">=",
BitShiftLeft = "<<",
BitShiftRight = ">>",
BitNeg = "~",
BitAnd = "&",
BitXor = "^",
BitOr = "|",
And = "&&",
Or = "||",
}
export type TNode = TNodeStatement;
export type TNodeStatement = TNodeExpression | TNodeStIf | TNodeStWhile | TNodeStFor | TNodeStatement[];
export type TNodeStIf = {
type: ENodeType.if,
cond: TNodeExpression,
then: TNodeStatement,
el?: TNodeStatement,
};
export type TNodeStWhile = {
type: ENodeType.while,
cond: TNodeExpression,
then: TNodeStatement,
/** is this`do {} while () ?` */
condAfter?: true,
};
export type TNodeStFor = {
type: ENodeType.for,
then: TNodeStatement,
pre?: TNodeExpression,
step?: TNodeExpression,
cond?: TNodeExpression,
};
export type TNodeExpression = TNodeVariable
| TNodeConstant | TNodeOperation | TNodeExpressionAssign;
export type TNodeVariable = {
type: ENodeType.variable,
name: string,
};
export type TNodeExpressionAssign = {
type: ENodeType.assign,
target: TNodeVariable,
expr: TNodeExpression,
};
export type TNodeOperation = {
type: ENodeType.operation,
args: TNodeExpression[],
} & ({
operation: EOperationType.function,
fnc: TNodeVariable,
} | {
operation: Exclude<EOperationType, EOperationType.function>,
});
export type TNodeConstant = {
type: ENodeType.constant,
value: boolean | string | number,
};