mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-07 05:59:03 +01:00
Prevent contants from being overridden
This commit is contained in:
parent
22bec9ff40
commit
6ad51c82ea
@ -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),
|
||||
|
@ -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,
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user