assert and test changes
This commit is contained in:
@@ -91,9 +91,7 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.lastInstructionIs(code.OpNoop) {
|
||||
c.emit(code.OpPop)
|
||||
}
|
||||
c.emit(code.OpPop)
|
||||
|
||||
case *ast.InfixExpression:
|
||||
if node.Operator == "<" || node.Operator == "<=" {
|
||||
@@ -182,9 +180,6 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
case *ast.IfExpression:
|
||||
if c.lastInstructionIs(code.OpPop) {
|
||||
c.removeLastPop()
|
||||
}
|
||||
c.l++
|
||||
err := c.Compile(node.Condition)
|
||||
c.l--
|
||||
@@ -240,6 +235,14 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
c.l--
|
||||
|
||||
if c.lastInstructionIs(code.OpPop) {
|
||||
c.removeLastPop()
|
||||
} else {
|
||||
if !c.lastInstructionIs(code.OpReturn) {
|
||||
c.emit(code.OpNull)
|
||||
}
|
||||
}
|
||||
|
||||
case *ast.AssignmentStatement:
|
||||
symbol, ok := c.symbolTable.Resolve(node.Name.Value)
|
||||
if !ok {
|
||||
@@ -366,8 +369,13 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
if c.lastInstructionIs(code.OpPop) {
|
||||
c.replaceLastPopWithReturn()
|
||||
}
|
||||
|
||||
// If the function doesn't end with a return statement add one with a
|
||||
// `return null;` and also handle the edge-case of empty functions.
|
||||
if !c.lastInstructionIs(code.OpReturn) {
|
||||
c.emit(code.OpNull)
|
||||
if !c.lastInstructionIs(code.OpNull) {
|
||||
c.emit(code.OpNull)
|
||||
}
|
||||
c.emit(code.OpReturn)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user