Prevent contants from being overridden

This commit is contained in:
PaddiM8 2021-09-22 15:33:58 +02:00 committed by PaddiM8
parent 22bec9ff40
commit 6ad51c82ea
3 changed files with 11 additions and 1 deletions

View File

@ -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<Stmt, CalcError> {
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),

View File

@ -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,

View File

@ -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))
}