Documentation
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use super::ArgSpec;
use super::ArithmeticBinop;
use super::ArithmeticUnop;
use super::Binop;
use super::Code;
use super::Func;
use super::GenObjPtr;
use super::Handler;
use super::Mark;
use super::Opcode;
use super::RcStr;
use super::Unop;
use super::Val;
use super::Var;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

pub struct Vm<H: Handler> {
    scope: Scope,
    handler: H,
}

impl<H: Handler> Vm<H> {
    pub fn new(handler: H) -> Self {
        Self {
            scope: Scope::new(),
            handler,
        }
    }
    pub fn exec(&mut self, code: &Code) -> Result<(), Val> {
        for var in code.vars() {
            self.scope.globals.insert(var.name.clone(), Val::Nil);
        }
        exec(&mut self.scope, &mut self.handler, code)?;
        Ok(())
    }
    pub fn run_tests(&mut self, prefixes: &Vec<RcStr>) -> Result<(), Val> {
        let tests = std::mem::replace(&mut self.scope.tests, vec![]);
        for test in tests {
            if prefixes
                .iter()
                .any(|prefix| test.name().starts_with(prefix.as_ref()))
            {
                print!("  test {}... ", test.name());
                callfunc(&mut self.scope, &mut self.handler, &test, vec![])?;
                println!("ok");
            }
        }
        Ok(())
    }
    pub fn exec_and_run_tests(&mut self, code: &Code, prefixes: &Vec<RcStr>) -> Result<(), Val> {
        self.exec(code)?;
        self.run_tests(prefixes)
    }
    pub fn trace(&self) -> &Vec<Mark> {
        &self.scope.trace
    }
}

pub fn callfunc<H: Handler>(
    scope: &mut Scope,
    handler: &mut H,
    func: &Code,
    mut args: Vec<Val>,
) -> Result<Val, Val> {
    // match args against parameters
    let spec = func.argspec();
    prepare_args(spec, &mut args)?;

    // initialize args: save the values into local vars
    scope.push(func.vars());
    for (i, arg) in args.into_iter().enumerate() {
        scope.set(VarScope::Local, i as u32, arg);
    }
    let ret = exec(scope, handler, func)?;
    scope.pop();
    Ok(ret)
}

fn mkgenobj(func: Rc<Code>, mut args: Vec<Val>) -> Result<Val, Val> {
    prepare_args(func.argspec(), &mut args)?;
    let mut locals = new_locals_from_vars(func.vars());
    for (i, arg) in args.into_iter().enumerate() {
        locals.set_by_index(i as u32, arg);
    }
    let genobj = GenObj {
        code: func,
        locals,
        i: 0,
        stack: vec![],
    };

    Ok(Val::GenObj(GenObjPtr(Rc::new(RefCell::new(genobj)))))
}

fn prepare_args(spec: &ArgSpec, args: &mut Vec<Val>) -> Result<(), Val> {
    if spec.var.is_some() || spec.def.len() > 0 {
        // has variadic parameter or at least one default parameter
        let argc = args.len();
        let argmin = spec.req.len();
        let argmax = argmin + spec.def.len();
        if spec.var.is_some() {
            if argc < argmin {
                return Err(Val::String(
                    format!("Expected at least {} args but got {}", argmin, argc).into(),
                ));
            }
        } else {
            if argc < argmin || argc > argmax {
                return Err(Val::String(
                    format!("Expected {} to {} args but got {}", argmin, argmax, argc).into(),
                ));
            }
        }
        let def = &spec.def;
        for i in argc..argmax {
            let default_val = def[i - argmin].1.clone();
            args.push(default_val);
        }
        if spec.var.is_some() {
            let rem_args: Vec<Val> = if argmax < argc {
                args.drain(argmax..argc).collect()
            } else {
                vec![]
            };
            args.push(rem_args.into());
        }
    } else {
        // no variadic parameter and no default parameters
        if args.len() != spec.req.len() {
            return Err(Val::String(
                format!("Expected {} args but got {}", spec.req.len(), args.len()).into(),
            ));
        }
    }
    Ok(())
}

pub fn exec<H: Handler>(scope: &mut Scope, handler: &mut H, code: &Code) -> Result<Val, Val> {
    let mut i = 0;
    let mut stack = Vec::new();
    while i < code.len() {
        match step(scope, handler, code, &mut i, &mut stack)? {
            StepVal::Return(val) => return Ok(val),
            StepVal::Yield(_) => return Err("Yielding does not make sense here".into()),
            StepVal::None => {}
        }
    }
    assert!(stack.is_empty());
    Ok(Val::Nil)
}

enum StepVal {
    Return(Val),
    Yield(Val),
    None,
}

