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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. | MACRO LAMBDA any_lparen param_list RPAREN maybe_anno expr END {
  411. $$ = NEW_EX();
  412. AS_EX($$)->type = EX_FUNCDECL;
  413. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  414. AS_EX($$)->funcdecl->name = NULL;
  415. AS_EX($$)->funcdecl->params = $4;
  416. AS_EX($$)->funcdecl->anno = $6;
  417. AS_EX($$)->funcdecl->body = NEW_ST();
  418. AS_EX($$)->funcdecl->body->type = ST_RET;
  419. AS_EX($$)->funcdecl->body->ret = NEW(ret_node);
  420. AS_EX($$)->funcdecl->body->ret->ret = $7;
  421. AS_EX($$)->funcdecl->flags = FUNC_IS_MACRO;
  422. }
  423. | index_expr { $$ = $1; }
  424. ;
  425. index_expr:
  426. 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; }
  427. | expr DOT IDENT {
  428. $$ = NEW_EX();
  429. AS_EX($$)->type = EX_INDEX;
  430. AS_EX($$)->index = NEW(index_node);
  431. AS_EX($$)->index->expr = $1;
  432. AS_EX($$)->index->index = NEW_EX();
  433. AS_EX($$)->index->index->type = EX_LIT;
  434. AS_EX($$)->index->index->lit = NEW(lit_node);
  435. AS_EX($$)->index->index->lit->type = LIT_STRING;
  436. AS_EX($$)->index->index->lit->str = $3;
  437. }
  438. | ref_expr { $$ = $1; }
  439. ;
  440. ex_index_expr:
  441. 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; }
  442. | expr DOT IDENT {
  443. $$ = NEW_EX();
  444. AS_EX($$)->type = EX_INDEX;
  445. AS_EX($$)->index = NEW(index_node);
  446. AS_EX($$)->index->expr = $1;
  447. AS_EX($$)->index->index = NEW_EX();
  448. AS_EX($$)->index->index->type = EX_LIT;
  449. AS_EX($$)->index->index->lit = NEW(lit_node);
  450. AS_EX($$)->index->index->lit->type = LIT_STRING;
  451. AS_EX($$)->index->index->lit->str = $3;
  452. }
  453. ;
  454. ref_expr:
  455. IDENT { $$ = NEW_EX(); AS_EX($$)->type = EX_REF; AS_EX($$)->ref = NEW(ref_node); AS_EX($$)->ref->ident = $1; }
  456. | lit_expr { $$ = $1; }
  457. ;
  458. lit_expr:
  459. 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); }
  460. | 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); }
  461. | 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); }
  462. | 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; }
  463. | NONE { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_NONE; }
  464. | gen_expr { $$ = $1; }
  465. ;
  466. gen_expr:
  467. LBRACKET expr_list RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_LISTGEN; AS_EX($$)->listgen = NEW(listgen_node); AS_EX($$)->listgen->list = $2; }
  468. | LBRACE assoc_list RBRACE { $$ = NEW_EX(); AS_EX($$)->type = EX_MAPGEN; AS_EX($$)->mapgen = NEW(mapgen_node); AS_EX($$)->mapgen->map = $2; }
  469. | paren_expr { $$ = $1; }
  470. ;
  471. paren_expr:
  472. any_lparen expr RPAREN { $$ = $2; }
  473. ;
  474. expr_list:
  475. /*empty*/ { $$ = NULL; }
  476. | expr {
  477. $$ = NEW(exprlist_node);
  478. AS($$, exprlist_node)->expr = $1;
  479. AS($$, exprlist_node)->next = NULL;
  480. }
  481. | expr_list COMMA { $$ = $1; }
  482. | expr_list expr {
  483. exprlist_node *cur = $1;
  484. if(!cur) {
  485. cur = NEW(exprlist_node);
  486. cur->expr = $2;
  487. cur->next = NULL;
  488. $$ = cur;
  489. } else {
  490. while(cur->next) cur = cur->next;
  491. cur->next = NEW(exprlist_node);
  492. cur = cur->next;
  493. cur->expr = $2;
  494. cur->next = NULL;
  495. $$ = $1;
  496. }
  497. }
  498. ;
  499. ident_list:
  500. /*empty*/ { $$ = NULL; }
  501. | IDENT {
  502. $$ = NEW(identlist_node);
  503. AS($$, identlist_node)->ident = $1;
  504. AS($$, identlist_node)->next = NULL;
  505. }
  506. | ident_list COMMA { $$ = $1; }
  507. | ident_list IDENT {
  508. identlist_node *cur = $1;
  509. if(!cur) {
  510. cur = NEW(identlist_node);
  511. cur->ident = $2;
  512. cur->next = NULL;
  513. $$ = cur;
  514. } else {
  515. while(cur->next) cur = cur->next;
  516. cur->next = NEW(identlist_node);
  517. cur = cur->next;
  518. cur->ident = $2;
  519. cur->next = NULL;
  520. $$ = $1;
  521. }
  522. }
  523. ;
  524. param_list:
  525. /*empty*/ { $$ = NULL; }
  526. | param_list IDENT ASSIGN expr {
  527. paramlist_node *pl = $1;
  528. identlist_node *curk;
  529. exprlist_node *curv;
  530. if(!pl) {
  531. pl = NEW(paramlist_node);
  532. pl->args = NULL;
  533. pl->annos = NULL;
  534. pl->clkeys = NULL;
  535. pl->clvalues = NULL;
  536. pl->rest = NULL;
  537. }
  538. if(!pl->clkeys) {
  539. pl->clkeys = NEW(identlist_node);
  540. curk = pl->clkeys;
  541. pl->clvalues = NEW(exprlist_node);
  542. curv = pl->clvalues;
  543. } else {
  544. curk = pl->clkeys;
  545. curv = pl->clvalues;
  546. while(curk->next) {
  547. curk = curk->next;
  548. curv = curv->next;
  549. }
  550. curk->next = NEW(identlist_node);
  551. curk = curk->next;
  552. curv->next = NEW(exprlist_node);
  553. curv = curv->next;
  554. }
  555. curk->ident = $2;
  556. curk->next = NULL;
  557. curv->expr = $4;
  558. curv->next = NULL;
  559. $$ = pl;
  560. }
  561. | param_list STAR IDENT {
  562. paramlist_node *pl = $1;
  563. if(!pl) {
  564. pl = NEW(paramlist_node);
  565. pl->args = NULL;
  566. pl->annos = NULL;
  567. pl->clkeys = NULL;
  568. pl->clvalues = NULL;
  569. pl->rest = NULL;
  570. }
  571. pl-> rest = $3;
  572. $$ = pl;
  573. }
  574. | param_list IDENT maybe_anno {
  575. paramlist_node *pl = $1;
  576. identlist_node *cura;
  577. exprlist_node *curn;
  578. if(!pl) {
  579. pl = NEW(paramlist_node);
  580. pl->args = NULL;
  581. pl->annos = NULL;
  582. pl->clkeys = NULL;
  583. pl->clvalues = NULL;
  584. pl->rest = NULL;
  585. }
  586. if(!pl->args) {
  587. pl->args = NEW(identlist_node);
  588. cura = pl->args;
  589. pl->annos = NEW(exprlist_node);
  590. curn = pl->annos;
  591. } else {
  592. cura = pl->args;
  593. curn = pl->annos;
  594. while(cura->next) {
  595. cura = cura->next;
  596. curn = curn->next;
  597. }
  598. cura->next = NEW(identlist_node);
  599. cura = cura->next;
  600. curn->next = NEW(exprlist_node);
  601. curn = curn->next;
  602. }
  603. cura->ident = $2;
  604. cura->next = NULL;
  605. curn->expr = $3;
  606. curn->next = NULL;
  607. $$ = pl;
  608. }
  609. | param_list COMMA { $$ = $1; }
  610. ;
  611. assoc_list:
  612. /*empty*/ { $$ = NULL; }
  613. | assoc_item {
  614. $$ = NEW(assoclist_node);
  615. AS($$, assoclist_node)->item = $1;
  616. AS($$, assoclist_node)->next = NULL;
  617. }
  618. | assoc_list COMMA { $$ = $1; }
  619. | assoc_list assoc_item {
  620. assoclist_node *cur = $1;
  621. if(!cur) {
  622. cur = NEW(assoclist_node);
  623. cur->item = $2;
  624. cur->next = NULL;
  625. $$ = cur;
  626. } else {
  627. while(cur->next) cur = cur->next;
  628. cur->next = NEW(assoclist_node);
  629. cur = cur->next;
  630. cur->item = $2;
  631. cur->next = NULL;
  632. $$ = $1;
  633. }
  634. }
  635. ;
  636. assoc_item:
  637. LBRACKET expr RBRACKET ASSIGN expr {
  638. $$ = NEW(associtem_node);
  639. AS($$, associtem_node)->key = $2;
  640. AS($$, associtem_node)->value = $5;
  641. }
  642. | IDENT ASSIGN expr {
  643. $$ = NEW(associtem_node);
  644. AS($$, associtem_node)->key = NEW_EX();
  645. AS($$, associtem_node)->key->type = EX_LIT;
  646. AS($$, associtem_node)->key->lit = NEW(lit_node);
  647. AS($$, associtem_node)->key->lit->type = LIT_STRING;
  648. AS($$, associtem_node)->key->lit->str = $1;
  649. AS($$, associtem_node)->value = $3;
  650. }
  651. ;
  652. any_lparen:
  653. LPAREN
  654. | BLPAREN
  655. ;
  656. maybe_anno:
  657. COLON expr { $$ = $2; }
  658. | /* empty */ { $$ = NULL; }
  659. ;
  660. %%
  661. // TODO