-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua.rs
367 lines (289 loc) · 9.8 KB
/
lua.rs
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
extern mod luaval;
use core::hashmap::linear;
use core::ops::*;
use core::vec::*;
use core::str;
use core::task::try;
use luaval::luaval::*;
/*
fn lpcall(reg: &mut ~[LuaVal]) -> ~[LuaVal] {
let luafunc = reg.remove(0);
match luafunc {
LFunc(subexec) => {
let owned_subexec = subexec.clone();
do try {
run(owned_subexec, reg);
}
}
_ => fail!(~"Tried to call a non-function!"),
};
return ~[];
}
*/
fn ltype(reg: &mut ~[LuaVal]) -> ~[LuaVal] {
return ~[ LString(@match reg[0] {
LString(_) => ~"string",
LNum(_) => ~"number",
LBool(_) => ~"boolean",
LTable(_, _) => ~"table",
LFunc(_) => ~"function",
LRustFunc(_) => ~"function",
LNil => ~"nil",
})
]
}
fn lprint(reg: &mut ~[LuaVal]) -> ~[LuaVal] {
let strs = map(*reg, |v| { v.to_str() });
let line = str::connect(strs, ~"\t");
io::println(line);
return ~[];
}
fn list_get(reg: &mut ~[LuaVal]) -> ~[LuaVal] {
let table = match reg[0] {
LTable(table, _) => table,
_ => fail!(~"ipairs: Expecting a table in arg 1!"),
};
let index = match reg[1] {
LNum(v) => v,
_ => fail!(~"ipairs: Expecting a table arg 2!"),
};
let lua_index = LNum(index+1f);
if table.contains_key(&lua_index) {
return ~[lua_index, *table.get(&lua_index ) ];
}
return ~[LNil, LNil];
}
fn ipairs(reg: &mut ~[LuaVal]) -> ~[LuaVal] {
let table = reg[0];
return ~[LRustFunc(list_get), table, LNum(0f) ];
}
fn run( execution: &Execution, regs: &mut ~[LuaVal] ) -> ~[LuaVal] {
let mut pc = 0;
let mut &execution_prime = &(*execution);
let mut ®s_prime = regs;
loop {
match execution_prime.prog[pc] {
IReturn(src, extent) => return from_fn( (extent-1) as uint, |i| { regs_prime[src+i as int] }),
ITailCall(func, extent, _) => match regs_prime[func] {
LFunc(f_execution) => {
execution_prime = copy(*f_execution);
regs_prime = from_fn( (extent-1) as uint, |i| { regs_prime[func+1+i as int] });
pc = 0;
}
_ => fail!(~"Cannot tail call to non-Lua function!")
},
_ => step(execution_prime.prog[pc], &mut pc, &mut regs_prime, &execution_prime.constants, execution_prime.globals)
}
}
}
fn step( instr: Instr, pc: &mut int, reg: &mut ~[LuaVal], constants: &~[LuaVal], globals: &mut linear::LinearMap<LuaVal, LuaVal> ) {
let jump = |n| *pc+=n;
let bump = || jump(1);;
let reg_k = |r: int| if r<0 { constants[-r - 1] } else { reg[r] };
let num_to_bool = |n:int| match n { 0 => false, _ => true };
let lval_to_bool = |lval:LuaVal| match lval {
LBool(x) => x,
LNil => false,
_ => true,
};
bump();
match instr {
IEq(check, t1, t2) => if (reg_k(t1) == reg_k(t2)) != num_to_bool(check) { bump(); },
ILt(check, t1, t2) => if (reg_k(t1) < reg_k(t2)) != num_to_bool(check) { bump(); },
ILe(check, t1, t2) => if (reg_k(t1) <= reg_k(t2)) != num_to_bool(check) { bump(); },
ITest(r, c) => if !(lval_to_bool(reg[r]) != num_to_bool(c)) { bump(); },
ITestSet(dst, r, c) => if (lval_to_bool(reg[r]) != num_to_bool(c)) { reg[dst] = reg[r]; } else { bump(); },
IAdd(dst, r1, r2) => reg[dst] = reg_k(r1) + reg_k(r2),
ISub(dst, r1, r2) => reg[dst] = reg_k(r1) - reg_k(r2),
IMul(dst, r1, r2) => reg[dst] = reg_k(r1) * reg_k(r2),
IDiv(dst, r1, r2) => reg[dst] = reg_k(r1) / reg_k(r2),
// TODO: this should operate over all the strings between r1 and r2
IConcat(dst, r1, r2) => reg[dst] = LString(@(reg_k(r1).to_str() + reg_k(r2).to_str())),
INot(dst, src) => reg[dst] = LBool(!lval_to_bool(reg[src])),
IUnm(dst, src) => reg[src] = match reg[dst] {
LNum(x) => LNum(-x),
_ => fail!(~"Attempt to perform arithmetic on a non-number value!")
},
ILen(dst, src) => reg[dst] = match reg[src] {
LString(x) => LNum(x.len() as float),
// TODO: need to handle tables and metatables
_ => fail!(~"Not a string!"),
},
ILoadK(dst, src) => reg[dst] = constants[src],
ILoadNil(start, end) => { grow(reg, end, &LNil); for uint::range(start, end) |i| { reg[i] = LNil; }; }
IMove(r1, r2) => reg[r1] = reg_k(r2),
ILoadBool(dst, b, c) => { reg[dst] = LBool(num_to_bool(b)); if num_to_bool(c) { bump(); } },
IGetGlobal(dst, k) => reg[dst] = match globals.find(&constants[k]) {
Some(v) => *v,
None => LNil,
},
ISetGlobal(src, k) => {globals.insert(constants[k], reg[src]); },
INewTable(dst, _, _) => reg[dst] = LTable(@mut linear::LinearMap::new(), @[]),
IGetTable(dst, tbl, index) => match reg[tbl] {
LTable(table, _) => reg[dst] = match table.find(®_k(index)) {
Some(v) => *v,
None => LNil,
},
_ => fail!(~"Tried to index a non-table"),
},
ISetTable(tbl, index, value) => match reg[tbl] {
LTable(table, _) => { table.insert(reg_k(index), reg_k(value)); },
// Note: need to handle meta-tables here
_ => fail!(~"Tried to insert a value into a non-table!")
},
ISelf(dst, tbl, method) => {
reg[dst+1] = reg[tbl];
match reg[tbl] {
LTable(table, _) => reg[dst] = *table.get(®_k(method)),
_ => fail!(~"Expecting a table for method call!")
}
}
ISetList(tbl, num, block) => {
match reg[tbl] {
LTable(hmap, _) => for int::range(0, num) |i| {
hmap.insert(LNum(((block-1)*50+i) as float), reg[tbl+i]);
},
_ => fail!(~"Expecting a table to set values!"),
}
},
IJmp(offset) => jump(offset),
IForPrep(index, offset) => { reg[index] = reg[index] - reg[index+2]; jump(offset); },
IForLoop(index, offset) => { reg[index] = reg[index] + reg[index+2];
let action = || { jump(offset); reg[index+3] = reg[index]; };
if reg[index+2] > LNum(0f) {
if reg[index] <= reg[index+1] { action(); }
}
else {
if reg[index] >= reg[index+1] { action(); }
}
}
ITForLoop(func, c) => {
//io::println("gothere in tforloop");
let mut args = ~[ reg[func+1], reg[func+2] ];
let ret_regs = match reg[func] {
LFunc(subexec) => run( subexec, &mut args),
LRustFunc(f) => f(&mut args),
_ => fail!(~"Tried to call a non-function!"),
};
for int::range(0, c) |i| { reg[func+3+i] = ret_regs[i]; };
if reg[func+3] != LNil {
reg[func+2] = reg[func+3];
}
else {
bump();
}
}
ICall(func, call_extent, ret_extent) => {
let mut reg_prime = from_fn( (call_extent-1) as uint, |i| { reg[func+1+i as int] });
//io::println(fmt!("calling %s", reg[func].to_str()));
let ret_regs = match reg[func] {
LFunc(subexec) => run( subexec, &mut reg_prime),
LRustFunc(f) => f(&mut reg_prime),
_ => fail!(~"Tried to call a non-function!"),
};
//io::println(fmt!("got %d items, expecting %d, first: %s", ret_regs.len() as int, ret_extent-2+1, ret_regs[0].to_str()));
match ret_extent as uint {
0 => return, // TODO: take all return values
1 => return,
_ => for int::range(0, ret_extent-2+1) |i| { reg[func+i] = ret_regs[i]; },
}
},
IReturn(_, _) => { /* can't get here */ },
ITailCall(_, _, _) => { /* can't get here */ },
}
}
fn main() {
// let registers = @mut [LNum(0.0f), LNum(3.0f), LNum(1.0f), LNum(2.0f)];
let globals = @mut linear::LinearMap::new();
let mut hmap: linear::LinearMap<LuaVal, LuaVal> = linear::LinearMap::new();
hmap.insert( LNum(1f), LNum(56f) );
hmap.insert( LNum(2f), LNum(57f) );
hmap.insert( LNum(3f), LNum(58f) );
/*
let subprog = Execution { globals: globals, state: @mut true, constants: ~[LNum(70f), LTable(@mut hmap, @[]), LString(@~"a")], prog: Program(~[
ILoadNil(0, 55),
ILoadK(1, 1),
IGetTable(2, 1, -3),
IReturn(2, 2)
]) };
*/
globals.insert(LString(@~"print"), LRustFunc(lprint));
globals.insert(LString(@~"ipairs"), LRustFunc(ipairs));
globals.insert(LString(@~"type"), LRustFunc(ltype));
let s = ~Execution { globals: globals, state: @mut true, constants: ~[
LTable(@mut hmap, @[]), LString(@~"print"), LString(@~"ipairs"), LString(@~"type"), LNil ], prog: Program(~[
INewTable(0, 0, 0),
ILoadK(1, 1),
ILoadK(2, 2),
ILoadK(3, 3),
ILoadK(4, 4),
ISetList(0, 4, 1),
IGetGlobal(0, 3),
IGetGlobal(1, 4),
ICall(0, 2, 2),
IMove(1, 0),
IGetGlobal(0, 1),
ICall(0, 2, 1),
IGetGlobal(0, 2),
ILoadK(1, 0),
ICall(0, 2, 4),
IJmp(4),
IGetGlobal(5, 1),
IMove(6, 3),
IMove(7, 4),
ICall(5, 3, 1),
ITForLoop(0, 2),
IJmp(-6),
IReturn(0, 1),
]) };
/*
let s = ~Execution { globals: globals, state: @mut true, constants: ~[LNum(500f), LNum(300f), LString(@~"print")], prog: Program(~[
IGetGlobal(1, 2),
ILoadK(2, 0),
ILoadK(3, 2),
ILen(3, 3),
ICall(1, 3, 1),
ILoadK(1, 0),
IAdd(3,1,-1),
ISub(3,3,2),
IReturn(3, 2),
]) };
let fancy = ~Execution { globals: globals, state: @mut true, constants: ~[LNum(0f), LNum(1f), LNum(100f)], prog: Program(~[
ILoadK(0, 0),
ILoadK(1, 1),
ILoadK(2, 2),
ILoadK(3, 1),
IForPrep(1, 1),
IAdd(0, 4, 0),
IForLoop(1, -2),
IReturn(0, 2),
]) };
let concat = ~Execution { globals: globals, state: @mut true, constants: ~[LNum(55f), LNum(33f), LNum(22f), LNum(66f)], prog: Program(~[
ILoadK(0, 0),
ILoadK(1, 1),
ILoadK(2, 2),
IConcat(3, 0, 1),
IConcat(3, 3, 1),
IConcat(3, 3, -4),
IReturn(3, 2),
]) };
*/
let mut regs = ~[LNum(5050f), LNum(0f),LNum(111f),LNum(0f), LNum(0f),LNum(0f), LNum(0f), LNum(0f),];
let out = run(s, &mut regs );
io::println( out.to_str() );
}
fn main_test() {
let v1 = LNum(1.0f);
let v2 = LNum(3.0f);
let v3 = v1 + v2;
let s1 = LString(@~"hello");
let s2 = LString(@~" world");
let s3 = s1 + s2;
io::println( (v2 < v1).to_str() );
io::println( s3.to_str() );
match v3 {
LNum(x) => { io::println(fmt!("%f", x)); },
LString(s) => { io::println(*s); },
_ => { ; },
}
}