-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvirtual-stack.ts
490 lines (486 loc) · 14.9 KB
/
virtual-stack.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
enum OP_TYPE {
NUMBER,
NAME,
ADD,
ASSIGN,
RETURN,
FUNCTION_DEFINE,
FUNCTION_NATIVE,
CALL,
}
function to_command_str(type) {
if (type == OP_TYPE.NUMBER) return "number";
if (type == OP_TYPE.NAME) return "name";
if (type == OP_TYPE.ADD) return "add";
if (type == OP_TYPE.ASSIGN) return "assign";
if (type == OP_TYPE.FUNCTION_DEFINE) return "function defined";
if (type == OP_TYPE.FUNCTION_NATIVE) return "function native";
if (type == OP_TYPE.CALL) return "call";
if (type == OP_TYPE.RETURN) return "return";
}
let instructions = [
[OP_TYPE.FUNCTION_DEFINE, "calcu",
[
[OP_TYPE.NAME, "a"],
[OP_TYPE.NAME, "b"],
[OP_TYPE.NAME, "d"],
],
[
[OP_TYPE.NAME, "a"],
[OP_TYPE.NAME, "b"],
[OP_TYPE.ADD],
[OP_TYPE.ASSIGN, "c"],
[OP_TYPE.RETURN, "c"],
]],
[OP_TYPE.CALL, "calcu", [
[OP_TYPE.NUMBER, "1"],
[OP_TYPE.NUMBER, "10"],
[OP_TYPE.NUMBER, "13"],
]],
[OP_TYPE.ASSIGN, "outA"],
[OP_TYPE.FUNCTION_NATIVE, "log", console.log],
[OP_TYPE.CALL, "log", [
[OP_TYPE.NAME, "outA"]
]]
];
class _Stack {
base: _Datum[] = [];
frame: Frame;//当前函数执行栈帧
_ptr: number = 0;
get ptr() {
return this.base[this._ptr];
}
push(data: _Datum) {
this.base.push(data);
this._ptr++;
}
pop() {
this._ptr--;
return this.base.pop();
}
}
function main() {
const context = new _Context();
const script = new _Script();
const result = new _Datum(DATUM_TYPE.UNDEF);
codeGenerate(context, script, instructions, 0, "\t");
codeInterpret(context, script, context.globalObject, result);
}
setTimeout(() => main());
//简易栈机虚拟机实现
function codeGenerate(context: _Context, script: _Script, instructions: any[], offset: number = 0, indent: string) {
indent += "\t";
const isCacheOffset = !!offset;
const atoms = script.atoms;
console.log(indent, "<codeGenerat>", instructions.length);
const dataView = new DataView(script.code);
for (let [operator, operand, ...reset] of instructions) {
console.log(indent, "operator", to_command_str(operator));
switch (operator) {
case OP_TYPE.FUNCTION_NATIVE:
case OP_TYPE.FUNCTION_DEFINE:
{
const [params, funBody] = reset;
//根据函数指令创建函数对象
const nameAtom = atoms[getAtomIndex(ATOM_TYPE.NAME, operand)];
let fun: _Function | null = null;
if (operator == OP_TYPE.FUNCTION_DEFINE) {
const funScript = new _Script();
fun = new _Function(nameAtom, funScript, context.staticLink);
fun.scope = new _Scope(context.staticLink);
//创建函数体
codeGenerate(context, funScript, funBody, 0, indent);
//提前创建参数符号
funScript.args = params.map(param => {
const parmaSymbol = new _Symbol(fun.scope, SYMOBL_TYPE.ARGUMENT, { key: funScript.atoms.find(item => item.val == param[1]) });
parmaSymbol.slot = -1;//占位
return parmaSymbol;
});
fun.scope.list.push(...fun.script.args);
} else {
fun = new _Function(nameAtom, null, context.staticLink);
fun.call = reset[0];
}
//创建方法符号
const funPropery = new _Property(new _Datum(DATUM_TYPE.FUNCTION, fun));
const funSymbol = new _Symbol(context.staticLink, SYMOBL_TYPE.PROPERTY, { key: nameAtom, value: funPropery });
fun.name = nameAtom;
//给作用域添加符号
context.staticLink.list.push(funSymbol);
}
break;
case OP_TYPE.CALL:
{
const [params] = reset;
appendBuffer(OP_TYPE.NAME, getAtomIndex(ATOM_TYPE.NAME, operand));
offset = codeGenerate(context, script, params, offset, indent);
appendBuffer(OP_TYPE.CALL, params.length);
}
break;
case OP_TYPE.NUMBER:
appendBuffer(OP_TYPE.NUMBER, getAtomIndex(ATOM_TYPE.NUMBER, parseInt(operand)));
break;
case OP_TYPE.NAME:
appendBuffer(OP_TYPE.NAME, getAtomIndex(ATOM_TYPE.NAME, operand));
break;
case OP_TYPE.ASSIGN:
appendBuffer(OP_TYPE.NAME, getAtomIndex(ATOM_TYPE.NAME, operand));
appendBuffer(operator);
break;
case OP_TYPE.RETURN:
appendBuffer(OP_TYPE.NAME, getAtomIndex(ATOM_TYPE.NAME, operand));
appendBuffer(operator);
break;
case OP_TYPE.ADD:
appendBuffer(operator);
break;
}
}
if (!isCacheOffset)
script.code = script.code.slice(0, offset);
return offset;
function appendBuffer(...params: number[]) {
console.log(indent, "appendBuffer", to_command_str(params[0]));
params.forEach(value => {
dataView.setUint8(offset, value);
offset += 8;
});
}
function getAtomIndex(type, val) {
let index = atoms.findIndex(item => item.flag == type && item.val == val);
if (index >= 0) return index;
atoms.push(new _Atom(type, val));
return atoms.length - 1;
}
}
function codeInterpret(context: _Context, script: _Script, slink: _Scope, result: _Datum) {
console.log("<codeInterpret>");
const stack = context.stack;
const atoms = script.atoms;
const codeView = new DataView(script.code);
let pc = 0;
//保存旧上下文
const oldslink = context.staticLink;
context.staticLink = slink;
while (pc < codeView.byteLength) {
//取指令
let opt = popBuffer();
console.log("pc", to_command_str(opt));
//分析处理指令
switch (opt) {
case OP_TYPE.CALL:
{
//通过nameAtom从当前作用域中找到symbol
let argc = popBuffer();
//直接从symbol中获取到function
const funDatum = stack.base[stack._ptr - argc - 1];
resolveValue(funDatum);
//创建栈帧
const frame = new Frame(stack);
frame.argc = argc;
frame._argv = stack._ptr - argc;
frame._vars = stack._ptr;
frame.fun = funDatum.fun;
frame.down = context.stack.frame;
//将已压入栈的参数,参数符号的栈实参位置设置
if (frame.fun.script) {
for (let i = 0; i < argc; i++) {
resolveValue(frame.argv[i]);
frame.fun.script.args[i].slot = i;
}
}
//压入栈
stack.frame = frame;
const result = new _Datum(DATUM_TYPE.UNDEF);
if (frame.fun.call) {
let params = frame.argv.map(item => {
resolveValue(item);
return item;
}).map(item => item.nval || item.sval);
frame.fun.call(...params);
} else {
codeInterpret(context, frame.fun.script, frame.fun.scope, result);
}
//调用完毕恢复栈帧
stack.frame.vars.forEach(() => stack.pop());//动态变量弹出
stack.frame.argv.forEach(() => stack.pop());//参数弹出
stack.pop();//方法名弹出
stack.frame = frame.down;
//结果压入栈中
stack.push(result);
}
break;
case OP_TYPE.NUMBER:
stack.push(atomTempDatum(getAtom()));
break;
case OP_TYPE.NAME:
stack.push(atomTempDatum(getAtom()));
break;
case OP_TYPE.ASSIGN:
{
let isOk: boolean, lval: _Datum, rval: _Datum;
lval = stack.pop();
resolveSymbol(lval);
rval = stack.pop();
isOk = resolveValue(rval);
if (!isOk) return;
if (lval.flag != DATUM_TYPE.SYMBOL) {
//定义symbol
let symbol = new _Symbol(context.staticLink, SYMOBL_TYPE.VARIABLE, { key: lval.atom });
if (stack.frame) {
stack.push(rval);
stack.frame.nvars++;
symbol.slot = stack.frame.nvars - 1;
} else {
symbol.entry.value = rval;
}
context.staticLink.list.push(symbol);
} else {
const sym = lval.symbol;
//已经是符号,根据符号类型,修改值
switch (sym.type) {
case SYMOBL_TYPE.ARGUMENT:
case SYMOBL_TYPE.VARIABLE:
if (sym.entry.value || !stack.frame)
sym.entry.value = rval;
else if (sym.type == SYMOBL_TYPE.ARGUMENT) stack.frame.argv[sym.slot] = rval;
else stack.frame.nvars[sym.slot] = rval;
break;
case SYMOBL_TYPE.PROPERTY:
(sym.entry.value as _Property).datum = rval;
break;
}
}
}
break;
case OP_TYPE.RETURN:
{
let val = stack.pop();
resolveValue(val);
Object.assign(result, val);
}
break;
case OP_TYPE.ADD:
{
let rval: _Datum, lval: _Datum;
rval = stack.pop();
lval = stack.pop();
resolveValue(rval);
resolveValue(lval);
const value = lval.nval + rval.nval;
stack.push(new _Datum(DATUM_TYPE.NUMBER, value));
}
break;
}
}
//还原旧上下文
context.staticLink = oldslink;
function resolveValue(datum: _Datum): boolean {
resolveSymbol(datum);
//如果datum还是符号,则需要进一步解析符号
if (datum.flag == DATUM_TYPE.SYMBOL) {
if (SYMOBL_TYPE.PROPERTY == datum.symbol.type) {
Object.assign(datum, (datum.symbol.entry.value as _Property).datum);
resolvePrimary(datum);
return true;
}
//参数和变量先从符号上尝试读取
if (datum.symbol.entry.value) {
Object.assign(datum, datum.symbol.entry.value);
resolvePrimary(datum);
return true;
}
//否则就从栈里找到该符号的栈帧中存储的变量
let targetFp: Frame;
for (let fp = stack.frame; fp; fp = fp.down) {
if (fp.fun.scope == datum.symbol.scope) {
targetFp = fp;
break;
}
}
if (!targetFp) return false;
if (datum.symbol.type == SYMOBL_TYPE.ARGUMENT) Object.assign(datum, targetFp.argv[datum.symbol.slot]);
else Object.assign(datum, targetFp.vars[datum.symbol.slot]);
resolvePrimary(datum);
} else if (datum.flag == DATUM_TYPE.ATOM) {
if (datum.atom.flag == ATOM_TYPE.NUMBER) {
datum.flag = DATUM_TYPE.NUMBER;
datum.nval = datum.atom.val as number;
} else if (datum.atom.flag == ATOM_TYPE.STRING) {
datum.flag = DATUM_TYPE.STRING;
datum.sval = datum.atom.val as string;
} else return false;
}
return true;
}
function resolvePrimary(datum: _Datum) {
if (datum.flag != DATUM_TYPE.ATOM) return;
resolveValue(datum);
}
/**
* 将如果是字面量找到symbol
*/
function resolveSymbol(datum: _Datum): boolean {
if (datum.flag == DATUM_TYPE.SYMBOL) return true;
if (datum.flag == DATUM_TYPE.ATOM) {
const symbol = findSymbolByAtom(context.staticLink, datum.atom);
if (!symbol) return false;
datum.symbol = symbol;
datum.flag = DATUM_TYPE.SYMBOL;
return true;
}
return false;
function findSymbolByAtom(scope: _Scope, atom: _Atom) {
let symbol = scope.list.find(symbol => symbol.entry?.key == atom);
if (!symbol && scope.parent) {
symbol = findSymbolByAtom(scope.parent, atom);
}
return symbol;
}
}
function atomTempDatum(atom: _Atom) {
return new _Datum(DATUM_TYPE.ATOM, atom);
}
function getAtom(): _Atom {
return atoms[popBuffer()];
}
function popBuffer() {
const index = codeView.getUint8(pc);
pc += 8;
return index;
}
}
//全局上下文
class _Context {
constructor() {
this.stack = new _Stack();
this.globalObject = new _Scope();
this.staticLink = this.globalObject;
}
staticLink: _Scope;//静态作用域
globalObject: _Scope;//顶级作用域
stack: _Stack;//执行的全局操作数存储区
}
//函数执行的上下文
class Frame {
_stack: _Stack;
fun: _Function;
constructor(stack: _Stack) {
this._stack = stack;
this.argc = 0;
this.nvars = 0;
this._argv = this._stack._ptr;
this._vars = this._stack._ptr;
}
argc: number;//函数参数数量
nvars: number;//本地变量数量
_argv: number;//stack的参数索引
_vars: number;//stack的参数索引
get argv() {
return this._stack.base.slice(this._argv, this._argv + this.argc);
};
get vars() {
return this._stack.base.slice(this._vars, this._vars + this.nvars);
}
down: Frame;//上一个执行栈帧
}
//表示字面量
enum ATOM_TYPE {
NUMBER,
STRING,
NAME
}
class _Atom {
constructor(flag: ATOM_TYPE, val: number | string) {
this.val = val;
this.flag = flag;
}
val: number | string;
flag: ATOM_TYPE;
}
class _Script {
constructor() {
this.code = new ArrayBuffer(1000);
this.atoms = [];
this.args = [];
}
args: _Symbol[];//参数
code: ArrayBuffer;//二进制指令
atoms: _Atom[];//字面量数组
}
class _Function {
constructor(name: _Atom, script: _Script, parent: _Scope) {
this.name = name;
this.script = script;
this.scope = parent;
}
scope: _Scope;
name: _Atom;
script: _Script;
call?: (...args) => void;//原生方法
}
class _Property {
datum: _Datum;
constructor(datum: _Datum) {
this.datum = datum;
}
}
enum SYMOBL_TYPE {
ARGUMENT,//栈中
VARIABLE,//栈中
PROPERTY,//symbol的值
}
type _SymbolEntry = {
key: _Atom;
value?: _Datum | _Property;
}
class _Symbol {
constructor(scope: _Scope, type: SYMOBL_TYPE, entry: _SymbolEntry) {
this.scope = scope;
this.type = type;
this.entry = entry;
}
scope: _Scope;//通过scope确定栈帧,在通过当前栈帧和sym的栈帧和slot确定stack中的datum
entry: _SymbolEntry;
slot: number;//符号在栈中的索引
type: SYMOBL_TYPE;//符号的类型 参数/变量
}
//存储入栈数据源 method,virable
enum DATUM_TYPE {
FUNCTION,//如果是方法则从获得函数信息
SYMBOL,//已解析的符号,在stack中有引用
ATOM,//基础元数据(字面量)类型,从atom中获取
STRING,
NUMBER,
OBJECT,
BOOL,
UNDEF,
}
type DATUM_VALUE = _Atom | _Function | _Symbol | number | boolean | string | _Scope;
class _Datum {
constructor(flag: DATUM_TYPE, value?: DATUM_VALUE) {
this.flag = flag;
if (flag == DATUM_TYPE.FUNCTION) this.fun = value as _Function;
if (flag == DATUM_TYPE.ATOM) this.atom = value as _Atom;
if (flag == DATUM_TYPE.SYMBOL) this.symbol = value as _Symbol;
if (flag == DATUM_TYPE.STRING) this.sval = value as string;
if (flag == DATUM_TYPE.NUMBER) this.nval = value as number;
if (flag == DATUM_TYPE.BOOL) this.bval = value as boolean;
if (flag == DATUM_TYPE.OBJECT) this.object = value as _Scope;
}
atom: _Atom;
object: _Scope;
fun: _Function;
symbol: _Symbol;
nval: number;
bval: boolean;
sval: string;
flag: DATUM_TYPE;
}
class _Scope {
constructor(parent?: _Scope) {
this.parent = parent;
}
list: _Symbol[] = [];//所有符号
parent: _Scope | null;
}