I don't know C++. Improve constructors?

This commit is contained in:
Danila Fedorin 2019-08-05 00:08:13 -07:00
parent f42cb900cf
commit 43d23963e2
1 changed files with 60 additions and 26 deletions

View File

@ -3,30 +3,13 @@
#include <vector> #include <vector>
struct ast { struct ast {
virtual ~ast(); virtual ~ast() = default;
}; };
using ast_ptr = std::unique_ptr<ast>; using ast_ptr = std::unique_ptr<ast>;
struct pattern { struct pattern {
virtual ~pattern(); virtual ~pattern() = default;
};
struct pattern_var : public pattern {
std::string var;
pattern_var(const char* v)
: var(v) {}
};
struct pattern_constr : public pattern {
std::string constr;
std::vector<std::string> params;
pattern_constr(const char* c, std::vector<std::string>&& p)
: constr(c) {
std::swap(params, p);
}
}; };
using pattern_ptr = std::unique_ptr<pattern>; using pattern_ptr = std::unique_ptr<pattern>;
@ -35,12 +18,18 @@ struct branch {
pattern_ptr pat; pattern_ptr pat;
ast_ptr expr; ast_ptr expr;
branch(pattern_ptr&& p, ast_ptr&& a) branch(pattern_ptr p, ast_ptr a)
: pat(std::move(p)), expr(std::move(a)) {} : pat(std::move(p)), expr(std::move(a)) {}
}; };
using branch_ptr = std::unique_ptr<branch>; using branch_ptr = std::unique_ptr<branch>;
struct definition {
virtual ~definition() = default;
};
using definition_ptr = std::unique_ptr<definition>;
enum binop { enum binop {
PLUS, PLUS,
MINUS, MINUS,
@ -48,12 +37,33 @@ enum binop {
DIVIDE DIVIDE
}; };
struct ast_int : public ast {
int value;
explicit ast_int(int v)
: value(v) {}
};
struct ast_lid : public ast {
std::string id;
explicit ast_lid(std::string i)
: id(std::move(i)) {}
};
struct ast_uid : public ast {
std::string id;
explicit ast_uid(std::string i)
: id(std::move(i)) {}
};
struct ast_binop : public ast { struct ast_binop : public ast {
binop op; binop op;
ast_ptr left; ast_ptr left;
ast_ptr right; ast_ptr right;
ast_binop(binop o, ast_ptr&& l, ast_ptr&& r) ast_binop(binop o, ast_ptr l, ast_ptr r)
: op(o), left(std::move(l)), right(std::move(r)) {} : op(o), left(std::move(l)), right(std::move(r)) {}
}; };
@ -61,16 +71,40 @@ struct ast_app : public ast {
ast_ptr left; ast_ptr left;
ast_ptr right; ast_ptr right;
ast_app(ast* l, ast* r) ast_app(ast_ptr l, ast_ptr r)
: left(l), right(r) {} : left(std::move(l)), right(std::move(r)) {}
}; };
struct ast_case : public ast { struct ast_case : public ast {
ast_ptr of; ast_ptr of;
std::vector<branch_ptr> branches; std::vector<branch_ptr> branches;
ast_case(ast_ptr&& o, std::vector<branch_ptr>&& b) ast_case(ast_ptr o, std::vector<branch_ptr> b)
: of(std::move(o)) { : of(std::move(o)), branches(std::move(b)) {}
std::swap(branches, b); };
struct pattern_var : public pattern {
std::string var;
pattern_var(std::string v)
: var(std::move(v)) {}
};
struct pattern_constr : public pattern {
std::string constr;
std::vector<std::string> params;
pattern_constr(std::string c, std::vector<std::string> p)
: constr(std::move(c)), params(std::move(p)) {}
};
struct definition_defn : public definition {
std::string name;
std::vector<std::string> params;
ast_ptr body;
definition_defn(std::string n, std::vector<std::string> p, ast_ptr b)
: name(std::move(n)), params(std::move(p)), body(std::move(b)) {
} }
}; };