Sun, 04 Apr 2010 21:08:23 +0100
Style fix
0 | 1 | |
2 | var OP_LOADK = 1; | |
3 | var OP_GETGLOBAL = 5; | |
4 | var OP_CALL = 28; | |
5 | ||
6 | function LValue(type, value) | |
7 | { | |
8 | this.type = type||"nil"; | |
9 | this.value = value||null; | |
10 | } | |
11 | ||
12 | LValue.prototype = { | |
13 | call: function (args) | |
14 | { | |
15 | if(typeof(this.value) == "function") | |
16 | { | |
17 | return this.value.apply(null, args.map(function (a) { return a.value; })); | |
18 | } | |
19 | }, | |
20 | index: function (key) | |
21 | { | |
22 | if(this.type == "table") | |
23 | { | |
24 | return this.value[key.value]; | |
25 | } | |
26 | }, | |
27 | setIndex: function (key, value) | |
28 | { | |
29 | if(this.type == "table") | |
30 | { | |
31 | this.value[key.value] = value; | |
32 | } | |
33 | } | |
34 | }; | |
35 | ||
36 | function LValueFromString(string) | |
37 | { | |
38 | return new LValue("string", string); | |
39 | } | |
40 | ||
41 | function LValueFromFunction(func) | |
42 | { | |
43 | return new LValue("function", func); | |
44 | } | |
45 | ||
46 | var default_environment = new LValue("table", {}); | |
47 | ||
48 | var print; | |
49 | if(typeof(document) == "object") | |
1 | 50 | print = function (a) { document.write(a+"<br/>") }; // Browser |
0 | 51 | else |
1 | 52 | print = require("sys").puts; // Nodejs |
53 | ||
0 | 54 | default_environment.setIndex(LValueFromString("print"), LValueFromFunction(print)); |
55 | ||
56 | function LFunction() | |
57 | { | |
58 | this.constants = []; | |
59 | this.instructions = []; | |
60 | this.environment = default_environment; | |
61 | return this; | |
62 | } | |
63 | ||
64 | LFunction.prototype = { | |
65 | addInstruction: function (instruction) | |
66 | { | |
67 | this.instructions.push(instruction); | |
68 | }, | |
69 | addConstant: function (constant) | |
70 | { | |
71 | this.constants.push(constant); | |
72 | } | |
73 | }; | |
74 | ||
75 | function LVM() | |
76 | { | |
77 | this.callstack = []; | |
78 | this.stack = []; | |
79 | return this; | |
80 | } | |
81 | ||
82 | LVM.prototype = { | |
83 | run: function (lfFunction) | |
84 | { | |
85 | this.currentFrame = {f:lfFunction,pc:0}; | |
86 | this.callstack.push(this.currentFrame); | |
87 | var instruction; | |
88 | while(this.callstack.length>0) | |
89 | { | |
90 | instruction = this.currentFrame.f.instructions[this.currentFrame.pc++]; | |
91 | if(!instruction) | |
92 | break; | |
93 | switch(instruction[0]) | |
94 | { | |
95 | case OP_GETGLOBAL: | |
96 | var name = this.currentFrame.f.constants[instruction[2]]; | |
97 | this.stack[instruction[1]] = this.currentFrame.f.environment.index(name); | |
98 | break; | |
99 | case OP_LOADK: | |
100 | var value = this.currentFrame.f.constants[instruction[2]]; | |
101 | this.stack[instruction[1]] = value; | |
102 | break; | |
103 | case OP_CALL: | |
104 | var f = this.stack[instruction[1]]; | |
105 | f.call(this.stack.splice(instruction[1]+1, instruction[1]+(instruction[2]-1))); | |
106 | break; | |
107 | case OP_RETURN: | |
108 | this.callstack.pop(); | |
109 | break; | |
110 | default: | |
111 | } | |
112 | } | |
113 | } | |
114 | }; | |
115 | ||
116 | var testvm = new LVM(); | |
117 | var mycode = new LFunction(); | |
118 | mycode.addConstant(LValueFromString("print")); | |
119 | mycode.addConstant(LValueFromString("Hello world")); | |
120 | mycode.addInstruction([OP_GETGLOBAL, 0, 0]); | |
121 | mycode.addInstruction([OP_LOADK, 1, 1]); | |
122 | mycode.addInstruction([OP_CALL, 0, 2, 1]); | |
123 | testvm.run(mycode); |