The Sol Programming Language!
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.

626 lines
22 KiB

  1. %{
  2. #include "sol.h"
  3. #include "ast.h"
  4. #include <string.h>
  5. #define YYSTYPE void *
  6. %}
  7. %define lr.type ielr
  8. %define api.pure full
  9. %token IF THEN ELSE
  10. %token WHILE FOR IN DO
  11. %token FUNC LAMBDA RETURN BREAK CONTINUE
  12. %token END NONE
  13. %token IDENT
  14. %token INT FLOAT STRING
  15. %token PLUS MINUS STAR SLASH PERCENT DSTAR BAND BOR BXOR BNOT LAND LOR LNOT
  16. %token ASSIGN ASSIGNPLUS ASSIGNMINUS ASSIGNSTAR ASSIGNSLASH ASSIGNDSTAR ASSIGNBAND ASSIGNBOR ASSIGNBXOR
  17. %token EQUAL NEQUAL LESS GREATER LESSEQ GREATEREQ RSHIFT LSHIFT
  18. %token LBRACE RBRACE BLPAREN LPAREN RPAREN LBRACKET RBRACKET DOT COLON SEMICOLON COMMA POUND
  19. %token TBANG
  20. %parse-param {stmt_node **program}
  21. %debug
  22. %locations
  23. %%
  24. program:
  25. stmt_list { *program = AS_ST($1); }
  26. ;
  27. stmt_list:
  28. stmt_list stmt {
  29. stmtlist_node *cur = AS_ST($1)->stmtlist;
  30. while(cur->next) cur = cur->next;
  31. if(cur->stmt) {
  32. cur->next = NEW(stmtlist_node);
  33. cur = cur->next;
  34. }
  35. cur->stmt = $2;
  36. cur->next = NULL;
  37. $$ = $1;
  38. }
  39. | /* empty */ {
  40. $$ = NEW_ST();
  41. AS_ST($$)->type = ST_LIST;
  42. AS_ST($$)->stmtlist = NEW(stmtlist_node);
  43. AS_ST($$)->stmtlist->stmt = NULL;
  44. AS_ST($$)->stmtlist->next = NULL;
  45. }
  46. ;
  47. stmt:
  48. expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_EXPR; AS_ST($$)->expr = $1; }
  49. | RETURN expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_RET; AS_ST($$)->ret = NEW(ret_node); AS_ST($$)->ret->ret = $2; }
  50. | RETURN { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_RET; AS_ST($$)->ret = NEW(ret_node); AS_ST($$)->ret->ret = NULL; }
  51. | BREAK { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_BREAK; AS_ST($$)->brk = NEW(break_node); AS_ST($$)->brk->val = NULL;}
  52. | BREAK expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_BREAK; AS_ST($$)->brk = NEW(break_node); AS_ST($$)->brk->val = $2; }
  53. | CONTINUE { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_CONT; AS_ST($$)->cont = NEW(cont_node); AS_ST($$)->cont->val = NULL; }
  54. | CONTINUE expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_CONT; AS_ST($$)->cont = NEW(cont_node); AS_ST($$)->cont->val = $2; }
  55. | stmt SEMICOLON { $$ = $1; }
  56. ;
  57. expr:
  58. control_expr { $$ = $1; }
  59. ;
  60. control_expr:
  61. IF expr THEN stmt_list END {
  62. $$ = NEW_EX();
  63. AS_EX($$)->type = EX_IFELSE;
  64. AS_EX($$)->ifelse = NEW(ifelse_node);
  65. AS_EX($$)->ifelse->cond = $2;
  66. AS_EX($$)->ifelse->iftrue = $4;
  67. AS_EX($$)->ifelse->iffalse = NULL;
  68. }
  69. | IF expr THEN stmt_list ELSE stmt_list END {
  70. $$ = NEW_EX();
  71. AS_EX($$)->type = EX_IFELSE;
  72. AS_EX($$)->ifelse = NEW(ifelse_node);
  73. AS_EX($$)->ifelse->cond = $2;
  74. AS_EX($$)->ifelse->iftrue = $4;
  75. AS_EX($$)->ifelse->iffalse = $6;
  76. }
  77. | WHILE expr DO stmt_list END {
  78. $$ = NEW_EX();
  79. AS_EX($$)->type = EX_LOOP;
  80. AS_EX($$)->loop = NEW(loop_node);
  81. AS_EX($$)->loop->cond = $2;
  82. AS_EX($$)->loop->loop = $4;
  83. }
  84. | FOR IDENT IN expr DO stmt_list END {
  85. $$ = NEW_EX();
  86. AS_EX($$)->type = EX_ITER;
  87. AS_EX($$)->iter = NEW(iter_node);
  88. AS_EX($$)->iter->var = $2;
  89. AS_EX($$)->iter->iter = $4;
  90. AS_EX($$)->iter->loop = $6;
  91. }
  92. | assign_expr { $$ = $1; }
  93. ;
  94. assign_expr:
  95. IDENT ASSIGN expr { $$ = NEW_EX(); AS_EX($$)->type = EX_ASSIGN; AS_EX($$)->assign = NEW(assign_node); AS_EX($$)->assign->ident = $1; AS_EX($$)->assign->value = $3; }
  96. | IDENT ASSIGNPLUS expr {
  97. $$ = NEW_EX();
  98. AS_EX($$)->type = EX_ASSIGN;
  99. AS_EX($$)->assign = NEW(assign_node);
  100. AS_EX($$)->assign->ident = $1;
  101. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_ADD, $1, $3);
  102. }
  103. | IDENT ASSIGNMINUS expr {
  104. $$ = NEW_EX();
  105. AS_EX($$)->type = EX_ASSIGN;
  106. AS_EX($$)->assign = NEW(assign_node);
  107. AS_EX($$)->assign->ident = $1;
  108. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_SUB, $1, $3);
  109. }
  110. | IDENT ASSIGNSTAR expr {
  111. $$ = NEW_EX();
  112. AS_EX($$)->type = EX_ASSIGN;
  113. AS_EX($$)->assign = NEW(assign_node);
  114. AS_EX($$)->assign->ident = $1;
  115. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_MUL, $1, $3);
  116. }
  117. | IDENT ASSIGNSLASH expr {
  118. $$ = NEW_EX();
  119. AS_EX($$)->type = EX_ASSIGN;
  120. AS_EX($$)->assign = NEW(assign_node);
  121. AS_EX($$)->assign->ident = $1;
  122. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_DIV, $1, $3);
  123. }
  124. | IDENT ASSIGNDSTAR expr {
  125. $$ = NEW_EX();
  126. AS_EX($$)->type = EX_ASSIGN;
  127. AS_EX($$)->assign = NEW(assign_node);
  128. AS_EX($$)->assign->ident = $1;
  129. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_POW, $1, $3);
  130. }
  131. | IDENT ASSIGNBAND expr {
  132. $$ = NEW_EX();
  133. AS_EX($$)->type = EX_ASSIGN;
  134. AS_EX($$)->assign = NEW(assign_node);
  135. AS_EX($$)->assign->ident = $1;
  136. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_BAND, $1, $3);
  137. }
  138. | IDENT ASSIGNBOR expr {
  139. $$ = NEW_EX();
  140. AS_EX($$)->type = EX_ASSIGN;
  141. AS_EX($$)->assign = NEW(assign_node);
  142. AS_EX($$)->assign->ident = $1;
  143. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_BOR, $1, $3);
  144. }
  145. | IDENT ASSIGNBXOR expr {
  146. $$ = NEW_EX();
  147. AS_EX($$)->type = EX_ASSIGN;
  148. AS_EX($$)->assign = NEW(assign_node);
  149. AS_EX($$)->assign->ident = $1;
  150. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_BXOR, $1, $3);
  151. }
  152. | ex_index_expr ASSIGN expr {
  153. if(AS_EX($1)->type != EX_INDEX) {
  154. yyerror("Assigning to non-indexing expression");
  155. YYABORT;
  156. }
  157. $$ = NEW_EX();
  158. AS_EX($$)->type = EX_SETINDEX;
  159. AS_EX($$)->setindex = NEW(setindex_node);
  160. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  161. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  162. AS_EX($$)->setindex->value = $3;
  163. ex_free(AS_EX($1));
  164. }
  165. | ex_index_expr ASSIGNPLUS expr {
  166. if(AS_EX($1)->type != EX_INDEX) {
  167. yyerror("Assigning to non-indexing expression");
  168. YYABORT;
  169. }
  170. $$ = NEW_EX();
  171. AS_EX($$)->type = EX_SETINDEX;
  172. AS_EX($$)->setindex = NEW(setindex_node);
  173. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  174. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  175. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_ADD, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  176. ex_free(AS_EX($1));
  177. }
  178. | ex_index_expr ASSIGNMINUS expr {
  179. if(AS_EX($1)->type != EX_INDEX) {
  180. yyerror("Assigning to non-indexing expression");
  181. YYABORT;
  182. }
  183. $$ = NEW_EX();
  184. AS_EX($$)->type = EX_SETINDEX;
  185. AS_EX($$)->setindex = NEW(setindex_node);
  186. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  187. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  188. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_SUB, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  189. ex_free(AS_EX($1));
  190. }
  191. | ex_index_expr ASSIGNSTAR expr {
  192. if(AS_EX($1)->type != EX_INDEX) {
  193. yyerror("Assigning to non-indexing expression");
  194. YYABORT;
  195. }
  196. $$ = NEW_EX();
  197. AS_EX($$)->type = EX_SETINDEX;
  198. AS_EX($$)->setindex = NEW(setindex_node);
  199. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  200. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  201. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_MUL, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  202. ex_free(AS_EX($1));
  203. }
  204. | ex_index_expr ASSIGNSLASH expr {
  205. if(AS_EX($1)->type != EX_INDEX) {
  206. yyerror("Assigning to non-indexing expression");
  207. YYABORT;
  208. }
  209. $$ = NEW_EX();
  210. AS_EX($$)->type = EX_SETINDEX;
  211. AS_EX($$)->setindex = NEW(setindex_node);
  212. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  213. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  214. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_DIV, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  215. ex_free(AS_EX($1));
  216. }
  217. | ex_index_expr ASSIGNDSTAR expr {
  218. if(AS_EX($1)->type != EX_INDEX) {
  219. yyerror("Assigning to non-indexing expression");
  220. YYABORT;
  221. }
  222. $$ = NEW_EX();
  223. AS_EX($$)->type = EX_SETINDEX;
  224. AS_EX($$)->setindex = NEW(setindex_node);
  225. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  226. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  227. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_POW, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  228. ex_free(AS_EX($1));
  229. }
  230. | ex_index_expr ASSIGNBAND expr {
  231. if(AS_EX($1)->type != EX_INDEX) {
  232. yyerror("Assigning to non-indexing expression");
  233. YYABORT;
  234. }
  235. $$ = NEW_EX();
  236. AS_EX($$)->type = EX_SETINDEX;
  237. AS_EX($$)->setindex = NEW(setindex_node);
  238. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  239. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  240. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_BAND, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  241. ex_free(AS_EX($1));
  242. }
  243. | ex_index_expr ASSIGNBOR expr {
  244. if(AS_EX($1)->type != EX_INDEX) {
  245. yyerror("Assigning to non-indexing expression");
  246. YYABORT;
  247. }
  248. $$ = NEW_EX();
  249. AS_EX($$)->type = EX_SETINDEX;
  250. AS_EX($$)->setindex = NEW(setindex_node);
  251. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  252. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  253. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_BOR, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  254. ex_free(AS_EX($1));
  255. }
  256. | ex_index_expr ASSIGNBXOR expr {
  257. if(AS_EX($1)->type != EX_INDEX) {
  258. yyerror("Assigning to non-indexing expression");
  259. YYABORT;
  260. }
  261. $$ = NEW_EX();
  262. AS_EX($$)->type = EX_SETINDEX;
  263. AS_EX($$)->setindex = NEW(setindex_node);
  264. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  265. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  266. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_BXOR, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  267. ex_free(AS_EX($1));
  268. }
  269. | logic_expr { $$ = $1; }
  270. ;
  271. logic_expr:
  272. logic_expr LAND ulogic_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LAND; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  273. | logic_expr LOR ulogic_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LOR; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  274. | ulogic_expr { $$ = $1; }
  275. ;
  276. ulogic_expr:
  277. LNOT ulogic_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_UNOP; AS_EX($$)->unop = NEW(unop_node); AS_EX($$)->unop->type = OP_LNOT; AS_EX($$)->unop->expr = $2; }
  278. | rel_expr { $$ = $1; }
  279. ;
  280. rel_expr:
  281. term_expr EQUAL rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_EQUAL; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  282. | term_expr NEQUAL rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_NEQUAL; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  283. | term_expr LESS rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LESS; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  284. | term_expr GREATER rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_GREATER; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  285. | term_expr LESSEQ rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LESSEQ; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  286. | term_expr GREATEREQ rel_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_GREATEREQ; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  287. | term_expr { $$ = $1; }
  288. ;
  289. term_expr:
  290. term_expr PLUS factor_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_ADD; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  291. | term_expr MINUS factor_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_SUB; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  292. | factor_expr { $$ = $1; }
  293. ;
  294. factor_expr:
  295. factor_expr STAR power_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_MUL; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  296. | factor_expr SLASH power_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_DIV; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  297. | factor_expr PERCENT power_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_MOD; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  298. | power_expr { $$ = $1; }
  299. ;
  300. power_expr:
  301. tbang_expr DSTAR power_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_POW; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  302. | tbang_expr { $$ = $1; }
  303. ;
  304. tbang_expr:
  305. binary_expr TBANG tbang_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_TBANG; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  306. | binary_expr { $$ = $1; }
  307. ;
  308. binary_expr:
  309. binary_expr BAND binary_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_BAND; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  310. | binary_expr BOR binary_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_BOR; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  311. | binary_expr BXOR binary_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_BXOR; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  312. | binary_expr LSHIFT binary_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_LSHIFT; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  313. | binary_expr RSHIFT binary_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_BINOP; AS_EX($$)->binop = NEW(binop_node); AS_EX($$)->binop->type = OP_RSHIFT; AS_EX($$)->binop->left = $1; AS_EX($$)->binop->right = $3; }
  314. | ubinary_expr { $$ = $1; }
  315. ;
  316. ubinary_expr:
  317. BNOT ubinary_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_UNOP; AS_EX($$)->unop = NEW(unop_node); AS_EX($$)->unop->type = OP_BNOT; AS_EX($$)->unop->expr = $2; }
  318. | ulen_expr { $$ = $1; }
  319. ;
  320. ulen_expr:
  321. POUND ulen_expr { $$ = NEW_EX(); AS_EX($$)->type = EX_UNOP; AS_EX($$)->unop = NEW(unop_node); AS_EX($$)->unop->type = OP_LEN; AS_EX($$)->unop->expr = $2; }
  322. | call_expr { $$ = $1; }
  323. ;
  324. call_expr:
  325. call_expr LPAREN expr_list RPAREN { $$ = NEW_EX(); AS_EX($$)->type = EX_CALL; AS_EX($$)->call = NEW(call_node); AS_EX($$)->call->expr = $1; AS_EX($$)->call->args = $3; AS_EX($$)->call->method = NULL; }
  326. | call_expr COLON IDENT LPAREN expr_list RPAREN {
  327. $$ = NEW_EX();
  328. AS_EX($$)->type = EX_CALL;
  329. AS_EX($$)->call = NEW(call_node);
  330. AS_EX($$)->call->expr = $1;
  331. AS_EX($$)->call->args = $5;
  332. AS_EX($$)->call->method = $3;
  333. }
  334. | funcdecl_expr { $$ = $1; }
  335. ;
  336. funcdecl_expr:
  337. FUNC IDENT any_lparen param_list RPAREN maybe_anno stmt_list END {
  338. $$ = NEW_EX();
  339. AS_EX($$)->type = EX_FUNCDECL;
  340. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  341. AS_EX($$)->funcdecl->name = $2;
  342. AS_EX($$)->funcdecl->params = $4;
  343. AS_EX($$)->funcdecl->anno = $6;
  344. AS_EX($$)->funcdecl->body = $7;
  345. }
  346. | FUNC any_lparen param_list RPAREN maybe_anno stmt_list END {
  347. $$ = NEW_EX();
  348. AS_EX($$)->type = EX_FUNCDECL;
  349. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  350. AS_EX($$)->funcdecl->name = NULL;
  351. AS_EX($$)->funcdecl->params = $3;
  352. AS_EX($$)->funcdecl->anno = $5;
  353. AS_EX($$)->funcdecl->body = $6;
  354. }
  355. | LAMBDA any_lparen param_list RPAREN maybe_anno expr END {
  356. $$ = NEW_EX();
  357. AS_EX($$)->type = EX_FUNCDECL;
  358. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  359. AS_EX($$)->funcdecl->name = NULL;
  360. AS_EX($$)->funcdecl->params = $3;
  361. AS_EX($$)->funcdecl->anno = $5;
  362. AS_EX($$)->funcdecl->body = NEW_ST();
  363. AS_EX($$)->funcdecl->body->type = ST_RET;
  364. AS_EX($$)->funcdecl->body->ret = NEW(ret_node);
  365. AS_EX($$)->funcdecl->body->ret->ret = $6;
  366. }
  367. | index_expr { $$ = $1; }
  368. ;
  369. index_expr:
  370. expr LBRACKET expr RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_INDEX; AS_EX($$)->index = NEW(index_node); AS_EX($$)->index->expr = $1; AS_EX($$)->index->index = $3; }
  371. | expr DOT IDENT {
  372. $$ = NEW_EX();
  373. AS_EX($$)->type = EX_INDEX;
  374. AS_EX($$)->index = NEW(index_node);
  375. AS_EX($$)->index->expr = $1;
  376. AS_EX($$)->index->index = NEW_EX();
  377. AS_EX($$)->index->index->type = EX_LIT;
  378. AS_EX($$)->index->index->lit = NEW(lit_node);
  379. AS_EX($$)->index->index->lit->type = LIT_STRING;
  380. AS_EX($$)->index->index->lit->str = $3;
  381. }
  382. | ref_expr { $$ = $1; }
  383. ;
  384. ex_index_expr:
  385. expr LBRACKET expr RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_INDEX; AS_EX($$)->index = NEW(index_node); AS_EX($$)->index->expr = $1; AS_EX($$)->index->index = $3; }
  386. | expr DOT IDENT {
  387. $$ = NEW_EX();
  388. AS_EX($$)->type = EX_INDEX;
  389. AS_EX($$)->index = NEW(index_node);
  390. AS_EX($$)->index->expr = $1;
  391. AS_EX($$)->index->index = NEW_EX();
  392. AS_EX($$)->index->index->type = EX_LIT;
  393. AS_EX($$)->index->index->lit = NEW(lit_node);
  394. AS_EX($$)->index->index->lit->type = LIT_STRING;
  395. AS_EX($$)->index->index->lit->str = $3;
  396. }
  397. ;
  398. ref_expr:
  399. IDENT { $$ = NEW_EX(); AS_EX($$)->type = EX_REF; AS_EX($$)->ref = NEW(ref_node); AS_EX($$)->ref->ident = $1; }
  400. | lit_expr { $$ = $1; }
  401. ;
  402. lit_expr:
  403. INT { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_INT; AS_EX($$)->lit->ival = *AS($1, long); free($1); }
  404. | MINUS INT { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_INT; AS_EX($$)->lit->ival = -(*AS($2, long)); free($2); }
  405. | FLOAT { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_FLOAT; AS_EX($$)->lit->fval = *AS($1, double); free($1); }
  406. | STRING { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_STRING; AS_EX($$)->lit->str = $1; }
  407. | NONE { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_NONE; }
  408. | gen_expr { $$ = $1; }
  409. ;
  410. gen_expr:
  411. LBRACKET expr_list RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_LISTGEN; AS_EX($$)->listgen = NEW(listgen_node); AS_EX($$)->listgen->list = $2; }
  412. | LBRACE assoc_list RBRACE { $$ = NEW_EX(); AS_EX($$)->type = EX_MAPGEN; AS_EX($$)->mapgen = NEW(mapgen_node); AS_EX($$)->mapgen->map = $2; }
  413. | paren_expr { $$ = $1; }
  414. ;
  415. paren_expr:
  416. any_lparen expr RPAREN { $$ = $2; }
  417. ;
  418. expr_list:
  419. /*empty*/ { $$ = NULL; }
  420. | expr {
  421. $$ = NEW(exprlist_node);
  422. AS($$, exprlist_node)->expr = $1;
  423. AS($$, exprlist_node)->next = NULL;
  424. }
  425. | expr_list COMMA { $$ = $1; }
  426. | expr_list expr {
  427. exprlist_node *cur = $1;
  428. while(cur->next) cur = cur->next;
  429. cur->next = NEW(exprlist_node);
  430. cur = cur->next;
  431. cur->expr = $2;
  432. cur->next = NULL;
  433. $$ = $1;
  434. }
  435. ;
  436. ident_list:
  437. /*empty*/ { $$ = NULL; }
  438. | IDENT {
  439. $$ = NEW(identlist_node);
  440. AS($$, identlist_node)->ident = $1;
  441. AS($$, identlist_node)->next = NULL;
  442. }
  443. | ident_list COMMA { $$ = $1; }
  444. | ident_list IDENT {
  445. identlist_node *cur = $1;
  446. while(cur->next) cur = cur->next;
  447. cur->next = NEW(identlist_node);
  448. cur = cur->next;
  449. cur->ident = $2;
  450. cur->next = NULL;
  451. $$ = $1;
  452. }
  453. ;
  454. param_list:
  455. /*empty*/ { $$ = NULL; }
  456. | param_list IDENT ASSIGN expr {
  457. paramlist_node *pl = $1;
  458. identlist_node *curk;
  459. exprlist_node *curv;
  460. if(!pl) {
  461. pl = NEW(paramlist_node);
  462. pl->args = NULL;
  463. pl->annos = NULL;
  464. pl->clkeys = NULL;
  465. pl->clvalues = NULL;
  466. pl->rest = NULL;
  467. }
  468. if(!pl->clkeys) {
  469. pl->clkeys = NEW(identlist_node);
  470. curk = pl->clkeys;
  471. pl->clvalues = NEW(exprlist_node);
  472. curv = pl->clvalues;
  473. } else {
  474. curk = pl->clkeys;
  475. curv = pl->clvalues;
  476. while(curk->next) {
  477. curk = curk->next;
  478. curv = curv->next;
  479. }
  480. curk->next = NEW(identlist_node);
  481. curk = curk->next;
  482. curv->next = NEW(exprlist_node);
  483. curv = curv->next;
  484. }
  485. curk->ident = $2;
  486. curk->next = NULL;
  487. curv->expr = $4;
  488. curv->next = NULL;
  489. $$ = pl;
  490. }
  491. | param_list STAR IDENT {
  492. paramlist_node *pl = $1;
  493. if(!pl) {
  494. pl = NEW(paramlist_node);
  495. pl->args = NULL;
  496. pl->annos = NULL;
  497. pl->clkeys = NULL;
  498. pl->clvalues = NULL;
  499. pl->rest = NULL;
  500. }
  501. pl-> rest = $3;
  502. $$ = pl;
  503. }
  504. | param_list IDENT maybe_anno {
  505. paramlist_node *pl = $1;
  506. identlist_node *cura;
  507. exprlist_node *curn;
  508. if(!pl) {
  509. pl = NEW(paramlist_node);
  510. pl->args = NULL;
  511. pl->annos = NULL;
  512. pl->clkeys = NULL;
  513. pl->clvalues = NULL;
  514. pl->rest = NULL;
  515. }
  516. if(!pl->args) {
  517. pl->args = NEW(identlist_node);
  518. cura = pl->args;
  519. pl->annos = NEW(exprlist_node);
  520. curn = pl->annos;
  521. } else {
  522. cura = pl->args;
  523. curn = pl->annos;
  524. while(cura->next) {
  525. cura = cura->next;
  526. curn = curn->next;
  527. }
  528. cura->next = NEW(identlist_node);
  529. cura = cura->next;
  530. curn->next = NEW(exprlist_node);
  531. curn = curn->next;
  532. }
  533. cura->ident = $2;
  534. cura->next = NULL;
  535. curn->expr = $3;
  536. curn->next = NULL;
  537. $$ = pl;
  538. }
  539. | param_list COMMA { $$ = $1; }
  540. ;
  541. assoc_list:
  542. /*empty*/ { $$ = NULL; }
  543. | assoc_item {
  544. $$ = NEW(assoclist_node);
  545. AS($$, assoclist_node)->item = $1;
  546. AS($$, assoclist_node)->next = NULL;
  547. }
  548. | assoc_list COMMA { $$ = $1; }
  549. | assoc_list assoc_item {
  550. assoclist_node *cur = $1;
  551. while(cur->next) cur = cur->next;
  552. cur->next = NEW(assoclist_node);
  553. cur = cur->next;
  554. cur->item = $2;
  555. cur->next = NULL;
  556. $$ = $1;
  557. }
  558. ;
  559. assoc_item:
  560. LBRACKET expr RBRACKET ASSIGN expr {
  561. $$ = NEW(associtem_node);
  562. AS($$, associtem_node)->key = $2;
  563. AS($$, associtem_node)->value = $5;
  564. }
  565. | IDENT ASSIGN expr {
  566. $$ = NEW(associtem_node);
  567. AS($$, associtem_node)->key = NEW_EX();
  568. AS($$, associtem_node)->key->type = EX_LIT;
  569. AS($$, associtem_node)->key->lit = NEW(lit_node);
  570. AS($$, associtem_node)->key->lit->type = LIT_STRING;
  571. AS($$, associtem_node)->key->lit->str = $1;
  572. AS($$, associtem_node)->value = $3;
  573. }
  574. ;
  575. any_lparen:
  576. LPAREN
  577. | BLPAREN
  578. ;
  579. maybe_anno:
  580. COLON expr { $$ = $2; }
  581. | /* empty */ { $$ = NULL; }
  582. ;
  583. %%
  584. // TODO