dc85dac7a9f58e3548a24d71d74c22e8c26bb22e
[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 extern YYLTYPE yylloc;
9
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 STRING
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 TBOOL TCHAR TINT TVOID VAR WHILE
39
40 %parse-param { struct ast **result }
41
42 %right BINOR
43 %right BINAND
44 %nonassoc EQ NEQ LEQ LE GEQ GE
45 %right CONS
46 %left PLUS MINUS
47 %left TIMES DIVIDE MODULO
48 %right POWER
49
50 %type <ast> start
51 %type <decl> fundecl
52 %type <expr> expr
53 %type <list> args body decls fargs field fnargs nargs funtype vardecls bbody
54 %type <stmt> stmt
55 %type <type> type ftype
56 %type <vardecl> vardecl
57
58 %%
59
60 start : decls { *result = ast($1); } ;
61 decls
62 : /* empty */ { $$ = NULL; }
63 | decls vardecl { $$ = list_cons(decl_var($2), $1); }
64 | decls fundecl { $$ = list_cons($2, $1); }
65 ;
66 vardecl
67 : VAR IDENT ASSIGN expr SEMICOLON { $$ = vardecl(NULL, $2, $4); }
68 | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
69 ;
70 fundecl
71 : IDENT BOPEN args BCLOSE CONS CONS funtype ARROW ftype COPEN vardecls body CCLOSE
72 { $$ = decl_fun($1, $3, $7, $9, $11, $12); }
73 | IDENT BOPEN args BCLOSE COPEN vardecls body CCLOSE
74 { $$ = decl_fun($1, $3, NULL, NULL, $6, $7); }
75 ;
76 vardecls
77 : /* empty */ { $$ = NULL; }
78 | vardecls vardecl { $$ = list_cons($2, $1); }
79 ;
80 funtype
81 : /* empty */ { $$ = NULL; }
82 | funtype ftype { $$ = list_cons($2, $1); }
83 ;
84 ftype
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); }
91 | IDENT { $$ = type_var($1); }
92 type
93 : BOPEN type COMMA type BCLOSE { $$ = type_tuple($2, $4); }
94 | SOPEN type SCLOSE { $$ = type_list($2); }
95 | TBOOL { $$ = type_basic(btbool); }
96 | TCHAR { $$ = type_basic(btchar); }
97 | TINT { $$ = type_basic(btint); }
98 | TVOID { $$ = type_basic(btvoid); }
99 ;
100 args
101 : /* empty */ { $$ = NULL; }
102 | nargs
103 ;
104 nargs
105 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
106 | IDENT { $$ = list_cons($1, NULL); }
107 ;
108 fargs
109 : /* empty */ { $$ = NULL; }
110 | fnargs
111 ;
112 fnargs
113 : fnargs COMMA expr { $$ = list_cons($3, $1); }
114 | expr { $$ = list_cons($1, NULL); }
115 ;
116 body
117 : /* empty */ { $$ = NULL; }
118 | body stmt { $$ = list_cons($2, $1); }
119 ;
120 field
121 : /* empty */ { $$ = NULL; }
122 | field DOT IDENT { $$ = list_cons($3, $1); }
123 ;
124 bbody
125 : COPEN body CCLOSE { $$ = $2; }
126 | stmt { $$ = list_cons($1, NULL); }
127 ;
128 stmt
129 : IF BOPEN expr BCLOSE bbody { $$ = stmt_if($3, $5, NULL); }
130 | IF BOPEN expr BCLOSE bbody ELSE bbody { $$ = stmt_if($3, $5, $7); }
131 | WHILE BOPEN expr BCLOSE bbody { $$ = stmt_while($3, $5); }
132 | IDENT field ASSIGN expr SEMICOLON { $$ = stmt_assign($1, $2, $4); }
133 | RETURN expr SEMICOLON { $$ = stmt_return($2); }
134 | RETURN SEMICOLON { $$ = stmt_return(NULL); }
135 | expr SEMICOLON { $$ = stmt_expr($1); }
136 ;
137 expr
138 : expr BINOR expr { $$ = expr_binop($1, binor, $3); }
139 | expr BINAND expr { $$ = expr_binop($1, binand, $3); }
140 | expr EQ expr { $$ = expr_binop($1, eq, $3); }
141 | expr NEQ expr { $$ = expr_binop($1, neq, $3); }
142 | expr LEQ expr { $$ = expr_binop($1, leq, $3); }
143 | expr LE expr { $$ = expr_binop($1, le, $3); }
144 | expr GEQ expr { $$ = expr_binop($1, geq, $3); }
145 | expr GE expr { $$ = expr_binop($1, ge, $3); }
146 | expr CONS expr { $$ = expr_binop($1, cons, $3); }
147 | expr PLUS expr { $$ = expr_binop($1, plus, $3); }
148 | expr MINUS expr { $$ = expr_binop($1, minus, $3); }
149 | expr TIMES expr { $$ = expr_binop($1, times, $3); }
150 | expr DIVIDE expr { $$ = expr_binop($1, divide, $3); }
151 | expr MODULO expr { $$ = expr_binop($1, modulo, $3); }
152 | expr POWER expr { $$ = expr_binop($1, power, $3); }
153 | MINUS expr %prec TIMES { $$ = expr_unop(negate, $2); }
154 | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2); }
155 | IDENT BOPEN fargs BCLOSE { $$ = expr_funcall($1, $3); }
156 | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4); }
157 | BOPEN expr BCLOSE { $$ = $2; }
158 | INTEGER
159 | BOOL
160 | CHAR
161 | STRING
162 | IDENT field { $$ = expr_ident($1, $2); }
163 | NIL { $$ = expr_nil(); }
164 ;