From 1b145e8f73de621e6fdcb71ad5b421d16a9858d4 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 16 Oct 2024 09:37:58 -0700 Subject: [PATCH] Fix bug that disabled the creation of recursive functions ...oops. The introduction and usage of `Compiler::emit_assign` deferred the declaration of a name to be declared *after* the RHS of an assignment had been evaluated. Normally, this is not a problem because you shouldn't be using the LHS that you're assigning in the RHS expression... except when you're defining a function. This has been enabled for functions *only* and well be enabled for other types as necessary. Signed-off-by: Alek Ratzloff --- src/compiler.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/compiler.rs b/src/compiler.rs index 300ab68..3e57712 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -588,6 +588,22 @@ impl StmtVisitor for Compiler<'_> { match stmt.op.kind { // normal assignment TokenKind::Eq => { + // Insert the name beforehand, but only if we're compiling a function + // I think this is the only instance where we care about inserting a name before + // it's used + if stmt + .rhs + .as_any_ref() + .downcast_ref::() + .is_some() + { + if self.is_global_scope() { + self.insert_global(name)?; + } else if self.get_local(name).is_none() { + self.insert_local(name.to_string())?; + } + } + // compile RHS self.compile_expr(&stmt.rhs)?;