147 lines
3.8 KiB
C++
147 lines
3.8 KiB
C++
#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;
|
|
}
|
|
|
|
template <>
|
|
std::string label<binop>(binop op) {
|
|
switch(op) {
|
|
case binop::plus:
|
|
return "PLUS";
|
|
case binop::minus:
|
|
return "MINUS";
|
|
case binop::times:
|
|
return "TIMES";
|
|
case binop::divide:
|
|
return "DIVIDEBY";
|
|
case binop::land:
|
|
return "AND";
|
|
case binop::lor:
|
|
return "OR";
|
|
case binop::eq:
|
|
return "EQ";
|
|
case binop::neq:
|
|
return "NEQ";
|
|
case binop::lt:
|
|
return "LT";
|
|
case binop::lte:
|
|
return "LTE";
|
|
case binop::gt:
|
|
return "GT";
|
|
case binop::gte:
|
|
return "GTE";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
template <>
|
|
std::string label<unop>(unop op) {
|
|
switch(op) {
|
|
case unop::lnot:
|
|
return "!";
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
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) {
|
|
into.push_back(prefix + wrap_label(std::string("identifier: ") + id, true));
|
|
}
|
|
|
|
void expr_int::print_dot(dot& into, std::string prefix) {
|
|
into.push_back(prefix + wrap_label(std::string("integer: ") + label(val), true));
|
|
}
|
|
|
|
void expr_float::print_dot(dot& into, std::string prefix) {
|
|
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);
|
|
}
|