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

View File

@ -3,30 +3,13 @@
#include <vector>
struct ast {
virtual ~ast();
virtual ~ast() = default;
};
using ast_ptr = std::unique_ptr<ast>;
struct pattern {
virtual ~pattern();
};
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);
}
virtual ~pattern() = default;
};
using pattern_ptr = std::unique_ptr<pattern>;
@ -35,12 +18,18 @@ struct branch {
pattern_ptr pat;
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)) {}
};
using branch_ptr = std::unique_ptr<branch>;
struct definition {
virtual ~definition() = default;
};
using definition_ptr = std::unique_ptr<definition>;
enum binop {
PLUS,
MINUS,
@ -48,12 +37,33 @@ enum binop {
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 {
binop op;
ast_ptr left;
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)) {}
};
@ -61,16 +71,40 @@ struct ast_app : public ast {
ast_ptr left;
ast_ptr right;
ast_app(ast* l, ast* r)
: left(l), right(r) {}
ast_app(ast_ptr l, ast_ptr r)
: left(std::move(l)), right(std::move(r)) {}
};
struct ast_case : public ast {
ast_ptr of;
std::vector<branch_ptr> branches;
ast_case(ast_ptr&& o, std::vector<branch_ptr>&& b)
: of(std::move(o)) {
std::swap(branches, b);
ast_case(ast_ptr o, std::vector<branch_ptr> b)
: of(std::move(o)), branches(std::move(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)) {
}
};