aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-07-26 20:13:28 +0000
committerMel <einebeere@gmail.com>2022-07-26 20:13:28 +0000
commit45b6f073fe398e820e9e4a82900bc282ee32af9b (patch)
tree560169935f21015c194a9e7b8c470e3cab8d30c1 /pkg
parent7392283d9e6b8c1ee31a3ad74ce8d13d4e8b19ec (diff)
downloadjinx-45b6f073fe398e820e9e4a82900bc282ee32af9b.tar.zst
jinx-45b6f073fe398e820e9e4a82900bc282ee32af9b.zip
Add OpRet manually for functions without return
Diffstat (limited to 'pkg')
-rw-r--r--pkg/lang/compiler/compiler.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/pkg/lang/compiler/compiler.go b/pkg/lang/compiler/compiler.go
index 5e2a645..9dbfd12 100644
--- a/pkg/lang/compiler/compiler.go
+++ b/pkg/lang/compiler/compiler.go
@@ -112,6 +112,14 @@ func (comp *Compiler) compileFnDeclStmt(t *code.Builder, fnDeclStmt ast.StmtFnDe
return err
}
+ // If the function did not end with a return statement, we need to add an OpRet for safety.
+ lastStmt := fnDeclStmt.Body.Stmts[len(fnDeclStmt.Body.Stmts)-1]
+ // TODO: Get rid of EmptyStmt so we can use the Kind field to determine if the last statement is a return statement.
+ if lastStmt.Kind != ast.StmtKindReturn {
+ functionTarget.AppendOp(code.OpPushNull)
+ functionTarget.AppendOp(code.OpRet)
+ }
+
fnScope := comp.scopes.CurrentFunction()
_ = comp.scopes.Exit() // Function declaration scopes do not pollute stack