parser_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 testLetStatement(t *testing.T, s ast.Statement, name string) bool {
  345. if s.TokenLiteral() != "let" {
  346. t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
  347. return false
  348. }
  349. // Statement is an interface, we need a concrete type for the value, and we
  350. // just determined this looked like a LetStatement.
  351. letStmt, ok := s.(*ast.LetStatement)
  352. if !ok {
  353. t.Errorf("s not *ast.LetStatement. got=%T", s)
  354. return false
  355. }
  356. if letStmt.Name.Value != name {
  357. t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
  358. return false
  359. }
  360. if letStmt.Name.TokenLiteral() != name {
  361. t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
  362. name, letStmt.Name.TokenLiteral())
  363. return false
  364. }
  365. return true
  366. }
  367. func testInfixExpression(
  368. t *testing.T,
  369. exp ast.Expression,
  370. left interface{},
  371. operator string,
  372. right interface{},
  373. ) bool {
  374. opExp, ok := exp.(*ast.InfixExpression)
  375. if !ok {
  376. t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
  377. return false
  378. }
  379. if !testLiteralExpression(t, opExp.Left, left) {
  380. return false
  381. }
  382. if opExp.Operator != operator {
  383. t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
  384. return false
  385. }
  386. if !testLiteralExpression(t, opExp.Right, right) {
  387. return false
  388. }
  389. return true
  390. }
  391. func testLiteralExpression(
  392. t *testing.T,
  393. exp ast.Expression,
  394. expected interface{},
  395. ) bool {
  396. switch v := expected.(type) {
  397. case int:
  398. return testIntegerLiteral(t, exp, int64(v))
  399. case int64:
  400. return testIntegerLiteral(t, exp, v)
  401. case string:
  402. return testIdentifier(t, exp, v)
  403. case bool:
  404. return testBooleanLiteral(t, exp, v)
  405. }
  406. t.Errorf("type of exp not handled. got=%T", exp)
  407. return false
  408. }
  409. func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
  410. integ, ok := il.(*ast.IntegerLiteral)
  411. if !ok {
  412. t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
  413. return false
  414. }
  415. if integ.Value != value {
  416. t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
  417. return false
  418. }
  419. if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
  420. t.Errorf("integ.TokenLiteral not %d. got=%s", value,
  421. integ.TokenLiteral())
  422. return false
  423. }
  424. return true
  425. }
  426. func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
  427. ident, ok := exp.(*ast.Identifier)
  428. if !ok {
  429. t.Errorf("exp not *ast.Identifier. got=%T", exp)
  430. return false
  431. }
  432. if ident.Value != value {
  433. t.Errorf("ident.Value not %s. got=%s", value, ident.Value)
  434. return false
  435. }
  436. if ident.TokenLiteral() != value {
  437. t.Errorf("ident.TokenLiteral not %s. got=%s", value,
  438. ident.TokenLiteral())
  439. return false
  440. }
  441. return true
  442. }
  443. func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
  444. bo, ok := exp.(*ast.Boolean)
  445. if !ok {
  446. t.Errorf("exp not *ast.Boolean. got=%T", exp)
  447. return false
  448. }
  449. if bo.Value != value {
  450. t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
  451. return false
  452. }
  453. if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
  454. t.Errorf("bo.TokenLiteral not %t. got=%s",
  455. value, bo.TokenLiteral())
  456. }
  457. return true
  458. }
  459. func checkParserErrors(t *testing.T, p *Parser) {
  460. errors := p.Errors()
  461. if len(errors) == 0 {
  462. return
  463. }
  464. t.Errorf("parser has %d errors", len(errors))
  465. for _, msg := range errors {
  466. t.Errorf("parser error: %q", msg)
  467. }
  468. t.FailNow()
  469. }