-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.js
More file actions
134 lines (113 loc) · 3.76 KB
/
Copy pathInterpreter.js
File metadata and controls
134 lines (113 loc) · 3.76 KB
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
const fs = require('fs');
class Interpreter {
constructor() {
this.environment = {
'$': 0,
};
/**
* to coś jest po to aby łapał np $[$[0]] -> $[2] -> 10
*/
this.memory = {
};
}
run(code) {
const ast = typeof code === 'string' ? JSON.parse(code) : code;
const Abydziałało = process.stdin.isTTY;
if (Abydziałało) {
process.stdin.setRawMode(true);
process.stdin.resume();
}
try {
this.evaluate(ast);
} finally {
if (Abydziałało) {
process.stdin.setRawMode(false);
process.stdin.pause();
}
}
}
evaluateCondition(condStr) {
const tokens = condStr.trim().match(/(-e|-ne|-l|-g)|(\$\[.+?\])|(-?\d+)/g);
if (!tokens || tokens.length < 3) return false;
const op = tokens[0];
const getVal = (r) => {
if (r.startsWith('$[')) {
let inner = r.slice(2, -1);
let i;
if (inner.startsWith('$[')) {
i = getVal(inner);
} else {
i = parseInt(inner, 10);
}
return this.memory[i] || 0;
}
return parseInt(r, 10);
};
const left = getVal(tokens[1]);
const right = getVal(tokens[2]);
switch (op) {
case '-e': return left === right;
case '-ne': return left !== right;
case '-l': return left < right;
case '-g': return left > right;
default: return false;
}
}
evaluate(node) {
if (!node) return;
switch (node.type) {
case 'Program': {
node.body.forEach(statement => this.evaluate(statement));
break;
}
case 'Literal':
return node.value;
case 'Identifier':
return this.environment[node.name];
case 'MemoryAccess':
return this.memory[this.evaluate(node.index)] || 0;
case 'AssignmentStatement':{
const i = this.evaluate(node.target.index);
const val = this.evaluate(node.value);
this.memory[i] = val;
return val;
}
case 'BinaryExpression': {
const left = this.evaluate(node.left);
const right = this.evaluate(node.right);
return node.operator === '+' ? left + right : left - right;
}
case 'PrintStatement': {
const asciiCode = this.evaluate(node.expression);
process.stdout.write(String.fromCharCode(asciiCode));
break;
}
case 'InputStatement':
return this._getch();
case 'RepeatStatement': {
while (this.evaluateCondition(node.condition) === false) {
const buf = Buffer.alloc(1);
try {
if (fs.readSync(0, buf, 0, 1, null) && buf[0] === 3) {
process.exit(0);
}
} catch (e) {}
for (const statement of node.body) {
this.evaluate(statement);
}
}
break;
}
default: throw new Error(`Błąd: ${node.type}`);
}
}
_getch() {
const buffer = Buffer.alloc(1);
try {
fs.readSync(0, buffer, 0, 1);
if (buffer[0] === 3) process.exit(0);
return buffer[0];
} catch (e) { return 0; }
}
}
module.exports = Interpreter;