Greater than and Less Than
This commit is contained in:
@@ -81,7 +81,7 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
case *ast.InfixExpression:
|
||||
if node.Operator == "<" {
|
||||
if node.Operator == "<" || node.Operator == "<=" {
|
||||
err := c.Compile(node.Right)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -91,7 +91,11 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.emit(code.OpGreaterThan)
|
||||
if node.Operator == "<=" {
|
||||
c.emit(code.OpGreaterThanEqual)
|
||||
} else {
|
||||
c.emit(code.OpGreaterThan)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -116,6 +120,8 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
c.emit(code.OpDiv)
|
||||
case ">":
|
||||
c.emit(code.OpGreaterThan)
|
||||
case ">=":
|
||||
c.emit(code.OpGreaterThanEqual)
|
||||
case "==":
|
||||
c.emit(code.OpEqual)
|
||||
case "!=":
|
||||
|
||||
@@ -120,6 +120,26 @@ func TestBooleanExpressions(t *testing.T) {
|
||||
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.OpGreaterThanEqual),
|
||||
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.OpGreaterThanEqual),
|
||||
code.Make(code.OpPop),
|
||||
},
|
||||
},
|
||||
{
|
||||
input: "1 == 2",
|
||||
expectedConstants: []interface{}{1, 2},
|
||||
|
||||
Reference in New Issue
Block a user