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

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