comparisons and booleans
This commit is contained in:
@@ -37,6 +37,20 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
c.emit(code.OpPop)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -56,6 +70,12 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
c.emit(code.OpMul)
|
||||
case "/":
|
||||
c.emit(code.OpDiv)
|
||||
case ">":
|
||||
c.emit(code.OpGreaterThan)
|
||||
case "==":
|
||||
c.emit(code.OpEqual)
|
||||
case "!=":
|
||||
c.emit(code.OpNotEqual)
|
||||
default:
|
||||
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}
|
||||
c.emit(code.OpConstant, c.addConstant(integer))
|
||||
|
||||
case *ast.Boolean:
|
||||
if node.Value {
|
||||
c.emit(code.OpTrue)
|
||||
} else {
|
||||
c.emit(code.OpFalse)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -73,6 +73,89 @@ func TestIntegerArithmetic(t *testing.T) {
|
||||
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) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user