aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src/vm.rs
blob: 22b769b0a660da8ff9f480938f5f201d7e4d9d98 (plain)
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
use sloth_bytecode::Instruction;

use crate::{Chunk, Data, Stack};

pub struct VM {
    vm_return: Option<Data>,
    stack: Stack,
}

impl VM {
    fn new() -> Self {
        Self {
            vm_return: None,
            stack: Stack::default(),
        }
    }

    fn execute(&mut self) {
        loop {
            self.execute_once();
        }
    }

    fn execute_once(&mut self) {
        //
    }

    fn run(&mut self, chunk: &Chunk) {
        let mut pointer = 0;

        loop {
            let instruction = Instruction::disassemble(&chunk.code, &mut pointer);

            match instruction {
                Instruction::Constant(idx) => {
                    let value = chunk.constants[idx as usize];
                    self.stack.push(value);
                }
                Instruction::Load(_) => todo!(),
                Instruction::Push(_) => todo!(),
                Instruction::Dup => {
                    let value = self.stack.pop();
                    self.stack.push(value);
                    self.stack.push(value);
                }
                Instruction::Del => {
                    self.stack.pop();
                }
                Instruction::Add => {
                    let value = match self.stack.pop2() {
                        (Data::Integer(lhs), Data::Integer(rhs)) => Data::Integer(lhs + rhs),
                        (Data::Float(lhs), Data::Float(rhs)) => Data::Float(lhs + rhs),
                        _ => panic!(),
                    };

                    self.stack.push(value);
                }
                Instruction::Sub => {
                    let value = match self.stack.pop2() {
                        (Data::Integer(lhs), Data::Integer(rhs)) => Data::Integer(lhs - rhs),
                        (Data::Float(lhs), Data::Float(rhs)) => Data::Float(lhs - rhs),
                        _ => panic!(),
                    };

                    self.stack.push(value);
                }
                Instruction::Mul => {
                    let value = match self.stack.pop2() {
                        (Data::Integer(lhs), Data::Integer(rhs)) => Data::Integer(lhs * rhs),
                        (Data::Float(lhs), Data::Float(rhs)) => Data::Float(lhs * rhs),
                        _ => panic!(),
                    };

                    self.stack.push(value);
                }
                Instruction::Div => {
                    let value = match self.stack.pop2() {
                        (Data::Integer(lhs), Data::Integer(rhs)) => Data::Integer(lhs / rhs),
                        (Data::Float(lhs), Data::Float(rhs)) => Data::Float(lhs / rhs),
                        _ => panic!(),
                    };

                    self.stack.push(value);
                }
                Instruction::Mod => {
                    let value = match self.stack.pop2() {
                        (Data::Integer(lhs), Data::Integer(rhs)) => Data::Integer(lhs % rhs),
                        (Data::Float(lhs), Data::Float(rhs)) => Data::Float(lhs % rhs),
                        _ => panic!(),
                    };

                    self.stack.push(value);
                }
                Instruction::Hlt => break,
                Instruction::Exit => break,
                Instruction::VMReturn => {
                    let value = self.stack.pop();
                    self.vm_return = Some(value);
                    break;
                }
                _ => unimplemented!(),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{Chunk, Data, VM};

    #[test]
    fn arithmetic_ops() {
        let mut vm = VM::new();

        // Addition
        vm.run(&Chunk {
            constants: vec![Data::Integer(7)],
            code: vec![
                0x00, 0, 0, 0, 0,    // Load constant from 0
                0x10, // Duplicate
                0x20, // Add
                0xF0, // Return VM
            ],
        });

        assert_eq!(vm.vm_return, Some(Data::Integer(14)));

        vm.run(&Chunk {
            constants: vec![Data::Integer(2), Data::Integer(11)],
            code: vec![
                0x00, 0, 0, 0, 0, // Load constant from 0
                0x00, 0, 0, 0, 1,    // Load constant from 1
                0x20, // Add
                0xF0, // Return VM
            ],
        });

        assert_eq!(vm.vm_return, Some(Data::Integer(13)));
    }
}