Simplify return operation
Some checks failed
Build / build (push) Failing after 1m11s
Test / build (push) Failing after 11m42s

This commit is contained in:
Chuck Smith
2024-03-18 16:33:39 -04:00
parent b47a39e1b2
commit 98c8582fdb
7 changed files with 87 additions and 51 deletions

View File

@@ -157,6 +157,9 @@ func (c *Compiler) Compile(node ast.Node) error {
}
case *ast.IfExpression:
if c.lastInstructionIs(code.OpPop) {
c.removeLastPop()
}
err := c.Compile(node.Condition)
if err != nil {
return err
@@ -314,7 +317,8 @@ func (c *Compiler) Compile(node ast.Node) error {
if c.lastInstructionIs(code.OpPop) {
c.replaceLastPopWithReturn()
}
if !c.lastInstructionIs(code.OpReturnValue) {
if !c.lastInstructionIs(code.OpReturn) {
c.emit(code.OpNull)
c.emit(code.OpReturn)
}
@@ -341,7 +345,7 @@ func (c *Compiler) Compile(node ast.Node) error {
return err
}
c.emit(code.OpReturnValue)
c.emit(code.OpReturn)
case *ast.CallExpression:
err := c.Compile(node.Function)
@@ -481,9 +485,9 @@ func (c *Compiler) leaveScope() code.Instructions {
func (c *Compiler) replaceLastPopWithReturn() {
lastPos := c.scopes[c.scopeIndex].lastInstruction.Position
c.replaceInstruction(lastPos, code.Make(code.OpReturnValue))
c.replaceInstruction(lastPos, code.Make(code.OpReturn))
c.scopes[c.scopeIndex].lastInstruction.Opcode = code.OpReturnValue
c.scopes[c.scopeIndex].lastInstruction.Opcode = code.OpReturn
}
type Bytecode struct {