Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Revert "rewrite enviroments"
Browse files Browse the repository at this point in the history
This reverts commit 9811e27.
  • Loading branch information
Thepigcat76 committed Nov 30, 2023
1 parent c3842f6 commit 966292e
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 191 deletions.
103 changes: 25 additions & 78 deletions src/evaluator/enviroment.rs
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() },
}
}
}
}
Loading

0 comments on commit 966292e

Please sign in to comment.