comparisons and booleans

This commit is contained in:
Chuck Smith
2024-02-05 16:58:59 -05:00
parent b4cc771baa
commit dcc869a6e2
5 changed files with 233 additions and 6 deletions

View File

@@ -17,6 +17,11 @@ const (
OpSub OpSub
OpMul OpMul
OpDiv OpDiv
OpTrue
OpFalse
OpEqual
OpNotEqual
OpGreaterThan
) )
type Definition struct { type Definition struct {
@@ -31,6 +36,11 @@ var definitions = map[Opcode]*Definition{
OpSub: {"OpSub", []int{}}, OpSub: {"OpSub", []int{}},
OpMul: {"OpMul", []int{}}, OpMul: {"OpMul", []int{}},
OpDiv: {"OpDiv", []int{}}, OpDiv: {"OpDiv", []int{}},
OpTrue: {"OpTrue", []int{}},
OpFalse: {"OpFalse", []int{}},
OpEqual: {"OpEqual", []int{}},
OpNotEqual: {"OpNotEqual", []int{}},
OpGreaterThan: {"OpGreaterThan", []int{}},
} }
func Lookup(op byte) (*Definition, error) { func Lookup(op byte) (*Definition, error) {

View File

@@ -37,6 +37,20 @@ func (c *Compiler) Compile(node ast.Node) error {
c.emit(code.OpPop) c.emit(code.OpPop)
case *ast.InfixExpression: case *ast.InfixExpression:
if node.Operator == "<" {
err := c.Compile(node.Right)
if err != nil {
return err
}
err = c.Compile(node.Left)
if err != nil {
return err
}
c.emit(code.OpGreaterThan)
return nil
}
err := c.Compile(node.Left) err := c.Compile(node.Left)
if err != nil { if err != nil {
return err return err
@@ -56,6 +70,12 @@ func (c *Compiler) Compile(node ast.Node) error {
c.emit(code.OpMul) c.emit(code.OpMul)
case "/": case "/":
c.emit(code.OpDiv) c.emit(code.OpDiv)
case ">":
c.emit(code.OpGreaterThan)
case "==":
c.emit(code.OpEqual)
case "!=":
c.emit(code.OpNotEqual)
default: default:
return fmt.Errorf("unknown operator %s", node.Operator) return fmt.Errorf("unknown operator %s", node.Operator)
} }
@@ -64,6 +84,13 @@ func (c *Compiler) Compile(node ast.Node) error {
integer := &object.Integer{Value: node.Value} integer := &object.Integer{Value: node.Value}
c.emit(code.OpConstant, c.addConstant(integer)) c.emit(code.OpConstant, c.addConstant(integer))
case *ast.Boolean:
if node.Value {
c.emit(code.OpTrue)
} else {
c.emit(code.OpFalse)
}
} }
return nil return nil

View File

@@ -73,6 +73,89 @@ func TestIntegerArithmetic(t *testing.T) {
runCompilerTests(t, tests) runCompilerTests(t, tests)
} }
func TestBooleanExpressions(t *testing.T) {
tests := []compilerTestCase{
{
input: "true",
expectedConstants: []interface{}{},
expectedInstructions: []code.Instructions{
code.Make(code.OpTrue),
code.Make(code.OpPop),
},
},
{
input: "false",
expectedConstants: []interface{}{},
expectedInstructions: []code.Instructions{
code.Make(code.OpFalse),
code.Make(code.OpPop),
},
},
{
input: "1 > 2",
expectedConstants: []interface{}{1, 2},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpGreaterThan),
code.Make(code.OpPop),
},
},
{
input: "1 < 2",
expectedConstants: []interface{}{2, 1},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpGreaterThan),
code.Make(code.OpPop),
},
},
{
input: "1 == 2",
expectedConstants: []interface{}{1, 2},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpEqual),
code.Make(code.OpPop),
},
},
{
input: "1 != 2",
expectedConstants: []interface{}{1, 2},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpNotEqual),
code.Make(code.OpPop),
},
},
{
input: "true == false",
expectedConstants: []interface{}{},
expectedInstructions: []code.Instructions{
code.Make(code.OpTrue),
code.Make(code.OpFalse),
code.Make(code.OpEqual),
code.Make(code.OpPop),
},
},
{
input: "true != false",
expectedConstants: []interface{}{},
expectedInstructions: []code.Instructions{
code.Make(code.OpTrue),
code.Make(code.OpFalse),
code.Make(code.OpNotEqual),
code.Make(code.OpPop),
},
},
}
runCompilerTests(t, tests)
}
func runCompilerTests(t *testing.T, tests []compilerTestCase) { func runCompilerTests(t *testing.T, tests []compilerTestCase) {
t.Helper() t.Helper()

View File

@@ -9,6 +9,9 @@ import (
const StackSize = 2048 const StackSize = 2048
var True = &object.Boolean{Value: true}
var False = &object.Boolean{Value: false}
type VM struct { type VM struct {
constants []object.Object constants []object.Object
instructions code.Instructions instructions code.Instructions
@@ -53,6 +56,24 @@ func (vm *VM) Run() error {
case code.OpPop: case code.OpPop:
vm.pop() vm.pop()
case code.OpTrue:
err := vm.push(True)
if err != nil {
return err
}
case code.OpFalse:
err := vm.push(False)
if err != nil {
return err
}
case code.OpEqual, code.OpNotEqual, code.OpGreaterThan:
err := vm.executeComparison(op)
if err != nil {
return err
}
} }
} }
@@ -111,3 +132,44 @@ func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.O
return vm.push(&object.Integer{Value: result}) return vm.push(&object.Integer{Value: result})
} }
func (vm *VM) executeComparison(op code.Opcode) error {
right := vm.pop()
left := vm.pop()
if left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ {
return vm.executeIntegerComparison(op, left, right)
}
switch op {
case code.OpEqual:
return vm.push(nativeBoolToBooleanObject(right == left))
case code.OpNotEqual:
return vm.push(nativeBoolToBooleanObject(right != left))
default:
return fmt.Errorf("unknown operator: %d (%s %s)", op, left.Type(), right.Type())
}
}
func (vm *VM) executeIntegerComparison(op code.Opcode, left, right object.Object) error {
leftValue := left.(*object.Integer).Value
rightValue := right.(*object.Integer).Value
switch op {
case code.OpEqual:
return vm.push(nativeBoolToBooleanObject(rightValue == leftValue))
case code.OpNotEqual:
return vm.push(nativeBoolToBooleanObject(rightValue != leftValue))
case code.OpGreaterThan:
return vm.push(nativeBoolToBooleanObject(leftValue > rightValue))
default:
return fmt.Errorf("unknown operator: %d", op)
}
}
func nativeBoolToBooleanObject(input bool) *object.Boolean {
if input {
return True
}
return False
}

