parser_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. package parser
  2. import (
  3. "fmt"
  4. "github/runnignwater/monkey/ast"
  5. "github/runnignwater/monkey/lexer"
  6. "log"
  7. "testing"
  8. )
  9. /**
  10. * @Author: simon
  11. * @Author: ynwdlxm@163.com
  12. * @Date: 2022/10/2 下午10:40
  13. * @Desc: LetStatement test case
  14. */
  15. func TestLetStatements(t *testing.T) {
  16. input := `
  17. let x = 5;
  18. let y = 10;
  19. let foo = 838383;
  20. `
  21. l := lexer.New(input)
  22. p := New(l)
  23. program := p.ParseProgram()
  24. checkParseErrors(t, p)
  25. if program == nil {
  26. t.Fatalf("ParseProgram() return nil")
  27. }
  28. if len(program.Statements) != 3 {
  29. t.Fatalf("Program.Statements does not contain 3 statements. got=%d", len(program.Statements))
  30. }
  31. tests := []struct {
  32. expectedIdentifies string
  33. }{
  34. {"x"},
  35. {"y"},
  36. {"foo"},
  37. }
  38. for i, tt := range tests {
  39. stmt := program.Statements[i]
  40. if !testLetStatement(t, stmt, tt.expectedIdentifies) {
  41. return
  42. }
  43. }
  44. }
  45. func TestReturnStatements(t *testing.T) {
  46. input := `
  47. return 5;
  48. return 10;
  49. return add(a,10);
  50. `
  51. l := lexer.New(input)
  52. p := New(l)
  53. program := p.ParseProgram()
  54. checkParseErrors(t, p)
  55. if len(program.Statements) != 3 {
  56. t.Fatalf("Program.Statements does not contain 3 statements. got=%d", len(program.Statements))
  57. }
  58. for _, stmt := range program.Statements {
  59. returnStmt, ok := stmt.(*ast.ReturnStatement)
  60. if !ok {
  61. t.Errorf("stmt not *ast.ReturnStatement. got=%T", stmt)
  62. continue
  63. }
  64. if returnStmt.TokenLiteral() != "return" {
  65. t.Errorf("returnStmt.TokenLiteral not 'return', got %q", returnStmt.TokenLiteral())
  66. }
  67. }
  68. }
  69. func checkParseErrors(t *testing.T, p *Parser) {
  70. errors := p.errors
  71. if len(errors) == 0 {
  72. return
  73. }
  74. t.Errorf("parse has %d errors.", len(errors))
  75. for _, msg := range errors {
  76. t.Errorf("parse error: %q", msg)
  77. }
  78. t.FailNow()
  79. }
  80. func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
  81. if s.TokenLiteral() != "let" {
  82. t.Errorf("s.TokenLiteral() not 'let'. got =%q", s.TokenLiteral())
  83. return false
  84. }
  85. letStmt, ok := s.(*ast.LetStatement)
  86. if !ok {
  87. t.Errorf("s is not *ast.LetStatement. got=%T", s)
  88. return false
  89. }
  90. if letStmt.Name.Value != name {
  91. t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
  92. return false
  93. }
  94. if letStmt.Name.TokenLiteral() != name {
  95. t.Errorf("s.name not '%s. got=%s", name, letStmt.Name)
  96. return false
  97. }
  98. return true
  99. }
  100. func TestIdentifier(t *testing.T) {
  101. input := "foobar;"
  102. l := lexer.New(input)
  103. p := New(l)
  104. program := p.ParseProgram()
  105. checkParseErrors(t, p)
  106. if len(program.Statements) != 1 {
  107. t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
  108. }
  109. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  110. if !ok {
  111. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  112. program.Statements[0])
  113. }
  114. ident, ok := stmt.Expression.(*ast.Identifier)
  115. if !ok {
  116. t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression)
  117. }
  118. if ident.Value != "foobar" {
  119. t.Fatalf("ident.Value not %s. got=%s", "foobar", ident.Value)
  120. }
  121. if ident.TokenLiteral() != "foobar" {
  122. t.Errorf("ident.TokenLiteral() not %s. got=%s", "foobar", ident.TokenLiteral())
  123. }
  124. }
  125. func TestIntegerLiteralExpression(t *testing.T) {
  126. input := "5;"
  127. l := lexer.New(input)
  128. p := New(l)
  129. program := p.ParseProgram()
  130. checkParseErrors(t, p)
  131. if len(program.Statements) != 1 {
  132. t.Fatalf("program has not enough statements. got=%d",
  133. len(program.Statements))
  134. }
  135. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  136. if !ok {
  137. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  138. program.Statements[0])
  139. }
  140. literal, ok := stmt.Expression.(*ast.IntegerLiteral)
  141. if !ok {
  142. t.Fatalf("exp not *ast.IntegerLiteral. got=%T", stmt.Expression)
  143. }
  144. if literal.Value != 5 {
  145. t.Errorf("literal.Value not %d. got=%d", 5, literal.Value)
  146. }
  147. if literal.TokenLiteral() != "5" {
  148. t.Errorf("literal.TokenLiteral not %s. got=%s", "5",
  149. literal.TokenLiteral())
  150. }
  151. }
  152. /**
  153. * Prefix Operators or "prefix expressions"
  154. * <prefix operator><expression>;
  155. * -5;
  156. * !foobar;
  157. * 5 + -10;
  158. */
  159. func TestParsingPrefixExpressions(t *testing.T) {
  160. prefixTests := []struct {
  161. input string
  162. operator string
  163. integerValue int64
  164. }{
  165. {"!5;", "!", 5},
  166. {"-15;", "-", 15},
  167. }
  168. for _, tt := range prefixTests {
  169. l := lexer.New(tt.input)
  170. p := New(l)
  171. program := p.ParseProgram()
  172. checkParseErrors(t, p)
  173. if len(program.Statements) != 1 {
  174. t.Fatalf("program.Statement does not contain %d statements. got=%d\n",
  175. 1, len(program.Statements))
  176. }
  177. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  178. if !ok {
  179. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  180. program.Statements[0])
  181. }
  182. exp, ok := stmt.Expression.(*ast.PrefixExpression)
  183. if !ok {
  184. log.Fatalf("stmt is not ast.PrefixExpression. got=%T", stmt.Expression)
  185. }
  186. if exp.Operator != tt.operator {
  187. t.Fatalf("exp.Operator is not '%s'. got=%s",
  188. tt.operator, exp.Operator)
  189. }
  190. if !testIntegerLiteral(t, exp.Right, tt.integerValue) {
  191. return
  192. }
  193. }
  194. }
  195. func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
  196. integ, ok := il.(*ast.IntegerLiteral)
  197. if !ok {
  198. t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
  199. return false
  200. }
  201. if integ.Value != value {
  202. t.Errorf("integ.Value not %d. get=%d", value, integ.Value)
  203. return false
  204. }
  205. if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
  206. t.Errorf("integ.TokenLiteral not %d. got=%s", value, integ.TokenLiteral())
  207. return false
  208. }
  209. return true
  210. }
  211. func testLiteralExpression(t *testing.T, exp ast.Expression, expected interface{}) bool {
  212. switch v := expected.(type) {
  213. case int:
  214. return testIntegerLiteral(t, exp, int64(v))
  215. case int64:
  216. return testIntegerLiteral(t, exp, v)
  217. case string:
  218. return testIdentifier(t, exp, v)
  219. case bool:
  220. return testBooleanLiteral(t, exp, v)
  221. }
  222. t.Errorf("type of exp not hand. got=%T", exp)
  223. return false
  224. }
  225. func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
  226. bo, ok := exp.(*ast.Boolean)
  227. if !ok {
  228. t.Errorf("exp not *ast.Boolean. got=%T", exp)
  229. return false
  230. }
  231. if bo.Value != value {
  232. t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
  233. return false
  234. }
  235. if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
  236. t.Errorf("bo.TokenLiteral not %t. got=%s", value, bo.TokenLiteral())
  237. return false
  238. }
  239. return true
  240. }
  241. func testIdentifier(t *testing.T, exp ast.Expression, v string) bool {
  242. ident, ok := exp.(*ast.Identifier)
  243. if !ok {
  244. t.Errorf("exp not *ast.Identifier. got=%T", exp)
  245. return false
  246. }
  247. if ident.Value != v {
  248. t.Errorf("ident.Value not %s. got=%s", v, ident.Value)
  249. return false
  250. }
  251. if ident.TokenLiteral() != v {
  252. t.Errorf("ident.TokenLiteral not %s. got=%s", v,
  253. ident.TokenLiteral())
  254. return false
  255. }
  256. return true
  257. }
  258. // <expression> <infix operator> <expression>
  259. func TestParsingInfixExpressions(t *testing.T) {
  260. infixTests := []struct {
  261. input string
  262. leftValue int64
  263. operator string
  264. rightValue int64
  265. }{
  266. {"5 + 5", 5, "+", 5},
  267. {"5 - 5", 5, "-", 5},
  268. {"5 * 5", 5, "*", 5},
  269. {"5 / 5", 5, "/", 5},
  270. {"5 > 5", 5, ">", 5},
  271. {"5 < 5", 5, "<", 5},
  272. {"5 == 5", 5, "==", 5},
  273. {"5 != 5", 5, "!=", 5},
  274. }
  275. for _, tt := range infixTests {
  276. l := lexer.New(tt.input)
  277. p := New(l)
  278. program := p.ParseProgram()
  279. checkParseErrors(t, p)
  280. if len(program.Statements) != 1 {
  281. t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
  282. 1, len(program.Statements))
  283. }
  284. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  285. if !ok {
  286. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  287. program.Statements[0])
  288. }
  289. exp, ok := stmt.Expression.(*ast.InfixExpression)
  290. if !ok {
  291. t.Fatalf("exp is not ast.InfixExpression. got =%T", stmt.Expression)
  292. }
  293. if !testIntegerLiteral(t, exp.Left, tt.leftValue) {
  294. return
  295. }
  296. if exp.Operator != tt.operator {
  297. t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator, exp.Operator)
  298. }
  299. if !testIntegerLiteral(t, exp.Right, tt.rightValue) {
  300. return
  301. }
  302. }
  303. }
  304. // 操作符 优先级测试
  305. func TestOperatorPrecedenceParsing(t *testing.T) {
  306. tests := []struct {
  307. input string
  308. expected string
  309. }{
  310. {
  311. "-a * b",
  312. "((-a) * b)",
  313. },
  314. {
  315. "!-a",
  316. "(!(-a))",
  317. },
  318. {
  319. "a + b + c",
  320. "((a + b) + c)",
  321. },
  322. {
  323. "a + b - c",
  324. "((a + b) - c)",
  325. },
  326. {
  327. "a * b * c",
  328. "((a * b) * c)",
  329. },
  330. {
  331. "a * b / c",
  332. "((a * b) / c)",
  333. },
  334. {
  335. "a + b / c",
  336. "(a + (b / c))",
  337. },
  338. {
  339. "a + b * c + d / e - f",
  340. "(((a + (b * c)) + (d / e)) - f)",
  341. },
  342. {
  343. "3 + 4; -5 *5",
  344. "(3 + 4)((-5) * 5)",
  345. },
  346. {
  347. "5 > 4 == 3 < 4",
  348. "((5 > 4) == (3 < 4))",
  349. },
  350. {
  351. "5 < 4 != 3 > 4",
  352. "((5 < 4) != (3 > 4))",
  353. },
  354. {
  355. "3 + 4 * 5 == 3 * 1 + 4 * 5",
  356. "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
  357. },
  358. {
  359. "3 + 4 * 5 == 3 * 1 + 4 * 5",
  360. "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
  361. },
  362. {
  363. "3 > 5 == false",
  364. "((3 > 5) == false)",
  365. },
  366. {
  367. "1 + (2 + 3) + 4",
  368. "((1 + (2 + 3)) + 4)",
  369. },
  370. {
  371. "(5 + 5) * 2",
  372. "((5 + 5) * 2)",
  373. },
  374. {
  375. "2 / (5 + 5)",
  376. "(2 / (5 + 5))"},
  377. {
  378. "-(5 + 5)",
  379. "(-(5 + 5))"},
  380. {
  381. "!(true == true)", "(!(true == true))",
  382. },
  383. {
  384. "a * [1, 2, 3, 4][b * c] *d",
  385. "((a * ([1, 2, 3, 4][(b * c)])) * d)",
  386. },
  387. {
  388. "add(a * b[2], b[1], 2 * [1, 2][1])",
  389. "add((a * (b[2])), (b[1]), (2 * ([1, 2][1])))",
  390. },
  391. }
  392. for _, tt := range tests {
  393. l := lexer.New(tt.input)
  394. p := New(l)
  395. program := p.ParseProgram()
  396. checkParseErrors(t, p)
  397. actual := program.String()
  398. if actual != tt.expected {
  399. t.Errorf("exptected=%q,\n got=%q", tt.expected, actual)
  400. }
  401. }
  402. }
  403. func TestBooleanExpression(t *testing.T) {
  404. tests := []struct {
  405. input string
  406. expectedBoolean bool
  407. }{
  408. {"true;", true},
  409. {"false;", false},
  410. }
  411. for _, tt := range tests {
  412. l := lexer.New(tt.input)
  413. p := New(l)
  414. program := p.ParseProgram()
  415. checkParseErrors(t, p)
  416. if len(program.Statements) != 1 {
  417. t.Fatalf("program has not enough statements. got=%d",
  418. len(program.Statements))
  419. }
  420. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  421. if !ok {
  422. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  423. program.Statements[0])
  424. }
  425. boolean, ok := stmt.Expression.(*ast.Boolean)
  426. if !ok {
  427. t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
  428. }
  429. if boolean.Value != tt.expectedBoolean {
  430. t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean,
  431. boolean.Value)
  432. }
  433. }
  434. }
  435. func TestIfExpression(t *testing.T) {
  436. input := `if (x < y) { x }`
  437. l := lexer.New(input)
  438. p := New(l)
  439. program := p.ParseProgram()
  440. checkParseErrors(t, p)
  441. if len(program.Statements) != 1 {
  442. t.Fatalf("program.Body does not contain %d statements. got=%d\n",
  443. 1, len(program.Statements))
  444. }
  445. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  446. if !ok {
  447. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
  448. }
  449. exp, ok := stmt.Expression.(*ast.IfExpression)
  450. if !ok {
  451. t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
  452. }
  453. if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
  454. return
  455. }
  456. if len(exp.Consequence.Statements) != 1 {
  457. t.Errorf("consequence is not 1 statements. got=%d\n",
  458. len(exp.Consequence.Statements))
  459. }
  460. consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
  461. if !ok {
  462. t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T", exp.Consequence.Statements[0])
  463. }
  464. if !testIdentifier(t, consequence.Expression, "x") {
  465. return
  466. }
  467. if exp.Alternative != nil {
  468. t.Errorf("exp.Alternative.Statements was not nil. got=%+v", exp.Alternative)
  469. }
  470. }
  471. func TestIfElseExpression(t *testing.T) {
  472. input := `if (x < y) { x } else { y }`
  473. l := lexer.New(input)
  474. p := New(l)
  475. program := p.ParseProgram()
  476. checkParseErrors(t, p)
  477. if len(program.Statements) != 1 {
  478. t.Fatalf("program.Body does not contain %d statements. got=%d\n",
  479. 1, len(program.Statements))
  480. }
  481. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  482. if !ok {
  483. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  484. program.Statements[0])
  485. }
  486. exp, ok := stmt.Expression.(*ast.IfExpression)
  487. if !ok {
  488. t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T", stmt.Expression)
  489. }
  490. if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
  491. return
  492. }
  493. if len(exp.Consequence.Statements) != 1 {
  494. t.Errorf("consequence is not 1 statements. got=%d\n",
  495. len(exp.Consequence.Statements))
  496. }
  497. consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
  498. if !ok {
  499. t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
  500. exp.Consequence.Statements[0])
  501. }
  502. if !testIdentifier(t, consequence.Expression, "x") {
  503. return
  504. }
  505. if len(exp.Alternative.Statements) != 1 {
  506. t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
  507. len(exp.Alternative.Statements))
  508. }
  509. alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
  510. if !ok {
  511. t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
  512. exp.Alternative.Statements[0])
  513. }
  514. if !testIdentifier(t, alternative.Expression, "y") {
  515. return
  516. }
  517. }
  518. func testInfixExpression(t *testing.T, exp ast.Expression, left interface{},
  519. operator string, right interface{}) bool {
  520. opExp, ok := exp.(*ast.InfixExpression)
  521. if !ok {
  522. t.Errorf("exp is not ast.OperatorExpression. got=%T(%s)", exp, exp)
  523. return false
  524. }
  525. if !testLiteralExpression(t, opExp.Left, left) {
  526. return false
  527. }
  528. if opExp.Operator != operator {
  529. t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
  530. return false
  531. }
  532. if !testLiteralExpression(t, opExp.Right, right) {
  533. return false
  534. }
  535. return true
  536. }
  537. func TestFunctionLiteral(t *testing.T) {
  538. input := `fn(x,y) {x+y}`
  539. l := lexer.New(input)
  540. p := New(l)
  541. program := p.ParseProgram()
  542. checkParseErrors(t, p)
  543. if len(program.Statements) != 1 {
  544. t.Fatalf("Program.Body does not contain %d statements. got=%d\n", 1, len(program.Statements))
  545. }
  546. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  547. if !ok {
  548. t.Fatalf("program.Statement[0] is not ast.ExpressionStatemtn. got=%T", program.Statements[0])
  549. }
  550. function, ok := stmt.Expression.(*ast.FunctionLiteral)
  551. if !ok {
  552. t.Fatalf("stmt.Expression is not ast.FunctionLiteral. got=%T", stmt.Expression)
  553. }
  554. if len(function.Parameters) != 2 {
  555. t.Fatalf("function literal parameters wrong. want 2, got =%d\n", len(function.Parameters))
  556. }
  557. testLiteralExpression(t, function.Parameters[0], "x")
  558. testLiteralExpression(t, function.Parameters[1], "y")
  559. if len(function.Body.Statements) != 1 {
  560. t.Fatalf("function.Body.Statements has not 1 statements. got=%d\n", len(function.Body.Statements))
  561. }
  562. bodyStmt, ok := function.Body.Statements[0].(*ast.ExpressionStatement)
  563. if !ok {
  564. t.Fatalf("function body stmt is not ast.ExpressionStatement. got=%T", function.Body.Statements[0])
  565. }
  566. testInfixExpression(t, bodyStmt.Expression, "x", "+", "y")
  567. }
  568. func TestFunctionParameterParsing(t *testing.T) {
  569. tests := []struct {
  570. input string
  571. expectedParams []string
  572. }{
  573. {input: "fn() {};", expectedParams: []string{}},
  574. {input: "fn(x){};", expectedParams: []string{"x"}},
  575. {input: "fn(x,y,z){};", expectedParams: []string{"x", "y", "z"}},
  576. }
  577. for _, tt := range tests {
  578. l := lexer.New(tt.input)
  579. p := New(l)
  580. program := p.ParseProgram()
  581. checkParseErrors(t, p)
  582. stmt := program.Statements[0].(*ast.ExpressionStatement)
  583. function := stmt.Expression.(*ast.FunctionLiteral)
  584. if len(function.Parameters) != len(tt.expectedParams) {
  585. t.Errorf("length parameters wrong. want %d, got=%d\n",
  586. len(tt.expectedParams), len(function.Parameters))
  587. }
  588. for i, ident := range tt.expectedParams {
  589. testLiteralExpression(t, function.Parameters[i], ident)
  590. }
  591. }
  592. }
  593. func TestCallExpressionParsing(t *testing.T) {
  594. input := `add(1, 2 * 3, 4+5);`
  595. l := lexer.New(input)
  596. p := New(l)
  597. program := p.ParseProgram()
  598. checkParseErrors(t, p)
  599. if len(program.Statements) != 1 {
  600. t.Fatalf("program.Statement does not contain %d statements. got=%d\n", 1, len(program.Statements))
  601. }
  602. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  603. if !ok {
  604. t.Fatalf("stmt is not ast.ExpressionStatement. got=%T", program.Statements[0])
  605. }
  606. exp, ok := stmt.Expression.(*ast.CallExpression)
  607. if !ok {
  608. t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T", stmt.Expression)
  609. }
  610. if !testIdentifier(t, exp.Function, "add") {
  611. return
  612. }
  613. if len(exp.Arguments) != 3 {
  614. t.Fatalf("wrong length arguments. got=%d", len(exp.Arguments))
  615. }
  616. testLiteralExpression(t, exp.Arguments[0], 1)
  617. testInfixExpression(t, exp.Arguments[1], 2, "*", 3)
  618. testInfixExpression(t, exp.Arguments[2], 4, "+", 5)
  619. }
  620. func TestStringLiteralExpression(t *testing.T) {
  621. input := `"hello world";`
  622. l := lexer.New(input)
  623. p := New(l)
  624. program := p.ParseProgram()
  625. checkParseErrors(t, p)
  626. stmt := program.Statements[0].(*ast.ExpressionStatement)
  627. literal, ok := stmt.Expression.(*ast.StringLiteral)
  628. if !ok {
  629. t.Fatalf("exp not *ast.StringLiteral. got=%T", stmt.Expression)
  630. }
  631. if literal.Value != "hello world" {
  632. t.Fatalf("literal.Value not %q. got=%q", "hello world", literal.Value)
  633. }
  634. }
  635. func TestArrayLiteralsParsing(t *testing.T) {
  636. input := "[1, 2 * 2, 3 + 3]"
  637. l := lexer.New(input)
  638. p := New(l)
  639. program := p.ParseProgram()
  640. checkParseErrors(t, p)
  641. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  642. array, ok := stmt.Expression.(*ast.ArrayLiteral)
  643. if !ok {
  644. t.Fatalf("exp not ast.ArrayLiteral. got=%T", stmt.Expression)
  645. }
  646. if len(array.Element) != 3 {
  647. t.Fatalf("len(array.Element) not 3. got=%d", len(array.Element))
  648. }
  649. testIntegerLiteral(t, array.Element[0], 1)
  650. testInfixExpression(t, array.Element[1], 2, "*", 2)
  651. testInfixExpression(t, array.Element[2], 3, "+", 3)
  652. }
  653. func TestIndexExpressionParsing(t *testing.T) {
  654. input := "myArray[1+1]"
  655. l := lexer.New(input)
  656. p := New(l)
  657. program := p.ParseProgram()
  658. checkParseErrors(t, p)
  659. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  660. indexExp, ok := stmt.Expression.(*ast.IndexExpression)
  661. if !ok {
  662. t.Fatalf("exp not *ast.IndexExpression. got=%T", stmt.Expression)
  663. }
  664. if !testIdentifier(t, indexExp.Left, "myArray") {
  665. return
  666. }
  667. if !testInfixExpression(t, indexExp.Index, 1, "+", 1) {
  668. return
  669. }
  670. }
  671. func TestHashLiteralParsing(t *testing.T) {
  672. input := `{"one": 1, "two":2, "three": 3}`
  673. l := lexer.New(input)
  674. p := New(l)
  675. program := p.ParseProgram()
  676. checkParseErrors(t, p)
  677. stmt := program.Statements[0].(*ast.ExpressionStatement)
  678. hash, ok := stmt.Expression.(*ast.HashLiteral)
  679. if !ok {
  680. t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
  681. }
  682. if len(hash.Pairs) != 3 {
  683. t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
  684. }
  685. expected := map[string]int64{
  686. "one": 1,
  687. "two": 2,
  688. "three": 3,
  689. }
  690. for k, v := range hash.Pairs {
  691. literal, ok := k.(*ast.StringLiteral)
  692. if !ok {
  693. t.Errorf("key is not ast.StringLiteral. got =%T", k)
  694. }
  695. expectedVal := expected[literal.String()]
  696. testIntegerLiteral(t, v, expectedVal)
  697. }
  698. }
  699. func TestEmptyHashLiteralParsing(t *testing.T) {
  700. input := `{}`
  701. l := lexer.New(input)
  702. p := New(l)
  703. program := p.ParseProgram()
  704. checkParseErrors(t, p)
  705. stmt := program.Statements[0].(*ast.ExpressionStatement)
  706. hash, ok := stmt.Expression.(*ast.HashLiteral)
  707. if !ok {
  708. t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
  709. }
  710. if len(hash.Pairs) != 0 {
  711. t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
  712. }
  713. }
  714. func TestParsingHashLiteralsWithExpressions(t *testing.T) {
  715. input := `{"one": 0 + 1, "two": 10 - 8, "three": 15 / 5}`
  716. l := lexer.New(input)
  717. p := New(l)
  718. program := p.ParseProgram()
  719. checkParseErrors(t, p)
  720. stmt := program.Statements[0].(*ast.ExpressionStatement)
  721. hash, ok := stmt.Expression.(*ast.HashLiteral)
  722. if !ok {
  723. t.Fatalf("exp is not ast.HashLiteral. got=%T", stmt.Expression)
  724. }
  725. if len(hash.Pairs) != 3 {
  726. t.Errorf("hash.Pairs has wrong length. got=%d", len(hash.Pairs))
  727. }
  728. tests := map[string]func(ast.Expression){
  729. "one": func(e ast.Expression) {
  730. testInfixExpression(t, e, 0, "+", 1)
  731. },
  732. "two": func(e ast.Expression) {
  733. testInfixExpression(t, e, 10, "-", 8)
  734. },
  735. "three": func(e ast.Expression) {
  736. testInfixExpression(t, e, 15, "/", 5)
  737. },
  738. }
  739. for key, value := range hash.Pairs {
  740. literal, ok := key.(*ast.StringLiteral)
  741. if !ok {
  742. t.Errorf("key is not ast.StringLiteral. got=%T", key)
  743. continue
  744. }
  745. testFunc, ok := tests[literal.String()]
  746. if !ok {
  747. t.Errorf("No test function for key %q found.", literal.String())
  748. continue
  749. }
  750. testFunc(value)
  751. }
  752. }