fn step<H: Handler>(
    scope: &mut Scope,
    handler: &mut H,
    code: &Code,
    i: &mut usize,
    stack: &mut Vec<Val>,
) -> Result<StepVal, Val> {
    let op = code.fetch(*i);
    *i += 1;
    match op {
        Opcode::Nil => {
            stack.push(Val::Nil);
        }
        Opcode::Bool(x) => {
            stack.push(Val::Bool(*x));
        }
        Opcode::Number(x) => {
            stack.push(Val::Number(*x));
        }
        Opcode::String(x) => {
            stack.push(Val::String(x.clone()));
        }
        Opcode::MakeList(len) => {
            let start = stack.len() - *len as usize;
            let list: Vec<_> = stack.drain(start..).collect();
            stack.push(list.into());
        }
        Opcode::NewFunc(code) => {
            stack.push(Val::Func(Func(code.clone())));
        }
        Opcode::Pop => {
            stack.pop().unwrap();
        }
        Opcode::Dup => {
            let x = stack.last().unwrap().clone();
            stack.push(x);
        }
        Opcode::Dup2 => {
            let len = stack.len();
            let a = stack[len - 2].clone();
            let b = stack[len - 1].clone();
            stack.push(a);
            stack.push(b);
        }
        Opcode::Unpack(n) => {
            let elements = stack.pop().unwrap();
            if let Some(list) = elements.list() {
                if list.borrow().len() != *n as usize {
                    scope.push_trace(code.marks()[*i - 1].clone());
                    return Err(Val::String(
                        format!(
                            "Expected {} values, but got a list with {} values",
                            n,
                            list.borrow().len(),
                        )
                        .into(),
                    ));
                }
                for item in list.borrow().iter() {
                    stack.push(item.clone());
                }
            } else {
                let genobj = elements.expect_genobj()?;
                let mut genobj = genobj.0.borrow_mut();
                let vec = genobj.to_vec(scope, handler)?;
                if vec.len() != *n as usize {
                    scope.push_trace(code.marks()[*i - 1].clone());
                    return Err(Val::String(
                        format!("Expected {} values, but got {} values", n, vec.len(),).into(),
                    ));
                }
                stack.extend(vec);
            }
        }
        Opcode::Get(vscope, index) => {
            let val = scope.get(*vscope, *index).clone();
            if let Val::Invalid = val {
                return Err(Val::String(
                    format!(
                        "Variable {} used before being set",
                        scope.get_name(*vscope, *index)
                    )
                    .into(),
                ));
            }
            stack.push(val);
        }
        Opcode::Set(vscope, index) => {
            let val = stack.pop().unwrap();
            scope.set(*vscope, *index, val);
        }
        Opcode::Tee(vscope, index) => {
            let val = stack.last().unwrap().clone();
            scope.set(*vscope, *index, val);
        }
        Opcode::Return => {
            let val = stack.pop().unwrap();
            return Ok(StepVal::Return(val));
        }
        Opcode::Yield => {
            let val = stack.pop().unwrap();
            return Ok(StepVal::Yield(val));
        }
        Opcode::Next => {
            let genobj = stack.last().unwrap().clone();
            let genobj = genobj.expect_genobj()?;
            let mut genobj = genobj.0.borrow_mut();
            match genobj.resume(scope, handler, Val::Nil)? {
                Some(val) => {
                    stack.push(val);
                    stack.push(true.into());
                }
                None => {
                    stack.push(Val::Nil);
                    stack.push(false.into());
                }
            }
        }
        Opcode::CallFunc(argc) => {
            let old_len = stack.len();
            let new_len = old_len - (*argc as usize);
            let args: Vec<Val> = stack.drain(new_len..).collect();
            let func = stack.pop().unwrap();
            let func = if let Some(func) = func.func() {
                func
            } else {
                scope.push_trace(code.marks()[*i - 1].clone());
                return Err(format!("{} is not a function", func).into());
            };

            scope.push_trace(code.marks()[*i - 1].clone());
            if func.generator() {
                // generator; create a generator object
                let genobj = mkgenobj(func.clone(), args)?;
                stack.push(genobj);
            } else {
                // this is a normal function, call it
                let ret = callfunc(scope, handler, &func, args)?;
                stack.push(ret);
            }
            scope.pop_trace();
        }
        Opcode::Binop(op) => {
            let rhs = stack.pop().unwrap();
            let lhs = stack.pop().unwrap();
            let ret = match op {
                // arithmetic operators
                Binop::Arithmetic(aop) => {
                    let lhs = if let Some(lhs) = lhs.number() {
                        lhs
                    } else {
                        scope.push_trace(code.marks()[*i - 1].clone());
                        return Err(format!(
                            concat!(
                                "The left hand side of this arithmetic operation ",
                                "should be a number but got {:?}"
                            ),
                            lhs
                        )
                        .into());
                    };
                    let rhs = if let Some(rhs) = rhs.number() {
                        rhs
                    } else {
                        scope.push_trace(code.marks()[*i - 1].clone());
                        return Err(format!(
                            concat!(
                                "The right hand side of this arithmetic operation ",
                                "should be a number but got {:?}"
                            ),
                            rhs
                        )
                        .into());
                    };
                    match aop {
                        ArithmeticBinop::Add => Val::Number(lhs + rhs),
                        ArithmeticBinop::Subtract => Val::Number(lhs - rhs),
                        ArithmeticBinop::Multiply => Val::Number(lhs * rhs),
                        ArithmeticBinop::Divide => Val::Number(lhs / rhs),
                        ArithmeticBinop::TruncDivide => Val::Number((lhs / rhs).trunc()),
                        ArithmeticBinop::Remainder => Val::Number(lhs % rhs),
                    }
                }

                // comparison operators
                Binop::Equal => Val::Bool(lhs == rhs),
                Binop::NotEqual => Val::Bool(lhs != rhs),
                Binop::LessThan => Val::Bool(lhs.lt(&rhs)?),
                Binop::LessThanOrEqual => Val::Bool(!rhs.lt(&lhs)?),
                Binop::GreaterThan => Val::Bool(rhs.lt(&lhs)?),
                Binop::GreaterThanOrEqual => Val::Bool(!lhs.lt(&rhs)?),

                // list
                Binop::Append => {
                    let list = lhs.expect_list()?;
                    list.borrow_mut().push(rhs);
                    lhs
                }
            };
            stack.push(ret);
        }
        Opcode::Unop(op) => {
            let val = stack.pop().unwrap();
            let ret = match op {
                Unop::Arithmetic(aop) => {
                    let val = if let Some(val) = val.number() {
                        val
                    } else {
                        scope.push_trace(code.marks()[*i - 1].clone());
                        return Err(format!(
                            concat!(
                                "The argument to this unary arithmetic operator ",
                                "should be a number but got {:?}"
                            ),
                            val
                        )
                        .into());
                    };
                    match aop {
                        ArithmeticUnop::Negative => Val::Number(-val),
                        ArithmeticUnop::Positive => Val::Number(val),
                    }
                }
                Unop::Len => match val {
                    Val::String(s) => Val::Number(s.charlen() as f64),
                    Val::List(list) => Val::Number(list.borrow().len() as f64),
                    _ => {
                        scope.push_trace(code.marks()[*i - 1].clone());
                        return Err(format!(
                            concat!("LEN requires a string or list argument but got {}"),
                            val,
                        )
                        .into());
                    }
                },
            };
            stack.push(ret);
        }
        Opcode::Print => {
            let x = stack.pop().unwrap();
            handler.print(scope, x)?;
        }
        Opcode::Disasm => {
            let f = stack.pop().unwrap();
            if let Val::Func(func) = &f {
                stack.push(func.0.format().into());
            } else {
                scope.push_trace(code.marks()[*i - 1].clone());
                return Err(
                    format!(concat!("DISASM requires a function argument but got {}"), f,).into(),
                );
            }
        }
        Opcode::AddToTest => {
            let val = stack.last().unwrap().clone();
            if let Val::Func(code) = &val {
                scope.tests.push(code.0.clone());
            } else {
                scope.push_trace(code.marks()[*i - 1].clone());
                return Err(format!(
                    concat!("Tests need to be functions, but {} is not a function"),
                    val,
                )
                .into());
            }
        }
        Opcode::Assert => {
            let val = stack.pop().unwrap();
            if !val.truthy() {
                scope.push_trace(code.marks()[*i - 1].clone());
                return Err(format!(concat!("Assertion failed"),).into());
            }
        }
        Opcode::AssertEq => {
            let rhs = stack.pop().unwrap();
            let lhs = stack.pop().unwrap();
            if lhs != rhs {
                scope.push_trace(code.marks()[*i - 1].clone());
                return Err(format!(
                    concat!("Assertion failed: expected {} to equal {}"),
                    lhs, rhs,
                )
                .into());
            }
        }
        Opcode::Goto(pos) => {
            *i = *pos as usize;
        }
        Opcode::GotoIfFalse(pos) => {
            let item = stack.pop().unwrap();
            if !item.truthy() {
                *i = *pos as usize;
            }
        }
        Opcode::GotoIfFalseNoPop(pos) => {
            let item = stack.last().unwrap();
            if !item.truthy() {
                *i = *pos as usize;
            }
        }
        Opcode::Label(_)
        | Opcode::UnresolvedGoto(_)
        | Opcode::UnresolvedGotoIfFalse(_)
        | Opcode::UnresolvedGotoIfFalseNoPop(_) => {
            panic!("Unresolved opcode: {:?}", op);
        }
    }
    Ok(StepVal::None)
}

