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
use crate::{heap, machine, native};
use enalang_ir as ir;
pub fn hash(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let val = ctx.vm.pop()?;
ctx.vm.push(
val.get_hash()
.map(|x| ir::Value::Number(x as f64))
.unwrap_or(ir::Value::Null),
)?;
Ok(())
}
pub fn drop_value(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
ctx.vm.pop()?;
Ok(())
}
pub fn peek_value_at(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let num = ctx.vm.pop_pointer()?;
match ctx.vm.stack.get(num) {
Some(i) => ctx.vm.push(i.clone()),
None => Err(machine::VMError::StackEnded),
}
}
pub fn drop_value_at(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let num = ctx.vm.pop_pointer()?;
if ((ctx.vm.stack.len() - 1) - num) >= ctx.vm.stack.len() {
return Err(machine::VMError::StackEnded);
}
let val = ctx.vm.stack.remove((ctx.vm.stack.len() - 1) - num);
ctx.vm.handle_minus(val)
}
pub fn swap(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let one = ctx.vm.stack.pop();
let two = ctx.vm.stack.pop();
if let Some(a) = one {
if let Some(b) = two {
ctx.vm.stack.push(a);
ctx.vm.stack.push(b);
}
}
Ok(())
}
fn shape_ptr_num_pair(one: ir::Value, two: ir::Value) -> Result<(usize, usize), machine::VMError> {
match (one, two) {
(ir::Value::Pointer(a), ir::Value::Number(b)) => Ok((a, b as usize)),
(ir::Value::Number(a), ir::Value::Pointer(b)) => Ok((a as usize, b)),
_ => Err(machine::VMError::ExpectedNumber),
}
}
pub fn plus(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let popped = (ctx.vm.pop()?, ctx.vm.pop()?);
if let (ir::Value::Number(a), ir::Value::Number(b)) = popped {
ctx.vm.push(ir::Value::Number(a + b))?;
} else if let Ok((a, b)) = shape_ptr_num_pair(popped.0, popped.1) {
ctx.vm.push(ir::Value::Pointer(a + b))?;
} else {
return Err(machine::VMError::ExpectedNumber);
}
Ok(())
}
pub fn mul(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Number(a * b))?;
} else {
return Err(machine::VMError::ExpectedNumber);
}
Ok(())
}
pub fn div(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Number(a / b))?;
} else {
return Err(machine::VMError::ExpectedNumber);
}
Ok(())
}
pub fn subst(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let popped = (ctx.vm.pop()?, ctx.vm.pop()?);
if let (ir::Value::Number(a), ir::Value::Number(b)) = popped {
ctx.vm.push(ir::Value::Number(a - b))?;
} else if let Ok((a, b)) = shape_ptr_num_pair(popped.0, popped.1) {
ctx.vm.push(ir::Value::Pointer(a - b))?;
} else {
return Err(machine::VMError::ExpectedNumber);
}
Ok(())
}
pub fn pow(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Number(a.powf(b)))?;
} else {
return Err(machine::VMError::ExpectedNumber);
}
Ok(())
}
pub fn root(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Number(a.powf(1.0 / b)))?;
} else {
return Err(machine::VMError::ExpectedNumber);
}
Ok(())
}
pub fn dup(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let val = ctx.vm.stack.last();
let val = match val {
Some(i) => i,
None => {
return Err(machine::VMError::StackEnded);
}
};
ctx.vm.push(val.clone())?;
Ok(())
}
pub fn equal(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let (a, b) = (ctx.vm.pop()?, ctx.vm.pop()?);
ctx.vm.push(ir::Value::Boolean(a == b))
}
pub fn block_exists(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let ir::Value::Block(name) = ctx.vm.pop()? {
ctx.vm.stack.push(ir::Value::Boolean(
ctx.vm.scope_manager.blocks().blocks.contains_key(&name),
));
Ok(())
} else {
Err(machine::VMError::ExpectedBlock)
}
}
pub fn alloc(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let size = ctx.vm.pop_pointer()?;
let block: heap::MemoryBlock;
{
block = heap::heap_result_into_vm(ctx.vm.heap.alloc(size))?;
}
ctx.vm.stack.push(ir::Value::Pointer(block.pointer));
Ok(())
}
pub fn realloc(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let new_ptr: usize;
if let (ir::Value::Pointer(pointer_value), i) = (ctx.vm.pop()?, ctx.vm.pop_pointer()?) {
new_ptr = heap::heap_result_into_vm(ctx.vm.heap.realloc(pointer_value, i))?;
} else {
return Err(machine::VMError::ExpectedPointer);
}
ctx.vm.stack.push(ir::Value::Pointer(new_ptr));
Ok(())
}
pub fn free(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let pointer = match ctx.vm.pop()? {
ir::Value::Pointer(i) => i,
_ => {
return Err(machine::VMError::ExpectedPointer);
}
};
match ctx.vm.heap.free(pointer) {
Err(_) => Err(machine::VMError::BadPointer(pointer)),
_ => Ok(()),
}
}
pub fn set_ref(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let ptr = if let ir::Value::Pointer(point) = ctx.vm.pop()? {
point
} else {
return Err(machine::VMError::ExpectedPointer);
};
let val = ctx.vm.stack.pop();
let val = match val {
Some(i) => i,
None => return Err(machine::VMError::ExpectedValue),
};
ctx.vm
.heap
.set(ptr, val.clone())
.map_err(machine::VMError::HeapError)?;
ctx.vm.handle_minus(val)?;
Ok(())
}
pub fn deref(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
let ptrval = match ctx.vm.stack.pop() {
Some(i) => i,
None => {
return Err(machine::VMError::StackEnded);
}
};
let val: ir::Value;
if let ir::Value::Pointer(value) = ptrval {
val = ctx.vm.heap.get(value).unwrap_or(ir::Value::Null);
ctx.vm
.heap
.rc_minus(value)
.map_err(machine::VMError::HeapError)?;
} else {
return Err(machine::VMError::ExpectedPointer);
}
ctx.vm.push(val)?;
Ok(())
}
pub fn call(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let ir::Value::Block(name) = ctx.vm.pop()? {
ctx.vm.run_block(&name)?;
Ok(())
} else {
Err(machine::VMError::ExpectedBlock)
}
}
pub fn neg(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let ir::Value::Boolean(b) = ctx.vm.pop()? {
ctx.vm.push(ir::Value::Boolean(!b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn or(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Boolean(a), ir::Value::Boolean(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Boolean(a || b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn and(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Boolean(a), ir::Value::Boolean(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Boolean(a && b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn gt(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Boolean(a > b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn lt(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Boolean(a < b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn lte(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Boolean(a <= b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn gte(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
if let (ir::Value::Number(a), ir::Value::Number(b)) = (ctx.vm.pop()?, ctx.vm.pop()?) {
ctx.vm.push(ir::Value::Boolean(a >= b))
} else {
Err(machine::VMError::ExpectedBoolean)
}
}
pub fn clear_stack(ctx: native::NativeHandlerCtx) -> Result<(), machine::VMError> {
for value in &ctx.vm.stack.clone() {
ctx.vm.handle_minus(value.clone())?;
}
ctx.vm.stack = Vec::new();
Ok(())
}
pub fn into_base(ctx: native::NativeHandlerCtx) {}
pub fn group() -> native::NativeGroup {
let mut group = native::NativeGroup::new("");
group.add_native("drop", drop_value).unwrap();
group.add_native("peek", peek_value_at).unwrap();
group.add_native("drop_at", drop_value_at).unwrap();
group.add_native("swap", swap).unwrap();
group.add_native("dup", dup).unwrap();
group.add_native("clear", clear_stack).unwrap();
group.add_native("+", plus).unwrap();
group.add_native("*", mul).unwrap();
group.add_native("hash", hash).unwrap();
group.add_native("/", div).unwrap();
group.add_native("-", subst).unwrap();
group.add_native("!", neg).unwrap();
group.add_native("or", or).unwrap();
group.add_native("and", and).unwrap();
group.add_native(">", gt).unwrap();
group.add_native("<", lt).unwrap();
group.add_native(">=", gte).unwrap();
group.add_native("<=", lte).unwrap();
group.add_native("pow", pow).unwrap();
group.add_native("root", root).unwrap();
group.add_native("==", equal).unwrap();
group.add_native("call", call).unwrap();
group.add_native("block_exists?", block_exists).unwrap();
group.add_native("@", deref).unwrap();
group.add_native("=", set_ref).unwrap();
group.add_native("alloc", alloc).unwrap();
group.add_native("unsafe_realloc", realloc).unwrap();
group.add_native("unsafe_free", free).unwrap();
group
}