Skip to content

Commit

Permalink
add basic type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser committed Jun 14, 2024
1 parent 4803b72 commit 222cbec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/backend/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ impl Environment {
process::exit(1);
}

match Val::is_same_type(containing_env.borrow().values.get(name).unwrap(), &value) {
Ok(()) => {}
Err(e) => {
println!("{}", e);
process::exit(1);
}
}

containing_env
.borrow_mut()
.values
Expand Down
15 changes: 15 additions & 0 deletions src/backend/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ pub enum Val {
Special(SpecialVal),
}

impl Val {
pub fn is_same_type(val1: &Val, val2: &Val) -> Result<(), String> {
let type1 = val1.get_type();
let type2 = val2.get_type();

if type1 != type2 {
return Err(format!(
"Type mismatch: {:?} is not the same as {:?}",
type1, type2
));
}
Ok(())
}
}

impl RuntimeVal for Val {
fn get_type(&self) -> ValueType {
match self {
Expand Down

0 comments on commit 222cbec

Please sign in to comment.