Fold runtime/ crate into this source tree
While I like the idea of having a runtime completely decoupled from the syntax and compiler, I don't think this is that big of a project for that to be necessary or even useful yet. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
47
src/vm/consts.rs
Normal file
47
src/vm/consts.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use crate::obj::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct ConstHandle(usize);
|
||||
|
||||
impl ConstHandle {
|
||||
pub fn index(&self) -> usize {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn new(handle: usize) -> Self {
|
||||
ConstHandle(handle)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ConstPool {
|
||||
pool: Vec<ObjRef>,
|
||||
}
|
||||
|
||||
impl ConstPool {
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
pub fn push(&mut self, value: ObjRef) -> ConstHandle {
|
||||
let hdl = ConstHandle::new(self.pool.len());
|
||||
self.pool.push(value);
|
||||
hdl
|
||||
}
|
||||
|
||||
pub fn get(&self, hdl: ConstHandle) -> &ObjRef {
|
||||
self.pool.get(hdl.index()).unwrap()
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.pool.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Index<ConstHandle> for ConstPool {
|
||||
type Output = ObjRef;
|
||||
|
||||
fn index(&self, hdl: ConstHandle) -> &Self::Output {
|
||||
self.get(hdl)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user