✨ more beautiful ast dump
This commit is contained in:
parent
77a14699f4
commit
7590a286c3
|
@ -6,7 +6,7 @@ namespace nasal {
|
|||
|
||||
bool ast_dumper::visit_use_stmt(use_stmt* node) {
|
||||
dump_indent();
|
||||
std::cout << "use" << format_location(node->get_location());
|
||||
std::cout << "use" << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_path()) {
|
||||
if (i==node->get_path().back()) {
|
||||
|
@ -20,48 +20,48 @@ bool ast_dumper::visit_use_stmt(use_stmt* node) {
|
|||
|
||||
bool ast_dumper::visit_null_expr(null_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "null" << format_location(node->get_location());
|
||||
std::cout << "null" << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_nil_expr(nil_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "nil" << format_location(node->get_location());
|
||||
std::cout << "nil" << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_number_literal(number_literal* node) {
|
||||
dump_indent();
|
||||
std::cout << "number " << node->get_number();
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_string_literal(string_literal* node) {
|
||||
dump_indent();
|
||||
std::cout << "string \"" << rawstr(node->get_content()) << "\"";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_identifier(identifier* node) {
|
||||
dump_indent();
|
||||
std::cout << "identifier " << node->get_name();
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_bool_literal(bool_literal* node) {
|
||||
dump_indent();
|
||||
std::cout << "bool " << node->get_flag();
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_vector_expr(vector_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "vector";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_elements()) {
|
||||
if (i==node->get_elements().back()) {
|
||||
|
@ -76,7 +76,7 @@ bool ast_dumper::visit_vector_expr(vector_expr* node) {
|
|||
bool ast_dumper::visit_hash_expr(hash_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "hash";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_members()) {
|
||||
if (i==node->get_members().back()) {
|
||||
|
@ -91,7 +91,7 @@ bool ast_dumper::visit_hash_expr(hash_expr* node) {
|
|||
bool ast_dumper::visit_hash_pair(hash_pair* node) {
|
||||
dump_indent();
|
||||
std::cout << "pair " << node->get_name();
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
if (node->get_value()) {
|
||||
push_indent();
|
||||
set_last();
|
||||
|
@ -104,7 +104,7 @@ bool ast_dumper::visit_hash_pair(hash_pair* node) {
|
|||
bool ast_dumper::visit_function(function* node) {
|
||||
dump_indent();
|
||||
std::cout << "function";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_parameter_list()) {
|
||||
i->accept(this);
|
||||
|
@ -118,7 +118,7 @@ bool ast_dumper::visit_function(function* node) {
|
|||
bool ast_dumper::visit_code_block(code_block* node) {
|
||||
dump_indent();
|
||||
std::cout << "block";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_expressions()) {
|
||||
if (i==node->get_expressions().back()) {
|
||||
|
@ -133,7 +133,7 @@ bool ast_dumper::visit_code_block(code_block* node) {
|
|||
bool ast_dumper::visit_parameter(parameter* node) {
|
||||
dump_indent();
|
||||
std::cout << "parameter " << node->get_parameter_name();
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
if (node->get_default_value()) {
|
||||
push_indent();
|
||||
set_last();
|
||||
|
@ -146,7 +146,7 @@ bool ast_dumper::visit_parameter(parameter* node) {
|
|||
bool ast_dumper::visit_ternary_operator(ternary_operator* node) {
|
||||
dump_indent();
|
||||
std::cout << "ternary_operator";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
node->get_condition()->accept(this);
|
||||
node->get_left()->accept(this);
|
||||
|
@ -166,7 +166,7 @@ bool ast_dumper::visit_binary_operator(binary_operator* node) {
|
|||
return true;
|
||||
}
|
||||
dump_indent();
|
||||
std::cout << "binary_operator ";
|
||||
std::cout << "binary_operator \"";
|
||||
switch(node->get_operator_type()) {
|
||||
case binary_operator::binary_type::add: std::cout << "+"; break;
|
||||
case binary_operator::binary_type::sub: std::cout << "-"; break;
|
||||
|
@ -185,7 +185,7 @@ bool ast_dumper::visit_binary_operator(binary_operator* node) {
|
|||
case binary_operator::binary_type::condition_and: std::cout << "and"; break;
|
||||
case binary_operator::binary_type::condition_or: std::cout << "or"; break;
|
||||
}
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << "\"" << format_location(node);
|
||||
push_indent();
|
||||
node->get_left()->accept(this);
|
||||
set_last();
|
||||
|
@ -200,13 +200,13 @@ bool ast_dumper::visit_unary_operator(unary_operator* node) {
|
|||
return true;
|
||||
}
|
||||
dump_indent();
|
||||
std::cout << "unary_operator ";
|
||||
std::cout << "unary_operator \"";
|
||||
switch(node->get_operator_type()) {
|
||||
case unary_operator::unary_type::negative: std::cout << "-"; break;
|
||||
case unary_operator::unary_type::logical_not: std::cout << "!"; break;
|
||||
case unary_operator::unary_type::bitwise_not: std::cout << "~"; break;
|
||||
}
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << "\"" << format_location(node);
|
||||
push_indent();
|
||||
set_last();
|
||||
node->get_value()->accept(this);
|
||||
|
@ -217,7 +217,7 @@ bool ast_dumper::visit_unary_operator(unary_operator* node) {
|
|||
bool ast_dumper::visit_call_expr(call_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "call_expr";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
if (!node->get_calls().size()) {
|
||||
set_last();
|
||||
|
@ -236,14 +236,14 @@ bool ast_dumper::visit_call_expr(call_expr* node) {
|
|||
bool ast_dumper::visit_call_hash(call_hash* node) {
|
||||
dump_indent();
|
||||
std::cout << "call_hash " << node->get_field();
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_call_vector(call_vector* node) {
|
||||
dump_indent();
|
||||
std::cout << "call_vector";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_slices()) {
|
||||
if (i==node->get_slices().back()) {
|
||||
|
@ -258,7 +258,7 @@ bool ast_dumper::visit_call_vector(call_vector* node) {
|
|||
bool ast_dumper::visit_call_function(call_function* node) {
|
||||
dump_indent();
|
||||
std::cout << "call_function";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_argument()) {
|
||||
if (i==node->get_argument().back()) {
|
||||
|
@ -273,7 +273,7 @@ bool ast_dumper::visit_call_function(call_function* node) {
|
|||
bool ast_dumper::visit_slice_vector(slice_vector* node) {
|
||||
dump_indent();
|
||||
std::cout << "slice";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
if (!node->get_end()) {
|
||||
set_last();
|
||||
|
@ -290,7 +290,7 @@ bool ast_dumper::visit_slice_vector(slice_vector* node) {
|
|||
bool ast_dumper::visit_definition_expr(definition_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "definition";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
if (node->get_variable_name()) {
|
||||
node->get_variable_name()->accept(this);
|
||||
|
@ -321,7 +321,7 @@ bool ast_dumper::visit_assignment_expr(assignment_expr* node) {
|
|||
case assignment_expr::assign_type::bitwise_or_equal: std::cout << "|="; break;
|
||||
case assignment_expr::assign_type::bitwise_xor_equal: std::cout << "^="; break;
|
||||
}
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
node->get_left()->accept(this);
|
||||
set_last();
|
||||
|
@ -333,7 +333,7 @@ bool ast_dumper::visit_assignment_expr(assignment_expr* node) {
|
|||
bool ast_dumper::visit_multi_identifier(multi_identifier* node) {
|
||||
dump_indent();
|
||||
std::cout << "multiple_identifier";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_variables()) {
|
||||
if (i==node->get_variables().back()) {
|
||||
|
@ -348,7 +348,7 @@ bool ast_dumper::visit_multi_identifier(multi_identifier* node) {
|
|||
bool ast_dumper::visit_tuple_expr(tuple_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "tuple";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
for(auto i : node->get_elements()) {
|
||||
if (i==node->get_elements().back()) {
|
||||
|
@ -363,7 +363,7 @@ bool ast_dumper::visit_tuple_expr(tuple_expr* node) {
|
|||
bool ast_dumper::visit_multi_assign(multi_assign* node) {
|
||||
dump_indent();
|
||||
std::cout << "multiple_assignment";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
node->get_tuple()->accept(this);
|
||||
set_last();
|
||||
|
@ -375,7 +375,7 @@ bool ast_dumper::visit_multi_assign(multi_assign* node) {
|
|||
bool ast_dumper::visit_while_expr(while_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "while";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
node->get_condition()->accept(this);
|
||||
set_last();
|
||||
|
@ -387,7 +387,7 @@ bool ast_dumper::visit_while_expr(while_expr* node) {
|
|||
bool ast_dumper::visit_for_expr(for_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "for";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
node->get_initial()->accept(this);
|
||||
node->get_condition()->accept(this);
|
||||
|
@ -405,7 +405,7 @@ bool ast_dumper::visit_iter_expr(iter_expr* node) {
|
|||
} else {
|
||||
std::cout << "iterator";
|
||||
}
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
set_last();
|
||||
if (node->get_name()) {
|
||||
|
@ -424,7 +424,7 @@ bool ast_dumper::visit_forei_expr(forei_expr* node) {
|
|||
} else {
|
||||
std::cout << "forindex";
|
||||
}
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
node->get_iterator()->accept(this);
|
||||
node->get_value()->accept(this);
|
||||
|
@ -437,7 +437,7 @@ bool ast_dumper::visit_forei_expr(forei_expr* node) {
|
|||
bool ast_dumper::visit_condition_expr(condition_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "condition";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
if (!node->get_elsif_stataments().size() &&
|
||||
!node->get_else_statement()) {
|
||||
|
@ -462,7 +462,7 @@ bool ast_dumper::visit_condition_expr(condition_expr* node) {
|
|||
bool ast_dumper::visit_if_expr(if_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "if";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
push_indent();
|
||||
if (node->get_condition()) {
|
||||
node->get_condition()->accept(this);
|
||||
|
@ -476,21 +476,21 @@ bool ast_dumper::visit_if_expr(if_expr* node) {
|
|||
bool ast_dumper::visit_continue_expr(continue_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "continue";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_break_expr(break_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "break";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ast_dumper::visit_return_expr(return_expr* node) {
|
||||
dump_indent();
|
||||
std::cout << "return";
|
||||
std::cout << format_location(node->get_location());
|
||||
std::cout << format_location(node);
|
||||
if (node->get_value()) {
|
||||
push_indent();
|
||||
set_last();
|
||||
|
|
|
@ -16,29 +16,37 @@ private:
|
|||
private:
|
||||
void push_indent() {
|
||||
if (indent.size()) {
|
||||
if (indent.back()=="|--") {
|
||||
indent.back() = "| ";
|
||||
} else if (indent.back()=="+--") {
|
||||
if (indent.back()=="├──") {
|
||||
indent.back() = "│ ";
|
||||
} else if (indent.back()=="└──") {
|
||||
indent.back() = " ";
|
||||
}
|
||||
}
|
||||
indent.push_back("|--");
|
||||
indent.push_back("├──");
|
||||
}
|
||||
void pop_indent() {indent.pop_back();}
|
||||
void set_last() {indent.back() = "+--";}
|
||||
|
||||
void pop_indent() {
|
||||
indent.pop_back();
|
||||
}
|
||||
|
||||
void set_last() {
|
||||
indent.back() = "└──";
|
||||
}
|
||||
|
||||
void dump_indent() {
|
||||
if (indent.size() && indent.back()=="| ") {
|
||||
indent.back() = "|--";
|
||||
if (indent.size() && indent.back()=="│ ") {
|
||||
indent.back() = "├──";
|
||||
}
|
||||
for(const auto& i : indent) {
|
||||
std::cout << i;
|
||||
}
|
||||
}
|
||||
std::string format_location(const span& location) {
|
||||
|
||||
std::string format_location(expr* node) {
|
||||
std::stringstream ss;
|
||||
ss << " -> ";
|
||||
location.dump_begin(ss);
|
||||
ss << "\n";
|
||||
ss << " → [";
|
||||
node->get_location().dump_begin(ss);
|
||||
ss << "]\n";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ std::ostream& help(std::ostream& out) {
|
|||
<< "option:\n"
|
||||
<< " -h, --help | get help.\n"
|
||||
<< " -v, --version | get version.\n"
|
||||
<< " -r, --repl | use repl interpreter(experimental).\n"
|
||||
<< " -r, --repl | use repl interpreter.\n"
|
||||
<< "\nnasal [option] <file> [argv]\n"
|
||||
<< "option:\n"
|
||||
<< " -a, --ast | view ast after link/optimize process.\n"
|
||||
|
@ -101,11 +101,12 @@ std::ostream& version(std::ostream& out) {
|
|||
for(u32 i = 0; i<5; ++i) {
|
||||
num = (num+rand())*(1.0/(RAND_MAX+1.0));
|
||||
}
|
||||
if (num<0.01) {
|
||||
// give you 5% to see this easter egg
|
||||
if (num<0.05) {
|
||||
nasal::parse::easter_egg();
|
||||
}
|
||||
|
||||
out << "nasal interpreter version " << __nasver__;
|
||||
out << "nasal version " << __nasver__;
|
||||
out << " " << nasal::get_platform() << " " << nasal::get_arch();
|
||||
out << " (" << __DATE__ << " " << __TIME__ << ")\n";
|
||||
return out;
|
||||
|
|
Loading…
Reference in New Issue