-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluaval.rs
225 lines (174 loc) · 4.17 KB
/
luaval.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
pub mod luaval {
use core::hashmap::linear;
use core::ops::*;
use core::to_bytes::*;
#[deriving(Eq)]
pub enum Instr {
IEq(int, int, int),
ILt(int, int, int),
ILe(int, int, int),
ITest(int, int),
ITestSet(int, int, int),
INot(int, int),
IUnm(int, int),
IAdd(int, int, int),
ISub(int, int, int),
IMul(int, int, int),
IDiv(int, int, int),
// IPow(int, int, int),
// IMod(int, int, int),
IConcat(int, int, int),
IJmp(int),
IMove(int, int),
ILoadK(int, int),
ILoadNil(uint, uint),
ILoadBool(int, int, int),
IGetGlobal(int, int),
ISetGlobal(int, int),
// IGetUpval(int, int),
// ISetUpval(int, int),
IReturn(int, int),
ICall(int, int, int),
ITailCall(int, int, int),
// IVarArg(int, int)
// IClosure(int, int),
// IClose(int),
ILen(int, int),
INewTable(int, int, int),
ISetList(int, int, int),
IGetTable(int, int, int),
ISetTable(int, int, int),
ISelf(int, int, int),
IForPrep(int, int),
IForLoop(int, int),
ITForLoop(int, int)
}
#[deriving(Eq)]
pub struct Execution {
state: @mut bool,
constants: ~[LuaVal],
prog: Program,
globals: @mut linear::LinearMap<LuaVal, LuaVal>,
}
#[deriving(Eq)]
pub struct Program(~[Instr]);
//#[deriving(Eq)]
enum LuaVal {
LTable(@mut linear::LinearMap<LuaVal, LuaVal>, @ [LuaVal]),
LString(@~str),
LNum(float),
LBool(bool),
LFunc(@Execution),
LRustFunc(extern "Rust" fn(reg: &mut ~[LuaVal]) -> ~[LuaVal] ),
LNil,
}
fn cmp_fn(a: extern "Rust" fn(&mut ~[LuaVal]) -> ~[LuaVal], b: extern "Rust" fn(&mut ~[LuaVal]) -> ~[LuaVal]) -> bool {
unsafe {
let a_: *() = cast::transmute(a), b_: *() = cast::transmute(b);
a_ == b_
}
}
impl Eq for LuaVal {
fn eq(&self, other: &LuaVal) -> bool {
match (self, other) {
(&LNum(x), &LNum(y)) => x == y,
(&LString(x), &LString(y)) => x == y,
(&LBool(x), &LBool(y)) => x == y,
(<able(x, x1), <able(y, y1)) => (x == y) && (x1 == y1),
(&LNil, &LNil) => true,
(&LRustFunc(x), &LRustFunc(y)) => { cmp_fn(x, y) }
(_, _) => false
}
}
fn ne(&self, other: &LuaVal) -> bool {
return !(self == other);
}
}
impl IterBytes for LuaVal {
fn iter_bytes(&self, lsb0: bool, f: Cb) {
match *self {
LString(x) => x.iter_bytes(lsb0, f),
LNum(x) => (x as uint).iter_bytes(lsb0, f),
LBool(x) => x.iter_bytes(lsb0, f),
LNil => (true, false).iter_bytes(lsb0, f),
_ => fail!(~"Tried to hash a function!"),
}
}
}
impl Add<LuaVal, LuaVal> for LuaVal {
fn add(&self, other: &LuaVal) -> LuaVal {
match (self, other) {
(&LNum(x), &LNum(y)) => LNum(x+y),
(&LString(x), &LString(y)) => LString(@(x.to_owned() + y.to_owned()) ),
_ => LNil,
}
}
}
impl Sub<LuaVal, LuaVal> for LuaVal {
fn sub(&self, other: &LuaVal) -> LuaVal {
match (self, other) {
(&LNum(x), &LNum(y)) => LNum(x-y),
_ => LNil,
}
}
}
impl Mul<LuaVal, LuaVal> for LuaVal {
fn mul(&self, other: &LuaVal) -> LuaVal {
match (self, other) {
(&LNum(x), &LNum(y)) => LNum(x*y),
_ => LNil,
}
}
}
impl Div<LuaVal, LuaVal> for LuaVal {
fn div(&self, other: &LuaVal) -> LuaVal {
match (self, other) {
(&LNum(x), &LNum(y)) => LNum(x/y),
_ => fail!(~"Attempt to divide non-number values!"),
}
}
}
impl Ord for LuaVal {
fn lt(&self, other: &LuaVal) -> bool {
match (self, other) {
(&LNum(x), &LNum(y)) => x < y,
(&LString(x), &LString(y)) => x < y,
_ => false,
}
}
fn le(&self, other: &LuaVal) -> bool {
match (self, other) {
(&LNum(x), &LNum(y)) => x <= y,
(&LString(x), &LString(y)) => x.to_owned() <= y.to_owned(),
_ => false,
}
}
fn ge(&self, other: &LuaVal) -> bool {
match (self, other) {
(&LNum(x), &LNum(y)) => x >= y,
(&LString(x), &LString(y)) => x.to_owned() >= y.to_owned(),
_ => false,
}
}
fn gt(&self, other: &LuaVal) -> bool {
match (self, other) {
(&LNum(x), &LNum(y)) => x > y,
(&LString(x), &LString(y)) => x.to_owned() > y.to_owned(),
_ => false,
}
}
}
impl ToStr for LuaVal {
fn to_str(&self) -> ~str {
match *self {
LNum(x) => x.to_str(),
LString(s) => s.to_owned(),
LNil => ~"nil",
LRustFunc(_) => ~"Rust function",
LFunc(_) => ~"Lua function",
LTable(_, _) => ~"Lua table",
LBool(b) => b.to_str(),
}
}
}
}