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.

527 lines
20 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 LPAREN RPAREN LBRACKET RBRACKET DOT COLON SEMICOLON COMMA POUND
  19. %parse-param {stmt_node **program}
  20. %debug
  21. %locations
  22. %%
  23. program:
  24. stmt_list { *program = AS_ST($1); }
  25. ;
  26. stmt_list:
  27. stmt_list stmt {
  28. stmtlist_node *cur = AS_ST($1)->stmtlist;
  29. while(cur->next) cur = cur->next;
  30. if(cur->stmt) {
  31. cur->next = NEW(stmtlist_node);
  32. cur = cur->next;
  33. }
  34. cur->stmt = $2;
  35. cur->next = NULL;
  36. $$ = $1;
  37. }
  38. | /* empty */ {
  39. $$ = NEW_ST();
  40. AS_ST($$)->type = ST_LIST;
  41. AS_ST($$)->stmtlist = NEW(stmtlist_node);
  42. AS_ST($$)->stmtlist->stmt = NULL;
  43. AS_ST($$)->stmtlist->next = NULL;
  44. }
  45. ;
  46. stmt:
  47. expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_EXPR; AS_ST($$)->expr = $1; }
  48. | RETURN expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_RET; AS_ST($$)->ret = NEW(ret_node); AS_ST($$)->ret->ret = $2; }
  49. | RETURN { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_RET; AS_ST($$)->ret = NEW(ret_node); AS_ST($$)->ret->ret = NULL; }
  50. | BREAK { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_BREAK; AS_ST($$)->brk = NEW(break_node); }
  51. | BREAK expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_BREAK; AS_ST($$)->brk = NEW(break_node); AS_ST($$)->brk->val = $2; }
  52. | CONTINUE { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_CONT; AS_ST($$)->cont = NEW(cont_node); }
  53. | CONTINUE expr { $$ = NEW_ST(); SET_LOC(AS_ST($$), @$); AS_ST($$)->type = ST_CONT; AS_ST($$)->cont = NEW(cont_node); AS_ST($$)->cont->val = $2; }
  54. | stmt SEMICOLON { $$ = $1; }
  55. ;
  56. expr:
  57. control_expr { $$ = $1; }
  58. ;
  59. control_expr:
  60. IF expr THEN stmt_list END {
  61. $$ = NEW_EX();
  62. AS_EX($$)->type = EX_IFELSE;
  63. AS_EX($$)->ifelse = NEW(ifelse_node);
  64. AS_EX($$)->ifelse->cond = $2;
  65. AS_EX($$)->ifelse->iftrue = $4;
  66. AS_EX($$)->ifelse->iffalse = NULL;
  67. }
  68. | IF expr THEN stmt_list ELSE stmt_list END {
  69. $$ = NEW_EX();
  70. AS_EX($$)->type = EX_IFELSE;
  71. AS_EX($$)->ifelse = NEW(ifelse_node);
  72. AS_EX($$)->ifelse->cond = $2;
  73. AS_EX($$)->ifelse->iftrue = $4;
  74. AS_EX($$)->ifelse->iffalse = $6;
  75. }
  76. | WHILE expr DO stmt_list END {
  77. $$ = NEW_EX();
  78. AS_EX($$)->type = EX_LOOP;
  79. AS_EX($$)->loop = NEW(loop_node);
  80. AS_EX($$)->loop->cond = $2;
  81. AS_EX($$)->loop->loop = $4;
  82. }
  83. | FOR IDENT IN expr DO stmt_list END {
  84. $$ = NEW_EX();
  85. AS_EX($$)->type = EX_ITER;
  86. AS_EX($$)->iter = NEW(iter_node);
  87. AS_EX($$)->iter->var = $2;
  88. AS_EX($$)->iter->iter = $4;
  89. AS_EX($$)->iter->loop = $6;
  90. }
  91. | assign_expr { $$ = $1; }
  92. ;
  93. assign_expr:
  94. 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; }
  95. | IDENT ASSIGNPLUS expr {
  96. $$ = NEW_EX();
  97. AS_EX($$)->type = EX_ASSIGN;
  98. AS_EX($$)->assign = NEW(assign_node);
  99. AS_EX($$)->assign->ident = $1;
  100. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_ADD, $1, $3);
  101. }
  102. | IDENT ASSIGNMINUS expr {
  103. $$ = NEW_EX();
  104. AS_EX($$)->type = EX_ASSIGN;
  105. AS_EX($$)->assign = NEW(assign_node);
  106. AS_EX($$)->assign->ident = $1;
  107. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_SUB, $1, $3);
  108. }
  109. | IDENT ASSIGNSTAR expr {
  110. $$ = NEW_EX();
  111. AS_EX($$)->type = EX_ASSIGN;
  112. AS_EX($$)->assign = NEW(assign_node);
  113. AS_EX($$)->assign->ident = $1;
  114. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_MUL, $1, $3);
  115. }
  116. | IDENT ASSIGNSLASH expr {
  117. $$ = NEW_EX();
  118. AS_EX($$)->type = EX_ASSIGN;
  119. AS_EX($$)->assign = NEW(assign_node);
  120. AS_EX($$)->assign->ident = $1;
  121. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_DIV, $1, $3);
  122. }
  123. | IDENT ASSIGNDSTAR expr {
  124. $$ = NEW_EX();
  125. AS_EX($$)->type = EX_ASSIGN;
  126. AS_EX($$)->assign = NEW(assign_node);
  127. AS_EX($$)->assign->ident = $1;
  128. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_POW, $1, $3);
  129. }
  130. | IDENT ASSIGNBAND expr {
  131. $$ = NEW_EX();
  132. AS_EX($$)->type = EX_ASSIGN;
  133. AS_EX($$)->assign = NEW(assign_node);
  134. AS_EX($$)->assign->ident = $1;
  135. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_BAND, $1, $3);
  136. }
  137. | IDENT ASSIGNBOR expr {
  138. $$ = NEW_EX();
  139. AS_EX($$)->type = EX_ASSIGN;
  140. AS_EX($$)->assign = NEW(assign_node);
  141. AS_EX($$)->assign->ident = $1;
  142. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_BOR, $1, $3);
  143. }
  144. | IDENT ASSIGNBXOR expr {
  145. $$ = NEW_EX();
  146. AS_EX($$)->type = EX_ASSIGN;
  147. AS_EX($$)->assign = NEW(assign_node);
  148. AS_EX($$)->assign->ident = $1;
  149. MAKE_REF_BINOP(AS_EX($$)->assign->value, OP_BXOR, $1, $3);
  150. }
  151. | ex_index_expr ASSIGN expr {
  152. if(AS_EX($1)->type != EX_INDEX) {
  153. yyerror("Assigning to non-indexing expression");
  154. YYABORT;
  155. }
  156. $$ = NEW_EX();
  157. AS_EX($$)->type = EX_SETINDEX;
  158. AS_EX($$)->setindex = NEW(setindex_node);
  159. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  160. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  161. AS_EX($$)->setindex->value = $3;
  162. ex_free(AS_EX($1));
  163. }
  164. | ex_index_expr ASSIGNPLUS expr {
  165. if(AS_EX($1)->type != EX_INDEX) {
  166. yyerror("Assigning to non-indexing expression");
  167. YYABORT;
  168. }
  169. $$ = NEW_EX();
  170. AS_EX($$)->type = EX_SETINDEX;
  171. AS_EX($$)->setindex = NEW(setindex_node);
  172. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  173. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  174. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_ADD, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  175. ex_free(AS_EX($1));
  176. }
  177. | ex_index_expr ASSIGNMINUS expr {
  178. if(AS_EX($1)->type != EX_INDEX) {
  179. yyerror("Assigning to non-indexing expression");
  180. YYABORT;
  181. }
  182. $$ = NEW_EX();
  183. AS_EX($$)->type = EX_SETINDEX;
  184. AS_EX($$)->setindex = NEW(setindex_node);
  185. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  186. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  187. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_SUB, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  188. ex_free(AS_EX($1));
  189. }
  190. | ex_index_expr ASSIGNSTAR expr {
  191. if(AS_EX($1)->type != EX_INDEX) {
  192. yyerror("Assigning to non-indexing expression");
  193. YYABORT;
  194. }
  195. $$ = NEW_EX();
  196. AS_EX($$)->type = EX_SETINDEX;
  197. AS_EX($$)->setindex = NEW(setindex_node);
  198. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  199. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  200. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_MUL, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  201. ex_free(AS_EX($1));
  202. }
  203. | ex_index_expr ASSIGNSLASH expr {
  204. if(AS_EX($1)->type != EX_INDEX) {
  205. yyerror("Assigning to non-indexing expression");
  206. YYABORT;
  207. }
  208. $$ = NEW_EX();
  209. AS_EX($$)->type = EX_SETINDEX;
  210. AS_EX($$)->setindex = NEW(setindex_node);
  211. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  212. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  213. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_DIV, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  214. ex_free(AS_EX($1));
  215. }
  216. | ex_index_expr ASSIGNDSTAR expr {
  217. if(AS_EX($1)->type != EX_INDEX) {
  218. yyerror("Assigning to non-indexing expression");
  219. YYABORT;
  220. }
  221. $$ = NEW_EX();
  222. AS_EX($$)->type = EX_SETINDEX;
  223. AS_EX($$)->setindex = NEW(setindex_node);
  224. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  225. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  226. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_POW, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  227. ex_free(AS_EX($1));
  228. }
  229. | ex_index_expr ASSIGNBAND expr {
  230. if(AS_EX($1)->type != EX_INDEX) {
  231. yyerror("Assigning to non-indexing expression");
  232. YYABORT;
  233. }
  234. $$ = NEW_EX();
  235. AS_EX($$)->type = EX_SETINDEX;
  236. AS_EX($$)->setindex = NEW(setindex_node);
  237. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  238. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  239. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_BAND, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  240. ex_free(AS_EX($1));
  241. }
  242. | ex_index_expr ASSIGNBOR expr {
  243. if(AS_EX($1)->type != EX_INDEX) {
  244. yyerror("Assigning to non-indexing expression");
  245. YYABORT;
  246. }
  247. $$ = NEW_EX();
  248. AS_EX($$)->type = EX_SETINDEX;
  249. AS_EX($$)->setindex = NEW(setindex_node);
  250. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  251. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  252. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_BOR, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  253. ex_free(AS_EX($1));
  254. }
  255. | ex_index_expr ASSIGNBXOR expr {
  256. if(AS_EX($1)->type != EX_INDEX) {
  257. yyerror("Assigning to non-indexing expression");
  258. YYABORT;
  259. }
  260. $$ = NEW_EX();
  261. AS_EX($$)->type = EX_SETINDEX;
  262. AS_EX($$)->setindex = NEW(setindex_node);
  263. AS_EX($$)->setindex->expr = ex_copy(AS_EX($1)->index->expr);
  264. AS_EX($$)->setindex->index = ex_copy(AS_EX($1)->index->index);
  265. MAKE_IDX_BINOP(AS_EX($$)->setindex->value, OP_BXOR, AS_EX($1)->index->expr, AS_EX($1)->index->index, $3);
  266. ex_free(AS_EX($1));
  267. }
  268. | logic_expr { $$ = $1; }
  269. ;
  270. logic_expr:
  271. 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; }
  272. | 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; }
  273. | ulogic_expr { $$ = $1; }
  274. ;
  275. ulogic_expr:
  276. 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; }
  277. | rel_expr { $$ = $1; }
  278. ;
  279. rel_expr:
  280. 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; }
  281. | 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; }
  282. | 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; }
  283. | 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; }
  284. | 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; }
  285. | 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; }
  286. | term_expr { $$ = $1; }
  287. ;
  288. term_expr:
  289. 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; }
  290. | 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; }
  291. | factor_expr { $$ = $1; }
  292. ;
  293. factor_expr:
  294. 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; }
  295. | 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; }
  296. | 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; }
  297. | power_expr { $$ = $1; }
  298. ;
  299. power_expr:
  300. power_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; }
  301. | binary_expr { $$ = $1; }
  302. ;
  303. binary_expr:
  304. 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; }
  305. | 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; }
  306. | 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; }
  307. | 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; }
  308. | 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; }
  309. | ubinary_expr { $$ = $1; }
  310. ;
  311. ubinary_expr:
  312. 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; }
  313. | ulen_expr { $$ = $1; }
  314. ;
  315. ulen_expr:
  316. 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; }
  317. | call_expr { $$ = $1; }
  318. ;
  319. call_expr:
  320. 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; }
  321. | call_expr COLON IDENT LPAREN expr_list RPAREN {
  322. $$ = NEW_EX();
  323. AS_EX($$)->type = EX_CALL;
  324. AS_EX($$)->call = NEW(call_node);
  325. AS_EX($$)->call->expr = NEW_EX();
  326. AS_EX($$)->call->expr->type = EX_INDEX;
  327. AS_EX($$)->call->expr->index = NEW(index_node);
  328. AS_EX($$)->call->expr->index->expr = $1;
  329. AS_EX($$)->call->expr->index->index = NEW_EX();
  330. AS_EX($$)->call->expr->index->index->type = EX_LIT;
  331. AS_EX($$)->call->expr->index->index->lit = NEW(lit_node);
  332. AS_EX($$)->call->expr->index->index->lit->type = LIT_STRING;
  333. AS_EX($$)->call->expr->index->index->lit->str = $3;
  334. AS_EX($$)->call->args = NEW(exprlist_node);
  335. AS_EX($$)->call->args->expr = ex_copy($1);
  336. AS_EX($$)->call->args->next = $5;
  337. }
  338. | funcdecl_expr { $$ = $1; }
  339. ;
  340. funcdecl_expr:
  341. FUNC IDENT LPAREN ident_list RPAREN stmt_list END {
  342. $$ = NEW_EX();
  343. AS_EX($$)->type = EX_FUNCDECL;
  344. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  345. AS_EX($$)->funcdecl->name = $2;
  346. AS_EX($$)->funcdecl->args = $4;
  347. AS_EX($$)->funcdecl->body = $6;
  348. }
  349. | FUNC LPAREN ident_list RPAREN stmt_list END {
  350. $$ = NEW_EX();
  351. AS_EX($$)->type = EX_FUNCDECL;
  352. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  353. AS_EX($$)->funcdecl->name = NULL;
  354. AS_EX($$)->funcdecl->args = $3;
  355. AS_EX($$)->funcdecl->body = $5;
  356. }
  357. | LAMBDA LPAREN ident_list RPAREN expr END {
  358. $$ = NEW_EX();
  359. AS_EX($$)->type = EX_FUNCDECL;
  360. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  361. AS_EX($$)->funcdecl->name = NULL;
  362. AS_EX($$)->funcdecl->args = $3;
  363. AS_EX($$)->funcdecl->body = NEW_ST();
  364. AS_EX($$)->funcdecl->body->type = ST_RET;
  365. AS_EX($$)->funcdecl->body->ret = NEW(ret_node);
  366. AS_EX($$)->funcdecl->body->ret->ret = $5;
  367. }
  368. | index_expr { $$ = $1; }
  369. ;
  370. index_expr:
  371. 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; }
  372. | expr DOT IDENT {
  373. $$ = NEW_EX();
  374. AS_EX($$)->type = EX_INDEX;
  375. AS_EX($$)->index = NEW(index_node);
  376. AS_EX($$)->index->expr = $1;
  377. AS_EX($$)->index->index = NEW_EX();
  378. AS_EX($$)->index->index->type = EX_LIT;
  379. AS_EX($$)->index->index->lit = NEW(lit_node);
  380. AS_EX($$)->index->index->lit->type = LIT_STRING;
  381. AS_EX($$)->index->index->lit->str = $3;
  382. }
  383. | ref_expr { $$ = $1; }
  384. ;
  385. ex_index_expr:
  386. 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; }
  387. | expr DOT IDENT {
  388. $$ = NEW_EX();
  389. AS_EX($$)->type = EX_INDEX;
  390. AS_EX($$)->index = NEW(index_node);
  391. AS_EX($$)->index->expr = $1;
  392. AS_EX($$)->index->index = NEW_EX();
  393. AS_EX($$)->index->index->type = EX_LIT;
  394. AS_EX($$)->index->index->lit = NEW(lit_node);
  395. AS_EX($$)->index->index->lit->type = LIT_STRING;
  396. AS_EX($$)->index->index->lit->str = $3;
  397. }
  398. ;
  399. ref_expr:
  400. IDENT { $$ = NEW_EX(); AS_EX($$)->type = EX_REF; AS_EX($$)->ref = NEW(ref_node); AS_EX($$)->ref->ident = $1; }
  401. | lit_expr { $$ = $1; }
  402. ;
  403. lit_expr:
  404. 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); }
  405. | 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); }
  406. | 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); }
  407. | 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; }
  408. | NONE { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_NONE; }
  409. | gen_expr { $$ = $1; }
  410. ;
  411. gen_expr:
  412. LBRACKET expr_list RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_LISTGEN; AS_EX($$)->listgen = NEW(listgen_node); AS_EX($$)->listgen->list = $2; }
  413. | LBRACE assoc_list RBRACE { $$ = NEW_EX(); AS_EX($$)->type = EX_MAPGEN; AS_EX($$)->mapgen = NEW(mapgen_node); AS_EX($$)->mapgen->map = $2; }
  414. | paren_expr { $$ = $1; }
  415. ;
  416. paren_expr:
  417. LPAREN expr RPAREN { $$ = $2; }
  418. ;
  419. expr_list:
  420. /*empty*/ { $$ = NULL; }
  421. | expr {
  422. $$ = NEW(exprlist_node);
  423. AS($$, exprlist_node)->expr = $1;
  424. AS($$, exprlist_node)->next = NULL;
  425. }
  426. | expr_list COMMA { $$ = $1; }
  427. | expr_list expr {
  428. exprlist_node *cur = $1;
  429. while(cur->next) cur = cur->next;
  430. cur->next = NEW(exprlist_node);
  431. cur = cur->next;
  432. cur->expr = $2;
  433. cur->next = NULL;
  434. $$ = $1;
  435. }
  436. ;
  437. ident_list:
  438. /*empty*/ { $$ = NULL; }
  439. | IDENT {
  440. $$ = NEW(identlist_node);
  441. AS($$, identlist_node)->ident = $1;
  442. AS($$, identlist_node)->next = NULL;
  443. }
  444. | ident_list COMMA { $$ = $1; }
  445. | ident_list IDENT {
  446. identlist_node *cur = $1;
  447. while(cur->next) cur = cur->next;
  448. cur->next = NEW(identlist_node);
  449. cur = cur->next;
  450. cur->ident = $2;
  451. cur->next = NULL;
  452. $$ = $1;
  453. }
  454. ;
  455. assoc_list:
  456. /*empty*/ { $$ = NULL; }
  457. | assoc_item {
  458. $$ = NEW(assoclist_node);
  459. AS($$, assoclist_node)->item = $1;
  460. AS($$, assoclist_node)->next = NULL;
  461. }
  462. | assoc_list COMMA { $$ = $1; }
  463. | assoc_list assoc_item {
  464. assoclist_node *cur = $1;
  465. while(cur->next) cur = cur->next;
  466. cur->next = NEW(assoclist_node);
  467. cur = cur->next;
  468. cur->item = $2;
  469. cur->next = NULL;
  470. $$ = $1;
  471. }
  472. ;
  473. assoc_item:
  474. LBRACKET expr RBRACKET ASSIGN expr {
  475. $$ = NEW(associtem_node);
  476. AS($$, associtem_node)->key = $2;
  477. AS($$, associtem_node)->value = $5;
  478. }
  479. | IDENT ASSIGN expr {
  480. $$ = NEW(associtem_node);
  481. AS($$, associtem_node)->key = NEW_EX();
  482. AS($$, associtem_node)->key->type = EX_LIT;
  483. AS($$, associtem_node)->key->lit = NEW(lit_node);
  484. AS($$, associtem_node)->key->lit->type = LIT_STRING;
  485. AS($$, associtem_node)->key->lit->str = $1;
  486. AS($$, associtem_node)->value = $3;
  487. }
  488. ;
  489. %%
  490. // TODO