General changes across the runtime crate in support of compile module

* Compile module is able to compile bytecode (or so it seems...)
* Runtime crate has had some new stuff added to it, mostly with objects
  and vtables. Still not 100% on the object method function call story,
  but I guess it'll be tackled when we get there.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-09-14 14:09:29 -07:00
parent e2c43dc911
commit 8e2cbb10a4
17 changed files with 743 additions and 175 deletions

74
src/compile/mod.rs Normal file
View File

@@ -0,0 +1,74 @@
pub mod thunk;
pub mod error;
use runtime::{obj::prelude::*, vm::consts::*};
use std::collections::HashMap;
pub struct Compile<'t> {
text: &'t str,
const_data: ConstData,
}
impl<'t> Compile<'t> {
pub fn new(text: &'t str) -> Self {
Compile { text, const_data: Default::default() }
}
pub fn text(&self) -> &'t str {
self.text
}
pub fn const_data(&self) -> &ConstData {
&self.const_data
}
pub fn const_data_mut(&mut self) -> &mut ConstData {
&mut self.const_data
}
/// Gets or inserts a static int reference.
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
self.const_data_mut()
.const_int(int)
}
/// Gets or inserts a static string reference.
pub fn const_str(&mut self, s: impl AsRef<str>) -> (ConstHandle, StrRef) {
self.const_data_mut()
.const_str(s)
}
}
#[derive(Debug, Default)]
pub struct ConstData {
ints: HashMap<i64, (ConstHandle, IntRef)>,
strs: HashMap<String, (ConstHandle, StrRef)>,
const_pool: ConstPool,
}
impl ConstData {
/// Gets or inserts a static int reference.
pub fn const_int(&mut self, int: i64) -> (ConstHandle, IntRef) {
if let Some(pair) = self.ints.get(&int) {
pair.clone()
} else {
let obj = Int::new_obj(int);
let hdl = self.const_pool.push(obj.clone());
self.ints.insert(int, (hdl, obj.clone()));
(hdl, obj)
}
}
/// Gets or inserts a static string reference.
pub fn const_str(&mut self, s: impl AsRef<str>) -> (ConstHandle, StrRef) {
let s = s.as_ref();
if let Some(pair) = self.strs.get(s) {
pair.clone()
} else {
let obj = Str::new_obj(s.to_string());
let hdl = self.const_pool.push(obj.clone());
self.strs.insert(s.to_string(), (hdl, obj.clone()));
(hdl, obj)
}
}
}