compiler!

This commit is contained in:
Chuck Smith
2024-01-24 19:35:32 -05:00
parent 423027cda0
commit fe78b7069b
4 changed files with 399 additions and 0 deletions

84
compiler/compiler.go Normal file
View File

@@ -0,0 +1,84 @@
package compiler
import (
"monkey/ast"
"monkey/code"
"monkey/object"
)
type Compiler struct {
instructions code.Instructions
constants []object.Object
}
func New() *Compiler {
return &Compiler{
instructions: code.Instructions{},
constants: []object.Object{},
}
}
func (c *Compiler) Compile(node ast.Node) error {
switch node := node.(type) {
case *ast.Program:
for _, s := range node.Statements {
err := c.Compile(s)
if err != nil {
return err
}
}
case *ast.ExpressionStatement:
err := c.Compile(node.Expression)
if err != nil {
return err
}
case *ast.InfixExpression:
err := c.Compile(node.Left)
if err != nil {
return err
}
err = c.Compile(node.Right)
if err != nil {
return err
}
case *ast.IntegerLiteral:
integer := &object.Integer{Value: node.Value}
c.emit(code.OpConstant, c.addConstant(integer))
}
return nil
}
func (c *Compiler) addConstant(obj object.Object) int {
c.constants = append(c.constants, obj)
return len(c.constants) - 1
}
func (c *Compiler) emit(op code.Opcode, operands ...int) int {
ins := code.Make(op, operands...)
pos := c.addInstruction(ins)
return pos
}
func (c *Compiler) Bytecode() *Bytecode {
return &Bytecode{
Instructions: c.instructions,
Constants: c.constants,
}
}
func (c *Compiler) addInstruction(ins []byte) int {
postNewInstruction := len(c.instructions)
c.instructions = append(c.instructions, ins...)
return postNewInstruction
}
type Bytecode struct {
Instructions code.Instructions
Constants []object.Object
}

120
compiler/compiler_test.go Normal file
View File

@@ -0,0 +1,120 @@
package compiler
import (
"fmt"
"monkey/ast"
"monkey/code"
"monkey/lexer"
"monkey/object"
"monkey/parser"
"testing"
)
type compilerTestCase struct {
input string
expectedConstants []interface{}
expectedInstructions []code.Instructions
}
func TestIntegerArithmetic(t *testing.T) {
tests := []compilerTestCase{
{
input: "1 + 2",
expectedConstants: []interface{}{1, 2},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
},
},
}
runCompilerTests(t, tests)
}
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
t.Helper()
for _, tt := range tests {
program := parse(tt.input)
compiler := New()
err := compiler.Compile(program)
if err != nil {
t.Fatalf("compiler error: %s", err)
}
bytecode := compiler.Bytecode()
err = testInstructions(tt.expectedInstructions, bytecode.Instructions)
if err != nil {
t.Fatalf("testInstructions failed: %s", err)
}
err = testConstants(t, tt.expectedConstants, bytecode.Constants)
if err != nil {
t.Fatalf("testConstants failed: %s", err)
}
}
}
func parse(input string) *ast.Program {
l := lexer.New(input)
p := parser.New(l)
return p.ParseProgram()
}
func testInstructions(expected []code.Instructions, actual code.Instructions) error {
concatted := concatInstructions(expected)
if len(actual) != len(concatted) {
return fmt.Errorf("wrong instructions length.\nwant=%q\ngot =%q", concatted, actual)
}
for i, ins := range concatted {
if actual[i] != ins {
return fmt.Errorf("wrong instruction at %d.\nwant=%q\ngot =%q", i, concatted, actual)
}
}
return nil
}
func concatInstructions(s []code.Instructions) code.Instructions {
out := code.Instructions{}
for _, ins := range s {
out = append(out, ins...)
}
return out
}
func testConstants(t *testing.T, expected []interface{}, actual []object.Object) error {
if len(expected) != len(actual) {
return fmt.Errorf("wrong number of constants. got=%d, want=%d", len(actual), len(expected))
}
for i, constant := range expected {
switch constant := constant.(type) {
case int:
err := testIntegerObject(int64(constant), actual[i])
if err != nil {
return fmt.Errorf("constant %d = testIntegerObject failed : %s", i, err)
}
}
}
return nil
}
func testIntegerObject(expected int64, actual object.Object) interface{} {
result, ok := actual.(*object.Integer)
if !ok {
return fmt.Errorf("object is not Integer. got=%T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%d, want=%d", result.Value, expected)
}
return nil
}