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) -> (ConstHandle, StrRef) { self.const_data_mut() .const_str(s) } } #[derive(Debug, Default)] pub struct ConstData { ints: HashMap, strs: HashMap, 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) -> (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) } } }