| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- package compiler
- import (
- "fmt"
- "github/runnignwater/monkey/ast"
- "github/runnignwater/monkey/code"
- "github/runnignwater/monkey/lexer"
- "github/runnignwater/monkey/object"
- "github/runnignwater/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),
- code.Make(code.OpAdd),
- code.Make(code.OpPop),
- },
- },
- {
- input: "1;2",
- expectedConstants: []interface{}{1, 2},
- expectedInstructions: []code.Instructions{
- code.Make(code.OpConstant, 0),
- code.Make(code.OpPop),
- code.Make(code.OpConstant, 1),
- 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.OpSub),
- 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.OpMul),
- code.Make(code.OpPop),
- },
- },
- {
- input: "2 / 1",
- expectedConstants: []interface{}{2, 1},
- expectedInstructions: []code.Instructions{
- code.Make(code.OpConstant, 0),
- code.Make(code.OpConstant, 1),
- code.Make(code.OpDiv),
- code.Make(code.OpPop),
- },
- },
- {
- input: "5 * (2 + 10)",
- expectedConstants: []interface{}{5, 2, 10},
- expectedInstructions: []code.Instructions{
- code.Make(code.OpConstant, 0),
- code.Make(code.OpConstant, 1),
- code.Make(code.OpConstant, 2),
- code.Make(code.OpAdd),
- code.Make(code.OpMul),
- code.Make(code.OpPop),
- },
- },
- {
- input: "-1",
- expectedConstants: []interface{}{1},
- expectedInstructions: []code.Instructions{
- code.Make(code.OpConstant, 0),
- code.Make(code.OpMinus),
- code.Make(code.OpPop),
- },
- },
- }
- runCompilerTests(t, tests)
- }
- func TestBooleanExpression(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),
- },
- },
- {
- input: "!true",
- expectedConstants: []interface{}{},
- expectedInstructions: []code.Instructions{
- code.Make(code.OpTrue),
- code.Make(code.OpBang),
- code.Make(code.OpPop),
- },
- },
- }
- 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 testConstants(
- t *testing.T,
- expected []interface{},
- actual []object.Object,
- ) error {
- t.Helper()
- 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) error {
- 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
- }
- 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\n got=%q", concatted, actual)
- }
- for i, ins := range concatted {
- if actual[i] != ins {
- return fmt.Errorf("wrong instructions at %d.\nwant=%q\n got=%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 parse(input string) *ast.Program {
- l := lexer.New(input)
- p := parser.New(l)
- return p.ParseProgram()
- }
|