fix pretty printing and make commandline interface
[ccc.git] / parse.y
diff --git a/parse.y b/parse.y
index ee3a55a..6229385 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -2,15 +2,15 @@
 #include <stdio.h>
 
 #include "ast.h"
-#include "y.tab.h"
+#include "parse.h"
 
 int yylex(void);
 extern YYLTYPE yylloc;
 
 void yyerror(struct ast **result, const char *str)
 {
-       fprintf(stderr, "%d-%d: %s\n", yylloc.first_line, yylloc.last_column, str);
        (void)result;
+       fprintf(stderr, "%d-%d: %s\n", yylloc.first_line, yylloc.last_column, str);
 }
 
 int yywrap()
@@ -20,6 +20,9 @@ int yywrap()
 
 %}
 
+%define parse.lac full
+%define parse.error verbose
+
 %union {
        struct expr *expr;
        struct stmt *stmt;
@@ -50,7 +53,7 @@ int yywrap()
 %type <ast> start
 %type <decl> fundecl
 %type <expr> expr
-%type <list> args body decls fargs field fnargs nargs funtype vardecls bbody
+%type <list> args body decls fargs field fnargs nargs funtype bbody
 %type <stmt> stmt
 %type <type> type ftype
 %type <vardecl> vardecl
@@ -68,21 +71,17 @@ vardecl
        | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
        ;
 fundecl
-       : IDENT BOPEN args BCLOSE CONS CONS funtype ARROW ftype COPEN vardecls body CCLOSE
-               { $$ = decl_fun($1, $3, $7, $9, $11, $12); }
-       | IDENT BOPEN args BCLOSE COPEN vardecls body CCLOSE
-               { $$ = decl_fun($1, $3, NULL, NULL, $6, $7); }
-       ;
-vardecls
-       : /* empty */ { $$ = NULL; }
-       | vardecls vardecl { $$ = list_cons($2, $1); }
+       : IDENT BOPEN args BCLOSE COPEN body CCLOSE
+               { $$ = decl_fun($1, $3, NULL, NULL, $6); }
+       | IDENT BOPEN args BCLOSE CONS CONS funtype ARROW ftype COPEN body CCLOSE
+               { $$ = decl_fun($1, $3, $7, $9, $11); }
        ;
 funtype
        : /* empty */ { $$ = NULL; }
        | funtype ftype { $$ = list_cons($2, $1); }
        ;
 /* don't allow vardecls to be fully polymorph, this complicates parsing a lot */
-type 
+type
        : BOPEN ftype COMMA ftype BCLOSE { $$ = type_tuple($2, $4); }
        | SOPEN ftype SCLOSE { $$ = type_list($2); }
        | TBOOL { $$ = type_basic(btbool); }
@@ -91,12 +90,7 @@ type
        | TVOID { $$ = type_basic(btvoid); }
        ;
 ftype
-       : BOPEN ftype COMMA ftype BCLOSE { $$ = type_tuple($2, $4); }
-       | SOPEN ftype SCLOSE { $$ = type_list($2); }
-       | TBOOL { $$ = type_basic(btbool); }
-       | TCHAR { $$ = type_basic(btchar); }
-       | TINT { $$ = type_basic(btint); }
-       | TVOID { $$ = type_basic(btvoid); }
+       : type
     | IDENT { $$ = type_var($1); }
        ;
 args