This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
142 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,48 @@ | ||
use std::collections::HashMap; | ||
|
||
use super::object::Object; | ||
|
||
type OptionalEnvObj = Option<Box<EnvObj>>; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum EnvObj { | ||
OBJ(Obj), | ||
SCOPE(Scope), | ||
} | ||
use crate::evaluator::object::Object; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Obj { | ||
pub obj: Box<Object>, | ||
pub struct EnvObj { | ||
pub is_const: bool, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Scope { | ||
objs: HashMap<String, OptionalEnvObj>, | ||
} | ||
|
||
impl Scope { | ||
pub fn set(&mut self, name: &String, obj: Obj) { | ||
match self.objs.insert(format!("$var_{}", name), Some(Box::from(EnvObj::OBJ(obj)))) { | ||
Some(_) => (), | ||
None => (), | ||
} | ||
} | ||
|
||
pub fn get(&mut self, name: &String) -> Result<Obj, ()> { | ||
match self.objs.get(&format!("$var_{}", name)) { | ||
Some(obj) => match obj { | ||
Some(val) => match &**val { | ||
EnvObj::OBJ(val) => Ok(val.to_owned()), | ||
EnvObj::SCOPE(_) => todo!(), | ||
}, | ||
None => todo!(), | ||
}, | ||
None => Err(()), | ||
} | ||
} | ||
pub is_local: bool, | ||
pub obj: Object, | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Environment { | ||
scope_id: i64, | ||
objs: HashMap<String, EnvObj>, | ||
store: HashMap<String, EnvObj>, | ||
} | ||
|
||
impl Environment { | ||
pub fn new() -> Self { | ||
Self { | ||
scope_id: 0, | ||
objs: HashMap::new(), | ||
} | ||
let s = HashMap::new(); | ||
Environment { store: s} | ||
} | ||
|
||
pub fn set_global(&mut self, name: &String, obj: Obj) { | ||
match self.objs.insert(format!("$var_{}", name), EnvObj::OBJ(obj)) { | ||
Some(_) => (), | ||
None => (), | ||
} | ||
pub fn get(&self, name: &String) -> Result<EnvObj, ()> { | ||
let obj = match self.store.get(name) { | ||
Some(val) => Ok(val.clone()), | ||
None => { | ||
Err(()) // Return an error if the key is not found | ||
} | ||
}; | ||
obj | ||
} | ||
|
||
pub fn get_global(&self, name: &String) -> Result<Obj, ()> { | ||
match self.objs.get(&format!("$var_{}", name)) { | ||
Some(obj) => match &obj { | ||
EnvObj::OBJ(val) => Ok(val.to_owned()), | ||
EnvObj::SCOPE(_) => todo!(), | ||
}, | ||
None => Err(()), | ||
} | ||
pub fn set(&mut self, name: &String, val: &Object, is_local: bool, is_const: bool) -> Object { | ||
self.store.insert(name.to_string(), EnvObj { is_const, is_local, obj: val.to_owned() }); | ||
val.clone() | ||
} | ||
|
||
pub fn modify_global(&mut self, name: &String, new_val: Object) { | ||
match self.objs.get_mut(&format!("$var_{}", name)) { | ||
Some(val) => match val { | ||
EnvObj::OBJ(obj) => if !obj.is_const { | ||
obj.obj = Box::new(new_val) | ||
} else { | ||
todo!("Cannot modify a constant variable") | ||
} | ||
EnvObj::SCOPE(_) => todo!(), | ||
pub fn modify(&mut self, name: &String, new_val: Object) { | ||
match self.store.get_mut(name) { | ||
Some(val) => if !val.is_const { | ||
val.obj = new_val | ||
} else { | ||
todo!("Cannot modify a constant variable") | ||
}, | ||
None => todo!(), | ||
} | ||
} | ||
|
||
pub fn create_scope(&mut self) -> Scope { | ||
self.scope_id += 1; | ||
match self.objs.insert( | ||
format!("#scope_{}", self.scope_id), | ||
EnvObj::SCOPE(Scope { objs: HashMap::new() }), | ||
) { | ||
Some(_) => Scope { objs: HashMap::new() }, | ||
None => Scope { objs: HashMap::new() }, | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.