compiler_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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 TestHashLiterals(t *testing.T) {
  321. tests := []compilerTestCase{
  322. {
  323. input: "{}",
  324. expectedConstants: []interface{}{},
  325. expectedInstructions: []code.Instructions{
  326. code.Make(code.OpHash, 0),
  327. code.Make(code.OpPop),
  328. },
  329. },
  330. {
  331. input: "{1:2, 3:4, 5:6}",
  332. expectedConstants: []interface{}{1, 2, 3, 4, 5, 6},
  333. expectedInstructions: []code.Instructions{
  334. code.Make(code.OpConstant, 0),
  335. code.Make(code.OpConstant, 1),
  336. code.Make(code.OpConstant, 2),
  337. code.Make(code.OpConstant, 3),
  338. code.Make(code.OpConstant, 4),
  339. code.Make(code.OpConstant, 5),
  340. code.Make(code.OpHash, 6),
  341. code.Make(code.OpPop),
  342. },
  343. },
  344. {
  345. input: "{1:2+3, 4:5*6}",
  346. expectedConstants: []interface{}{1, 2, 3, 4, 5, 6},
  347. expectedInstructions: []code.Instructions{
  348. code.Make(code.OpConstant, 0),
  349. code.Make(code.OpConstant, 1),
  350. code.Make(code.OpConstant, 2),
  351. code.Make(code.OpAdd),
  352. code.Make(code.OpConstant, 3),
  353. code.Make(code.OpConstant, 4),
  354. code.Make(code.OpConstant, 5),
  355. code.Make(code.OpMul),
  356. code.Make(code.OpHash, 4),
  357. code.Make(code.OpPop),
  358. },
  359. },
  360. }
  361. runCompilerTests(t, tests)
  362. }
  363. func TestIndexExpressions(t *testing.T) {
  364. tests := []compilerTestCase{
  365. {
  366. input: "[1,2,3][1+1]",
  367. expectedConstants: []interface{}{1, 2, 3, 1, 1},
  368. expectedInstructions: []code.Instructions{
  369. code.Make(code.OpConstant, 0),
  370. code.Make(code.OpConstant, 1),
  371. code.Make(code.OpConstant, 2),
  372. code.Make(code.OpArray, 3),
  373. code.Make(code.OpConstant, 3),
  374. code.Make(code.OpConstant, 4),
  375. code.Make(code.OpAdd),
  376. code.Make(code.OpIndex),
  377. code.Make(code.OpPop),
  378. },
  379. },
  380. {
  381. input: "{1:2}[2-1]",
  382. expectedConstants: []interface{}{1, 2, 2, 1},
  383. expectedInstructions: []code.Instructions{
  384. code.Make(code.OpConstant, 0),
  385. code.Make(code.OpConstant, 1),
  386. code.Make(code.OpHash, 2),
  387. code.Make(code.OpConstant, 2),
  388. code.Make(code.OpConstant, 3),
  389. code.Make(code.OpSub),
  390. code.Make(code.OpIndex),
  391. code.Make(code.OpPop),
  392. },
  393. },
  394. }
  395. runCompilerTests(t, tests)
  396. }
  397. func TestFunctions(t *testing.T) {
  398. tests := []compilerTestCase{
  399. {
  400. input: `fn() {return 5 + 10; }`,
  401. expectedConstants: []interface{}{
  402. 5,
  403. 10,
  404. []code.Instructions{
  405. code.Make(code.OpConstant, 0),
  406. code.Make(code.OpConstant, 1),
  407. code.Make(code.OpAdd),
  408. code.Make(code.OpReturnValue),
  409. },
  410. },
  411. expectedInstructions: []code.Instructions{
  412. code.Make(code.OpConstant, 2),
  413. code.Make(code.OpPop),
  414. },
  415. },
  416. {
  417. input: `fn(){5+10}`,
  418. expectedConstants: []interface{}{
  419. 5, 10,
  420. []code.Instructions{
  421. code.Make(code.OpConstant, 0),
  422. code.Make(code.OpConstant, 1),
  423. code.Make(code.OpAdd),
  424. code.Make(code.OpReturnValue),
  425. },
  426. },
  427. expectedInstructions: []code.Instructions{
  428. code.Make(code.OpConstant, 2),
  429. code.Make(code.OpPop),
  430. },
  431. },
  432. {
  433. input: `fn(){1;2}`,
  434. expectedConstants: []interface{}{
  435. 1, 2,
  436. []code.Instructions{
  437. code.Make(code.OpConstant, 0),
  438. code.Make(code.OpPop),
  439. code.Make(code.OpConstant, 1),
  440. code.Make(code.OpReturnValue),
  441. },
  442. },
  443. expectedInstructions: []code.Instructions{
  444. code.Make(code.OpConstant, 2),
  445. code.Make(code.OpPop),
  446. },
  447. },
  448. }
  449. runCompilerTests(t, tests)
  450. }
  451. func TestCompilerScopes(t *testing.T) {
  452. compiler := New()
  453. if compiler.scopeIndex != 0 {
  454. t.Errorf("scopeIndex wrong. got=%d, want=%d", compiler.scopeIndex, 0)
  455. }
  456. compiler.emit(code.OpMul)
  457. compiler.enterScope()
  458. if compiler.scopeIndex != 1 {
  459. t.Errorf("scopeIndex wrong. got=%d, want=%d", compiler.scopeIndex, 0)
  460. }
  461. compiler.emit(code.OpSub)
  462. if len(compiler.scopes[compiler.scopeIndex].instructions) != 1 {
  463. t.Errorf("instructions length wrong. got=%d, want = 1",
  464. len(compiler.scopes[compiler.scopeIndex].instructions))
  465. }
  466. last := compiler.scopes[compiler.scopeIndex].lastInstruction
  467. if last.Opcode != code.OpSub {
  468. t.Errorf("lastInstruction.Opcde wrong. got=%d, want=%d", last.Opcode, code.OpSub)
  469. }
  470. compiler.leaveScope()
  471. if compiler.scopeIndex != 0 {
  472. t.Errorf("scopeIndex wrong. got=%d, want=%d", compiler.scopeIndex, 0)
  473. }
  474. compiler.emit(code.OpAdd)
  475. if len(compiler.scopes[compiler.scopeIndex].instructions) != 2 {
  476. t.Errorf("instructions length wrong. got=%d",
  477. len(compiler.scopes[compiler.scopeIndex].instructions))
  478. }
  479. last = compiler.scopes[compiler.scopeIndex].lastInstruction
  480. if last.Opcode != code.OpAdd {
  481. t.Errorf("lastInstruction.Opcode wrong. got=%d, want=%d", last.Opcode, code.OpAdd)
  482. }
  483. previous := compiler.scopes[compiler.scopeIndex].previousInstruction
  484. if previous.Opcode != code.OpMul {
  485. t.Errorf("previousInstruction.Opcode wrong. got=%d, want=%d", previous.Opcode, code.OpMul)
  486. }
  487. }
  488. func TestFunctionWithoutReturnValue(t *testing.T) {
  489. tests := []compilerTestCase{
  490. {input: `fn() {}`,
  491. expectedConstants: []interface{}{
  492. []code.Instructions{code.Make(code.OpReturn)},
  493. },
  494. expectedInstructions: []code.Instructions{
  495. code.Make(code.OpConstant, 0),
  496. code.Make(code.OpPop),
  497. },
  498. },
  499. }
  500. runCompilerTests(t, tests)
  501. }
  502. func TestFunctionCalls(t *testing.T) {
  503. tests := []compilerTestCase{
  504. {
  505. input: `fn () { 24 } ()`,
  506. expectedConstants: []interface{}{
  507. 24,
  508. []code.Instructions{
  509. code.Make(code.OpConstant, 0), // The literal "24"
  510. code.Make(code.OpReturnValue),
  511. },
  512. },
  513. expectedInstructions: []code.Instructions{
  514. code.Make(code.OpConstant, 1), // The compiled function
  515. code.Make(code.OpCall),
  516. code.Make(code.OpPop),
  517. },
  518. },
  519. {
  520. input: `let noArg = fn(){24};noArg();`,
  521. expectedConstants: []interface{}{
  522. 24,
  523. []code.Instructions{
  524. code.Make(code.OpConstant, 0),
  525. code.Make(code.OpReturnValue)},
  526. },
  527. expectedInstructions: []code.Instructions{
  528. code.Make(code.OpConstant, 1), // The compiled function
  529. code.Make(code.OpSetGlobal, 0),
  530. code.Make(code.OpGetGlobal, 0),
  531. code.Make(code.OpCall),
  532. code.Make(code.OpPop),
  533. },
  534. },
  535. }
  536. runCompilerTests(t, tests)
  537. }
  538. func runCompilerTests(t *testing.T, tests []compilerTestCase) {
  539. t.Helper()
  540. for _, tt := range tests {
  541. program := parse(tt.input)
  542. compiler := New()
  543. err := compiler.Compile(program)
  544. if err != nil {
  545. t.Fatalf("compiler error: %s", err)
  546. }
  547. bytecode := compiler.ByteCode()
  548. err = testInstructions(tt.expectedInstructions, bytecode.Instructions)
  549. if err != nil {
  550. t.Fatalf("testInstructions failed: %s", err)
  551. }
  552. err = testConstants(t, tt.expectedConstants, bytecode.Constants)
  553. if err != nil {
  554. t.Fatalf("testConstants failed: %s", err)
  555. }
  556. }
  557. }
  558. func testConstants(
  559. t *testing.T,
  560. expected []interface{},
  561. actual []object.Object,
  562. ) error {
  563. t.Helper()
  564. if len(expected) != len(actual) {
  565. return fmt.Errorf("wrong number of constants. got=%d, want=%d", len(actual), len(expected))
  566. }
  567. for i, constant := range expected {
  568. switch constant := constant.(type) {
  569. case int:
  570. err := testIntegerObject(int64(constant), actual[i])
  571. if err != nil {
  572. return fmt.Errorf("constant %d -- testIntegerObject failed: %s",
  573. i, err)
  574. }
  575. case string:
  576. err := testStringObject(constant, actual[i])
  577. if err != nil {
  578. return fmt.Errorf("constant %d - testStringObject failed: %s", i, err)
  579. }
  580. case []code.Instructions:
  581. fn, ok := actual[i].(*object.CompileFunction)
  582. if !ok {
  583. return fmt.Errorf("constant %d -- not a function: %T", i, actual[i])
  584. }
  585. err := testInstructions(constant, fn.Instructions)
  586. if err != nil {
  587. return fmt.Errorf("constant %d -- testInstructions failed: %s", i, err)
  588. }
  589. }
  590. }
  591. return nil
  592. }
  593. func testStringObject(expected string, actual object.Object) error {
  594. result, ok := actual.(*object.String)
  595. if !ok {
  596. return fmt.Errorf("object is not String. got=%T (%+v)", actual, actual)
  597. }
  598. if result.Value != expected {
  599. return fmt.Errorf("object has wrong value. got=%s, want=%q", result.Value, expected)
  600. }
  601. return nil
  602. }
  603. func testIntegerObject(expected int64, actual object.Object) error {
  604. result, ok := actual.(*object.Integer)
  605. if !ok {
  606. return fmt.Errorf("object is not Integer. got=%T (%+v)", actual, actual)
  607. }
  608. if result.Value != expected {
  609. return fmt.Errorf("object has wrong value. got=%d, want=%d",
  610. result.Value, expected)
  611. }
  612. return nil
  613. }
  614. func testInstructions(
  615. expected []code.Instructions,
  616. actual code.Instructions,
  617. ) error {
  618. concatted := concatInstructions(expected)
  619. if len(actual) != len(concatted) {
  620. return fmt.Errorf("wrong instructions length.\nwant=%q\n got=%q", concatted, actual)
  621. }
  622. for i, ins := range concatted {
  623. if actual[i] != ins {
  624. return fmt.Errorf("wrong instructions at %d.\nwant=%q\n got=%q", i, concatted, actual)
  625. }
  626. }
  627. return nil
  628. }
  629. func concatInstructions(s []code.Instructions) code.Instructions {
  630. out := code.Instructions{}
  631. for _, ins := range s {
  632. out = append(out, ins...)
  633. }
  634. return out
  635. }
  636. func parse(input string) *ast.Program {
  637. l := lexer.New(input)
  638. p := parser.New(l)
  639. return p.ParseProgram()
  640. }