types and locations
[ccc.git] / parse.y
1 %{
2 #include <stdio.h>
3
4 #include "ast.h"
5 #include "y.tab.h"
6
7 int yylex(void);
8
9 extern YYLTYPE yylloc;
10 void yyerror(struct ast **result, const char *str)
11 {
12 fprintf(stderr, "%d-%d: %s\n", yylloc.first_line, yylloc.last_column, str);
13 (void)result;
14 }
15
16 int yywrap()
17 {
18 return 1;
19 }
20
21 %}
22
23 %union {
24 struct expr *expr;
25 struct stmt *stmt;
26 struct list *list;
27 struct vardecl *vardecl;
28 struct decl *decl;
29 struct type *type;
30 char *ident;
31 }
32
33 %locations
34 %token <ident> IDENT
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
39
40 %parse-param { struct ast **result }
41
42 %right ARROW
43 %right BINOR
44 %right BINAND
45 %nonassoc EQ NEQ LEQ LE GEQ GE
46 %right CONS
47 %left PLUS MINUS
48 %left TIMES DIVIDE MODULO
49 %right POWER
50
51 %type <ast> start
52 %type <decl> fundecl
53 %type <expr> expr
54 %type <list> args body decls fargs field fnargs nargs funtype vardecls
55 %type <stmt> stmt
56 %type <type> type
57 %type <vardecl> vardecl
58
59 %%
60
61 start : decls { *result = ast($1); } ;
62 decls
63 : { $$ = NULL; }
64 | decls vardecl { $$ = list_cons(decl_var($2), $1); }
65 | decls fundecl { $$ = list_cons($2, $1); }
66 ;
67 vardecl
68 : VAR IDENT ASSIGN expr SEMICOLON { $$ = vardecl(NULL, $2, $4); }
69 | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
70 ;
71 fundecl
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); }
76 ;
77 vardecls
78 : { $$ = NULL; }
79 | vardecls vardecl { $$ = list_cons($2, $1); }
80 ;
81 funtype
82 : { $$ = NULL; }
83 | funtype type { $$ = list_cons($2, $1); }
84 ;
85 type
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); }
90 ;
91 args
92 : { $$ = NULL; }
93 | nargs
94 ;
95 nargs
96 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
97 | IDENT { $$ = list_cons($1, NULL); }
98 ;
99 fargs
100 : { $$ = NULL; }
101 | fnargs
102 ;
103 fnargs
104 : fnargs COMMA expr { $$ = list_cons($3, $1); }
105 | expr { $$ = list_cons($1, NULL); }
106 ;
107 body
108 : { $$ = NULL; }
109 | body stmt { $$ = list_cons($2, $1); }
110 ;
111 stmt
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); }
120 ;
121 field
122 : { $$ = NULL; }
123 | field DOT IDENT { $$ = list_cons($3, $1); }
124 expr
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); }
145 | INTEGER
146 | BOOL
147 | CHAR
148 | IDENT field { $$ = expr_ident($1, $2); }
149 | NIL { $$ = expr_nil(); }
150 ;