about summary refs log tree commit diff
path: root/pkg/lang/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/compiler')
-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