added stack overflow error

This commit is contained in:
Moritz Gmeiner 2024-09-01 20:47:42 +02:00
commit 6386df22c0
3 changed files with 10 additions and 8 deletions

View file

@ -48,6 +48,8 @@ pub enum RuntimeError {
UndefinedAttribute { class: Rc<LoxClass>, name: Box<str> }, UndefinedAttribute { class: Rc<LoxClass>, name: Box<str> },
#[error("RuntimeError: {value} is not a valid superclass")] #[error("RuntimeError: {value} is not a valid superclass")]
InvalidSuperclass { value: Value }, InvalidSuperclass { value: Value },
#[error("RuntimeError: stack overflow")]
StackOverflow,
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]

View file

@ -206,7 +206,7 @@ impl Eval for Expr {
let mut methods: FxHashMap<String, Value> = FxHashMap::default(); let mut methods: FxHashMap<String, Value> = FxHashMap::default();
// this is the scope "this" will get injected in // this is the scope "this" will get injected in
env.enter_scope(); env.enter_scope()?;
for method_expr in method_exprs.iter() { for method_expr in method_exprs.iter() {
let method = method_expr.eval(env)?; let method = method_expr.eval(env)?;
@ -280,7 +280,7 @@ impl Eval for Stmt {
env.define(name.clone(), initializer); env.define(name.clone(), initializer);
} }
Stmt::Block { statements } => { Stmt::Block { statements } => {
env.enter_scope(); env.enter_scope()?;
for statement in statements { for statement in statements {
if let Err(err) = statement.eval(env) { if let Err(err) = statement.eval(env) {
env.exit_scope(); env.exit_scope();
@ -318,7 +318,7 @@ pub fn call_fun(fun: Rc<LoxFunction>, env: &mut Environment) -> EvalResult<Value
}); });
} }
env.enter_scope(); env.enter_scope()?;
env.define(fun.name(), Value::Function(fun.clone())); env.define(fun.name(), Value::Function(fun.clone()));

View file

@ -2,14 +2,14 @@ use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ResolverError { pub enum ResolverError {
#[error("Can't read variable {name} in its own initializer.")] #[error("ResolverError: Can't read variable {name} in its own initializer.")]
VarInOwnInitializer { name: Box<str> }, VarInOwnInitializer { name: Box<str> },
#[error("Variable {name} not defined")] #[error("ResolverError: Variable {name} not defined")]
UnresolvableVariable { name: Box<str> }, UnresolvableVariable { name: Box<str> },
#[error("Return outside of function definition")] #[error("ResolverError: Return outside of function definition")]
ReturnOutsideFunction, ReturnOutsideFunction,
#[error("this outside of method")] #[error("ResolverError: this outside of method")]
ThisOutsideMethod, ThisOutsideMethod,
#[error("super outside of subclass method")] #[error("ResolverError: super outside of subclass method")]
SuperOutsideMethod, SuperOutsideMethod,
} }