parser_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. package parser
  2. import (
  3. "testing"
  4. "fmt"
  5. "code.osinet.fr/fgm/waiig15/ast"
  6. "code.osinet.fr/fgm/waiig15/lexer"
  7. )
  8. func TestLetStatements(t *testing.T) {
  9. // Try removing the ident, the =, or both, to get human-readable errors.
  10. input := `
  11. let x = 5;
  12. let y = 10;
  13. let foobar = 838383;
  14. `
  15. l := lexer.New(input)
  16. p := New(l)
  17. program := p.ParseProgram()
  18. checkParserErrors(t, p)
  19. if program == nil {
  20. t.Fatalf("ParseProgram() returned nil.")
  21. }
  22. if len(program.Statements) != 3 {
  23. t.Fatalf("program.Statements does not contain 3 statements, got=%d",
  24. len(program.Statements))
  25. }
  26. tests := []struct {
  27. expectedIdentifier string
  28. }{
  29. {"x"},
  30. {"y"},
  31. {"foobar"},
  32. }
  33. for i, tt := range tests {
  34. stmt := program.Statements[i]
  35. if !testLetStatement(t, stmt, tt.expectedIdentifier) {
  36. return
  37. }
  38. }
  39. }
  40. func TestReturnStatements(t *testing.T) {
  41. // Try removing the ident, the =, or both, to get human-readable errors.
  42. input := `
  43. return 5;
  44. return 10;
  45. return 993322;
  46. `
  47. l := lexer.New(input)
  48. p := New(l)
  49. program := p.ParseProgram()
  50. checkParserErrors(t, p)
  51. if program == nil {
  52. t.Fatalf("ParseProgram() returned nil.")
  53. }
  54. if len(program.Statements) != 3 {
  55. t.Fatalf("program.Statements does not contain 3 statements, got=%d",
  56. len(program.Statements))
  57. }
  58. for _, stmt := range program.Statements {
  59. // Statement is an interface, we need a concrete type for the value, and
  60. // our test input only contains Let statements.
  61. returnStmt, ok := stmt.(*ast.ReturnStatement)
  62. if !ok {
  63. t.Errorf("s not *ast.ReturnStatement{}, got=%T", stmt)
  64. continue
  65. }
  66. if returnStmt.TokenLiteral() != "return" {
  67. t.Errorf("s.TokenLiteral not 'return', got=%q",
  68. stmt.TokenLiteral())
  69. }
  70. }
  71. }
  72. func TestIdentifierExpression(t *testing.T) {
  73. const input = "foobar"
  74. l := lexer.New(input)
  75. p := New(l)
  76. program := p.ParseProgram()
  77. checkParserErrors(t, p)
  78. if len(program.Statements) != 1 {
  79. t.Fatalf("program has not enough statements. got=%d",
  80. len(program.Statements))
  81. }
  82. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  83. if !ok {
  84. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. Got=%T",
  85. program.Statements)
  86. }
  87. ident, ok := stmt.Expression.(*ast.Identifier)
  88. if !ok {
  89. t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression)
  90. }
  91. // Why not use input instead of inline strings ?
  92. if ident.Value != input {
  93. t.Errorf("ident.Value not %s. got=%s", input,
  94. ident.Value)
  95. }
  96. if ident.TokenLiteral() != input {
  97. t.Errorf("ident.TokenLiteral not %s. got=%s", input,
  98. ident.TokenLiteral())
  99. }
  100. }
  101. func TestIntegerLiteralExpression(t *testing.T) {
  102. input := "5;"
  103. l := lexer.New(input)
  104. p := New(l)
  105. program := p.ParseProgram()
  106. checkParserErrors(t, p)
  107. if len(program.Statements) != 1 {
  108. t.Fatalf("program does not have 1 statement. got=%d",
  109. len(program.Statements))
  110. }
  111. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  112. if !ok {
  113. t.Fatalf("program.Statements[0] is not *ast.ExpressionStatement. got=%T",
  114. program.Statements[0])
  115. }
  116. literal, ok := stmt.Expression.(*ast.IntegerLiteral)
  117. if !ok {
  118. t.Fatalf("exp not *ast.IntegerLiteral. got=%T", stmt.Expression)
  119. }
  120. if literal.Value != 5 {
  121. t.Errorf("literal.Value not %d. got=%d", 5, literal.Value)
  122. }
  123. if literal.TokenLiteral() != "5" {
  124. t.Errorf("literal.TokenLiteral not %s. got=%s", "5",
  125. literal.TokenLiteral())
  126. }
  127. }
  128. func TestParsingPrefixExpressions(t *testing.T) {
  129. prefixTests := []struct {
  130. input string
  131. operator string
  132. value interface{}
  133. }{
  134. {"!5", "!", 5},
  135. {"-15", "-", 15},
  136. {"!true", "!", true},
  137. {"!false", "!", false},
  138. }
  139. for _, tt := range prefixTests {
  140. l := lexer.New(tt.input)
  141. p := New(l)
  142. program := p.ParseProgram()
  143. checkParserErrors(t, p)
  144. if len(program.Statements) != 1 {
  145. t.Fatalf("program.Statements does not contain %d statements, got=%d\n",
  146. 1, len(program.Statements))
  147. }
  148. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  149. if !ok {
  150. t.Fatalf("program.STatements[0] is not ast.ExpressionStatement. got=%T",
  151. program.Statements[0])
  152. }
  153. exp, ok := stmt.Expression.(*ast.PrefixExpression)
  154. if !ok {
  155. t.Fatalf("stms is not ast.PrefixExpression. got=%T",
  156. stmt.Expression)
  157. }
  158. if exp.Operator != tt.operator {
  159. t.Fatalf("exp.Operator is not '%s'. got=%s",
  160. tt.operator, exp.Operator)
  161. }
  162. if !testLiteralExpression(t, exp.Right, tt.value) {
  163. // No error message, they have already been generated in testLiteralExpression.
  164. return
  165. }
  166. }
  167. }
  168. func TestParsingInfixExpressions(t *testing.T) {
  169. infixTests := []struct {
  170. input string
  171. leftValue interface{}
  172. operator string
  173. rightValue interface{}
  174. }{
  175. {"5 + 5;", 5, "+", 5},
  176. {"5 - 5;", 5, "-", 5},
  177. {"5 * 5;", 5, "*", 5},
  178. {"5 / 5;", 5, "/", 5},
  179. {"5 > 5;", 5, ">", 5},
  180. {"5 < 5;", 5, "<", 5},
  181. {"5 == 5;", 5, "==", 5},
  182. {"5 != 5;", 5, "!=", 5},
  183. {"true == true", true, "==", true},
  184. {"true != false", true, "!=", false},
  185. {"false == false", false, "==", false},
  186. }
  187. for _, tt := range infixTests {
  188. l := lexer.New(tt.input)
  189. p := New(l)
  190. program := p.ParseProgram()
  191. checkParserErrors(t, p)
  192. if len(program.Statements) != 1 {
  193. t.Fatalf("program.Statements does not contain %d statements. got=%d",
  194. 1, len(program.Statements))
  195. }
  196. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  197. if !ok {
  198. t.Fatalf("program.Statements[0] is not expressionStatement. got=%T",
  199. program.Statements[0])
  200. }
  201. exp, ok := stmt.Expression.(*ast.InfixExpression)
  202. if !ok {
  203. t.Fatalf("exp is not infixExpression. got=%T", stmt.Expression)
  204. }
  205. if !testInfixExpression(t, exp, tt.leftValue, tt.operator, tt.rightValue) {
  206. // No error message, they have already been generated in testInfixExpression.
  207. return
  208. }
  209. }
  210. }
  211. func TestOperatorPrecedenceParsing(t *testing.T) {
  212. tests := []struct {
  213. input string
  214. expected string
  215. }{
  216. {
  217. "-a * b",
  218. "((-a) * b)",
  219. },
  220. {
  221. "!-a",
  222. "(!(-a))",
  223. },
  224. {
  225. "a + b + c",
  226. "((a + b) + c)",
  227. },
  228. {
  229. "a + b - c",
  230. "((a + b) - c)",
  231. },
  232. {
  233. "a * b * c",
  234. "((a * b) * c)",
  235. },
  236. {
  237. "a * b / c",
  238. "((a * b) / c)",
  239. },
  240. {
  241. "a + b / c",
  242. "(a + (b / c))",
  243. },
  244. {
  245. "a + b * c + d / e - f",
  246. "(((a + (b * c)) + (d / e)) - f)",
  247. },
  248. {
  249. "3 + 4; -5 * 5",
  250. "(3 + 4)((-5) * 5)",
  251. },
  252. {
  253. "5 > 4 == 3 < 4",
  254. "((5 > 4) == (3 < 4))",
  255. },
  256. {
  257. "5 < 4 != 3 > 4",
  258. "((5 < 4) != (3 > 4))",
  259. },
  260. {
  261. "3 + 4 * 5 == 3 * 1 + 4 * 5",
  262. "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
  263. },
  264. {
  265. "true",
  266. "true",
  267. },
  268. {
  269. "false",
  270. "false",
  271. },
  272. {
  273. "3 > 5 == false",
  274. "((3 > 5) == false)",
  275. },
  276. {
  277. "3 < 5 == true",
  278. "((3 < 5) == true)",
  279. },
  280. {
  281. "1 + (2 + 3) + 4",
  282. "((1 + (2 + 3)) + 4)",
  283. },
  284. {
  285. "(5 + 5) * 2",
  286. "((5 + 5) * 2)",
  287. },
  288. {
  289. "2 / (5 + 5)",
  290. "(2 / (5 + 5))",
  291. },
  292. {
  293. "-(5 + 5)",
  294. "(-(5 + 5))",
  295. },
  296. {
  297. "!(true == true)",
  298. "(!(true == true))",
  299. },
  300. }
  301. for _, tt := range tests {
  302. l := lexer.New(tt.input)
  303. p := New(l)
  304. program := p.ParseProgram()
  305. checkParserErrors(t, p)
  306. actual := program.String()
  307. if actual != tt.expected {
  308. t.Errorf("expected=%q, got=%q", tt.expected, actual)
  309. }
  310. }
  311. }
  312. func TestBooleanExpression(t *testing.T) {
  313. tests := []struct {
  314. input string
  315. expectedBoolean bool
  316. }{
  317. {"true", true},
  318. {"false", false},
  319. }
  320. for _, tt := range tests {
  321. l := lexer.New(tt.input)
  322. p := New(l)
  323. program := p.ParseProgram()
  324. checkParserErrors(t, p)
  325. if len(program.Statements) != 1 {
  326. t.Fatalf("program has not enough statements. got=%d",
  327. len(program.Statements))
  328. }
  329. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  330. if !ok {
  331. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  332. program.Statements[0])
  333. }
  334. boolean, ok := stmt.Expression.(*ast.Boolean)
  335. if !ok {
  336. t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
  337. }
  338. if boolean.Value != tt.expectedBoolean {
  339. t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean,
  340. boolean.Value)
  341. }
  342. }
  343. }
  344. func TestIfExpression(t *testing.T) {
  345. // Notice: no semicolon, no return.
  346. input := `if (x < y) { x }`
  347. l := lexer.New(input)
  348. p := New(l)
  349. program := p.ParseProgram()
  350. checkParserErrors(t, p)
  351. if len(program.Statements) != 1 {
  352. t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
  353. 1, len(program.Statements))
  354. }
  355. // This is the external statement (if) not the one in Consequence (block).
  356. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  357. if !ok {
  358. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  359. program.Statements[0])
  360. }
  361. exp, ok := stmt.Expression.(*ast.IfExpression)
  362. if !ok {
  363. t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T",
  364. stmt.Expression)
  365. }
  366. if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
  367. // No error message, they have already been generated in testInfixExpression.
  368. return
  369. }
  370. if len(exp.Consequence.Statements) != 1 {
  371. t.Fatalf("consequence is not 1 statements. got=%d\n",
  372. len(exp.Consequence.Statements))
  373. }
  374. consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
  375. if !ok {
  376. t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
  377. exp.Consequence.Statements[0])
  378. }
  379. if !testIdentifier(t, consequence.Expression, "x") {
  380. // No error message, they have already been generated in testIdentifier.
  381. return
  382. }
  383. if exp.Alternative != nil {
  384. t.Errorf("exp.Alternative.Statements was not nil. got=%+v",
  385. exp.Alternative)
  386. }
  387. }
  388. func TestIfElseExpression(t *testing.T) {
  389. input := `if (x < y) { x } else { y }`
  390. l := lexer.New(input)
  391. p := New(l)
  392. program := p.ParseProgram()
  393. checkParserErrors(t, p)
  394. if len(program.Statements) != 1 {
  395. t.Fatalf("program.Statements does not contain %d statements. got=%d\n",
  396. 1, len(program.Statements))
  397. }
  398. // This is the external statement (if) not the one in Consequence (block).
  399. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  400. if !ok {
  401. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  402. program.Statements[0])
  403. }
  404. exp, ok := stmt.Expression.(*ast.IfExpression)
  405. if !ok {
  406. t.Fatalf("stmt.Expression is not ast.IfExpression. got=%T",
  407. stmt.Expression)
  408. }
  409. if !testInfixExpression(t, exp.Condition, "x", "<", "y") {
  410. // No error message, they have already been generated in testInfixExpression.
  411. return
  412. }
  413. if len(exp.Consequence.Statements) != 1 {
  414. t.Errorf("consequence is not 1 statements. got=%d\n",
  415. len(exp.Consequence.Statements))
  416. }
  417. consequence, ok := exp.Consequence.Statements[0].(*ast.ExpressionStatement)
  418. if !ok {
  419. t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
  420. exp.Consequence.Statements[0])
  421. }
  422. if !testIdentifier(t, consequence.Expression, "x") {
  423. return
  424. }
  425. if len(exp.Alternative.Statements) != 1 {
  426. t.Errorf("exp.Alternative.Statements does not contain 1 statements. got=%d\n",
  427. len(exp.Alternative.Statements))
  428. }
  429. alternative, ok := exp.Alternative.Statements[0].(*ast.ExpressionStatement)
  430. if !ok {
  431. t.Fatalf("Statements[0] is not ast.ExpressionStatement. got=%T",
  432. exp.Alternative.Statements[0])
  433. }
  434. if !testIdentifier(t, alternative.Expression, "y") {
  435. return
  436. }
  437. }
  438. func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
  439. if s.TokenLiteral() != "let" {
  440. t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
  441. return false
  442. }
  443. // Statement is an interface, we need a concrete type for the value, and we
  444. // just determined this looked like a LetStatement.
  445. letStmt, ok := s.(*ast.LetStatement)
  446. if !ok {
  447. t.Errorf("s not *ast.LetStatement. got=%T", s)
  448. return false
  449. }
  450. if letStmt.Name.Value != name {
  451. t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
  452. return false
  453. }
  454. if letStmt.Name.TokenLiteral() != name {
  455. t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
  456. name, letStmt.Name.TokenLiteral())
  457. return false
  458. }
  459. return true
  460. }
  461. func testInfixExpression(
  462. t *testing.T,
  463. exp ast.Expression,
  464. left interface{},
  465. operator string,
  466. right interface{},
  467. ) bool {
  468. opExp, ok := exp.(*ast.InfixExpression)
  469. if !ok {
  470. t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
  471. return false
  472. }
  473. if !testLiteralExpression(t, opExp.Left, left) {
  474. return false
  475. }
  476. if opExp.Operator != operator {
  477. t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
  478. return false
  479. }
  480. if !testLiteralExpression(t, opExp.Right, right) {
  481. return false
  482. }
  483. return true
  484. }
  485. func testLiteralExpression(
  486. t *testing.T,
  487. exp ast.Expression,
  488. expected interface{},
  489. ) bool {
  490. switch v := expected.(type) {
  491. case int:
  492. return testIntegerLiteral(t, exp, int64(v))
  493. case int64:
  494. return testIntegerLiteral(t, exp, v)
  495. case string:
  496. return testIdentifier(t, exp, v)
  497. case bool:
  498. return testBooleanLiteral(t, exp, v)
  499. }
  500. t.Errorf("type of exp not handled. got=%T", exp)
  501. return false
  502. }
  503. func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
  504. integ, ok := il.(*ast.IntegerLiteral)
  505. if !ok {
  506. t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
  507. return false
  508. }
  509. if integ.Value != value {
  510. t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
  511. return false
  512. }
  513. if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
  514. t.Errorf("integ.TokenLiteral not %d. got=%s", value,
  515. integ.TokenLiteral())
  516. return false
  517. }
  518. return true
  519. }
  520. func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
  521. ident, ok := exp.(*ast.Identifier)
  522. if !ok {
  523. t.Errorf("exp not *ast.Identifier. got=%T", exp)
  524. return false
  525. }
  526. if ident.Value != value {
  527. t.Errorf("ident.Value not %s. got=%s", value, ident.Value)
  528. return false
  529. }
  530. if ident.TokenLiteral() != value {
  531. t.Errorf("ident.TokenLiteral not %s. got=%s", value,
  532. ident.TokenLiteral())
  533. return false
  534. }
  535. return true
  536. }
  537. func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
  538. bo, ok := exp.(*ast.Boolean)
  539. if !ok {
  540. t.Errorf("exp not *ast.Boolean. got=%T", exp)
  541. return false
  542. }
  543. if bo.Value != value {
  544. t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
  545. return false
  546. }
  547. if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
  548. t.Errorf("bo.TokenLiteral not %t. got=%s",
  549. value, bo.TokenLiteral())
  550. }
  551. return true
  552. }
  553. func checkParserErrors(t *testing.T, p *Parser) {
  554. errors := p.Errors()
  555. if len(errors) == 0 {
  556. return
  557. }
  558. t.Errorf("parser has %d errors", len(errors))
  559. for _, msg := range errors {
  560. t.Errorf("parser error: %q", msg)
  561. }
  562. t.FailNow()
  563. }