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.

parser.y 23KB

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