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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. std::map<std::string, definition_data_ptr> defs_data;
  10. std::map<std::string, definition_defn_ptr> defs_defn;
  11. extern yy::parser::symbol_type yylex();
  12. %}
  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 OCURLY
  23. %token CCURLY
  24. %token OPAREN
  25. %token CPAREN
  26. %token COMMA
  27. %token ARROW
  28. %token EQUAL
  29. %token <std::string> LID
  30. %token <std::string> UID
  31. %language "c++"
  32. %define api.value.type variant
  33. %define api.token.constructor
  34. %type <std::vector<std::string>> lowercaseParams
  35. %type <std::vector<branch_ptr>> branches
  36. %type <std::vector<constructor_ptr>> constructors
  37. %type <std::vector<parsed_type_ptr>> typeList
  38. %type <parsed_type_ptr> type nonArrowType typeListElement
  39. %type <ast_ptr> aAdd aMul case app appBase
  40. %type <definition_data_ptr> data
  41. %type <definition_defn_ptr> defn
  42. %type <branch_ptr> branch
  43. %type <pattern_ptr> pattern
  44. %type <constructor_ptr> constructor
  45. %start program
  46. %%
  47. program
  48. : definitions { }
  49. ;
  50. definitions
  51. : definitions definition { }
  52. | definition { }
  53. ;
  54. definition
  55. : defn { auto name = $1->name; defs_defn[name] = std::move($1); }
  56. | data { auto name = $1->name; defs_data[name] = std::move($1); }
  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. ;
  88. case
  89. : CASE aAdd OF OCURLY branches CCURLY
  90. { $$ = ast_ptr(new ast_case(std::move($2), std::move($5))); }
  91. ;
  92. branches
  93. : branches branch { $$ = std::move($1); $$.push_back(std::move($2)); }
  94. | branch { $$ = std::vector<branch_ptr>(); $$.push_back(std::move($1));}
  95. ;
  96. branch
  97. : pattern ARROW OCURLY aAdd CCURLY
  98. { $$ = branch_ptr(new branch(std::move($1), std::move($4))); }
  99. ;
  100. pattern
  101. : LID { $$ = pattern_ptr(new pattern_var(std::move($1))); }
  102. | UID lowercaseParams
  103. { $$ = pattern_ptr(new pattern_constr(std::move($1), std::move($2))); }
  104. ;
  105. data
  106. : DATA UID lowercaseParams EQUAL OCURLY constructors CCURLY
  107. { $$ = definition_data_ptr(new definition_data(std::move($2), std::move($3), std::move($6))); }
  108. ;
  109. constructors
  110. : constructors COMMA constructor { $$ = std::move($1); $$.push_back(std::move($3)); }
  111. | constructor
  112. { $$ = std::vector<constructor_ptr>(); $$.push_back(std::move($1)); }
  113. ;
  114. constructor
  115. : UID typeList
  116. { $$ = constructor_ptr(new constructor(std::move($1), std::move($2))); }
  117. ;
  118. type
  119. : nonArrowType ARROW type { $$ = parsed_type_ptr(new parsed_type_arr(std::move($1), std::move($3))); }
  120. | nonArrowType { $$ = std::move($1); }
  121. ;
  122. nonArrowType
  123. : UID typeList { $$ = parsed_type_ptr(new parsed_type_app(std::move($1), std::move($2))); }
  124. | LID { $$ = parsed_type_ptr(new parsed_type_var(std::move($1))); }
  125. | OPAREN type CPAREN { $$ = std::move($2); }
  126. ;
  127. typeListElement
  128. : OPAREN type CPAREN { $$ = std::move($2); }
  129. | UID { $$ = parsed_type_ptr(new parsed_type_app(std::move($1), {})); }
  130. | LID { $$ = parsed_type_ptr(new parsed_type_var(std::move($1))); }
  131. ;
  132. typeList
  133. : %empty { $$ = std::vector<parsed_type_ptr>(); }
  134. | typeList typeListElement { $$ = std::move($1); $$.push_back(std::move($2)); }
  135. ;