Make parser create trees

This commit is contained in:
2019-05-20 11:12:53 -07:00
parent c1e4433011
commit f098f91455
4 changed files with 75 additions and 66 deletions

View File

@@ -25,7 +25,26 @@ struct expr {
virtual ~expr() = default;
};
typedef std::unique_ptr<expr> expr_ptr;
typedef expr* expr_ptr;
struct expr_id : expr {
std::string id;
expr_id(std::string i) :
id(std::move(i)) {}
};
struct expr_int : expr {
int val;
expr_int(int i) : val(i) {}
};
struct expr_float : expr {
double val;
expr_float(double f) : val(f) {}
};
struct expr_binop : expr {
binop op;
@@ -61,7 +80,7 @@ struct stmt {
virtual ~stmt() = default;
};
typedef std::unique_ptr<stmt> stmt_ptr;
typedef stmt* stmt_ptr;
struct stmt_block : stmt {
std::vector<stmt_ptr> children;
@@ -80,14 +99,21 @@ struct stmt_while : stmt {
expr_ptr cond;
stmt_ptr body;
stmt_while(expr_ptr c, stmt_ptr b)
: cond(std::move(c)), body(std::move(b)) {}
stmt_while(expr_ptr c, stmt_ptr b) :
cond(std::move(c)), body(std::move(b)) {}
};
struct stmt_break : stmt {
};
struct stmt_expr : stmt {
expr_ptr child;
stmt_expr(expr_ptr c) :
child(std::move(c)) {}
};
template <typename T, typename ... Ts>
stmt_ptr make_stmt(Ts&& ... ts) {
return stmt_ptr(new T(std::move(ts)...));