Skip to content

Commit

Permalink
fix: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 16, 2024
1 parent 6e6f238 commit 02bbe70
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 44 deletions.
1 change: 0 additions & 1 deletion lykiadb-server/src/engine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ pub fn report_error(
}
#[cfg(test)]
mod tests {


use super::*;
use lykiadb_lang::{
Expand Down
22 changes: 12 additions & 10 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ impl Interpreter {
&self.interner.get_or_intern(name),
)
} else {
self.root_env.read(
name,
&self.interner.get_or_intern(name),
)
self.root_env.read(name, &self.interner.get_or_intern(name))
}
}

Expand Down Expand Up @@ -370,7 +367,11 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
evaluated.clone(),
)
} else {
self.root_env.assign( &dst.name, self.interner.get_or_intern(&dst.name), evaluated.clone(),)
self.root_env.assign(
&dst.name,
self.interner.get_or_intern(&dst.name),
evaluated.clone(),
)
};
if result.is_err() {
return Err(result.err().unwrap());
Expand Down Expand Up @@ -587,13 +588,14 @@ impl VisitorMut<RV, HaltReason> for Interpreter {
}
Stmt::Declaration { dst, expr, .. } => {
let evaluated = self.visit_expr(expr)?;
self.env.define(
self.interner.get_or_intern(&dst.name),
evaluated.clone(),
);
self.env
.define(self.interner.get_or_intern(&dst.name), evaluated.clone());
}
Stmt::Block { body: stmts, .. } => {
return self.execute_block(stmts, Arc::new(EnvironmentFrame::new(Some(Arc::clone(&self.env)))));
return self.execute_block(
stmts,
Arc::new(EnvironmentFrame::new(Some(Arc::clone(&self.env)))),
);
}
Stmt::If {
condition,
Expand Down
2 changes: 1 addition & 1 deletion lykiadb-server/src/value/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Function {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Function::Stateful(_) | Function::Lambda { .. } => write!(f, "<native_fn>"),
Function::UserDefined { .. } => write!(f, "<user_defined_fn>"),
Function::UserDefined { .. } => write!(f, "<user_defined_fn>"),
}
}
}
Expand Down
60 changes: 28 additions & 32 deletions lykiadb-server/src/value/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct EnvironmentFrame {
pub parent: Option<Arc<EnvironmentFrame>>,
}


#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub enum EnvironmentError {
/*AssignmentToUndefined {
Expand All @@ -33,7 +32,6 @@ macro_rules! env_ancestor {
}};
}


impl From<EnvironmentError> for ExecutionError {
fn from(err: EnvironmentError) -> Self {
ExecutionError::Environment(err)
Expand All @@ -42,19 +40,17 @@ impl From<EnvironmentError> for ExecutionError {

impl EnvironmentFrame {
pub fn new(parent: Option<Arc<EnvironmentFrame>>) -> EnvironmentFrame {
EnvironmentFrame { parent, map: RwLock::new(FxHashMap::default()) }
EnvironmentFrame {
parent,
map: RwLock::new(FxHashMap::default()),
}
}

pub fn define(&self, name: SymbolU32, value: RV) {
self.map.write().unwrap().insert(name, value);
}

pub fn assign(
&self,
key: &str,
key_sym: SymbolU32,
value: RV,
) -> Result<bool, HaltReason> {
pub fn assign(&self, key: &str, key_sym: SymbolU32, value: RV) -> Result<bool, HaltReason> {
if self.map.read().unwrap().contains_key(&key_sym) {
self.map.write().unwrap().insert(key_sym, value);
return Ok(true);
Expand All @@ -67,7 +63,7 @@ impl EnvironmentFrame {
}
.into(),
)),
|parent| parent.as_ref().assign(key, key_sym, value)
|parent| parent.as_ref().assign(key, key_sym, value),
)
}

Expand All @@ -94,7 +90,7 @@ impl EnvironmentFrame {
.into(),
)),
|parent| parent.read(key, key_sym),
)
);
}

pub fn read_at(
Expand All @@ -105,13 +101,18 @@ impl EnvironmentFrame {
) -> Result<RV, HaltReason> {
env_ancestor!(env, distance)
.map
.read().unwrap()
.get(&key_sym).map_or(Err(HaltReason::Error(
EnvironmentError::Other {
message: format!("Variable '{}' was not found", key),
}
.into(),
)), |v| Ok(v.clone()))
.read()
.unwrap()
.get(&key_sym)
.map_or(
Err(HaltReason::Error(
EnvironmentError::Other {
message: format!("Variable '{}' was not found", key),
}
.into(),
)),
|v| Ok(v.clone()),
)
}
}

Expand Down Expand Up @@ -147,9 +148,7 @@ mod test {
root.define(interner.get_or_intern("five"), RV::Num(5.0));
let child = super::EnvironmentFrame::new(Some(Arc::new(root)));
assert_eq!(
child
.read("five", &interner.get_or_intern("five"))
.unwrap(),
child.read("five", &interner.get_or_intern("five")).unwrap(),
RV::Num(5.0)
);
}
Expand All @@ -158,23 +157,22 @@ mod test {
fn test_write_to_parent() {
let root = Arc::new(super::EnvironmentFrame::new(None));
let mut interner = get_interner();

root.define(interner.get_or_intern("five"), RV::Num(5.0));

let child = super::EnvironmentFrame::new(Some(root.clone()));

child.assign("five", interner.get_or_intern("five"), RV::Num(5.1))

child
.assign("five", interner.get_or_intern("five"), RV::Num(5.1))
.unwrap();

assert_eq!(
root.read("five", &interner.get_or_intern("five"))
.unwrap(),
root.read("five", &interner.get_or_intern("five")).unwrap(),
RV::Num(5.1)
);

assert_eq!(
child.read("five", &interner.get_or_intern("five"))
.unwrap(),
child.read("five", &interner.get_or_intern("five")).unwrap(),
RV::Num(5.1)
);
}
Expand All @@ -183,9 +181,7 @@ mod test {
fn test_read_undefined_variable() {
let env = super::EnvironmentFrame::new(None);
let mut interner = get_interner();
assert!(env
.read("five", &interner.get_or_intern("five"))
.is_err());
assert!(env.read("five", &interner.get_or_intern("five")).is_err());
}

#[test]
Expand Down

0 comments on commit 02bbe70

Please sign in to comment.