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
|
||||
|
||||
Reference in New Issue
Block a user