locations
[ccc.git] / parse.y
diff --git a/parse.y b/parse.y
index 7cad6e5..dcfbad6 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -5,8 +5,8 @@
 #include "y.tab.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);
@@ -35,11 +35,10 @@ int yywrap()
 %token <expr> BOOL CHAR INTEGER
 %token ARROW ASSIGN BCLOSE BINAND BINOR BOPEN CCLOSE COMMA CONS COPEN DIVIDE
 %token DOT ELSE ERROR IF INVERSE MINUS MODULO NIL PLUS POWER RETURN SEMICOLON
-%token SCLOSE SOPEN TIMES VAR WHILE
+%token SCLOSE SOPEN TIMES TBOOL TCHAR TINT TVOID VAR WHILE
 
 %parse-param { struct ast **result }
 
-%right ARROW
 %right BINOR
 %right BINAND
 %nonassoc EQ NEQ LEQ LE GEQ GE
@@ -60,13 +59,12 @@ int yywrap()
 
 start : decls { *result = ast($1); } ;
 decls
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | decls vardecl { $$ = list_cons(decl_var($2), $1); }
        | decls fundecl { $$ = list_cons($2, $1); }
        ;
 vardecl
        : VAR IDENT ASSIGN expr SEMICOLON { $$ = vardecl(NULL, $2, $4); }
-       | type IDENT ASSIGN expr SEMICOLON { $$ = vardecl($1, $2, $4); }
        ;
 fundecl
        : IDENT BOPEN args BCLOSE CONS CONS funtype ARROW type COPEN vardecls body CCLOSE
@@ -75,21 +73,24 @@ fundecl
                { $$ = decl_fun($1, $3, NULL, NULL, $6, $7); }
        ;
 vardecls
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | vardecls vardecl { $$ = list_cons($2, $1); }
        ;
 funtype
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | funtype type { $$ = list_cons($2, $1); }
+       | funtype IDENT { $$ = list_cons(type_var($2), $1); }
        ;
 type
        : BOPEN type COMMA type BCLOSE { $$ = type_tuple($2, $4); }
-       | BOPEN type BCLOSE { $$ = $2; }
        | SOPEN type SCLOSE { $$ = type_list($2); }
-       | IDENT { $$ = type_var($1); }
+       | TBOOL { $$ = type_basic(btbool); }
+       | TCHAR { $$ = type_basic(btchar); }
+       | TINT { $$ = type_basic(btint); }
+       | TVOID { $$ = type_basic(btvoid); }
        ;
 args
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | nargs
        ;
 nargs
@@ -97,7 +98,7 @@ nargs
        | IDENT { $$ = list_cons($1, NULL); }
        ;
 fargs
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | fnargs
        ;
 fnargs
@@ -105,7 +106,7 @@ fnargs
        | expr { $$ = list_cons($1, NULL); }
        ;
 body
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | body stmt { $$ = list_cons($2, $1); }
        ;
 stmt
@@ -113,13 +114,13 @@ stmt
                { $$ = stmt_if($3, $6, $10); }
        | WHILE BOPEN expr BCLOSE COPEN body CCLOSE
                { $$ = stmt_while($3, $6); }
-       | expr SEMICOLON { $$ = stmt_expr($1); }
        | IDENT ASSIGN expr SEMICOLON { $$ = stmt_assign($1, $3); }
        | RETURN expr SEMICOLON { $$ = stmt_return($2); }
        | RETURN SEMICOLON { $$ = stmt_return(NULL); }
+       | expr SEMICOLON { $$ = stmt_expr($1); }
        ;
 field
-       : { $$ = NULL; }
+       : /* empty */ { $$ = NULL; }
        | field DOT IDENT { $$ = list_cons($3, $1); }
 expr
        : expr BINOR expr { $$ = expr_binop($1, binor, $3); }
@@ -139,9 +140,9 @@ expr
        | expr POWER expr { $$ = expr_binop($1, power, $3); }
        | MINUS expr %prec TIMES { $$ = expr_unop(negate, $2); }
        | INVERSE expr %prec TIMES { $$ = expr_unop(inverse, $2); }
+       | IDENT BOPEN fargs BCLOSE { $$ = expr_funcall($1, $3); }
        | BOPEN expr COMMA expr BCLOSE { $$ = expr_tuple($2, $4); }
        | BOPEN expr BCLOSE { $$ = $2; }
-       | IDENT BOPEN fargs BCLOSE { $$ = expr_funcall($1, $3); }
        | INTEGER
        | BOOL
        | CHAR