compiler_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 TestConditionals(t *testing.T) {
  183. tests := []compilerTestCase{
  184. {
  185. input: "if (true) {10} ; 3333;",
  186. expectedConstants: []interface{}{10, 3333},
  187. expectedInstructions: []code.Instructions{
  188. // 0000
  189. code.Make(code.OpTrue),
  190. // 0001
  191. code.Make(code.OpJumpNotTruthy, 10),
  192. // 0004
  193. code.Make(code.OpConstant, 0),
  194. // 0007
  195. code.Make(code.OpJump, 11),
  196. // 0010
  197. code.Make(code.OpNull),
  198. // 0011
  199. code.Make(code.OpPop),
  200. // 0012
  201. code.Make(code.OpConstant, 1),
  202. // 0015
  203. code.Make(code.OpPop),
  204. },
  205. },
  206. {
  207. input: "if(true) {10} else {20}; 3333",
  208. expectedConstants: []interface{}{10, 20, 3333},
  209. expectedInstructions: []code.Instructions{
  210. // 0000
  211. code.Make(code.OpTrue),
  212. // 0001
  213. code.Make(code.OpJumpNotTruthy, 10),
  214. // 0004
  215. code.Make(code.OpConstant, 0),
  216. // 0007
  217. code.Make(code.OpJump, 13),
  218. // 0010
  219. code.Make(code.OpConstant, 1),
  220. // 0013
  221. code.Make(code.OpPop),
  222. // 0014
  223. code.Make(code.OpConstant, 2),
  224. // 0017
  225. code.Make(code.OpPop),
  226. },
  227. },
  228. }
  229. runCompilerTests(t, tests)
  230. }
  231. func TestGlobalLetStatements(t *testing.T) {
  232. tests := []compilerTestCase{
  233. {
  234. input: `let one = 1; let two = 2;`,
  235. expectedConstants: []interface{}{1, 2},
  236. expectedInstructions: []code.Instructions{
  237. code.Make(code.OpConstant, 0),
  238. code.Make(code.OpSetGlobal, 0),
  239. code.Make(code.OpConstant, 1),
  240. code.Make(code.OpSetGlobal, 1),
  241. },
  242. },
  243. {
  244. input: `let one = 1; one;`,
  245. expectedConstants: []interface{}{1},
  246. expectedInstructions: []code.Instructions{
  247. code.Make(code.OpConstant, 0),
  248. code.Make(code.OpSetGlobal, 0),
  249. code.Make(code.OpGetGlobal, 0),
  250. code.Make(code.OpPop),
  251. },
  252. },
  253. }
  254. runCompilerTests(t, tests)
  255. }
  256. func TestStringExpression(t *testing.T) {
  257. tests := []compilerTestCase{
  258. {
  259. input: `"monkey"`,
  260. expectedConstants: []interface{}{"monkey"},
  261. expectedInstructions: []code.Instructions{
  262. code.Make(code.OpConstant, 0),
  263. code.Make(code.OpPop),
  264. },
  265. },
  266. {
  267. input: `"monkey" + "language"`,
  268. expectedConstants: []interface{}{"monkey", "language"},
  269. expectedInstructions: []code.Instructions{
  270. code.Make(code.OpConstant, 0),
  271. code.Make(code.OpConstant, 1),
  272. code.Make(code.OpAdd),
  273. code.Make(code.OpPop),
  274. },
  275. },
  276. }
  277. runCompilerTests(t, tests)
  278. }
  279. func TestArrayLiteral(t *testing.T) {
  280. tests := []compilerTestCase{
  281. {
  282. input: "[]",
  283. expectedConstants: []interface{}{},
  284. expectedInstructions: []code.Instructions{
  285. code.Make(code.OpArray, 0),
  286. code.Make(code.OpPop),
  287. },
  288. },
  289. {
  290. input: "[1, 2, 3]",
  291. expectedConstants: []interface{}{1, 2, 3},
  292. expectedInstructions: []code.Instructions{
  293. code.Make(code.OpConstant, 0),
  294. code.Make(code.OpConstant, 1),
  295. code.Make(code.OpConstant, 2),
  296. code.Make(code.OpArray, 3),
  297. code.Make(code.OpPop),
  298. },
  299. },
  300. {
  301. input: "[1+2, 3 - 4, 5 *6]",
  302. expectedConstants: []interface{}{1, 2, 3, 4, 5, 6},
  303. expectedInstructions: []code.Instructions{
  304. code.Make(code.OpConstant, 0),
  305. code.Make(code.OpConstant, 1),
  306. code.Make(code.OpAdd),
  307. code.Make(code.OpConstant, 2),
  308. code.Make(code.OpConstant, 3),
  309. code.Make(code.OpSub),
  310. code.Make(code.OpConstant, 4),
  311. code.Make(code.OpConstant, 5),
  312. code.Make(code.OpMul),
  313. code.Make(code.OpArray, 3),
  314. code.Make(code.OpPop),
  315. },
  316. },
  317. }
  318. runCompilerTests(t, tests)
  319. }
  320. func runCompilerTests(t *testing.T, tests []compilerTestCase) {
  321. t.Helper()
  322. for _, tt := range tests {
  323. program := parse(tt.input)
  324. compiler := New()
  325. err := compiler.Compile(program)
  326. if err != nil {
  327. t.Fatalf("compiler error: %s", err)
  328. }
  329. bytecode := compiler.ByteCode()
  330. err = testInstructions(tt.expectedInstructions, bytecode.Instructions)
  331. if err != nil {
  332. t.Fatalf("testInstructions failed: %s", err)
  333. }
  334. err = testConstants(t, tt.expectedConstants, bytecode.Constants)
  335. if err != nil {
  336. t.Fatalf("testConstants failed: %s", err)
  337. }
  338. }
  339. }
  340. func testConstants(
  341. t *testing.T,
  342. expected []interface{},
  343. actual []object.Object,
  344. ) error {
  345. t.Helper()
  346. if len(expected) != len(actual) {
  347. return fmt.Errorf("wrong number of constants. got=%d, want=%d", len(actual), len(expected))
  348. }
  349. for i, constant := range expected {
  350. switch constant := constant.(type) {
  351. case int:
  352. err := testIntegerObject(int64(constant), actual[i])
  353. if err != nil {
  354. return fmt.Errorf("constant %d -- testIntegerObject failed: %s",
  355. i, err)
  356. }
  357. case string:
  358. err := testStringObject(constant, actual[i])
  359. if err != nil {
  360. return fmt.Errorf("constant %d - testStringObject failed: %s", i, err)
  361. }
  362. }
  363. }
  364. return nil
  365. }
  366. func testStringObject(expected string, actual object.Object) error {
  367. result, ok := actual.(*object.String)
  368. if !ok {
  369. return fmt.Errorf("object is not String. got=%T (%+v)", actual, actual)
  370. }
  371. if result.Value != expected {
  372. return fmt.Errorf("object has wrong value. got=%s, want=%q", result.Value, expected)
  373. }
  374. return nil
  375. }
  376. func testIntegerObject(expected int64, actual object.Object) error {
  377. result, ok := actual.(*object.Integer)
  378. if !ok {
  379. return fmt.Errorf("object is not Integer. got=%T (%+v)", actual, actual)
  380. }
  381. if result.Value != expected {
  382. return fmt.Errorf("object has wrong value. got=%d, want=%d",
  383. result.Value, expected)
  384. }
  385. return nil
  386. }
  387. func testInstructions(
  388. expected []code.Instructions,
  389. actual code.Instructions,
  390. ) error {
  391. concatted := concatInstructions(expected)
  392. if len(actual) != len(concatted) {
  393. return fmt.Errorf("wrong instructions length.\nwant=%q\n got=%q", concatted, actual)
  394. }
  395. for i, ins := range concatted {
  396. if actual[i] != ins {
  397. return fmt.Errorf("wrong instructions at %d.\nwant=%q\n got=%q", i, concatted, actual)
  398. }
  399. }
  400. return nil
  401. }
  402. func concatInstructions(s []code.Instructions) code.Instructions {
  403. out := code.Instructions{}
  404. for _, ins := range s {
  405. out = append(out, ins...)
  406. }
  407. return out
  408. }
  409. func parse(input string) *ast.Program {
  410. l := lexer.New(input)
  411. p := parser.New(l)
  412. return p.ParseProgram()
  413. }