Add parsing of let/in.

This commit is contained in:
Danila Fedorin 2020-05-28 14:44:12 -07:00
parent c84ff11d0d
commit a97b50f497
3 changed files with 18 additions and 11 deletions

View File

@ -125,8 +125,8 @@ struct ast_let : public ast {
definition_group definitions; definition_group definitions;
ast_ptr in; ast_ptr in;
ast_let(ast_ptr i, definition_group g) ast_let(definition_group g, ast_ptr i)
: in(std::move(i)), definitions(std::move(g)) {} : definitions(std::move(g)), in(std::move(i)) {}
void print(int indent, std::ostream& to) const; void print(int indent, std::ostream& to) const;
void find_free(type_mgr& mgr, type_env_ptr& env, std::set<std::string>& into); void find_free(type_mgr& mgr, type_env_ptr& env, std::set<std::string>& into);

View File

@ -22,6 +22,8 @@ extern yy::parser::symbol_type yylex();
%token DATA %token DATA
%token CASE %token CASE
%token OF %token OF
%token LET
%token IN
%token OCURLY %token OCURLY
%token CCURLY %token CCURLY
%token OPAREN %token OPAREN
@ -40,8 +42,9 @@ extern yy::parser::symbol_type yylex();
%type <std::vector<branch_ptr>> branches %type <std::vector<branch_ptr>> branches
%type <std::vector<constructor_ptr>> constructors %type <std::vector<constructor_ptr>> constructors
%type <std::vector<parsed_type_ptr>> typeList %type <std::vector<parsed_type_ptr>> typeList
%type <definition_group> definitions
%type <parsed_type_ptr> type nonArrowType typeListElement %type <parsed_type_ptr> type nonArrowType typeListElement
%type <ast_ptr> aAdd aMul case app appBase %type <ast_ptr> aAdd aMul case let app appBase
%type <definition_data_ptr> data %type <definition_data_ptr> data
%type <definition_defn_ptr> defn %type <definition_defn_ptr> defn
%type <branch_ptr> branch %type <branch_ptr> branch
@ -53,17 +56,13 @@ extern yy::parser::symbol_type yylex();
%% %%
program program
: definitions { } : definitions { global_defs = std::move($1); }
; ;
definitions definitions
: definitions definition { } : definitions defn { $$ = std::move($1); auto name = $2->name; $$.defs_defn[name] = std::move($2); }
| definition { } | definitions data { $$ = std::move($1); auto name = $2->name; $$.defs_data[name] = std::move($2); }
; | %empty { $$ = definition_group(); }
definition
: defn { auto name = $1->name; global_defs.defs_defn[name] = std::move($1); }
| data { auto name = $1->name; global_defs.defs_data[name] = std::move($1); }
; ;
defn defn
@ -100,6 +99,12 @@ appBase
| UID { $$ = ast_ptr(new ast_uid(std::move($1))); } | UID { $$ = ast_ptr(new ast_uid(std::move($1))); }
| OPAREN aAdd CPAREN { $$ = std::move($2); } | OPAREN aAdd CPAREN { $$ = std::move($2); }
| case { $$ = std::move($1); } | case { $$ = std::move($1); }
| let { $$ = std::move($1); }
;
let
: LET OCURLY definitions CCURLY IN OCURLY aAdd CCURLY
{ $$ = ast_ptr(new ast_let(std::move($3), std::move($7))); }
; ;
case case

View File

@ -22,6 +22,8 @@ defn { return yy::parser::make_DEFN(); }
data { return yy::parser::make_DATA(); } data { return yy::parser::make_DATA(); }
case { return yy::parser::make_CASE(); } case { return yy::parser::make_CASE(); }
of { return yy::parser::make_OF(); } of { return yy::parser::make_OF(); }
let { return yy::parser::make_LET(); }
in { return yy::parser::make_IN(); }
\{ { return yy::parser::make_OCURLY(); } \{ { return yy::parser::make_OCURLY(); }
\} { return yy::parser::make_CCURLY(); } \} { return yy::parser::make_CCURLY(); }
\( { return yy::parser::make_OPAREN(); } \( { return yy::parser::make_OPAREN(); }