parser_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. for _, tt := range tests {
  282. l := lexer.New(tt.input)
  283. p := New(l)
  284. program := p.ParseProgram()
  285. checkParserErrors(t, p)
  286. actual := program.String()
  287. if actual != tt.expected {
  288. t.Errorf("expected=%q, got=%q", tt.expected, actual)
  289. }
  290. }
  291. }
  292. func TestBooleanExpression(t *testing.T) {
  293. tests := []struct {
  294. input string
  295. expectedBoolean bool
  296. }{
  297. {"true", true},
  298. {"false", false},
  299. }
  300. for _, tt := range tests {
  301. l := lexer.New(tt.input)
  302. p := New(l)
  303. program := p.ParseProgram()
  304. checkParserErrors(t, p)
  305. if len(program.Statements) != 1 {
  306. t.Fatalf("program has not enough statements. got=%d",
  307. len(program.Statements))
  308. }
  309. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  310. if !ok {
  311. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  312. program.Statements[0])
  313. }
  314. boolean, ok := stmt.Expression.(*ast.Boolean)
  315. if !ok {
  316. t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
  317. }
  318. if boolean.Value != tt.expectedBoolean {
  319. t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean,
  320. boolean.Value)
  321. }
  322. }
  323. }
  324. func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
  325. if s.TokenLiteral() != "let" {
  326. t.Errorf("s.TokenLiteral not 'let'. got=%q", s.TokenLiteral())
  327. return false
  328. }
  329. // Statement is an interface, we need a concrete type for the value, and we
  330. // just determined this looked like a LetStatement.
  331. letStmt, ok := s.(*ast.LetStatement)
  332. if !ok {
  333. t.Errorf("s not *ast.LetStatement. got=%T", s)
  334. return false
  335. }
  336. if letStmt.Name.Value != name {
  337. t.Errorf("letStmt.Name.Value not '%s'. got=%s", name, letStmt.Name.Value)
  338. return false
  339. }
  340. if letStmt.Name.TokenLiteral() != name {
  341. t.Errorf("letStmt.Name.TokenLiteral() not '%s'. got=%s",
  342. name, letStmt.Name.TokenLiteral())
  343. return false
  344. }
  345. return true
  346. }
  347. func testInfixExpression(
  348. t *testing.T,
  349. exp ast.Expression,
  350. left interface{},
  351. operator string,
  352. right interface{},
  353. ) bool {
  354. opExp, ok := exp.(*ast.InfixExpression)
  355. if !ok {
  356. t.Errorf("exp is not ast.InfixExpression. got=%T(%s)", exp, exp)
  357. return false
  358. }
  359. if !testLiteralExpression(t, opExp.Left, left) {
  360. return false
  361. }
  362. if opExp.Operator != operator {
  363. t.Errorf("exp.Operator is not '%s'. got=%q", operator, opExp.Operator)
  364. return false
  365. }
  366. if !testLiteralExpression(t, opExp.Right, right) {
  367. return false
  368. }
  369. return true
  370. }
  371. func testLiteralExpression(
  372. t *testing.T,
  373. exp ast.Expression,
  374. expected interface{},
  375. ) bool {
  376. switch v := expected.(type) {
  377. case int:
  378. return testIntegerLiteral(t, exp, int64(v))
  379. case int64:
  380. return testIntegerLiteral(t, exp, v)
  381. case string:
  382. return testIdentifier(t, exp, v)
  383. case bool:
  384. return testBooleanLiteral(t, exp, v)
  385. }
  386. t.Errorf("type of exp not handled. got=%T", exp)
  387. return false
  388. }
  389. func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
  390. integ, ok := il.(*ast.IntegerLiteral)
  391. if !ok {
  392. t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
  393. return false
  394. }
  395. if integ.Value != value {
  396. t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
  397. return false
  398. }
  399. if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
  400. t.Errorf("integ.TokenLiteral not %d. got=%s", value,
  401. integ.TokenLiteral())
  402. return false
  403. }
  404. return true
  405. }
  406. func testIdentifier(t *testing.T, exp ast.Expression, value string) bool {
  407. ident, ok := exp.(*ast.Identifier)
  408. if !ok {
  409. t.Errorf("exp not *ast.Identifier. got=%T", exp)
  410. return false
  411. }
  412. if ident.Value != value {
  413. t.Errorf("ident.Value not %s. got=%s", value, ident.Value)
  414. return false
  415. }
  416. if ident.TokenLiteral() != value {
  417. t.Errorf("ident.TokenLiteral not %s. got=%s", value,
  418. ident.TokenLiteral())
  419. return false
  420. }
  421. return true
  422. }
  423. func testBooleanLiteral(t *testing.T, exp ast.Expression, value bool) bool {
  424. bo, ok := exp.(*ast.Boolean)
  425. if !ok {
  426. t.Errorf("exp not *ast.Boolean. got=%T", exp)
  427. return false
  428. }
  429. if bo.Value != value {
  430. t.Errorf("bo.Value not %t. got=%t", value, bo.Value)
  431. return false
  432. }
  433. if bo.TokenLiteral() != fmt.Sprintf("%t", value) {
  434. t.Errorf("bo.TokenLiteral not %t. got=%s",
  435. value, bo.TokenLiteral())
  436. }
  437. return true
  438. }
  439. func checkParserErrors(t *testing.T, p *Parser) {
  440. errors := p.Errors()
  441. if len(errors) == 0 {
  442. return
  443. }
  444. t.Errorf("parser has %d errors", len(errors))
  445. for _, msg := range errors {
  446. t.Errorf("parser error: %q", msg)
  447. }
  448. t.FailNow()
  449. }