diff --git a/kalk/src/parser.rs b/kalk/src/parser.rs index cf126be..26c9303 100644 --- a/kalk/src/parser.rs +++ b/kalk/src/parser.rs @@ -112,6 +112,7 @@ pub enum CalcError { UndefinedVar(String), UnableToInvert(String), UnableToSolveEquation, + UnableToOverrideConstant(String), UnableToParseExpression, Unknown, } @@ -140,6 +141,7 @@ impl ToString for CalcError { CalcError::UndefinedVar(name) => format!("Undefined variable: '{}'.", name), CalcError::UnableToParseExpression => format!("Unable to parse expression."), CalcError::UnableToSolveEquation => format!("Unable to solve equation."), + CalcError::UnableToOverrideConstant(name) => format!("Unable to override constant: '{}'.", name), CalcError::Unknown => format!("Unknown error."), } } @@ -317,6 +319,10 @@ fn parse_var_decl_stmt(context: &mut Context) -> Result { return Err(CalcError::VariableReferencesItself); } + if prelude::is_constant(&identifier.value) { + return Err(CalcError::UnableToOverrideConstant(identifier.value.into())); + } + Ok(Stmt::VarDecl( Identifier::from_full_name(&identifier.value), Box::new(expr), diff --git a/kalk/src/prelude/mod.rs b/kalk/src/prelude/mod.rs index db2b75c..5271c22 100644 --- a/kalk/src/prelude/mod.rs +++ b/kalk/src/prelude/mod.rs @@ -171,6 +171,10 @@ pub fn is_prelude_func(identifier: &str) -> bool { || BINARY_FUNCS.contains_key(identifier) } +pub fn is_constant(identifier: &str) -> bool { + CONSTANTS.contains_key(identifier) +} + pub fn call_unary_func( context: &mut interpreter::Context, name: &str, diff --git a/kalk/src/symbol_table.rs b/kalk/src/symbol_table.rs index fc78764..207cc1a 100644 --- a/kalk/src/symbol_table.rs +++ b/kalk/src/symbol_table.rs @@ -86,7 +86,7 @@ impl SymbolTable { } pub fn contains_var(&self, identifier: &str) -> bool { - prelude::CONSTANTS.contains_key(identifier) + prelude::is_constant(identifier) || identifier == "i" || self.hashmap.contains_key(&format!("var.{}", identifier)) }