~starkingdoms/starkingdoms

ref: 339f063faf61671754325b60bcad3ab02492e742 starkingdoms/kabel_test/src/vm.rs -rw-r--r-- 4.5 KiB
339f063f — ghostly_zsh print, add, sub run with manually typed bytecode 1 year, 4 months ago
                                                                                
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
use kabel::{mismatched_types, runtime_error::KabelRuntimeError, wrong_type};

use crate::string::ObjString;

pub struct VM {
    ip: usize,
    chunk: Vec<u8>,
    stack: Vec<Value>,
    pool: Vec<Value>,
    lines: Vec<(usize, usize)>, // line #, repeats number of instructions
    text: Vec<String>
}

impl VM {
    pub fn new(bytecode: Vec<u8>, lines: Vec<(usize, usize)>, pool: Vec<Value>, text: String) -> Self {
        Self {
            ip: 0,
            chunk: bytecode,
            stack: Vec::new(),
            pool,
            lines,
            text: text.lines().map(|s| s.to_string()).collect(),
        }
    }
    pub fn run(&mut self) -> Result<(), KabelRuntimeError> {
        use Value::*;
        while self.ip < self.chunk.len() {
            match self.read() {
                0x00 => { // CONSTANT
                    let byte = self.read() as usize;
                    self.stack.push(self.pool[byte])
                }
                0x01 => { // ADD
                    match (self.stack.pop().unwrap(), self.stack.pop().unwrap()) {
                        (Num(v1), Num(v2)) => self.stack.push(Num(v1 + v2)),
                        (Str(v1), Str(v2)) => {
                            self.stack.push(Str(v1 + v2));
                            v1.free();
                            v2.free();
                        },
                        (Bool(_v1), Bool(_v2)) => {
                            return Err(wrong_type!(self, "Cannot add booleans"))
                        }
                        (v1, v2) => {
                            if let Str(v1) = v1 {
                                v1.free();
                            }
                            if let Str(v2) = v2 {
                                v2.free();
                            }
                            return Err(mismatched_types!(self, "Mismatched types: {} and {}", v1.type_str(), v2.type_str()))
                        }
                    }
                }
                0x02 => { // SUB
                    match (self.stack.pop().unwrap(), self.stack.pop().unwrap()) {
                        (Num(v1), Num(v2)) => self.stack.push(Num(v1 - v2)),
                        (Str(v1), Str(v2)) => {
                            v1.free(); v2.free();
                            return Err(wrong_type!(self, "Cannot subtract strings"))
                        },
                        (Bool(_v1), Bool(_v2)) => {
                            return Err(wrong_type!(self, "Cannot subtract booleans"))
                        }
                        (v1, v2) => {
                            if let Str(v1) = v1 {
                                v1.free();
                            }
                            if let Str(v2) = v2 {
                                v2.free();
                            }
                            return Err(mismatched_types!(self, "Mismatched types: {} and {}", v1.type_str(), v2.type_str()))
                        }
                    }
                }

                0xFE => { // PRINT
                    let value = self.stack.pop().unwrap();
                    match value {
                        Num(v) => println!("{}", v),
                        Str(v) => println!("{}", v.to_string()),
                        Bool(v) => println!("{}", v),
                    }
                }
                _ => {}
            }
        }
        Ok(())
    }
    pub fn read(&mut self) -> u8 {
        let byte = self.chunk[self.ip];
        self.ip += 1;
        byte
    }
    pub fn find_line(&mut self) -> usize { // returns line # at ip
        let mut line_ip = 0;
        for (line, rep) in self.lines.clone() {
            if line_ip + rep > self.ip {
                return line;
            }
            line_ip += rep;
        }
        panic!("Something went wrong in finding line for error")
    }
}

impl Drop for VM {
    fn drop(&mut self) {
        for element in self.stack.clone() {
            match element {
                Value::Str(v1) => v1.free(),
                _ => {}
            }
        }
        for element in self.pool.clone() {
            match element {
                Value::Str(v1) => v1.free(),
                _ => {}
            }
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub enum Value {
    Num(f32), Str(ObjString), Bool(bool),
}

impl Value {
    pub fn type_str(&self) -> String {
        match self {
            Value::Num(_) => "number".to_string(),
            Value::Str(_) => "string".to_string(),
            Value::Bool(_) => "bool".to_string(),
        }
    }
}