Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. /* description: Parses end executes mathematical expressions. */
  2.  
  3. /* lexical grammar */
  4. %lex
  5.  
  6. %%
  7. \s+ /* skip whitespace */
  8. "lambda" return "LAMBDA";
  9. "fix" return "FIX";
  10. "LAMBDA" return "TYPE_LAMBDA";
  11. "as" return "AS";
  12. "returns" return "RETURNS";
  13. "alias" return "ALIAS";
  14. "forall" return "FORALL";
  15. "=" return "=";
  16. "in" return "IN";
  17.  
  18. "(" return "(";
  19. ")" return ")";
  20. "[" return "[";
  21. "]" return "]";
  22. "<" return "<";
  23. ">" return ">";
  24. "{" return "{";
  25. "}" return "}";
  26. "," return ",";
  27.  
  28. "+" return "+";
  29. "*" return "*";
  30.  
  31. ":" return ":";
  32. "->" return "->";
  33. "int" return "INT";
  34.  
  35. "fold" return "FOLD";
  36. "unfold" return "UNFOLD";
  37.  
  38. "rec" return "REC";
  39.  
  40. "case" return "CASE";
  41. "|" return "|";
  42.  
  43. "#" return "#";
  44.  
  45. "unit" return "UNIT";
  46. [0-9]+\b return "NUMERAL";
  47. \`[a-zA-Z0-9_]+\b return "LABEL";
  48. [a-zA-Z_]+\b return "IDENT";
  49. "." return "DOT";
  50.  
  51. <<EOF>> return "EOF";
  52.  
  53. /lex
  54.  
  55. /* operator associations and precedence */
  56.  
  57. %left MEXP_TO_EXP GENERICIZE
  58. %left DOT LAMBDA TYPE_LAMBDA FIX ALIAS
  59. %left NUMERAL LABEL IDENT
  60. %left CASE
  61. %left '+'
  62. %left '*'
  63. %right '->'
  64. %left '(' ')' '[' ']' '{' '}' '<' '>'
  65. %left CALL FOLD UNFOLD
  66. %left '#'
  67. %left ':'
  68.  
  69. %start expressions
  70.  
  71. %% /* language grammar */
  72.  
  73. expressions
  74. : e EOF
  75.  
  76. ;
  77.  
  78. untyped_ident
  79. : IDENT
  80. {$ = yytext;}
  81. ;
  82.  
  83. ident
  84. : untyped_ident ":" type %prec GENERICIZE
  85. {$ = { kind: "TYPED_IDENT", type: $3, value: $1 };}
  86. | untyped_ident
  87. {$ = { kind: "UNTYPED_IDENT", value: $1};}
  88. ;
  89.  
  90. label
  91. : LABEL
  92. {$ = (yytext).slice(1);}
  93. ;
  94.  
  95. tuple_element
  96. : label e
  97. { $={ label: $1, value: $2 };}
  98. ;
  99.  
  100. tuple_contents
  101. : tuple_element
  102. {$ = [$1];}
  103. | tuple_contents "," tuple_element
  104. {$ = [...($1), $3] }
  105. ;
  106.  
  107. tuple
  108. : "{" tuple_contents "}"
  109. {$=$2;}
  110. ;
  111.  
  112. case_entry
  113. : label ident "->" e
  114. {$ = { label: $1, binding: $2, value: $4 };}
  115. ;
  116.  
  117. case_entry_list
  118. : case_entry "|" case_entry
  119. {$ = [$1, $3 ];}
  120. | case_entry_list "|" case_entry
  121. {$ = [...($1), $3] }
  122. ;
  123.  
  124. case
  125. : CASE "(" mexp ")" "{" case_entry_list "}"
  126. {$ = { kind:"CASE", binding: $3, value: $6};}
  127. ;
  128.  
  129. e
  130. : e "+" e
  131. {$ = { kind: "PLUS", value: [$1, $3] };}
  132. | e "*" e
  133. {$ = { kind: "TIMES", value: [$1, $3] };}
  134. | mexp %prec MEXP_TO_EXP
  135. {$ = $1;}
  136. ;
  137.  
  138.  
  139. mexp
  140. : ALIAS untyped_ident "=" type IN e
  141. {$ = { kind: "ALIAS", value: $6, ident: $2, type: $4};}
  142. | mexp mexp %prec CALL
  143. {$ = { kind: "CALL", value: $1, subst: $2 };}
  144. | "(" e ")"
  145. {$ = $2;}
  146. | mexp "[" type "]" %prec CALL
  147. {$ = { kind: "TYPE_CALL", value: $1, subst: $3};}
  148. | FOLD AS type mexp
  149. {$ = { kind: "FOLD", value: $4, type: $3};}
  150. | UNFOLD mexp
  151. {$ = { kind: "UNFOLD", value: $2};}
  152. | LAMBDA ident DOT e
  153. {$ = { kind: "LAMBDA", value: $4, binding: $2 };}
  154. | TYPE_LAMBDA untyped_ident DOT e
  155. {$ = { kind: "TYPE_LAMBDA", value: $4, binding: $2};}
  156. | FIX untyped_ident ident RETURNS type DOT e
  157. {$ = { kind: "FIXED", fn: $2, value: $7, returnType: $5, binding: $3};}
  158. | ident
  159. {$ = $1}
  160. | tuple
  161. {$ = { kind: "TUPLE", value: $1 };}
  162. | mexp "#" label %prec CALL
  163. {$ = { kind: "EXTRACT", value: $1, productLabel: $3 };}
  164. | NUMERAL
  165. {$ = { kind: "NUMBER", value: Number(yytext) };}
  166. | "(" ")"
  167. {$ = { kind: "UNIT" };}
  168. | case
  169. {$ = $1;}
  170. | label e AS type %prec CALL
  171. {$ = { kind: "SUM", sumLabel: $1, value: $2, type: $4 };}
  172. ;
  173.  
  174.  
  175. label_type_single
  176. : label type
  177. {$ = { label: $1, value: $2 };}
  178. ;
  179.  
  180. sum_type
  181. : label_type_single
  182. {$ = [$1]}
  183. | sum_type "+" label_type_single
  184. {$ = [...($1), $3] }
  185. ;
  186.  
  187. product_type
  188. : label_type_single
  189. {$ = [$1]}
  190. | product_type "*" label_type_single
  191. {$ = [...($1), $3] }
  192. ;
  193.  
  194. type
  195. : INT
  196. {$ = { kind: "INT" };}
  197. | UNIT
  198. {$ = { kind: "UNIT" };}
  199. | untyped_ident
  200. {$ = { kind: "TYPE_VAR", value: $1};}
  201. | "<" sum_type ">" %prec "+"
  202. {$ = { kind: "SUM", value: $2 };}
  203. | "{" product_type "}" %prec "*"
  204. {$ = { kind: "PRODUCT", value: $2 };}
  205. | type "->" type
  206. {$ = { kind: "ARROW", value: [$1, $3] };}
  207. | REC untyped_ident DOT type
  208. {$ = { kind: "REC", value: $4, binding: $2};}
  209. | FORALL untyped_ident DOT type
  210. {$ = { kind: "NEEDS_CONSTRAINT", binding: $2, type: $4};}
  211. | "(" type ")"
  212. {$ = $2}
  213. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement