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
use crate::{
    expressions::{Expr, Value},
    lexing::Lexer,
    parsing::Parser,
    syntax::Punct,
};
use std::{
    collections::{BTreeMap, BTreeSet},
    fmt,
    ops::RangeInclusive,
};

#[cfg(test)]
mod tests;

pub mod compilation;

#[derive(Clone, PartialEq, Debug)]
pub(crate) enum RlValue {
    Unit,
    Addr(usize),
    Num(i32),
    Str(String),
    // A function is multiple instructions at a certain address
    // that `usize` is the "instruction pointer" of the first instruction of the function
    Function(usize, u8),
    Range(RangeInclusive<i32>),
}

impl fmt::Display for RlValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use self::RlValue::*;
        match self {
            Unit => write!(f, "()"),
            Num(x) => write!(f, "{}", x),
            Str(ref s) => write!(f, "'{}'", s),
            _ => write!(f, ".."),
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub(crate) enum Instruction {
    // Jumps to the address at the TOS - 1
    Ret,
    // Jumps to the address at the TOS
    Jump,
    Push(RlValue),
    PushLocal(String),
    Declare(String),
    Add,
    Sub,
    Mul,
    Call(String),
    CallNative(String),
    MakeRange,
}

#[derive(Default)]
struct Scope<'l> {
    parent: Option<&'l Scope<'l>>,
    bindings: BTreeMap<&'l str, RlValue>,
}

impl<'t> Scope<'t> {
    pub fn new(parent: &'t Scope) -> Self {
        Scope::init(Some(parent))
    }

    fn init(parent: Option<&'t Scope>) -> Self {
        Scope {
            parent,
            bindings: BTreeMap::new(),
        }
    }

    pub fn empty() -> Self {
        Default::default()
    }

    pub fn lookup(&self, ident: &str) -> Option<&RlValue> {
        self.bindings.get(ident)
    }
}

#[derive(Default)]
pub(crate) struct VM<'l> {
    instr_ptr: usize,
    current_scope: Scope<'l>,
    stack: Vec<RlValue>,
}

macro_rules! num_instr {
    ($self:expr, $op:expr) => {{
        let y = $self.stack.pop().unwrap();
        let x = $self.stack.pop().unwrap();

        match (x, y) {
            (RlValue::Num(x), RlValue::Num(y)) => $self.stack.push(RlValue::Num($op(x, y))),
            _ => unimplemented!(),
        }
    }};
}

impl<'l> VM<'l> {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn lookup_num(&self, ident: &str) -> Option<i32> {
        match self.current_scope.bindings.get(ident) {
            Some(RlValue::Num(x)) => Some(*x),
            _ => None,
        }
    }

    pub fn lookup_str(&self, ident: &str) -> Option<String> {
        match self.current_scope.bindings.get(ident) {
            Some(RlValue::Str(ref s)) => Some(String::from(s)),
            _ => None,
        }
    }

    pub fn lookup_list(&self, ident: &str) -> Option<Vec<i32>> {
        match self.current_scope.bindings.get(ident) {
            Some(RlValue::Range(range)) => Some(range.clone().collect()),
            _ => None,
        }
    }

    fn lookup(&self, ident: &str) -> Option<&RlValue> {
        match self.current_scope.lookup(ident) {
            v @ Some(_) => v,
            _ => match self.current_scope.parent {
                Some(p) => p.lookup(ident),
                _ => None,
            },
        }
    }

    pub fn execute(&mut self, instrs: &'l [Instruction]) {
        while self.instr_ptr < instrs.len() {
            let instr = &instrs[self.instr_ptr];
            self.instr_ptr += 1;

            match instr {
                Instruction::Ret => {
                    // Functions push their result to the TOS,
                    // so in order to return, we need the address at TOS - 1
                    match self.stack.remove(self.stack.len() - 2) {
                        RlValue::Addr(addr) => {
                            self.instr_ptr = addr;
                        }
                        _ => unimplemented!(),
                    }
                }
                Instruction::Jump => match self.stack.pop() {
                    Some(RlValue::Addr(addr)) => self.instr_ptr = addr,
                    _ => unimplemented!(),
                },
                Instruction::Push(v) => self.stack.push(v.clone()),
                Instruction::PushLocal(ref ident) => {
                    self.stack.push(self.lookup(ident).unwrap().clone());
                }
                Instruction::Add => num_instr!(self, |x, y| x + y),
                Instruction::Sub => num_instr!(self, |x, y| x - y),
                Instruction::Mul => num_instr!(self, |x, y| x * y),
                Instruction::Declare(ident) => {
                    self.current_scope
                        .bindings
                        .insert(ident, self.stack.pop().unwrap());
                }
                Instruction::MakeRange => {
                    let to = self.stack.pop().unwrap();
                    let from = self.stack.pop().unwrap();
                    match (from, to) {
                        (RlValue::Num(from), RlValue::Num(to)) => {
                            self.stack.push(RlValue::Range(from..=to))
                        }
                        _ => unimplemented!(),
                    };
                }
                Instruction::Call(ref ident) => {
                    match self.lookup(ident) {
                        Some(RlValue::Function(addr, args)) => {
                            let n = *args;
                            let ptr = self.instr_ptr;

                            self.instr_ptr = *addr;

                            // need to return to the next instruction after the function call
                            // skip the top N args, which are the called function's args
                            self.stack
                                .insert(self.stack.len() - (n as usize), RlValue::Addr(ptr));
                        }
                        _ => {
                            unimplemented!("Function {} does not exist in the current scope", ident)
                        }
                    }
                }
                Instruction::CallNative(ref ident) => match ident.as_ref() {
                    "print" => {
                        let val = self.stack.pop().unwrap();
                        println!("{}", val);
                    }
                    _ => (),
                },
                _ => unimplemented!("{:?}", &instr),
            }
        }
    }
}