compiler_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package compiler
  2. import (
  3. "fmt"
  4. "github/runnignwater/monkey/ast"
  5. "github/runnignwater/monkey/code"
  6. "github/runnignwater/monkey/lexer"
  7. "github/runnignwater/monkey/object"
  8. "github/runnignwater/monkey/parser"
  9. "testing"
  10. )
  11. type compilerTestCase struct {
  12. input string
  13. expectedConstants []interface{}
  14. expectedInstructions []code.Instructions
  15. }
  16. func TestIntegerArithmetic(t *testing.T) {
  17. tests := []compilerTestCase{
  18. {
  19. input: "1 + 2",
  20. expectedConstants: []interface{}{1, 2},
  21. expectedInstructions: []code.Instructions{
  22. code.Make(code.OpConstant, 0),
  23. code.Make(code.OpConstant, 1),
  24. code.Make(code.OpAdd),
  25. code.Make(code.OpPop),
  26. },
  27. },
  28. {
  29. input: "1;2",
  30. expectedConstants: []interface{}{1, 2},
  31. expectedInstructions: []code.Instructions{
  32. code.Make(code.OpConstant, 0),
  33. code.Make(code.OpPop),
  34. code.Make(code.OpConstant, 1),
  35. code.Make(code.OpPop),
  36. },
  37. },
  38. }
  39. runCompilerTests(t, tests)
  40. }
  41. func runCompilerTests(t *testing.T, tests []compilerTestCase) {
  42. t.Helper()
  43. for _, tt := range tests {
  44. program := parse(tt.input)
  45. compiler := New()
  46. err := compiler.Compile(program)
  47. if err != nil {
  48. t.Fatalf("compiler error: %s", err)
  49. }
  50. bytecode := compiler.ByteCode()
  51. err = testInstructions(tt.expectedInstructions, bytecode.Instructions)
  52. if err != nil {
  53. t.Fatalf("testInstructions failed: %s", err)
  54. }
  55. err = testConstants(t, tt.expectedConstants, bytecode.Constants)
  56. if err != nil {
  57. t.Fatalf("testConstants failed: %s", err)
  58. }
  59. }
  60. }
  61. func testConstants(
  62. t *testing.T,
  63. expected []interface{},
  64. actual []object.Object,
  65. ) error {
  66. t.Helper()
  67. if len(expected) != len(actual) {
  68. return fmt.Errorf("wrong number of constants. got=%d, want=%d", len(actual), len(expected))
  69. }
  70. for i, constant := range expected {
  71. switch constant := constant.(type) {
  72. case int:
  73. err := testIntegerObject(int64(constant), actual[i])
  74. if err != nil {
  75. return fmt.Errorf("constant %d -- testIntegerObject failed: %s",
  76. i, err)
  77. }
  78. }
  79. }
  80. return nil
  81. }
  82. func testIntegerObject(expected int64, actual object.Object) error {
  83. result, ok := actual.(*object.Integer)
  84. if !ok {
  85. return fmt.Errorf("object is not Integer. got=%T (%+v)", actual, actual)
  86. }
  87. if result.Value != expected {
  88. return fmt.Errorf("object has wrong value. got=%d, want=%d",
  89. result.Value, expected)
  90. }
  91. return nil
  92. }
  93. func testInstructions(
  94. expected []code.Instructions,
  95. actual code.Instructions,
  96. ) error {
  97. concatted := concatInstructions(expected)
  98. if len(actual) != len(concatted) {
  99. return fmt.Errorf("wrong instructions length.\nwant=%q\ngot=%q", concatted, actual)
  100. }
  101. for i, ins := range concatted {
  102. if actual[i] != ins {
  103. return fmt.Errorf("wrong instructions at %d.\nwant=%q\ngot=%q", i, concatted, actual)
  104. }
  105. }
  106. return nil
  107. }
  108. func concatInstructions(s []code.Instructions) code.Instructions {
  109. out := code.Instructions{}
  110. for _, ins := range s {
  111. out = append(out, ins...)
  112. }
  113. return out
  114. }
  115. func parse(input string) *ast.Program {
  116. l := lexer.New(input)
  117. p := parser.New(l)
  118. return p.ParseProgram()
  119. }