Null my old friend
This commit is contained in:
@@ -134,16 +134,15 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
c.removeLastPop()
|
||||
}
|
||||
|
||||
// Emit an `OnJump` with a bogus value
|
||||
jumpPos := c.emit(code.OpJump, 9999)
|
||||
|
||||
afterConsequencePos := len(c.instructions)
|
||||
c.changeOperand(jumpNotTruthyPos, afterConsequencePos)
|
||||
|
||||
if node.Alternative == nil {
|
||||
afterConsequencePos := len(c.instructions)
|
||||
c.changeOperand(jumpNotTruthyPos, afterConsequencePos)
|
||||
c.emit(code.OpNull)
|
||||
} else {
|
||||
// Emit an `OnJump` with a bogus value
|
||||
jumpPos := c.emit(code.OpJump, 9999)
|
||||
|
||||
afterConsequencePos := len(c.instructions)
|
||||
c.changeOperand(jumpNotTruthyPos, afterConsequencePos)
|
||||
|
||||
err = c.Compile(node.Alternative)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -152,12 +151,11 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
if c.lastInstructionIsPop() {
|
||||
c.removeLastPop()
|
||||
}
|
||||
|
||||
afterAlternativePos := len(c.instructions)
|
||||
c.changeOperand(jumpPos, afterAlternativePos)
|
||||
|
||||
}
|
||||
|
||||
afterAlternativePos := len(c.instructions)
|
||||
c.changeOperand(jumpPos, afterAlternativePos)
|
||||
|
||||
case *ast.BlockStatement:
|
||||
for _, s := range node.Statements {
|
||||
err := c.Compile(s)
|
||||
|
||||
@@ -182,17 +182,22 @@ func TestConditionals(t *testing.T) {
|
||||
`,
|
||||
expectedConstants: []interface{}{10, 3333},
|
||||
expectedInstructions: []code.Instructions{
|
||||
// 0000
|
||||
code.Make(code.OpTrue),
|
||||
// 0001
|
||||
code.Make(code.OpJumpNotTruthy, 7),
|
||||
code.Make(code.OpJumpNotTruthy, 10),
|
||||
// 0004
|
||||
code.Make(code.OpConstant, 0),
|
||||
// 0007
|
||||
code.Make(code.OpPop),
|
||||
// 0008
|
||||
code.Make(code.OpConstant, 1),
|
||||
code.Make(code.OpJump, 11),
|
||||
// 0010
|
||||
code.Make(code.OpNull),
|
||||
// 0011
|
||||
code.Make(code.OpPop),
|
||||
// 0012
|
||||
code.Make(code.OpConstant, 1),
|
||||
// 0015
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
}, {
|
||||
input: `
|
||||
|
||||
Reference in New Issue
Block a user