12 void yyerror(struct ast **result, const char *str)
15 fprintf(stderr, "error: %s\n", str);
30 struct vardecl vardecl;
35 //%define parse.error verbose
37 %token <expr> BOOL CHAR INTEGER
38 %token ASSIGN BCLOSE BINAND BINOR BOPEN CCLOSE COMMA CONS COPEN DIVIDE DOT ELSE
39 %token IF INVERSE MINUS MODULO NIL PLUS POWER RETURN SEMICOLON TIMES VAR WHILE
41 %parse-param { struct ast **result }
45 %nonassoc EQ NEQ LEQ LE GEQ GE
48 %left TIMES DIVIDE MODULO
53 %type <list> args body decls fargs field fnargs nargs
55 %type <vardecl> vardecl
60 start : decls { *result = ast($1); } ;
63 | decls vardecl SEMICOLON { $$ = list_cons(decl_var($2), $1); }
64 | decls fundecl { $$ = list_cons($2, $1); }
67 : VAR IDENT ASSIGN expr { $$ = vardecl($2, $4); }
70 : IDENT BOPEN args BCLOSE COPEN body CCLOSE
71 { $$ = decl_fun($1, $3, $6); }
78 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
79 | IDENT { $$ = list_cons($1, NULL); }
86 : fnargs COMMA expr { $$ = list_cons($3, $1); }
87 | expr { $$ = list_cons($1, NULL); }
91 | body stmt { $$ = list_cons($2, $1); }
94 : IF BOPEN expr BCLOSE COPEN body CCLOSE ELSE COPEN body CCLOSE
95 { $$ = stmt_if($3, $6, $10); }
96 | WHILE BOPEN expr BCLOSE COPEN body CCLOSE
97 { $$ = stmt_while($3, $6); }
98 | expr SEMICOLON { $$ = stmt_expr($1); }
99 | IDENT ASSIGN expr SEMICOLON { $$ = stmt_assign($1, $3); }
100 | RETURN expr SEMICOLON { $$ = stmt_return($2); }
101 | RETURN SEMICOLON { $$ = stmt_return(NULL); }
102 | vardecl SEMICOLON { $$ = stmt_vardecl($1); }
106 | field DOT IDENT { $$ = list_cons($3, $1); }
108 : expr BINOR expr { $$ = expr_binop($1, binor, $3); }
109 | expr BINAND expr { $$ = expr_binop($1, binand, $3); }
110 | expr EQ expr { $$ = expr_binop($1, eq, $3); }
111 | expr NEQ expr { $$ = expr_binop($1, neq, $3); }
112 | expr LEQ expr { $$ = expr_binop($1, leq, $3); }
113 | expr LE expr { $$ = expr_binop($1, le, $3); }
114 | expr GEQ expr { $$ = expr_binop($1, geq, $3); }
115 | expr GE expr { $$ = expr_binop($1, ge, $3); }
116 | expr CONS expr { $$ = expr_binop($1, cons, $3); }
117 | expr PLUS expr { $$ = expr_binop($1, plus, $3); }
118 | expr MINUS expr { $$ = expr_binop($1, minus, $3); }
119 | expr TIMES expr { $$ = expr_binop($1, times, $3); }
120 | expr DIVIDE expr { $$ = expr_binop($1, divide, $3); }
121 | expr MODULO expr { $$ = expr_binop($1, modulo, $3); }
122 | expr POWER expr { $$ = expr_binop($1, power, $3); }
123 | MINUS expr { $$ = expr_unop(negate, $2); }
124 | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2); }
125 | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4); }
126 | BOPEN expr BCLOSE { $$ = $2; }
127 | IDENT BOPEN fargs BCLOSE { $$ = expr_funcall($1, $3); }
131 | IDENT field { $$ = expr_ident($1, $2); }
132 | NIL { $$ = expr_nil(); }