10 void yyerror(struct ast **result, const char *str)
12 fprintf(stderr, "%d-%d: %s\n", yylloc.first_line, yylloc.last_column, str);
27 struct vardecl *vardecl;
35 %token <expr> BOOL CHAR INTEGER
36 %token ARROW ASSIGN BCLOSE BINAND BINOR BOPEN CCLOSE COMMA CONS COPEN DIVIDE
37 %token DOT ELSE ERROR IF INVERSE MINUS MODULO NIL PLUS POWER RETURN SEMICOLON
38 %token SCLOSE SOPEN TIMES VAR WHILE
40 %parse-param { struct ast **result }
45 %nonassoc EQ NEQ LEQ LE GEQ GE
48 %left TIMES DIVIDE MODULO
54 %type <list> args body decls fargs field fnargs nargs funtype vardecls
57 %type <vardecl> vardecl
61 start : decls { *result = ast($1); } ;
64 | decls vardecl { $$ = list_cons(decl_var($2), $1); }
65 | decls fundecl { $$ = list_cons($2, $1); }
68 : VAR IDENT ASSIGN expr SEMICOLON { $$ = vardecl(NULL, $2, $4); }
69 | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
72 : IDENT BOPEN args BCLOSE CONS CONS funtype ARROW type COPEN vardecls body CCLOSE
73 { $$ = decl_fun($1, $3, $7, $9, $11, $12); }
74 | IDENT BOPEN args BCLOSE COPEN vardecls body CCLOSE
75 { $$ = decl_fun($1, $3, NULL, NULL, $6, $7); }
79 | vardecls vardecl { $$ = list_cons($2, $1); }
83 | funtype type { $$ = list_cons($2, $1); }
86 : BOPEN type COMMA type BCLOSE { $$ = type_tuple($2, $4); }
87 | BOPEN type BCLOSE { $$ = $2; }
88 | SOPEN type SCLOSE { $$ = type_list($2); }
89 | IDENT { $$ = type_var($1); }
96 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
97 | IDENT { $$ = list_cons($1, NULL); }
104 : fnargs COMMA expr { $$ = list_cons($3, $1); }
105 | expr { $$ = list_cons($1, NULL); }
109 | body stmt { $$ = list_cons($2, $1); }
112 : IF BOPEN expr BCLOSE COPEN body CCLOSE ELSE COPEN body CCLOSE
113 { $$ = stmt_if($3, $6, $10); }
114 | WHILE BOPEN expr BCLOSE COPEN body CCLOSE
115 { $$ = stmt_while($3, $6); }
116 | expr SEMICOLON { $$ = stmt_expr($1); }
117 | IDENT ASSIGN expr SEMICOLON { $$ = stmt_assign($1, $3); }
118 | RETURN expr SEMICOLON { $$ = stmt_return($2); }
119 | RETURN SEMICOLON { $$ = stmt_return(NULL); }
123 | field DOT IDENT { $$ = list_cons($3, $1); }
125 : expr BINOR expr { $$ = expr_binop($1, binor, $3); }
126 | expr BINAND expr { $$ = expr_binop($1, binand, $3); }
127 | expr EQ expr { $$ = expr_binop($1, eq, $3); }
128 | expr NEQ expr { $$ = expr_binop($1, neq, $3); }
129 | expr LEQ expr { $$ = expr_binop($1, leq, $3); }
130 | expr LE expr { $$ = expr_binop($1, le, $3); }
131 | expr GEQ expr { $$ = expr_binop($1, geq, $3); }
132 | expr GE expr { $$ = expr_binop($1, ge, $3); }
133 | expr CONS expr { $$ = expr_binop($1, cons, $3); }
134 | expr PLUS expr { $$ = expr_binop($1, plus, $3); }
135 | expr MINUS expr { $$ = expr_binop($1, minus, $3); }
136 | expr TIMES expr { $$ = expr_binop($1, times, $3); }
137 | expr DIVIDE expr { $$ = expr_binop($1, divide, $3); }
138 | expr MODULO expr { $$ = expr_binop($1, modulo, $3); }
139 | expr POWER expr { $$ = expr_binop($1, power, $3); }
140 | MINUS expr %prec TIMES { $$ = expr_unop(negate, $2); }
141 | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2); }
142 | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4); }
143 | BOPEN expr BCLOSE { $$ = $2; }
144 | IDENT BOPEN fargs BCLOSE { $$ = expr_funcall($1, $3); }
148 | IDENT field { $$ = expr_ident($1, $2); }
149 | NIL { $$ = expr_nil(); }