From a97b50f497be846ae007cdba3ca0954b7cc0bedf Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 28 May 2020 14:44:12 -0700 Subject: [PATCH] Add parsing of let/in. --- code/compiler/12/ast.hpp | 4 ++-- code/compiler/12/parser.y | 23 ++++++++++++++--------- code/compiler/12/scanner.l | 2 ++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/code/compiler/12/ast.hpp b/code/compiler/12/ast.hpp index 68082ab..d3887bd 100644 --- a/code/compiler/12/ast.hpp +++ b/code/compiler/12/ast.hpp @@ -125,8 +125,8 @@ struct ast_let : public ast { definition_group definitions; ast_ptr in; - ast_let(ast_ptr i, definition_group g) - : in(std::move(i)), definitions(std::move(g)) {} + ast_let(definition_group g, ast_ptr i) + : definitions(std::move(g)), in(std::move(i)) {} void print(int indent, std::ostream& to) const; void find_free(type_mgr& mgr, type_env_ptr& env, std::set& into); diff --git a/code/compiler/12/parser.y b/code/compiler/12/parser.y index 5256fae..bae7ad0 100644 --- a/code/compiler/12/parser.y +++ b/code/compiler/12/parser.y @@ -22,6 +22,8 @@ extern yy::parser::symbol_type yylex(); %token DATA %token CASE %token OF +%token LET +%token IN %token OCURLY %token CCURLY %token OPAREN @@ -40,8 +42,9 @@ extern yy::parser::symbol_type yylex(); %type > branches %type > constructors %type > typeList +%type definitions %type type nonArrowType typeListElement -%type aAdd aMul case app appBase +%type aAdd aMul case let app appBase %type data %type defn %type branch @@ -53,17 +56,13 @@ extern yy::parser::symbol_type yylex(); %% program - : definitions { } + : definitions { global_defs = std::move($1); } ; definitions - : definitions definition { } - | definition { } - ; - -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); } + : definitions defn { $$ = std::move($1); auto name = $2->name; $$.defs_defn[name] = std::move($2); } + | definitions data { $$ = std::move($1); auto name = $2->name; $$.defs_data[name] = std::move($2); } + | %empty { $$ = definition_group(); } ; defn @@ -100,6 +99,12 @@ appBase | UID { $$ = ast_ptr(new ast_uid(std::move($1))); } | OPAREN aAdd CPAREN { $$ = std::move($2); } | 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 diff --git a/code/compiler/12/scanner.l b/code/compiler/12/scanner.l index c8a4429..c417de1 100644 --- a/code/compiler/12/scanner.l +++ b/code/compiler/12/scanner.l @@ -22,6 +22,8 @@ defn { return yy::parser::make_DEFN(); } data { return yy::parser::make_DATA(); } case { return yy::parser::make_CASE(); } 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_CCURLY(); } \( { return yy::parser::make_OPAREN(); }