Null my old friend
This commit is contained in:
12
vm/vm.go
12
vm/vm.go
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
const StackSize = 2048
|
||||
|
||||
var Null = &object.Null{}
|
||||
var True = &object.Boolean{Value: true}
|
||||
var False = &object.Boolean{Value: false}
|
||||
|
||||
@@ -99,6 +100,12 @@ func (vm *VM) Run() error {
|
||||
if !isTruthy(condition) {
|
||||
ip = pos - 1
|
||||
}
|
||||
|
||||
case code.OpNull:
|
||||
err := vm.push(Null)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +118,9 @@ func isTruthy(obj object.Object) bool {
|
||||
case *object.Boolean:
|
||||
return obj.Value
|
||||
|
||||
case *object.Null:
|
||||
return false
|
||||
|
||||
default:
|
||||
return true
|
||||
}
|
||||
@@ -211,6 +221,8 @@ func (vm *VM) executeBangOperator() error {
|
||||
return vm.push(False)
|
||||
case False:
|
||||
return vm.push(True)
|
||||
case Null:
|
||||
return vm.push(True)
|
||||
default:
|
||||
return vm.push(False)
|
||||
}
|
||||
|
||||
@@ -54,6 +54,11 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
|
||||
if err != nil {
|
||||
t.Errorf("testBooleanObject failed: %s", err)
|
||||
}
|
||||
|
||||
case *object.Null:
|
||||
if actual != Null {
|
||||
t.Errorf("object is not Null: %T (%+v)", actual, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,6 +143,7 @@ func TestBooleanExpressions(t *testing.T) {
|
||||
{"!!true", true},
|
||||
{"!!false", false},
|
||||
{"!!5", true},
|
||||
{"!(if (false) { 5; })", true},
|
||||
}
|
||||
|
||||
runVmTests(t, tests)
|
||||
@@ -152,6 +158,9 @@ func TestConditionals(t *testing.T) {
|
||||
{"if (1 < 2) { 10 }", 10},
|
||||
{"if (1 < 2) { 10 } else { 20 }", 10},
|
||||
{"if (1 > 2) { 10 } else { 20 }", 20},
|
||||
{"if (1 > 2) { 10 }", Null},
|
||||
{"if (false) { 10 }", Null},
|
||||
{"if ((if (false) { 10 })) { 10 } else { 20 }", 20},
|
||||
}
|
||||
|
||||
runVmTests(t, tests)
|
||||
|
||||
Reference in New Issue
Block a user