This repository was 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
/
Copy pathIntrerpreter.ts
165 lines (150 loc) · 5.47 KB
/
Intrerpreter.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
TNode, TNodeExpression, TNodeVariable, ENodeType,
EOperationType, TNodeStFor, TNodeStWhile, TNodeExpressionAssign, TNodeConstant, TNodeStIf, TNodeOperation
} from "./ExecutionTree";
import { readIntSync, formatString } from "./common";
import { isBoolean } from "util";
export class Interpreter {
private evalFor(node: TNodeStFor) {
let res: any;
for (this.eval(node.pre);
node.cond ? this.eval(node.cond) : true;
this.eval(node.step)) {
res = this.eval(node.then);
}
return res;
}
private evalWhile(node: TNodeStWhile) {
let res: any;
if (!node.condAfter)
while (this.eval(node.cond)) {
res = this.eval(node.then);
}
else
do {
res = this.eval(node.then);
} while (this.eval(node.cond));
return res;
}
private evalIf(node: TNodeStIf) {
const res = this.eval(node.cond);
if (res)
return this.eval(node.then);
return this.eval(node.el);
}
private evalAssign(node: TNodeExpressionAssign) {
const exprRes = this.eval(node.expr);
this.context[node.target.name] = exprRes;
return exprRes;
}
private evalConstant(node: TNodeConstant) {
if (typeof node.value === "string")
return unescape(node.value).split("\\n").join("\n");
if (typeof node.value === "boolean")
return node.value && 1 || 0;
return node.value;
}
private evalVariable(node: TNodeVariable): number | ((...args: TNodeExpression[]) => number) {
return this.context[node.name] || 0;
}
private evalOperation(node: TNodeOperation) {
if (node.operation === EOperationType.function) {
const fnc = this.evalVariable(node.fnc);
if (typeof fnc !== "function")
throw new Error(`given argument is not function name:${JSON.stringify(node.fnc)}`);
// function will eval arguments by itself
return fnc(...node.args);
} else {
const fnc = getFucntionFromOperator(node.operation);
const res = fnc(...node.args.map(x => this.eval(x)));
return isBoolean(res) ? res && 1 || 0 : res;
}
}
private eval(node: TNode): any {
if (!node)
return;
if (Array.isArray(node)) {
return node.map(x => this.eval(x));
} else
switch (node.type) {
case ENodeType.assign:
return this.evalAssign(node);
case ENodeType.constant:
return this.evalConstant(node);
case ENodeType.for:
return this.evalFor(node);
case ENodeType.if:
return this.evalIf(node);
case ENodeType.operation:
return this.evalOperation(node);
case ENodeType.variable:
return this.evalVariable(node);
case ENodeType.while:
return this.evalWhile(node);
default:
throw Error(`invalid node type ${(node || {} as any).type}`);
}
}
private readonly context = {
print: (strËxpr: TNodeExpression, ...otherArgs: TNodeExpression[]) => {
const otherArgsRes = this.eval(otherArgs);
const str = this.eval(strËxpr);
const fomrated = formatString(str, otherArgsRes);
process.stdout.write(fomrated);
},
scan: (varName: TNodeVariable) => {
return this.eval({
type: ENodeType.assign,
expr: {
type: ENodeType.operation,
operation: EOperationType.function,
args: [],
fnc: {
type: ENodeType.variable,
name: "$scan",
},
},
target: varName,
});
},
$scan: () => {
return readIntSync();
},
};
public start() {
return this.eval(this.tree);
}
private readonly tree: TNode;
constructor(tree: TNode) {
this.tree = tree;
}
}
const getFucntionFromOperator = (operator: Exclude<EOperationType, EOperationType.function>):
((...args: number[]) => (number | boolean)) => {
switch (operator) {
case EOperationType.Plus: return (...args: number[]) => args[0] + (args[1] || 0);
case EOperationType.Minus: return (...args: number[]) =>
args.length === 1 ? - args[0] : args[0] - args[1];
case EOperationType.Mul: return (...args: number[]) => args[0] * args[1];
case EOperationType.Div: return (...args: number[]) => args[0] / args[1];
case EOperationType.Mod: return (...args: number[]) => args[0] % args[1];
case EOperationType.Eq: return (...args: number[]) => args[0] === args[1];
case EOperationType.EqNot: return (...args: number[]) => args[0] !== args[1];
case EOperationType.Lt: return (...args: number[]) => args[0] < args[1];
case EOperationType.Gt: return (...args: number[]) => args[0] > args[1];
case EOperationType.Lte: return (...args: number[]) => args[0] <= args[1];
case EOperationType.Gte: return (...args: number[]) => args[0] >= args[1];
case EOperationType.And: return (...args: number[]) => args[0] && args[1];
case EOperationType.Or: return (...args: number[]) => args[0] || args[1];
// tslint:disable: no-bitwise
case EOperationType.BitShiftLeft: return (...args: number[]) => args[0] << args[1];
case EOperationType.BitShiftRight: return (...args: number[]) => args[0] >> args[1];
case EOperationType.BitAnd: return (...args: number[]) => args[0] & args[1];
case EOperationType.BitXor: return (...args: number[]) => args[0] ^ args[1];
case EOperationType.BitOr: return (...args: number[]) => args[0] | args[1];
// tslint:enable: no-bitwise
case EOperationType.Neg: return (...args: number[]) => !args[0];
default:
throw new Error(`operator not recognized '${operator}'`);
}
};