#[derive(Debug, Clone, Copy)]
pub enum VarScope {
    Local,
    Global,
}

pub struct Scope {
    globals: IndexedMap,
    locals: Vec<IndexedMap>,
    trace: Vec<Mark>,
    tests: Vec<Rc<Code>>,
}

impl Scope {
    pub fn new() -> Self {
        Self {
            globals: IndexedMap::new(),
            locals: vec![],
            trace: vec![],
            tests: vec![],
        }
    }
    pub fn get_name(&self, vscope: VarScope, index: u32) -> &RcStr {
        match vscope {
            VarScope::Global => self.globals.get_key(index).unwrap(),
            VarScope::Local => self.locals.last().unwrap().get_key(index).unwrap(),
        }
    }
    pub fn get(&self, vscope: VarScope, index: u32) -> &Val {
        match vscope {
            VarScope::Global => &self.globals.values[index as usize],
            VarScope::Local => &self.locals.last().unwrap().values[index as usize],
        }
    }
    pub fn set(&mut self, vscope: VarScope, index: u32, val: Val) {
        match vscope {
            VarScope::Global => self.globals.values[index as usize] = val,
            VarScope::Local => self.locals.last_mut().unwrap().values[index as usize] = val,
        }
    }
    pub fn push(&mut self, locals: &Vec<Var>) {
        self.locals.push(new_locals_from_vars(locals));
    }
    pub fn push_existing_locals(&mut self, map: IndexedMap) {
        self.locals.push(map);
    }
    pub fn pop(&mut self) -> IndexedMap {
        self.locals.pop().unwrap()
    }
    pub fn push_trace(&mut self, mark: Mark) {
        self.trace.push(mark);
    }
    pub fn pop_trace(&mut self) {
        self.trace.pop();
    }
}

