compiler_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. input: "1 - 2",
  40. expectedConstants: []interface{}{1, 2},
  41. expectedInstructions: []code.Instructions{
  42. code.Make(code.OpConstant, 0),
  43. code.Make(code.OpConstant, 1),
  44. code.Make(code.OpSub),
  45. code.Make(code.OpPop),
  46. },
  47. },
  48. {
  49. input: "1 * 2",
  50. expectedConstants: []interface{}{1, 2},
  51. expectedInstructions: []code.Instructions{
  52. code.Make(code.OpConstant, 0),
  53. code.Make(code.OpConstant, 1),
  54. code.Make(code.OpMul),
  55. code.Make(code.OpPop),
  56. },
  57. },
  58. {
  59. input: "2 / 1",
  60. expectedConstants: []interface{}{2, 1},
  61. expectedInstructions: []code.Instructions{
  62. code.Make(code.OpConstant, 0),
  63. code.Make(code.OpConstant, 1),
  64. code.Make(code.OpDiv),
  65. code.Make(code.OpPop),
  66. },
  67. },
  68. {
  69. input: "5 * (2 + 10)",
  70. expectedConstants: []interface{}{5, 2, 10},
  71. expectedInstructions: []code.Instructions{
  72. code.Make(code.OpConstant, 0),
  73. code.Make(code.OpConstant, 1),
  74. code.Make(code.OpConstant, 2),
  75. code.Make(code.OpAdd),
  76. code.Make(code.OpMul),
  77. code.Make(code.OpPop),
  78. },
  79. },
  80. {
  81. input: "-1",
  82. expectedConstants: []interface{}{1},
  83. expectedInstructions: []code.Instructions{
  84. code.Make(code.OpConstant, 0),
  85. code.Make(code.OpMinus),
  86. code.Make(code.OpPop),
  87. },
  88. },
  89. }
  90. runCompilerTests(t, tests)
  91. }
  92. func TestBooleanExpression(t *testing.T) {
  93. tests := []compilerTestCase{
  94. {
  95. input: "true",
  96. expectedConstants: []interface{}{},
  97. expectedInstructions: []code.Instructions{
  98. code.Make(code.OpTrue),
  99. code.Make(code.OpPop),
  100. },
  101. },
  102. {
  103. input: "false",
  104. expectedConstants: []interface{}{},
  105. expectedInstructions: []code.Instructions{
  106. code.Make(code.OpFalse),
  107. code.Make(code.OpPop),
  108. },
  109. },
  110. {
  111. input: "1 > 2",
  112. expectedConstants: []interface{}{1, 2},
  113. expectedInstructions: []code.Instructions{
  114. code.Make(code.OpConstant, 0),
  115. code.Make(code.OpConstant, 1),
  116. code.Make(code.OpGreaterThan),
  117. code.Make(code.OpPop),
  118. },
  119. },
  120. {
  121. input: "1 < 2",
  122. expectedConstants: []interface{}{2, 1},
  123. expectedInstructions: []code.Instructions{
  124. code.Make(code.OpConstant, 0),
  125. code.Make(code.OpConstant, 1),
  126. code.Make(code.OpGreaterThan),
  127. code.Make(code.OpPop),
  128. },
  129. },
  130. {
  131. input: "1 == 2",
  132. expectedConstants: []interface{}{1, 2},
  133. expectedInstructions: []code.Instructions{
  134. code.Make(code.OpConstant, 0),
  135. code.Make(code.OpConstant, 1),
  136. code.Make(code.OpEqual),
  137. code.Make(code.OpPop),
  138. },
  139. },
  140. {
  141. input: "1 != 2",
  142. expectedConstants: []interface{}{1, 2},
  143. expectedInstructions: []code.Instructions{
  144. code.Make(code.OpConstant, 0),
  145. code.Make(code.OpConstant, 1),
  146. code.Make(code.OpNotEqual),
  147. code.Make(code.OpPop),
  148. },
  149. },
  150. {
  151. input: "true == false",
  152. expectedConstants: []interface{}{},
  153. expectedInstructions: []code.Instructions{
  154. code.Make(code.OpTrue),
  155. code.Make(code.OpFalse),
  156. code.Make(code.OpEqual),
  157. code.Make(code.OpPop),
  158. },
  159. },
  160. {
  161. input: "true != false",
  162. expectedConstants: []interface{}{},
  163. expectedInstructions: []code.Instructions{
  164. code.Make(code.OpTrue),
  165. code.Make(code.OpFalse),
  166. code.Make(code.OpNotEqual),
  167. code.Make(code.OpPop),
  168. },
  169. },
  170. {
  171. input: "!true",
  172. expectedConstants: []interface{}{},
  173. expectedInstructions: []code.Instructions{
  174. code.Make(code.OpTrue),
  175. code.Make(code.OpBang),
  176. code.Make(code.OpPop),
  177. },
  178. },
  179. }
  180. runCompilerTests(t, tests)
  181. }
  182. func runCompilerTests(t *testing.T, tests []compilerTestCase) {
  183. t.Helper()
  184. for _, tt := range tests {
  185. program := parse(tt.input)
  186. compiler := New()
  187. err := compiler.Compile(program)
  188. if err != nil {
  189. t.Fatalf("compiler error: %s", err)
  190. }
  191. bytecode := compiler.ByteCode()
  192. err = testInstructions(tt.expectedInstructions, bytecode.Instructions)
  193. if err != nil {
  194. t.Fatalf("testInstructions failed: %s", err)
  195. }
  196. err = testConstants(t, tt.expectedConstants, bytecode.Constants)
  197. if err != nil {
  198. t.Fatalf("testConstants failed: %s", err)
  199. }
  200. }
  201. }
  202. func testConstants(
  203. t *testing.T,
  204. expected []interface{},
  205. actual []object.Object,
  206. ) error {
  207. t.Helper()
  208. if len(expected) != len(actual) {
  209. return fmt.Errorf("wrong number of constants. got=%d, want=%d", len(actual), len(expected))
  210. }
  211. for i, constant := range expected {
  212. switch constant := constant.(type) {
  213. case int:
  214. err := testIntegerObject(int64(constant), actual[i])
  215. if err != nil {
  216. return fmt.Errorf("constant %d -- testIntegerObject failed: %s",
  217. i, err)
  218. }
  219. }
  220. }
  221. return nil
  222. }
  223. func testIntegerObject(expected int64, actual object.Object) error {
  224. result, ok := actual.(*object.Integer)
  225. if !ok {
  226. return fmt.Errorf("object is not Integer. got=%T (%+v)", actual, actual)
  227. }
  228. if result.Value != expected {
  229. return fmt.Errorf("object has wrong value. got=%d, want=%d",
  230. result.Value, expected)
  231. }
  232. return nil
  233. }
  234. func testInstructions(
  235. expected []code.Instructions,
  236. actual code.Instructions,
  237. ) error {
  238. concatted := concatInstructions(expected)
  239. if len(actual) != len(concatted) {
  240. return fmt.Errorf("wrong instructions length.\nwant=%q\n got=%q", concatted, actual)
  241. }
  242. for i, ins := range concatted {
  243. if actual[i] != ins {
  244. return fmt.Errorf("wrong instructions at %d.\nwant=%q\n got=%q", i, concatted, actual)
  245. }
  246. }
  247. return nil
  248. }
  249. func concatInstructions(s []code.Instructions) code.Instructions {
  250. out := code.Instructions{}
  251. for _, ins := range s {
  252. out = append(out, ins...)
  253. }
  254. return out
  255. }
  256. func parse(input string) *ast.Program {
  257. l := lexer.New(input)
  258. p := parser.New(l)
  259. return p.ParseProgram()
  260. }