View File

@@ -48,6 +48,12 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
if err != nil { if err != nil {
t.Errorf("testIntegerObject failed: %s", err) t.Errorf("testIntegerObject failed: %s", err)
} }
case bool:
err := testBooleanObject(bool(expected), actual)
if err != nil {
t.Errorf("testBooleanObject failed: %s", err)
}
} }
} }
@@ -70,6 +76,19 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
return nil return nil
} }
func testBooleanObject(expected bool, actual object.Object) interface{} {
result, ok := actual.(*object.Boolean)
if !ok {
return fmt.Errorf("object is not Boolean. got=%T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%t, want=%t", result.Value, expected)
}
return nil
}
func TestIntegerArithmetic(t *testing.T) { func TestIntegerArithmetic(t *testing.T) {
tests := []vmTestCase{ tests := []vmTestCase{
{"1", 1}, {"1", 1},
@@ -83,6 +102,32 @@ func TestIntegerArithmetic(t *testing.T) {
{"5 * 2 + 10", 20}, {"5 * 2 + 10", 20},
{"5 + 2 * 10", 25}, {"5 + 2 * 10", 25},
{"5 * (2 + 10)", 60}, {"5 * (2 + 10)", 60},
{"1 < 2", true},
{"1 > 2", false},
{"1 < 1", false},
{"1 > 1", false},
{"1 == 1", true},
{"1 != 1", false},
{"1 == 2", false},
{"1 != 2", true},
{"true == true", true},
{"false == false", true},
{"true == false", false},
{"true != false", true},
{"false != true", true},
{"(1 < 2) == true", true},
{"(1 < 2) == false", false},
{"(1 > 2) == true", false},
{"(1 > 2) == false", true},
}
runVmTests(t, tests)
}
func TestBooleanExpressions(t *testing.T) {
tests := []vmTestCase{
{"true", true},
{"false", false},
} }
runVmTests(t, tests) runVmTests(t, tests)