Finish assignment 3
This commit is contained in:
28
tree.hpp
28
tree.hpp
@@ -2,6 +2,8 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
typedef std::vector<std::string> dot;
|
||||
|
||||
enum class binop {
|
||||
plus,
|
||||
minus,
|
||||
@@ -23,6 +25,8 @@ enum class unop {
|
||||
|
||||
struct expr {
|
||||
virtual ~expr() = default;
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix) = 0;
|
||||
};
|
||||
|
||||
typedef expr* expr_ptr;
|
||||
@@ -32,18 +36,24 @@ struct expr_id : expr {
|
||||
|
||||
expr_id(std::string i) :
|
||||
id(std::move(i)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct expr_int : expr {
|
||||
int val;
|
||||
|
||||
expr_int(int i) : val(i) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct expr_float : expr {
|
||||
double val;
|
||||
|
||||
expr_float(double f) : val(f) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct expr_binop : expr {
|
||||
@@ -53,6 +63,8 @@ struct expr_binop : expr {
|
||||
|
||||
expr_binop(binop nop, expr_ptr l, expr_ptr r) :
|
||||
op(nop), left(std::move(l)), right(std::move(r)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct expr_unop : expr {
|
||||
@@ -61,6 +73,8 @@ struct expr_unop : expr {
|
||||
|
||||
expr_unop(unop nop, expr_ptr c) :
|
||||
op(nop), child(std::move(c)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct expr_assign : expr {
|
||||
@@ -69,6 +83,8 @@ struct expr_assign : expr {
|
||||
|
||||
expr_assign(std::string v, expr_ptr c) :
|
||||
var(std::move(v)), child(std::move(c)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
template <typename T, typename ... Ts>
|
||||
@@ -78,12 +94,16 @@ expr_ptr make_expr(Ts&& ... ts) {
|
||||
|
||||
struct stmt {
|
||||
virtual ~stmt() = default;
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix) = 0;
|
||||
};
|
||||
|
||||
typedef stmt* stmt_ptr;
|
||||
|
||||
struct stmt_block : stmt {
|
||||
std::vector<stmt_ptr> children;
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct stmt_if : stmt {
|
||||
@@ -93,6 +113,8 @@ struct stmt_if : stmt {
|
||||
|
||||
stmt_if(expr_ptr c, stmt_ptr t, stmt_ptr e = nullptr) :
|
||||
cond(std::move(c)), then(std::move(t)), els(std::move(e)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct stmt_while : stmt {
|
||||
@@ -101,10 +123,12 @@ struct stmt_while : stmt {
|
||||
|
||||
stmt_while(expr_ptr c, stmt_ptr b) :
|
||||
cond(std::move(c)), body(std::move(b)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct stmt_break : stmt {
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
struct stmt_expr : stmt {
|
||||
@@ -112,6 +136,8 @@ struct stmt_expr : stmt {
|
||||
|
||||
stmt_expr(expr_ptr c) :
|
||||
child(std::move(c)) {}
|
||||
|
||||
virtual void print_dot(dot& into, std::string prefix);
|
||||
};
|
||||
|
||||
template <typename T, typename ... Ts>
|
||||
|
||||
Reference in New Issue
Block a user