fix pretty printing and make commandline interface
[ccc.git] / parse.y
1 %{
2 #include <stdio.h>
3
4 #include "ast.h"
5 #include "parse.h"
6
7 int yylex(void);
8 extern YYLTYPE yylloc;
9
10 void yyerror(struct ast **result, const char *str)
11 {
12 (void)result;
13 fprintf(stderr, "%d-%d: %s\n", yylloc.first_line, yylloc.last_column, str);
14 }
15
16 int yywrap()
17 {
18 return 1;
19 }
20
21 %}
22
23 %define parse.lac full
24 %define parse.error verbose
25
26 %union {
27 struct expr *expr;
28 struct stmt *stmt;
29 struct list *list;
30 struct vardecl *vardecl;
31 struct decl *decl;
32 struct type *type;
33 char *ident;
34 }
35
36 %locations
37 %token <ident> IDENT
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
42
43 %parse-param { struct ast **result }
44
45 %right BINOR
46 %right BINAND
47 %nonassoc EQ NEQ LEQ LE GEQ GE
48 %right CONS
49 %left PLUS MINUS
50 %left TIMES DIVIDE MODULO
51 %right POWER
52
53 %type <ast> start
54 %type <decl> fundecl
55 %type <expr> expr
56 %type <list> args body decls fargs field fnargs nargs funtype bbody
57 %type <stmt> stmt
58 %type <type> type ftype
59 %type <vardecl> vardecl
60
61 %%
62
63 start : decls { *result = ast($1); } ;
64 decls
65 : /* empty */ { $$ = NULL; }
66 | decls vardecl { $$ = list_cons(decl_var($2), $1); }
67 | decls fundecl { $$ = list_cons($2, $1); }
68 ;
69 vardecl
70 : VAR IDENT ASSIGN expr SEMICOLON { $$ = vardecl(NULL, $2, $4); }
71 | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
72 ;
73 fundecl
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); }
78 ;
79 funtype
80 : /* empty */ { $$ = NULL; }
81 | funtype ftype { $$ = list_cons($2, $1); }
82 ;
83 /* don't allow vardecls to be fully polymorph, this complicates parsing a lot */
84 type
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 ;
92 ftype
93 : type
94 | IDENT { $$ = type_var($1); }
95 ;
96 args
97 : /* empty */ { $$ = NULL; }
98 | nargs
99 ;
100 nargs
101 : nargs COMMA IDENT { $$ = list_cons($3, $1); }
102 | IDENT { $$ = list_cons($1, NULL); }
103 ;
104 fargs
105 : /* empty */ { $$ = NULL; }
106 | fnargs
107 ;
108 fnargs
109 : fnargs COMMA expr { $$ = list_cons($3, $1); }
110 | expr { $$ = list_cons($1, NULL); }
111 ;
112 body
113 : /* empty */ { $$ = NULL; }
114 | body stmt { $$ = list_cons($2, $1); }
115 ;
116 field
117 : /* empty */ { $$ = NULL; }
118 | field DOT IDENT { $$ = list_cons($3, $1); }
119 ;
120 bbody
121 : COPEN body CCLOSE { $$ = $2; }
122 | stmt { $$ = list_cons($1, NULL); }
123 ;
124 stmt
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); }
133 ;
134 expr
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; }
155 | INTEGER
156 | BOOL
157 | CHAR
158 | STRING
159 | IDENT field { $$ = expr_ident($1, $2); }
160 | NIL { $$ = expr_nil(); }
161 ;