fn new_locals_from_vars(vars: &Vec<Var>) -> IndexedMap {
    let mut map = IndexedMap::new();
    for var in vars {
        let index = map.insert(var.name.clone(), Val::Nil);
        assert_eq!(index, var.index);
    }
    map
}

pub struct IndexedMap {
    values: Vec<Val>,
    map: HashMap<RcStr, u32>,
}

impl IndexedMap {
    pub fn new() -> Self {
        Self {
            values: vec![],
            map: HashMap::new(),
        }
    }
    pub fn insert(&mut self, key: RcStr, val: Val) -> u32 {
        let i = self.values.len() as u32;
        self.values.push(val);
        self.map.insert(key, i);
        i
    }
    pub fn set_by_index(&mut self, i: u32, val: Val) {
        self.values[i as usize] = val;
    }
    pub fn get_by_key(&self, key: &RcStr) -> Option<&Val> {
        self.map.get(key).map(|i| &self.values[*i as usize])
    }
    pub fn get_by_index(&self, i: u32) -> Option<&Val> {
        self.values.get(i as usize)
    }
    pub fn get_key(&self, index: u32) -> Option<&RcStr> {
        for (key, i) in &self.map {
            if *i == index {
                return Some(key);
            }
        }
        None
    }
    pub fn len(&self) -> usize {
        self.values.len()
    }
}

/// generator object
/// created by calling generator functions
pub struct GenObj {
    code: Rc<Code>,
    locals: IndexedMap,
    i: usize,
    stack: Vec<Val>,
}

impl GenObj {
    pub fn resume<H: Handler>(
        &mut self,
        scope: &mut Scope,
        handler: &mut H,
        val: Val,
    ) -> Result<Option<Val>, Val> {
        if self.i >= self.code.len() {
            return Ok(None);
        }
        scope.push_existing_locals(std::mem::replace(&mut self.locals, IndexedMap::new()));
        self.stack.push(val);
        let result = self.loop_(scope, handler);
        self.locals = scope.pop();
        result
    }
    pub fn to_vec<H: Handler>(
        &mut self,
        scope: &mut Scope,
        handler: &mut H,
    ) -> Result<Vec<Val>, Val> {
        let mut ret = Vec::new();
        while let Some(val) = self.resume(scope, handler, Val::Nil)? {
            ret.push(val);
        }
        Ok(ret)
    }
    fn loop_<H: Handler>(
        &mut self,
        scope: &mut Scope,
        handler: &mut H,
    ) -> Result<Option<Val>, Val> {
        while self.i < self.code.len() {
            match step(scope, handler, &self.code, &mut self.i, &mut self.stack)? {
                StepVal::Return(_) => {
                    self.i = self.code.len();
                    self.clear(); // release all local vars etc
                    return Ok(None);
                }
                StepVal::Yield(val) => return Ok(Some(val)),
                StepVal::None => {}
            }
        }
        self.clear();
        Ok(None)
    }
    fn clear(&mut self) {
        self.stack = vec![];
        self.locals = IndexedMap::new();
    }
}