aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src/lib.rs
blob: 9cf552b438f222e9f63c34ec02cffa00f3e3400f (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#![allow(dead_code)]
#![warn(
    clippy::wildcard_imports,
    clippy::string_add,
    clippy::string_add_assign,
    clippy::manual_ok_or,
    unused_lifetimes
)]

pub mod native;
pub mod sloth_std;
pub mod value;
pub mod vm;

use std::ops::{Index, IndexMut};

use value::{Object, ObjectType};

use crate::value::Primitive;
pub use crate::vm::VM;

#[derive(Default)]
pub struct Chunk {
    pub constants: Vec<Primitive>,
    pub code: Vec<u8>,
}

const STACK_SIZE: usize = 1024;

#[derive(Debug)]
pub struct Stack {
    stack: [Primitive; STACK_SIZE],
    top: usize,
}

impl Default for Stack {
    fn default() -> Self {
        Self {
            top: Default::default(),
            stack: [Primitive::Empty; STACK_SIZE],
        }
    }
}

impl Stack {
    #[inline(always)]
    pub fn push(&mut self, value: Primitive) {
        if self.top >= STACK_SIZE {
            panic!("Stack overflow");
        }

        self.stack[self.top] = value;
        self.top += 1;
    }

    #[inline(always)]
    pub fn pop(&mut self) -> Primitive {
        if self.top == 0 {
            panic!("Stack underflow");
        }

        self.top -= 1;
        self.stack[self.top]
    }

    #[inline(always)]
    pub fn pop2(&mut self) -> (Primitive, Primitive) {
        (self.pop(), self.pop())
    }

    #[inline(always)]
    pub fn peek(&self) -> Primitive {
        self.stack[self.top - 1]
    }

    #[inline(always)]
    pub fn peek_nth(&self, nth: usize) -> Primitive {
        self.stack[self.top - 1 - nth]
    }
}

impl Index<usize> for Stack {
    type Output = Primitive;

    fn index(&self, index: usize) -> &Self::Output {
        &self.stack[index]
    }
}

impl IndexMut<usize> for Stack {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.stack[index]
    }
}

pub struct ObjectMap {
    free: usize,
    heap: Vec<Object>,
}

impl Default for ObjectMap {
    fn default() -> Self {
        Self::with_capacity(32)
    }
}

impl From<Vec<Object>> for ObjectMap {
    fn from(heap: Vec<Object>) -> Self {
        let mut free = heap.len();
        for (idx, obj) in heap.iter().enumerate() {
            if let ObjectType::Free { .. } = obj.typ {
                free = idx;
                break;
            }
        }

        Self { free, heap }
    }
}

impl ObjectMap {
    pub fn with_capacity(capacity: usize) -> Self {
        let mut heap = Vec::with_capacity(capacity);
        for i in 0..capacity {
            heap.push(Object::new(ObjectType::Free { next: i + 1 }));
        }

        Self { free: 0, heap }
    }

    pub fn allocate(&mut self, object: Object) -> usize {
        let current = self.free;
        if current >= self.heap.len() {
            self.heap
                .push(Object::new(ObjectType::Free { next: current + 1 }))
        }

        let ObjectType::Free { next } = self.heap[current].typ else {
            panic!("Allocation failed: Expected free location wasn't free");
        };

        self.heap[current] = object;
        self.free = next;

        current
    }

    pub fn get(&self, idx: usize) -> Option<&Object> {
        self.heap.get(idx)
    }

    pub fn get_mut(&mut self, idx: usize) -> Option<&mut Object> {
        self.heap.get_mut(idx)
    }
}