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 <alekratz@gmail.com>
This commit is contained in:
2024-10-16 09:37:58 -07:00
parent 14e2e4d008
commit 1b145e8f73

View File

@@ -588,6 +588,22 @@ impl StmtVisitor for Compiler<'_> {
match stmt.op.kind { match stmt.op.kind {
// normal assignment // normal assignment
TokenKind::Eq => { 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::<FunctionExpr>()
.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 // compile RHS
self.compile_expr(&stmt.rhs)?; self.compile_expr(&stmt.rhs)?;