From 8450e2c35dcb3e1cdbab183fe1cd660ad2038711 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 6 Aug 2019 14:24:26 -0700 Subject: [PATCH] Address listed flaws in implementation --- code/compiler/02/ast.hpp | 18 ++++++++++++++++++ code/compiler/02/main.cpp | 2 +- code/compiler/02/parser.y | 26 +++++++++++++++++++++++++- content/blog/02_compiler_parsing.md | 12 +++--------- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/code/compiler/02/ast.hpp b/code/compiler/02/ast.hpp index e734d78..70c2af9 100644 --- a/code/compiler/02/ast.hpp +++ b/code/compiler/02/ast.hpp @@ -24,6 +24,16 @@ struct branch { using branch_ptr = std::unique_ptr; +struct constructor { + std::string name; + std::vector types; + + constructor(std::string n, std::vector ts) + : name(std::move(n)), types(std::move(ts)) {} +}; + +using constructor_ptr = std::unique_ptr; + struct definition { virtual ~definition() = default; }; @@ -108,3 +118,11 @@ struct definition_defn : public definition { } }; + +struct definition_data : public definition { + std::string name; + std::vector constructors; + + definition_data(std::string n, std::vector cs) + : name(std::move(n)), constructors(std::move(cs)) {} +}; diff --git a/code/compiler/02/main.cpp b/code/compiler/02/main.cpp index ce3d541..dc9312a 100644 --- a/code/compiler/02/main.cpp +++ b/code/compiler/02/main.cpp @@ -2,7 +2,7 @@ #include "parser.hpp" void yy::parser::error(const std::string& msg) { - std::cout << "An error occured: " << std::endl; + std::cout << "An error occured: " << msg << std::endl; } extern std::vector program; diff --git a/code/compiler/02/parser.y b/code/compiler/02/parser.y index 986c8a5..d8c45b7 100644 --- a/code/compiler/02/parser.y +++ b/code/compiler/02/parser.y @@ -35,10 +35,12 @@ extern yy::parser::symbol_type yylex(); %type > lowercaseParams %type > program definitions %type > branches +%type > constructors %type aAdd aMul case app appBase -%type definition +%type definition defn data %type branch %type pattern +%type constructor %start program @@ -54,6 +56,11 @@ definitions ; definition + : defn { $$ = std::move($1); } + | data { $$ = std::move($1); } + ; + +defn : DEFN LID lowercaseParams EQUAL OCURLY aAdd CCURLY { $$ = definition_ptr( new definition_defn(std::move($2), std::move($3), std::move($6))); } @@ -109,3 +116,20 @@ pattern | UID lowercaseParams { $$ = pattern_ptr(new pattern_constr(std::move($1), std::move($2))); } ; + +data + : DATA UID EQUAL OCURLY constructors CCURLY + { $$ = definition_ptr(new definition_data(std::move($2), std::move($5))); } + ; + +constructors + : constructors COMMA constructor { $$ = std::move($1); $$.push_back(std::move($3)); } + | constructor + { $$ = std::vector(); $$.push_back(std::move($1)); } + ; + +constructor + : UID lowercaseParams + { $$ = constructor_ptr(new constructor(std::move($1), std::move($2))); } + ; + diff --git a/content/blog/02_compiler_parsing.md b/content/blog/02_compiler_parsing.md index 871a71c..3364bba 100644 --- a/content/blog/02_compiler_parsing.md +++ b/content/blog/02_compiler_parsing.md @@ -288,12 +288,6 @@ wrong: ./a.out }{ ``` -We are told an error occured. Excellent! - -There's still a number of flaws with our parser: - -2. We don't print errors properly. -3. We also have no way of verifying our tree was built correctly. -1. We're missing the data declaration, from both our C++ source and from the Bison grammars. - -This post is getting a little long, so we will revisit the parser in the next one. See you then! +We are told an error occured. Excellent! We're not really sure what our tree looks like, though. +We just know there's __stuff__ in the list of definitions. We will revisit our trees +in the next post, adding code to print them and to verify that our programs make some sense.