From cfa42158af7b4ea44040ab0fbe1b84edd0deabd8 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 30 Aug 2010 13:37:50 +0000 Subject: [PATCH] Clean up reflection a bit, and how we do pretty printing of ast nodes. Registered new internal_to_string in reflection module so that we can have automatic pretty printing of ast nodes during repl eval. --- .../chaiscript/language/chaiscript_common.hpp | 19 +++++++++++++++++++ .../chaiscript/language/chaiscript_engine.hpp | 11 ----------- .../chaiscript/language/chaiscript_parser.hpp | 5 ++++- src/reflection.cpp | 2 +- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index dfae417..483c43a 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -90,6 +90,25 @@ namespace chaiscript } */ + /** + * Prints the contents of an AST node, including its children, recursively + */ + std::string to_string(std::string prepend = "") { + std::ostringstream oss; + + oss << prepend << "(" << ast_node_type_to_string(this->identifier) << ") " + << this->text << " : " << this->start.line << ", " << this->start.column << std::endl; + + for (unsigned int j = 0; j < this->children.size(); ++j) { + oss << this->children[j]->to_string(prepend + " "); + } + return oss.str(); + } + + std::string internal_to_string() { + return to_string(); + } + virtual Boxed_Value eval(Dispatch_Engine &) { Boxed_Value bv; throw std::runtime_error("Undispatched ast_node (internal error)"); diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 64c5dcf..f0536ed 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -538,17 +538,6 @@ namespace chaiscript return engine; } - /** - * Prints the contents of an AST node, including its children, recursively - */ - void debug_print(AST_NodePtr t, std::string prepend = "") { - std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " - << t->text << " : " << t->start.line << ", " << t->start.column << std::endl; - for (unsigned int j = 0; j < t->children.size(); ++j) { - debug_print(t->children[j], prepend + " "); - } - } - /** * Helper function for loading a file */ diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index d702de4..8d7c0f9 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -103,19 +103,22 @@ namespace chaiscript /** * Prints the parsed ast_nodes as a tree */ + /* void debug_print(AST_NodePtr t, std::string prepend = "") { std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << std::endl; for (unsigned int j = 0; j < t->children.size(); ++j) { debug_print(t->children[j], prepend + " "); } } + */ /** * Shows the current stack of matched ast_nodes */ void show_match_stack() { for (unsigned int i = 0; i < match_stack.size(); ++i) { - debug_print(match_stack[i]); + //debug_print(match_stack[i]); + std::cout << match_stack[i]->to_string(); } } diff --git a/src/reflection.cpp b/src/reflection.cpp index fb304a1..fcb3428 100644 --- a/src/reflection.cpp +++ b/src/reflection.cpp @@ -25,6 +25,7 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_reflect ((filename)) ((start)) ((end)) + ((internal_to_string)) ((children)) ); @@ -33,7 +34,6 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_reflect (chaiscript::ChaiScript_Parser ()), ((parse)) ((ast)) - ((show_match_stack)) );