Simplify return operation
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user