compiler_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. },
  25. },
  26. }
  27. runCompilerTests(t, tests)
  28. }
  29. func runCompilerTests(t *testing.T, tests []compilerTestCase) {
  30. t.Helper()
  31. for _, tt := range tests {
  32. program := parse(tt.input)
  33. compiler := New()
  34. err := compiler.Compile(program)
  35. if err != nil {
  36. t.Fatalf("compiler error: %s", err)
  37. }
  38. bytecode := compiler.ByteCode()
  39. err = testInstructions(tt.expectedInstructions, bytecode.Instructions)
  40. if err != nil {
  41. t.Fatalf("testInstructions failed: %s", err)
  42. }
  43. err = testConstants(t, tt.expectedConstants, bytecode.Constants)
  44. if err != nil {
  45. t.Fatalf("testConstants failed: %s", err)
  46. }
  47. }
  48. }
  49. func testConstants(
  50. t *testing.T,
  51. expected []interface{},
  52. actual []object.Object,
  53. ) error {
  54. if len(expected) != len(actual) {
  55. return fmt.Errorf("wrong number of constants. got=%d, want=%d", len(actual), len(expected))
  56. }
  57. for i, constant := range expected {
  58. switch constant := constant.(type) {
  59. case int:
  60. err := testIntegerObject(int64(constant), actual[i])
  61. if err != nil {
  62. return fmt.Errorf("constant %d -- testIntegerObject failed: %s",
  63. i, err)
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. func testIntegerObject(expected int64, actual object.Object) error {
  70. result, ok := actual.(*object.Integer)
  71. if !ok {
  72. return fmt.Errorf("object is not Integer. got=%T (%+v)", actual, actual)
  73. }
  74. if result.Value != expected {
  75. return fmt.Errorf("object has wrong value. got=%d, want=%d",
  76. result.Value, expected)
  77. }
  78. return nil
  79. }
  80. func testInstructions(
  81. expected []code.Instructions,
  82. actual code.Instructions,
  83. ) error {
  84. concatted := concatInstructions(expected)
  85. if len(actual) != len(concatted) {
  86. return fmt.Errorf("wrong instructions length.\nwant=%q\ngot=%q", concatted, actual)
  87. }
  88. for i, ins := range concatted {
  89. if actual[i] != ins {
  90. return fmt.Errorf("wrong instructions at %d.\nwant=%q\ngot=%q", i, concatted, actual)
  91. }
  92. }
  93. return nil
  94. }
  95. func concatInstructions(s []code.Instructions) code.Instructions {
  96. out := code.Instructions{}
  97. for _, ins := range s {
  98. out = append(out, ins...)
  99. }
  100. return out
  101. }
  102. func parse(input string) *ast.Program {
  103. l := lexer.New(input)
  104. p := parser.New(l)
  105. return p.ParseProgram()
  106. }