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.

505 lines
19 KiB

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