mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-31 08:59:15 +01:00
Prevent self-referencing variables
This commit is contained in:
parent
865947dccd
commit
e8c362c72e
@ -84,6 +84,7 @@ pub enum CalcError {
|
|||||||
InvalidOperator,
|
InvalidOperator,
|
||||||
InvalidUnit,
|
InvalidUnit,
|
||||||
TimedOut,
|
TimedOut,
|
||||||
|
VariableReferencesItself,
|
||||||
UnexpectedToken(TokenKind, TokenKind),
|
UnexpectedToken(TokenKind, TokenKind),
|
||||||
UndefinedFn(String),
|
UndefinedFn(String),
|
||||||
UndefinedVar(String),
|
UndefinedVar(String),
|
||||||
@ -195,6 +196,9 @@ fn parse_var_decl_stmt(context: &mut Context) -> Result<Stmt, CalcError> {
|
|||||||
let identifier = advance(context).clone();
|
let identifier = advance(context).clone();
|
||||||
advance(context); // Equal sign
|
advance(context); // Equal sign
|
||||||
let expr = parse_expr(context)?;
|
let expr = parse_expr(context)?;
|
||||||
|
if inverter::contains_var(&context.symbol_table, &expr, &identifier.value) {
|
||||||
|
return Err(CalcError::VariableReferencesItself);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Stmt::VarDecl(identifier.value, Box::new(expr)))
|
Ok(Stmt::VarDecl(identifier.value, Box::new(expr)))
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ fn print_calc_err(err: CalcError) {
|
|||||||
InvalidOperator => format!("Invalid operator."),
|
InvalidOperator => format!("Invalid operator."),
|
||||||
InvalidUnit => format!("Invalid unit."),
|
InvalidUnit => format!("Invalid unit."),
|
||||||
TimedOut => format!("Operation took too long."),
|
TimedOut => format!("Operation took too long."),
|
||||||
|
VariableReferencesItself => format!("Variable references itself."),
|
||||||
UnexpectedToken(got, expected) => {
|
UnexpectedToken(got, expected) => {
|
||||||
format!("Unexpected token: '{:?}', expected '{:?}'.", got, expected)
|
format!("Unexpected token: '{:?}', expected '{:?}'.", got, expected)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user