You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.0 KiB

#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;
}
3 years ago
template <>
std::string label<bool>(bool v) {
return v ? "TRUE" : "FALSE";
}
template <>
std::string label<binop>(binop op) {
switch(op) {
case binop::plus:
3 years ago
return "PLUS";
case binop::minus:
3 years ago
return "MINUS";
case binop::times:
3 years ago
return "TIMES";
case binop::divide:
3 years ago
return "DIVIDEBY";
case binop::land:
3 years ago
return "AND";
case binop::lor:
3 years ago
return "OR";
case binop::eq:
3 years ago
return "EQ";
case binop::neq:
3 years ago
return "NEQ";
case binop::lt:
3 years ago
return "LT";
case binop::lte:
3 years ago
return "LTE";
case binop::gt:
3 years ago
return "GT";
case binop::gte:
3 years ago
return "GTE";
default:
return "";
}
}
template <>
std::string label<unop>(unop op) {
switch(op) {
case unop::lnot:
return "!";
default:
return "";
}
}
template <typename T>
3 years ago
std::string wrap_label(T v, bool box = false) {
return std::string(" [label=\"") + label(v) + "\" " + (box ? "shape=\"box\"" : "") + "]";
}
void expr_id::print_dot(dot& into, std::string prefix) {
3 years ago
into.push_back(prefix + wrap_label(std::string("identifier: ") + id, true));
}
3 years ago
void expr_bool::print_dot(dot& into, std::string prefix) {
into.push_back(prefix + wrap_label(std::string("Boolean: ") + label(val), true));
}
void expr_int::print_dot(dot& into, std::string prefix) {
3 years ago
into.push_back(prefix + wrap_label(std::string("Integer: ") + label(val), true));
}
void expr_float::print_dot(dot& into, std::string prefix) {
3 years ago
into.push_back(prefix + wrap_label(std::string("Float: ") + label(val), true));
}
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);
}