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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. %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); }
  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); }
  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; }
  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 = NEW_EX();
  331. AS_EX($$)->call->expr->type = EX_INDEX;
  332. AS_EX($$)->call->expr->index = NEW(index_node);
  333. AS_EX($$)->call->expr->index->expr = $1;
  334. AS_EX($$)->call->expr->index->index = NEW_EX();
  335. AS_EX($$)->call->expr->index->index->type = EX_LIT;
  336. AS_EX($$)->call->expr->index->index->lit = NEW(lit_node);
  337. AS_EX($$)->call->expr->index->index->lit->type = LIT_STRING;
  338. AS_EX($$)->call->expr->index->index->lit->str = $3;
  339. AS_EX($$)->call->args = NEW(exprlist_node);
  340. AS_EX($$)->call->args->expr = ex_copy($1);
  341. AS_EX($$)->call->args->next = $5;
  342. }
  343. | funcdecl_expr { $$ = $1; }
  344. ;
  345. funcdecl_expr:
  346. FUNC IDENT LPAREN ident_list RPAREN stmt_list END {
  347. $$ = NEW_EX();
  348. AS_EX($$)->type = EX_FUNCDECL;
  349. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  350. AS_EX($$)->funcdecl->name = $2;
  351. AS_EX($$)->funcdecl->args = $4;
  352. AS_EX($$)->funcdecl->body = $6;
  353. }
  354. | FUNC LPAREN ident_list RPAREN stmt_list END {
  355. $$ = NEW_EX();
  356. AS_EX($$)->type = EX_FUNCDECL;
  357. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  358. AS_EX($$)->funcdecl->name = NULL;
  359. AS_EX($$)->funcdecl->args = $3;
  360. AS_EX($$)->funcdecl->body = $5;
  361. }
  362. | LAMBDA LPAREN ident_list RPAREN expr END {
  363. $$ = NEW_EX();
  364. AS_EX($$)->type = EX_FUNCDECL;
  365. AS_EX($$)->funcdecl = NEW(funcdecl_node);
  366. AS_EX($$)->funcdecl->name = NULL;
  367. AS_EX($$)->funcdecl->args = $3;
  368. AS_EX($$)->funcdecl->body = NEW_ST();
  369. AS_EX($$)->funcdecl->body->type = ST_RET;
  370. AS_EX($$)->funcdecl->body->ret = NEW(ret_node);
  371. AS_EX($$)->funcdecl->body->ret->ret = $5;
  372. }
  373. | index_expr { $$ = $1; }
  374. ;
  375. index_expr:
  376. 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; }
  377. | expr DOT IDENT {
  378. $$ = NEW_EX();
  379. AS_EX($$)->type = EX_INDEX;
  380. AS_EX($$)->index = NEW(index_node);
  381. AS_EX($$)->index->expr = $1;
  382. AS_EX($$)->index->index = NEW_EX();
  383. AS_EX($$)->index->index->type = EX_LIT;
  384. AS_EX($$)->index->index->lit = NEW(lit_node);
  385. AS_EX($$)->index->index->lit->type = LIT_STRING;
  386. AS_EX($$)->index->index->lit->str = $3;
  387. }
  388. | ref_expr { $$ = $1; }
  389. ;
  390. ex_index_expr:
  391. 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; }
  392. | expr DOT IDENT {
  393. $$ = NEW_EX();
  394. AS_EX($$)->type = EX_INDEX;
  395. AS_EX($$)->index = NEW(index_node);
  396. AS_EX($$)->index->expr = $1;
  397. AS_EX($$)->index->index = NEW_EX();
  398. AS_EX($$)->index->index->type = EX_LIT;
  399. AS_EX($$)->index->index->lit = NEW(lit_node);
  400. AS_EX($$)->index->index->lit->type = LIT_STRING;
  401. AS_EX($$)->index->index->lit->str = $3;
  402. }
  403. ;
  404. ref_expr:
  405. IDENT { $$ = NEW_EX(); AS_EX($$)->type = EX_REF; AS_EX($$)->ref = NEW(ref_node); AS_EX($$)->ref->ident = $1; }
  406. | lit_expr { $$ = $1; }
  407. ;
  408. lit_expr:
  409. 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); }
  410. | 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); }
  411. | 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); }
  412. | 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; }
  413. | NONE { $$ = NEW_EX(); AS_EX($$)->type = EX_LIT; AS_EX($$)->lit = NEW(lit_node); AS_EX($$)->lit->type = LIT_NONE; }
  414. | gen_expr { $$ = $1; }
  415. ;
  416. gen_expr:
  417. LBRACKET expr_list RBRACKET { $$ = NEW_EX(); AS_EX($$)->type = EX_LISTGEN; AS_EX($$)->listgen = NEW(listgen_node); AS_EX($$)->listgen->list = $2; }
  418. | LBRACE assoc_list RBRACE { $$ = NEW_EX(); AS_EX($$)->type = EX_MAPGEN; AS_EX($$)->mapgen = NEW(mapgen_node); AS_EX($$)->mapgen->map = $2; }
  419. | paren_expr { $$ = $1; }
  420. ;
  421. paren_expr:
  422. LPAREN expr RPAREN { $$ = $2; }
  423. ;
  424. expr_list:
  425. /*empty*/ { $$ = NULL; }
  426. | expr {
  427. $$ = NEW(exprlist_node);
  428. AS($$, exprlist_node)->expr = $1;
  429. AS($$, exprlist_node)->next = NULL;
  430. }
  431. | expr_list COMMA { $$ = $1; }
  432. | expr_list expr {
  433. exprlist_node *cur = $1;
  434. while(cur->next) cur = cur->next;
  435. cur->next = NEW(exprlist_node);
  436. cur = cur->next;
  437. cur->expr = $2;
  438. cur->next = NULL;
  439. $$ = $1;
  440. }
  441. ;
  442. ident_list:
  443. /*empty*/ { $$ = NULL; }
  444. | IDENT {
  445. $$ = NEW(identlist_node);
  446. AS($$, identlist_node)->ident = $1;
  447. AS($$, identlist_node)->next = NULL;
  448. }
  449. | ident_list COMMA { $$ = $1; }
  450. | ident_list IDENT {
  451. identlist_node *cur = $1;
  452. while(cur->next) cur = cur->next;
  453. cur->next = NEW(identlist_node);
  454. cur = cur->next;
  455. cur->ident = $2;
  456. cur->next = NULL;
  457. $$ = $1;
  458. }
  459. ;
  460. assoc_list:
  461. /*empty*/ { $$ = NULL; }
  462. | assoc_item {
  463. $$ = NEW(assoclist_node);
  464. AS($$, assoclist_node)->item = $1;
  465. AS($$, assoclist_node)->next = NULL;
  466. }
  467. | assoc_list COMMA { $$ = $1; }
  468. | assoc_list assoc_item {
  469. assoclist_node *cur = $1;
  470. while(cur->next) cur = cur->next;
  471. cur->next = NEW(assoclist_node);
  472. cur = cur->next;
  473. cur->item = $2;
  474. cur->next = NULL;
  475. $$ = $1;
  476. }
  477. ;
  478. assoc_item:
  479. LBRACKET expr RBRACKET ASSIGN expr {
  480. $$ = NEW(associtem_node);
  481. AS($$, associtem_node)->key = $2;
  482. AS($$, associtem_node)->value = $5;
  483. }
  484. | IDENT ASSIGN expr {
  485. $$ = NEW(associtem_node);
  486. AS($$, associtem_node)->key = NEW_EX();
  487. AS($$, associtem_node)->key->type = EX_LIT;
  488. AS($$, associtem_node)->key->lit = NEW(lit_node);
  489. AS($$, associtem_node)->key->lit->type = LIT_STRING;
  490. AS($$, associtem_node)->key->lit->str = $1;
  491. AS($$, associtem_node)->value = $3;
  492. }
  493. ;
  494. %%
  495. // TODO