10 void yyerror(struct ast **result, const char *str)
13 fprintf(stderr, "%d-%d: %s\n", yylloc.first_line, yylloc.last_column, str);
23 %define parse.lac full
24 %define parse.error verbose
30 struct vardecl *vardecl;
38 %token <expr> BOOL CHAR INTEGER STRING
39 %token ARROW ASSIGN BCLOSE BINAND BINOR BOPEN CCLOSE COMMA CONS COPEN DIVIDE
40 %token DOT ELSE ERROR IF INVERSE MINUS MODULO NIL PLUS POWER RETURN SEMICOLON
41 %token SCLOSE SOPEN TIMES TBOOL TCHAR TINT TVOID VAR WHILE
43 %parse-param { struct ast **result }
47 %nonassoc EQ NEQ LEQ LE GEQ GE
50 %left TIMES DIVIDE MODULO
56 %type <list> args body decls fargs field fnargs nargs funtype bbody
58 %type <type> type ftype
59 %type <vardecl> vardecl
63 start : decls { *result = ast($1); } ;
65 : /* empty */ { $$ = NULL; }
66 | decls vardecl { $$ = list_cons(decl_var($2), $1); }
67 | decls fundecl { $$ = list_cons($2, $1); }
70 : VAR IDENT ASSIGN expr SEMICOLON { $$ = vardecl(NULL, $2, $4); }
71 | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
74 : IDENT BOPEN args BCLOSE COPEN body CCLOSE
75 { $$ = decl_fun($1, $3, NULL, NULL, $6); }
76 | IDENT BOPEN args BCLOSE CONS CONS funtype ARROW ftype COPEN body CCLOSE
77 { $$ = decl_fun($1, $3, $7, $9, $11); }
80 : /* empty */ { $$ = NULL; }
81 | funtype ftype { $$ = list_cons($2, $1); }
83 /* don't allow vardecls to be fully polymorph, this complicates parsing a lot */
85 : BOPEN ftype COMMA ftype BCLOSE { $$ = type_tuple($2, $4); }
86 | SOPEN ftype SCLOSE { $$ = type_list($2); }
87 | TBOOL { $$ = type_basic(btbool); }
88 | TCHAR { $$ = type_basic(btchar); }
89 | TINT { $$ = type_basic(btint); }
90 | TVOID { $$ = type_basic(btvoid); }
94 | IDENT { $$ = type_var($1); }
97 : /* empty */ { $$ = NULL; }
101 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
102 | IDENT { $$ = list_cons($1, NULL); }
105 : /* empty */ { $$ = NULL; }
109 : fnargs COMMA expr { $$ = list_cons($3, $1); }
110 | expr { $$ = list_cons($1, NULL); }
113 : /* empty */ { $$ = NULL; }
114 | body stmt { $$ = list_cons($2, $1); }
117 : /* empty */ { $$ = NULL; }
118 | field DOT IDENT { $$ = list_cons($3, $1); }
121 : COPEN body CCLOSE { $$ = $2; }
122 | stmt { $$ = list_cons($1, NULL); }
125 : IF BOPEN expr BCLOSE bbody { $$ = stmt_if($3, $5, NULL); }
126 | IF BOPEN expr BCLOSE bbody ELSE bbody { $$ = stmt_if($3, $5, $7); }
127 | WHILE BOPEN expr BCLOSE bbody { $$ = stmt_while($3, $5); }
128 | IDENT field ASSIGN expr SEMICOLON { $$ = stmt_assign($1, $2, $4); }
129 | RETURN expr SEMICOLON { $$ = stmt_return($2); }
130 | RETURN SEMICOLON { $$ = stmt_return(NULL); }
131 | vardecl { $$ = stmt_vardecl($1); }
132 | expr SEMICOLON { $$ = stmt_expr($1); }
135 : expr BINOR expr { $$ = expr_binop($1, binor, $3); }
136 | expr BINAND expr { $$ = expr_binop($1, binand, $3); }
137 | expr EQ expr { $$ = expr_binop($1, eq, $3); }
138 | expr NEQ expr { $$ = expr_binop($1, neq, $3); }
139 | expr LEQ expr { $$ = expr_binop($1, leq, $3); }
140 | expr LE expr { $$ = expr_binop($1, le, $3); }
141 | expr GEQ expr { $$ = expr_binop($1, geq, $3); }
142 | expr GE expr { $$ = expr_binop($1, ge, $3); }
143 | expr CONS expr { $$ = expr_binop($1, cons, $3); }
144 | expr PLUS expr { $$ = expr_binop($1, plus, $3); }
145 | expr MINUS expr { $$ = expr_binop($1, minus, $3); }
146 | expr TIMES expr { $$ = expr_binop($1, times, $3); }
147 | expr DIVIDE expr { $$ = expr_binop($1, divide, $3); }
148 | expr MODULO expr { $$ = expr_binop($1, modulo, $3); }
149 | expr POWER expr { $$ = expr_binop($1, power, $3); }
150 | MINUS expr %prec TIMES { $$ = expr_unop(negate, $2); }
151 | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2); }
152 | IDENT BOPEN fargs BCLOSE field { $$ = expr_funcall($1, $3, $5); }
153 | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4); }
154 | BOPEN expr BCLOSE { $$ = $2; }
159 | IDENT field { $$ = expr_ident($1, $2); }
160 | NIL { $$ = expr_nil(); }