support code generation through () blocks

This commit is contained in:
2024-10-25 20:55:07 -04:00
parent d29fa2b9b0
commit a28dedb1ea
5 changed files with 654 additions and 475 deletions

View File

@@ -55,7 +55,7 @@ impl Display for Type {
}
/// Represents the result of executing a ParseTree with an Executor
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
pub enum Value {
Float(f64),
Int(i64),
@@ -66,6 +66,20 @@ pub enum Value {
Nil,
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Float(l0), Self::Float(r0)) => l0 == r0,
(Self::Int(l0), Self::Int(r0)) => l0 == r0,
(Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
(Self::String(l0), Self::String(r0)) => l0 == r0,
(Self::Array(l0, l1), Self::Array(r0, r1)) => l0 == r0 && l1 == r1,
(Self::Function(_), Self::Function(_)) => false,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
impl Value {
pub(crate) fn get_type(&self) -> Type {
match self {
@@ -94,7 +108,7 @@ impl Display for Value {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug)]
enum Cache {
Cached(Value),
Uncached(ParseTree),
@@ -107,12 +121,6 @@ struct Object {
value: Cache,
}
impl PartialEq for Object {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl Object {
pub fn variable(tree: ParseTree, globals: HashMap<String, Arc<Mutex<Object>>>, locals: HashMap<String, Arc<Mutex<Object>>>) -> Self {
Self {
@@ -143,12 +151,12 @@ impl Object {
match self.value.clone() {
Cache::Cached(v) => Ok(v),
Cache::Uncached(tree) => {
let mut tree = vec![Ok(tree)].into_iter();
let mut t = vec![Ok(tree.clone())].into_iter();
let mut exec = Executor::new(&mut tree, &mut self.globals)
let mut exec = Executor::new(&mut t, &mut self.globals)
.locals(self.locals.clone());
let v = exec.next().unwrap()?;
let v = exec.exec(Box::new(tree))?;
self.value = Cache::Cached(v.clone());
@@ -157,11 +165,11 @@ impl Object {
}
}
pub fn locals(&self) -> HashMap<String, Arc<Mutex<Object>>> {
pub fn _locals(&self) -> HashMap<String, Arc<Mutex<Object>>> {
self.locals.clone()
}
pub fn globals(&self) -> HashMap<String, Arc<Mutex<Object>>> {
pub fn _globals(&self) -> HashMap<String, Arc<Mutex<Object>>> {
self.globals.clone()
}
}