parser_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. func TestOperatorPrecedenceParsing(t *testing.T) {
  305. tests := []struct {
  306. input string
  307. expected string
  308. }{
  309. {
  310. "-a * b",
  311. "((-a) * b)",
  312. },
  313. {
  314. "!-a",
  315. "(!(-a))",
  316. },
  317. {
  318. "a + b + c",
  319. "((a + b) + c)",
  320. },
  321. {
  322. "a + b - c",
  323. "((a + b) - c)",
  324. },
  325. {
  326. "a * b * c",
  327. "((a * b) * c)",
  328. },
  329. {
  330. "a * b / c",
  331. "((a * b) / c)",
  332. },
  333. {
  334. "a + b / c",
  335. "(a + (b / c))",
  336. },
  337. {
  338. "a + b * c + d / e - f",
  339. "(((a + (b * c)) + (d / e)) - f)",
  340. },
  341. {
  342. "3 + 4; -5 *5",
  343. "(3 + 4)((-5) * 5)",
  344. },
  345. {
  346. "5 > 4 == 3 < 4",
  347. "((5 > 4) == (3 < 4))",
  348. },
  349. {
  350. "5 < 4 != 3 > 4",
  351. "((5 < 4) != (3 > 4))",
  352. },
  353. {
  354. "3 + 4 * 5 == 3 * 1 + 4 * 5",
  355. "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
  356. },
  357. {
  358. "3 + 4 * 5 == 3 * 1 + 4 * 5",
  359. "((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
  360. },
  361. {
  362. "3 > 5 == false",
  363. "((3 > 5) == false)",
  364. },
  365. }
  366. for _, tt := range tests {
  367. fmt.Printf("=====================================parse %s, begin============================\n", tt.input)
  368. l := lexer.New(tt.input)
  369. p := New(l)
  370. program := p.ParseProgram()
  371. checkParseErrors(t, p)
  372. actual := program.String()
  373. if actual != tt.expected {
  374. t.Errorf("exptected=%q, got=%q", tt.expected, actual)
  375. }
  376. fmt.Printf("=====================================parse %s, end============================\n", tt.input)
  377. }
  378. }
  379. func TestBooleanExpression(t *testing.T) {
  380. tests := []struct {
  381. input string
  382. expectedBoolean bool
  383. }{
  384. {"true;", true},
  385. {"false;", false},
  386. }
  387. for _, tt := range tests {
  388. l := lexer.New(tt.input)
  389. p := New(l)
  390. program := p.ParseProgram()
  391. checkParseErrors(t, p)
  392. if len(program.Statements) != 1 {
  393. t.Fatalf("program has not enough statements. got=%d",
  394. len(program.Statements))
  395. }
  396. stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
  397. if !ok {
  398. t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
  399. program.Statements[0])
  400. }
  401. boolean, ok := stmt.Expression.(*ast.Boolean)
  402. if !ok {
  403. t.Fatalf("exp not *ast.Boolean. got=%T", stmt.Expression)
  404. }
  405. if boolean.Value != tt.expectedBoolean {
  406. t.Errorf("boolean.Value not %t. got=%t", tt.expectedBoolean,
  407. boolean.Value)
  408. }
  409. }
  410. }