Address listed flaws in implementation

This commit is contained in:
Danila Fedorin 2019-08-06 14:24:26 -07:00
parent 34e967f364
commit 8450e2c35d
4 changed files with 47 additions and 11 deletions

View File

@ -24,6 +24,16 @@ struct branch {
using branch_ptr = std::unique_ptr<branch>;
struct constructor {
std::string name;
std::vector<std::string> types;
constructor(std::string n, std::vector<std::string> ts)
: name(std::move(n)), types(std::move(ts)) {}
};
using constructor_ptr = std::unique_ptr<constructor>;
struct definition {
virtual ~definition() = default;
};
@ -108,3 +118,11 @@ struct definition_defn : public definition {
}
};
struct definition_data : public definition {
std::string name;
std::vector<constructor_ptr> constructors;
definition_data(std::string n, std::vector<constructor_ptr> cs)
: name(std::move(n)), constructors(std::move(cs)) {}
};

View File

@ -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<definition_ptr> program;

View File

@ -35,10 +35,12 @@ extern yy::parser::symbol_type yylex();
%type <std::vector<std::string>> lowercaseParams
%type <std::vector<definition_ptr>> program definitions
%type <std::vector<branch_ptr>> branches
%type <std::vector<constructor_ptr>> constructors
%type <ast_ptr> aAdd aMul case app appBase
%type <definition_ptr> definition
%type <definition_ptr> definition defn data
%type <branch_ptr> branch
%type <pattern_ptr> pattern
%type <constructor_ptr> 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<constructor_ptr>(); $$.push_back(std::move($1)); }
;
constructor
: UID lowercaseParams
{ $$ = constructor_ptr(new constructor(std::move($1), std::move($2))); }
;

View File

@ -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.