A Hugo incarnation of the blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser.y 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. %{
  2. #include <string>
  3. #include <iostream>
  4. #include <map>
  5. #include "ast.hpp"
  6. #include "definition.hpp"
  7. #include "parser.hpp"
  8. #include "parsed_type.hpp"
  9. definition_group global_defs;
  10. extern yy::parser::symbol_type yylex();
  11. %}
  12. %token BACKSLASH
  13. %token PLUS
  14. %token TIMES
  15. %token MINUS
  16. %token DIVIDE
  17. %token <int> INT
  18. %token DEFN
  19. %token DATA
  20. %token CASE
  21. %token OF
  22. %token LET
  23. %token IN
  24. %token OCURLY
  25. %token CCURLY
  26. %token OPAREN
  27. %token CPAREN
  28. %token COMMA
  29. %token ARROW
  30. %token EQUAL
  31. %token <std::string> LID
  32. %token <std::string> UID
  33. %language "c++"
  34. %define api.value.type variant
  35. %define api.token.constructor
  36. %type <std::vector<std::string>> lowercaseParams
  37. %type <std::vector<branch_ptr>> branches
  38. %type <std::vector<constructor_ptr>> constructors
  39. %type <std::vector<parsed_type_ptr>> typeList
  40. %type <definition_group> definitions
  41. %type <parsed_type_ptr> type nonArrowType typeListElement
  42. %type <ast_ptr> aAdd aMul case let lambda app appBase
  43. %type <definition_data_ptr> data
  44. %type <definition_defn_ptr> defn
  45. %type <branch_ptr> branch
  46. %type <pattern_ptr> pattern
  47. %type <constructor_ptr> constructor
  48. %start program
  49. %%
  50. program
  51. : definitions { global_defs = std::move($1); global_defs.vis = visibility::global; }
  52. ;
  53. definitions
  54. : definitions defn { $$ = std::move($1); auto name = $2->name; $$.defs_defn[name] = std::move($2); }
  55. | definitions data { $$ = std::move($1); auto name = $2->name; $$.defs_data[name] = std::move($2); }
  56. | %empty { $$ = definition_group(); }
  57. ;
  58. defn
  59. : DEFN LID lowercaseParams EQUAL OCURLY aAdd CCURLY
  60. { $$ = definition_defn_ptr(
  61. new definition_defn(std::move($2), std::move($3), std::move($6))); }
  62. ;
  63. lowercaseParams
  64. : %empty { $$ = std::vector<std::string>(); }
  65. | lowercaseParams LID { $$ = std::move($1); $$.push_back(std::move($2)); }
  66. ;
  67. aAdd
  68. : aAdd PLUS aMul { $$ = ast_ptr(new ast_binop(PLUS, std::move($1), std::move($3))); }
  69. | aAdd MINUS aMul { $$ = ast_ptr(new ast_binop(MINUS, std::move($1), std::move($3))); }
  70. | aMul { $$ = std::move($1); }
  71. ;
  72. aMul
  73. : aMul TIMES app { $$ = ast_ptr(new ast_binop(TIMES, std::move($1), std::move($3))); }
  74. | aMul DIVIDE app { $$ = ast_ptr(new ast_binop(DIVIDE, std::move($1), std::move($3))); }
  75. | app { $$ = std::move($1); }
  76. ;
  77. app
  78. : app appBase { $$ = ast_ptr(new ast_app(std::move($1), std::move($2))); }
  79. | appBase { $$ = std::move($1); }
  80. ;
  81. appBase
  82. : INT { $$ = ast_ptr(new ast_int($1)); }
  83. | LID { $$ = ast_ptr(new ast_lid(std::move($1))); }
  84. | UID { $$ = ast_ptr(new ast_uid(std::move($1))); }
  85. | OPAREN aAdd CPAREN { $$ = std::move($2); }
  86. | case { $$ = std::move($1); }
  87. | let { $$ = std::move($1); }
  88. | lambda { $$ = std::move($1); }
  89. ;
  90. let
  91. : LET OCURLY definitions CCURLY IN OCURLY aAdd CCURLY
  92. { $$ = ast_ptr(new ast_let(std::move($3), std::move($7))); }
  93. ;
  94. lambda
  95. : BACKSLASH lowercaseParams ARROW OCURLY aAdd CCURLY
  96. { $$ = ast_ptr(new ast_lambda(std::move($2), std::move($5))); }
  97. ;
  98. case
  99. : CASE aAdd OF OCURLY branches CCURLY
  100. { $$ = ast_ptr(new ast_case(std::move($2), std::move($5))); }
  101. ;
  102. branches
  103. : branches branch { $$ = std::move($1); $$.push_back(std::move($2)); }
  104. | branch { $$ = std::vector<branch_ptr>(); $$.push_back(std::move($1));}
  105. ;
  106. branch
  107. : pattern ARROW OCURLY aAdd CCURLY
  108. { $$ = branch_ptr(new branch(std::move($1), std::move($4))); }
  109. ;
  110. pattern
  111. : LID { $$ = pattern_ptr(new pattern_var(std::move($1))); }
  112. | UID lowercaseParams
  113. { $$ = pattern_ptr(new pattern_constr(std::move($1), std::move($2))); }
  114. ;
  115. data
  116. : DATA UID lowercaseParams EQUAL OCURLY constructors CCURLY
  117. { $$ = definition_data_ptr(new definition_data(std::move($2), std::move($3), std::move($6))); }
  118. ;
  119. constructors
  120. : constructors COMMA constructor { $$ = std::move($1); $$.push_back(std::move($3)); }
  121. | constructor
  122. { $$ = std::vector<constructor_ptr>(); $$.push_back(std::move($1)); }
  123. ;
  124. constructor
  125. : UID typeList
  126. { $$ = constructor_ptr(new constructor(std::move($1), std::move($2))); }
  127. ;
  128. type
  129. : nonArrowType ARROW type { $$ = parsed_type_ptr(new parsed_type_arr(std::move($1), std::move($3))); }
  130. | nonArrowType { $$ = std::move($1); }
  131. ;
  132. nonArrowType
  133. : UID typeList { $$ = parsed_type_ptr(new parsed_type_app(std::move($1), std::move($2))); }
  134. | LID { $$ = parsed_type_ptr(new parsed_type_var(std::move($1))); }
  135. | OPAREN type CPAREN { $$ = std::move($2); }
  136. ;
  137. typeListElement
  138. : OPAREN type CPAREN { $$ = std::move($2); }
  139. | UID { $$ = parsed_type_ptr(new parsed_type_app(std::move($1), {})); }
  140. | LID { $$ = parsed_type_ptr(new parsed_type_var(std::move($1))); }
  141. ;
  142. typeList
  143. : %empty { $$ = std::vector<parsed_type_ptr>(); }
  144. | typeList typeListElement { $$ = std::move($1); $$.push_back(std::move($2)); }
  145. ;