#include <stdio.h>
#include "ast.h"
-#include "y.tab.h"
+#include "list.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()
%}
+%define parse.lac full
+%define parse.error verbose
+
%union {
struct expr *expr;
struct stmt *stmt;
struct list *list;
struct vardecl *vardecl;
- struct decl *decl;
+ struct fundecl *fundecl;
struct type *type;
char *ident;
}
%right POWER
%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
+%type <fundecl> fundecl
%%
decls
: /* empty */ { $$ = NULL; }
| decls vardecl { $$ = list_cons(decl_var($2), $1); }
- | decls fundecl { $$ = list_cons($2, $1); }
+ | decls fundecl { $$ = list_cons(decl_fun($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 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
+ { $$ = fundecl($1, $3, NULL, NULL, $6); }
+ | IDENT BOPEN args BCLOSE CONS CONS funtype ARROW ftype COPEN body CCLOSE
+ { $$ = fundecl($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); }
| 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