Assignment-3/tree.cpp

156 lines
4.0 KiB
C++
Raw Permalink Normal View History

2019-05-20 13:28:33 -07:00
#include "tree.hpp"
#include <string>
template <typename T>
std::string label(T v) {
return std::to_string(v);
}
template <>
std::string label<std::string>(std::string v) {
return v;
}
template <>
std::string label<const char*>(const char* v) {
return v;
}
2019-05-27 21:57:41 -07:00
template <>
std::string label<bool>(bool v) {
return v ? "TRUE" : "FALSE";
}
2019-05-20 13:28:33 -07:00
template <>
std::string label<binop>(binop op) {
switch(op) {
case binop::plus:
2019-05-27 21:51:39 -07:00
return "PLUS";
2019-05-20 13:28:33 -07:00
case binop::minus:
2019-05-27 21:51:39 -07:00
return "MINUS";
2019-05-20 13:28:33 -07:00
case binop::times:
2019-05-27 21:51:39 -07:00
return "TIMES";
2019-05-20 13:28:33 -07:00
case binop::divide:
2019-05-27 21:51:39 -07:00
return "DIVIDEBY";
2019-05-20 13:28:33 -07:00
case binop::land:
2019-05-27 21:51:39 -07:00
return "AND";
2019-05-20 13:28:33 -07:00
case binop::lor:
2019-05-27 21:51:39 -07:00
return "OR";
2019-05-20 13:28:33 -07:00
case binop::eq:
2019-05-27 21:51:39 -07:00
return "EQ";
2019-05-20 13:28:33 -07:00
case binop::neq:
2019-05-27 21:51:39 -07:00
return "NEQ";
2019-05-20 13:28:33 -07:00
case binop::lt:
2019-05-27 21:51:39 -07:00
return "LT";
2019-05-20 13:28:33 -07:00
case binop::lte:
2019-05-27 21:51:39 -07:00
return "LTE";
2019-05-20 13:28:33 -07:00
case binop::gt:
2019-05-27 21:51:39 -07:00
return "GT";
2019-05-20 13:28:33 -07:00
case binop::gte:
2019-05-27 21:51:39 -07:00
return "GTE";
2019-05-20 13:28:33 -07:00
default:
return "";
}
}
template <>
std::string label<unop>(unop op) {
switch(op) {
case unop::lnot:
return "!";
default:
return "";
}
}
template <typename T>
2019-05-27 21:51:39 -07:00
std::string wrap_label(T v, bool box = false) {
return std::string(" [label=\"") + label(v) + "\" " + (box ? "shape=\"box\"" : "") + "]";
2019-05-20 13:28:33 -07:00
}
void expr_id::print_dot(dot& into, std::string prefix) {
2019-05-27 21:51:39 -07:00
into.push_back(prefix + wrap_label(std::string("identifier: ") + id, true));
2019-05-20 13:28:33 -07:00
}
2019-05-27 21:57:41 -07:00
void expr_bool::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(std::string("Boolean: ") + label(val), true));
}
2019-05-20 13:28:33 -07:00
void expr_int::print_dot(dot& into, std::string prefix) {
2019-05-27 21:57:41 -07:00
into.push_back(prefix + wrap_label(std::string("Integer: ") + label(val), true));
2019-05-20 13:28:33 -07:00
}
void expr_float::print_dot(dot& into, std::string prefix) {
2019-05-27 21:57:41 -07:00
into.push_back(prefix + wrap_label(std::string("Float: ") + label(val), true));
2019-05-20 13:28:33 -07:00
}
void expr_binop::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(op));
std::string lp = prefix + "_left";
std::string rp = prefix + "_right";
left->print_dot(into, lp);
right->print_dot(into, rp);
into.push_back(prefix + "->" + lp);
into.push_back(prefix + "->" + rp);
}
void expr_unop::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(op));
std::string cp = prefix + "_child";
child->print_dot(into, cp);
into.push_back(prefix + "->" + cp);
}
void expr_assign::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(var + "="));
std::string cp = prefix + "_child";
child->print_dot(into, cp);
into.push_back(prefix + "->" + cp);
}
void stmt_block::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label("block"));
int index = 0;
for(auto& child : children) {
std::string cp = prefix + "_" + std::to_string(index);
child->print_dot(into, cp);
into.push_back(prefix + "->" + cp);
index++;
}
}
void stmt_if::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label("if"));
std::string cp = prefix + "_cond";
std::string tp = prefix + "_then";
std::string ep = prefix + "_else";
cond->print_dot(into, cp);
then->print_dot(into, tp);
if(els) els->print_dot(into, ep);
into.push_back(prefix + "->" + cp);
into.push_back(prefix + "->" + tp);
if(els) into.push_back(prefix + "->" + ep);
}
void stmt_while::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label("while"));
std::string cp = prefix + "_cond";
std::string bp = prefix + "_body";
cond->print_dot(into, cp);
body->print_dot(into, bp);
into.push_back(prefix + "->" + cp);
into.push_back(prefix + "->" + bp);
}
void stmt_break::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label("break"));
}
void stmt_expr::print_dot(dot& into, std::string prefix) {
child->print_dot(into, prefix);
}