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 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
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
54 %type <stmt> stmt
55 %type <type> type
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 ;
69 fundecl
70 : IDENT BOPEN args BCLOSE CONS CONS funtype ARROW type COPEN vardecls body CCLOSE
71 { $$ = decl_fun($1, $3, $7, $9, $11, $12); }
72 | IDENT BOPEN args BCLOSE COPEN vardecls body CCLOSE
73 { $$ = decl_fun($1, $3, NULL, NULL, $6, $7); }
74 ;
75 vardecls
76 : /* empty */ { $$ = NULL; }
77 | vardecls vardecl { $$ = list_cons($2, $1); }
78 ;
79 funtype
80 : /* empty */ { $$ = NULL; }
81 | funtype type { $$ = list_cons($2, $1); }
82 | funtype IDENT { $$ = list_cons(type_var($2), $1); }
83 ;
84 type
85 : BOPEN type COMMA type BCLOSE { $$ = type_tuple($2, $4); }
86 | SOPEN type 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 ;
92 args
93 : /* empty */ { $$ = NULL; }
94 | nargs
95 ;
96 nargs
97 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
98 | IDENT { $$ = list_cons($1, NULL); }
99 ;
100 fargs
101 : /* empty */ { $$ = NULL; }
102 | fnargs
103 ;
104 fnargs
105 : fnargs COMMA expr { $$ = list_cons($3, $1); }
106 | expr { $$ = list_cons($1, NULL); }
107 ;
108 body
109 : /* empty */ { $$ = NULL; }
110 | body stmt { $$ = list_cons($2, $1); }
111 ;
112 stmt
113 : IF BOPEN expr BCLOSE COPEN body CCLOSE ELSE COPEN body CCLOSE
114 { $$ = stmt_if($3, $6, $10); }
115 | WHILE BOPEN expr BCLOSE COPEN body CCLOSE
116 { $$ = stmt_while($3, $6); }
117 | IDENT ASSIGN expr SEMICOLON { $$ = stmt_assign($1, $3); }
118 | RETURN expr SEMICOLON { $$ = stmt_return($2); }
119 | RETURN SEMICOLON { $$ = stmt_return(NULL); }
120 | expr SEMICOLON { $$ = stmt_expr($1); }
121 ;
122 field
123 : /* empty */ { $$ = NULL; }
124 | field DOT IDENT { $$ = list_cons($3, $1); }
125 expr
126 : expr BINOR expr { $$ = expr_binop($1, binor, $3); }
127 | expr BINAND expr { $$ = expr_binop($1, binand, $3); }
128 | expr EQ expr { $$ = expr_binop($1, eq, $3); }
129 | expr NEQ expr { $$ = expr_binop($1, neq, $3); }
130 | expr LEQ expr { $$ = expr_binop($1, leq, $3); }
131 | expr LE expr { $$ = expr_binop($1, le, $3); }
132 | expr GEQ expr { $$ = expr_binop($1, geq, $3); }
133 | expr GE expr { $$ = expr_binop($1, ge, $3); }
134 | expr CONS expr { $$ = expr_binop($1, cons, $3); }
135 | expr PLUS expr { $$ = expr_binop($1, plus, $3); }
136 | expr MINUS expr { $$ = expr_binop($1, minus, $3); }
137 | expr TIMES expr { $$ = expr_binop($1, times, $3); }
138 | expr DIVIDE expr { $$ = expr_binop($1, divide, $3); }
139 | expr MODULO expr { $$ = expr_binop($1, modulo, $3); }
140 | expr POWER expr { $$ = expr_binop($1, power, $3); }
141 | MINUS expr %prec TIMES { $$ = expr_unop(negate, $2); }
142 | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2); }
143 | IDENT BOPEN fargs BCLOSE { $$ = expr_funcall($1, $3); }
144 | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4); }
145 | BOPEN expr BCLOSE { $$ = $2; }
146 | INTEGER
147 | BOOL
148 | CHAR
149 | IDENT field { $$ = expr_ident($1, $2); }
150 | NIL { $$ = expr_nil(); }
151 ;