-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathinterpreter2.js
executable file
·184 lines (170 loc) · 5.49 KB
/
interpreter2.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const parser = require('@babel/parser');
const { codeFrameColumns } = require('@babel/code-frame');
const chalk = require('chalk');
const sourceCode = `
const a = 2;
function add(a, b) {
return a + b;
}
console.log(add(1, 2));
`;
const ast = parser.parse(sourceCode, {
sourceType: 'unambiguous'
});
class Scope {
constructor(parentScope) {
this.parent = parentScope;
this.declarations = [];
}
set(name, value) {
this.declarations[name] = value;
}
getLocal(name) {
return this.declarations[name];
}
get(name) {
let res = this.getLocal(name);
if (res === undefined && this.parent) {
res = this.parent.get(name);
}
return res;
}
has(name) {
return !!this.getLocal(name);
}
}
function getIdentifierValue(node, scope) {
if (node.type === 'Identifier') {
return scope.get(node.name);
} else {
return evaluate(node, scope);
}
}
const evaluator = (function() {
const astInterpreters = {
Program (node, scope) {
node.body.forEach(item => {
evaluate(item, scope);
})
},
VariableDeclaration(node, scope) {
node.declarations.forEach((item) => {
evaluate(item, scope);
});
},
VariableDeclarator(node, scope) {
const declareName = evaluate(node.id);
if (scope.get(declareName)) {
throw Error('duplicate declare variable:' + declareName);
} else {
scope.set(declareName, evaluate(node.init, scope));
}
},
ExpressionStatement(node, scope) {
return evaluate(node.expression, scope);
},
MemberExpression(node, scope) {
const obj = scope.get(evaluate(node.object));
return obj[evaluate(node.property)]
},
FunctionDeclaration(node, scope) {
const declareName = evaluate(node.id);
if (scope.get(declareName)) {
throw Error('duplicate declare variable:' + declareName);
} else {
scope.set(declareName, function(...args) {
const funcScope = new Scope();
funcScope.parent = scope;
node.params.forEach((item, index) => {
funcScope.set(item.name, args[index]);
});
funcScope.set('this', this);
return evaluate(node.body, funcScope);
});
}
},
ReturnStatement(node, scope) {
return evaluate(node.argument, scope);
},
BlockStatement(node, scope) {
for (let i = 0; i< node.body.length; i++) {
if (node.body[i].type === 'ReturnStatement') {
return evaluate(node.body[i], scope);
}
evaluate(node.body[i], scope);
}
},
CallExpression(node, scope) {
const args = node.arguments.map(item => {
if (item.type === 'Identifier') {
return scope.get(item.name);
}
return evaluate(item, scope);
});
if(node.callee.type === 'MemberExpression') {
const fn = evaluate(node.callee, scope);
const obj = evaluate(node.callee.object, scope);
return fn.apply(obj, args);
} else {
const fn = scope.get(evaluate(node.callee, scope));
return fn.apply(null, args);
}
},
BinaryExpression(node, scope) {
const leftValue = getIdentifierValue(node.left, scope);
const rightValue = getIdentifierValue(node.right, scope);;
switch(node.operator) {
case '+':
return leftValue + rightValue;
case '-':
return leftValue - rightValue;
case '*':
return leftValue * rightValue;
case '/':
return leftValue / rightValue;
default:
throw Error('upsupported operator:' + node.operator);
}
},
Identifier(node, scope) {
return node.name;
},
NumericLiteral(node, scope) {
return node.value;
}
}
const evaluate = (node, scope) => {
try {
return astInterpreters[node.type](node, scope);
} catch(e) {
if (e && e.message && e.message.indexOf('astInterpreters[node.type] is not a function') != -1) {
console.error('unsupported ast type: ' + node.type);
console.error(codeFrameColumns(sourceCode, node.loc, {
highlightCode: true
}));
} else {
console.error(node.type + ':',e.message);
console.error(codeFrameColumns(sourceCode, node.loc, {
highlightCode: true
}));
}
}
}
return {
evaluate
}
})();
const globalScope = new Scope();
globalScope.set('console', {
log: function (...args) {
console.log(chalk.green(...args));
},
error: function (...args) {
console.log(chalk.red(...args));
},
error: function (...args) {
console.log(chalk.orange(...args));
},
});
evaluator.evaluate(ast.program, globalScope);
// console.log(globalScope);