This commit is contained in:
Chuck Smith
2024-01-25 11:35:16 -05:00
parent fe78b7069b
commit ca263209a4
6 changed files with 179 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package compiler
import (
"fmt"
"monkey/ast"
"monkey/code"
"monkey/object"
@@ -45,6 +46,13 @@ func (c *Compiler) Compile(node ast.Node) error {
return err
}
switch node.Operator {
case "+":
c.emit(code.OpAdd)
default:
return fmt.Errorf("unknown operator %s", node.Operator)
}
case *ast.IntegerLiteral:
integer := &object.Integer{Value: node.Value}
c.emit(code.OpConstant, c.addConstant(integer))

View File

@@ -24,6 +24,7 @@ func TestIntegerArithmetic(t *testing.T) {
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpAdd),
},
},
}