From 637164e457dec5c5adadde46d5fe6f02b472fe9c Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 15 Mar 2011 17:35:14 -0600 Subject: [PATCH] Move around some namespaces for documentation purposes. Fix problems with building on clang 2.8. Remove unneeded function for get_engine() and fix functor<> calls that take a Boxed_Value --- .../chaiscript/dispatchkit/dispatchkit.hpp | 63 +- .../dispatchkit/function_call_detail.hpp | 3 +- .../dispatchkit/proxy_functions.hpp | 40 +- .../dispatchkit/proxy_functions_detail.hpp | 55 +- .../chaiscript/language/chaiscript_engine.hpp | 14 +- .../chaiscript/language/chaiscript_eval.hpp | 2441 +++++++++-------- .../chaiscript/language/chaiscript_parser.hpp | 283 +- src/main.cpp | 2 +- unittests/functor_creation_test.cpp | 14 +- 9 files changed, 1478 insertions(+), 1437 deletions(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 4fe1434..88e097b 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -257,37 +257,48 @@ namespace chaiscript }; - /** - * Exception thrown in the case that a multi method dispatch fails - * because no matching function was found - * at runtime due to either an arity_error, a guard_error or a bad_boxed_cast - * exception - */ - struct reserved_word_error : std::runtime_error + namespace exception { - reserved_word_error(const std::string &t_word) throw() - : std::runtime_error("Reserved word not allowed in object name: " + word), word(t_word) + /** + * Exception thrown in the case that a multi method dispatch fails + * because no matching function was found + * at runtime due to either an arity_error, a guard_error or a bad_boxed_cast + * exception + */ + class reserved_word_error : public std::runtime_error { - } + public: + reserved_word_error(const std::string &t_word) throw() + : std::runtime_error("Reserved word not allowed in object name: " + t_word), m_word(t_word) + { + } - std::string word; + virtual ~reserved_word_error() throw() {} - virtual ~reserved_word_error() throw() {} - }; + std::string word() const + { + return m_word; + } - /** - * Exception thrown in the case that a non-const object was added as a shared object - */ - struct global_non_const : std::runtime_error - { - global_non_const() throw() - : std::runtime_error("a global object must be const") + private: + std::string m_word; + + }; + + /** + * Exception thrown in the case that a non-const object was added as a shared object + */ + class global_non_const : public std::runtime_error { - } - - virtual ~global_non_const() throw() {} - }; + public: + global_non_const() throw() + : std::runtime_error("a global object must be const") + { + } + virtual ~global_non_const() throw() {} + }; + } /** * Main class for the dispatchkit. Handles management @@ -377,7 +388,7 @@ namespace chaiscript validate_object_name(name); if (!obj.is_const()) { - throw global_non_const(); + throw exception::global_non_const(); } #ifndef CHAISCRIPT_NO_THREADS @@ -909,7 +920,7 @@ namespace chaiscript if (m_state.m_reserved_words.find(name) != m_state.m_reserved_words.end()) { - throw reserved_word_error(name); + throw exception::reserved_word_error(name); } } diff --git a/include/chaiscript/dispatchkit/function_call_detail.hpp b/include/chaiscript/dispatchkit/function_call_detail.hpp index 61eebc8..d6798c8 100644 --- a/include/chaiscript/dispatchkit/function_call_detail.hpp +++ b/include/chaiscript/dispatchkit/function_call_detail.hpp @@ -6,7 +6,7 @@ #include -#define addparam(z,n,text) params.push_back(boost::is_reference::value?Boxed_Value(boost::ref(BOOST_PP_CAT(p, n))):Boxed_Value(BOOST_PP_CAT(p, n) )); +#define addparam(z,n,text) params.push_back((boost::is_reference::value&&!(boost::is_same::type>::type>::value))?Boxed_Value(boost::ref(BOOST_PP_CAT(p, n))):Boxed_Value(BOOST_PP_CAT(p, n) )); #define curry(z,n,text) BOOST_PP_CAT(_, BOOST_PP_INC(n)) @@ -25,6 +25,7 @@ namespace chaiscript { namespace detail { + /** * Internal helper class for handling the return * value of a build_function_caller diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index 3b1e147..825b097 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -324,7 +324,7 @@ namespace chaiscript Bound_Function(const Const_Proxy_Function &t_f, const std::vector &t_args) : Proxy_Function_Base(build_param_type_info(t_f, t_args)), - m_f(t_f), m_args(t_args), m_arity(t_f->get_arity()<0?-1:get_param_types().size()-1) + m_f(t_f), m_args(t_args), m_arity(t_f->get_arity()<0?-1:static_cast(get_param_types().size())-1) { assert(m_f->get_arity() < 0 || m_f->get_arity() == static_cast(m_args.size())); } @@ -569,26 +569,30 @@ namespace chaiscript T Class::* m_attr; }; - /** - * Exception thrown in the case that a multi method dispatch fails - * because no matching function was found - * at runtime due to either an arity_error, a guard_error or a bad_boxed_cast - * exception - */ - struct dispatch_error : std::runtime_error + namespace exception { - dispatch_error() throw() - : std::runtime_error("No matching function to dispatch to") + /** + * Exception thrown in the case that a multi method dispatch fails + * because no matching function was found + * at runtime due to either an arity_error, a guard_error or a bad_boxed_cast + * exception + */ + class dispatch_error : public std::runtime_error { - } + public: + dispatch_error() throw() + : std::runtime_error("No matching function to dispatch to") + { + } - dispatch_error(bool is_const) throw() - : std::runtime_error(std::string("No matching function to dispatch to") + (is_const?", parameter is const":"")) - { - } + dispatch_error(bool is_const) throw() + : std::runtime_error(std::string("No matching function to dispatch to") + (is_const?", parameter is const":"")) + { + } - virtual ~dispatch_error() throw() {} - }; + virtual ~dispatch_error() throw() {} + }; + } /** * Take a vector of functions and a vector of parameters. Attempt to execute @@ -617,7 +621,7 @@ namespace chaiscript ++begin; } - throw dispatch_error(plist.empty()?false:plist[0].is_const()); + throw exception::dispatch_error(plist.empty()?false:plist[0].is_const()); } /** diff --git a/include/chaiscript/dispatchkit/proxy_functions_detail.hpp b/include/chaiscript/dispatchkit/proxy_functions_detail.hpp index 036ad5e..8402959 100644 --- a/include/chaiscript/dispatchkit/proxy_functions_detail.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions_detail.hpp @@ -49,29 +49,6 @@ namespace chaiscript }; } - namespace detail - { - template - struct Do_Call - { - template - static Boxed_Value go(const boost::function &fun, const std::vector ¶ms) - { - return Handle_Return::handle(call_func(fun, params)); - } - }; - - template<> - struct Do_Call - { - template - static Boxed_Value go(const boost::function &fun, const std::vector ¶ms) - { - call_func(fun, params); - return Handle_Return::handle(); - } - }; - } } #define BOOST_PP_ITERATION_LIMITS ( 0, 10 ) @@ -143,3 +120,35 @@ namespace chaiscript #undef n #endif + + +#ifndef BOOST_PP_IS_ITERATING + +namespace chaiscript +{ + namespace detail + { + template + struct Do_Call + { + template + static Boxed_Value go(const boost::function &fun, const std::vector ¶ms) + { + return Handle_Return::handle(chaiscript::detail::call_func(fun, params)); + } + }; + + template<> + struct Do_Call + { + template + static Boxed_Value go(const boost::function &fun, const std::vector ¶ms) + { + chaiscript::detail::call_func(fun, params); + return Handle_Return::handle(); + } + }; + } +} + +#endif diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 36beb00..5526f56 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -298,6 +298,13 @@ namespace chaiscript } } + /** + * Returns the current evaluation m_engine + */ + Dispatch_Engine &get_eval_engine() { + return m_engine; + } + public: ChaiScript(const std::vector &t_modulepaths = std::vector(), @@ -481,13 +488,6 @@ namespace chaiscript } - /** - * Returns the current evaluation m_engine - */ - Dispatch_Engine &get_eval_engine() { - return m_engine; - } - /** * Helper function for loading a file */ diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 872d135..1382c60 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -42,761 +42,691 @@ namespace chaiscript } - struct Binary_Operator_AST_Node : public AST_Node { - public: - Binary_Operator_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Xor, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Binary_Operator_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; + namespace eval + { + struct Binary_Operator_AST_Node : public AST_Node { + public: + Binary_Operator_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Xor, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Binary_Operator_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; - try { - retval = this->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; - } - - for (size_t i = 1; i < this->children.size(); i += 2) { try { - retval = t_ss.call_function(this->children[i]->text, retval, this->children[i+1]->eval(t_ss)); + retval = this->children[0]->eval(t_ss); } - catch(const dispatch_error &){ - throw Eval_Error("Can not find appropriate '" + this->children[i]->text + "'"); - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[i+1]); + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); throw; } - } - return retval; - } - - }; - - struct Error_AST_Node : public AST_Node { - public: - Error_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Error, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - - virtual ~Error_AST_Node() {} - }; - - struct Int_AST_Node : public AST_Node { - public: - Int_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Int, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Int_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &){ - return const_var(int(atoi(this->text.c_str()))); - } - - }; - - struct Float_AST_Node : public AST_Node { - public: - Float_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Float, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Float_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &){ - return const_var(double(atof(this->text.c_str()))); - } - - }; - - struct Id_AST_Node : public AST_Node { - public: - Id_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Id, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Id_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - if (this->text == "true") { - return const_var(true); - } - else if (this->text == "false") { - return const_var(false); - } - else if (this->text == "Infinity") { - return const_var(std::numeric_limits::infinity()); - } - else if (this->text == "NaN") { - return const_var(std::numeric_limits::quiet_NaN()); - } - else { - try { - return t_ss.get_object(this->text); - } - catch (std::exception &) { - throw Eval_Error("Can not find object: " + this->text); - } - } - } - }; - - struct Char_AST_Node : public AST_Node { - public: - Char_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Char, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Char_AST_Node() {} - }; - - struct Str_AST_Node : public AST_Node { - public: - Str_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Str, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Str_AST_Node() {} - }; - - struct Eol_AST_Node : public AST_Node { - public: - Eol_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Eol, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Eol_AST_Node() {} - }; - - struct Fun_Call_AST_Node : public AST_Node { - public: - Fun_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Fun_Call, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Fun_Call_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Param_List_Builder plb; - - if ((this->children.size() > 1) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) { - for (size_t i = 0; i < this->children[1]->children.size(); ++i) { + for (size_t i = 1; i < this->children.size(); i += 2) { try { - plb << this->children[1]->children[i]->eval(t_ss); + retval = t_ss.call_function(this->children[i]->text, retval, this->children[i+1]->eval(t_ss)); + } + catch(const exception::dispatch_error &){ + throw Eval_Error("Can not find appropriate '" + this->children[i]->text + "'"); } catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[1]->children[i]); + ee.call_stack.push_back(this->children[i+1]); throw; } } + + return retval; } - Dispatch_Engine::Stack prev_stack = t_ss.get_stack(); - Dispatch_Engine::Stack new_stack = t_ss.new_stack(); + }; - try { - Boxed_Value fn = this->children[0]->eval(t_ss); + struct Error_AST_Node : public AST_Node { + public: + Error_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Error, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - try { - t_ss.set_stack(new_stack); - const Boxed_Value &retval = (*boxed_cast(fn))(plb); - t_ss.set_stack(prev_stack); - return retval; - } - catch(const dispatch_error &e){ - t_ss.set_stack(prev_stack); - throw Eval_Error(std::string(e.what()) + " with function '" + this->children[0]->text + "'"); - } - catch(Return_Value &rv) { - t_ss.set_stack(prev_stack); - return rv.retval; - } - catch(...) { - t_ss.set_stack(prev_stack); - throw; - } - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - t_ss.set_stack(prev_stack); - throw Eval_Error(ee.reason); + virtual ~Error_AST_Node() {} + }; + + struct Int_AST_Node : public AST_Node { + public: + Int_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Int, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Int_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &){ + return const_var(int(atoi(this->text.c_str()))); } - } + }; - }; + struct Float_AST_Node : public AST_Node { + public: + Float_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Float, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Float_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &){ + return const_var(double(atof(this->text.c_str()))); + } - struct Inplace_Fun_Call_AST_Node : public AST_Node { - public: - Inplace_Fun_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inplace_Fun_Call, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Inplace_Fun_Call_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Param_List_Builder plb; + }; - if ((this->children.size() > 1) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) { - for (size_t i = 0; i < this->children[1]->children.size(); ++i) { + struct Id_AST_Node : public AST_Node { + public: + Id_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Id, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Id_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + if (this->text == "true") { + return const_var(true); + } + else if (this->text == "false") { + return const_var(false); + } + else if (this->text == "Infinity") { + return const_var(std::numeric_limits::infinity()); + } + else if (this->text == "NaN") { + return const_var(std::numeric_limits::quiet_NaN()); + } + else { try { - plb << this->children[1]->children[i]->eval(t_ss); + return t_ss.get_object(this->text); } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[1]->children[i]); - throw; + catch (std::exception &) { + throw Eval_Error("Can not find object: " + this->text); } } } + }; - try { - Boxed_Value fn = this->children[0]->eval(t_ss); + struct Char_AST_Node : public AST_Node { + public: + Char_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Char, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Char_AST_Node() {} + }; - try { - return (*boxed_cast(fn))(plb); - } - catch(const dispatch_error &e){ - throw Eval_Error(std::string(e.what()) + " with function '" + this->children[0]->text + "'"); - } - catch(Return_Value &rv) { - return rv.retval; - } - catch(...) { - throw; - } - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw Eval_Error(ee.reason); - } + struct Str_AST_Node : public AST_Node { + public: + Str_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Str, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Str_AST_Node() {} + }; - } + struct Eol_AST_Node : public AST_Node { + public: + Eol_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Eol, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Eol_AST_Node() {} + }; - }; + struct Fun_Call_AST_Node : public AST_Node { + public: + Fun_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Fun_Call, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Fun_Call_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Param_List_Builder plb; - struct Arg_List_AST_Node : public AST_Node { - public: - Arg_List_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Arg_List, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Arg_List_AST_Node() {} - }; - - struct Variable_AST_Node : public AST_Node { - public: - Variable_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Variable, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Variable_AST_Node() {} - }; - - struct Equation_AST_Node : public AST_Node { - public: - Equation_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Equation, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Equation_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; - try { - retval = this->children.back()->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children.back()); - throw; - } - - if (this->children.size() > 1) { - for (int i = static_cast(this->children.size())-3; i >= 0; i -= 2) { - if (this->children[i+1]->text == "=") { + if ((this->children.size() > 1) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) { + for (size_t i = 0; i < this->children[1]->children.size(); ++i) { try { - Boxed_Value lhs = this->children[i]->eval(t_ss); - - try { - if (lhs.is_undef()) { - retval = t_ss.call_function("clone", retval); - retval.clear_dependencies(); - } - - try { - retval = t_ss.call_function(this->children[i+1]->text, lhs, retval); - } - catch(const dispatch_error &){ - throw Eval_Error(std::string("Mismatched types in equation") + (lhs.is_const()?", lhs is const.":".")); - } - } - catch(const dispatch_error &){ - throw Eval_Error("Can not clone right hand side of equation"); - } + plb << this->children[1]->children[i]->eval(t_ss); } catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]); - throw; - } - } - else if (this->children[i+1]->text == ":=") { - try { - Boxed_Value lhs = this->children[i]->eval(t_ss); - if (lhs.is_undef() || type_match(lhs, retval)) { - lhs.assign(retval); - } - else { - throw Eval_Error("Mismatched types in equation"); - } - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]); - throw; - } - } - else { - try { - retval = t_ss.call_function(this->children[i+1]->text, this->children[i]->eval(t_ss), retval); - } - catch(const dispatch_error &){ - throw Eval_Error("Can not find appropriate '" + this->children[i+1]->text + "'"); - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]); + ee.call_stack.push_back(this->children[1]->children[i]); throw; } } } - } - return retval; - } - }; - struct Var_Decl_AST_Node : public AST_Node { - public: - Var_Decl_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Var_Decl, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Var_Decl_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - try { - t_ss.add_object(this->children[0]->text, Boxed_Value()); - } - catch (reserved_word_error &) { - throw Eval_Error("Reserved word used as variable '" + this->children[0]->text + "'"); - } - return t_ss.get_object(this->children[0]->text); - } + Dispatch_Engine::Stack prev_stack = t_ss.get_stack(); + Dispatch_Engine::Stack new_stack = t_ss.new_stack(); - }; - - struct Comparison_AST_Node : public Binary_Operator_AST_Node { - public: - Comparison_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Comparison, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Comparison_AST_Node() {} - }; - - struct Additive_AST_Node : public Binary_Operator_AST_Node { - public: - Additive_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Additive, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Additive_AST_Node() {} - }; - - struct Multiplicative_AST_Node : public Binary_Operator_AST_Node { - public: - Multiplicative_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Multiplicative, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Multiplicative_AST_Node() {} - }; - - struct Array_Call_AST_Node : public AST_Node { - public: - Array_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Array_Call, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Array_Call_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; - - try { - retval = this->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; - } - - for (size_t i = 1; i < this->children.size(); ++i) { try { - retval = t_ss.call_function("[]", retval, this->children[i]->eval(t_ss)); - } - catch(std::out_of_range &) { - throw Eval_Error("Out of bounds exception"); - } - catch(const dispatch_error &){ - throw Eval_Error("Can not find appropriate array lookup '[]' "); - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]); - throw; - } - } - - return retval; - } - }; - - struct Dot_Access_AST_Node : public AST_Node { - public: - Dot_Access_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Dot_Access, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Dot_Access_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; - try { - retval = this->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; - } - - if (this->children.size() > 1) { - for (size_t i = 2; i < this->children.size(); i+=2) { - Param_List_Builder plb; - plb << retval; - - if (this->children[i]->children.size() > 1) { - for (size_t j = 0; j < this->children[i]->children[1]->children.size(); ++j) { - try { - plb << this->children[i]->children[1]->children[j]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]->children[1]->children[j]); - throw; - } - } - } - - std::string fun_name; - if ((this->children[i]->identifier == AST_Node_Type::Fun_Call) || (this->children[i]->identifier == AST_Node_Type::Array_Call)) { - fun_name = this->children[i]->children[0]->text; - } - else { - fun_name = this->children[i]->text; - } - - Dispatch_Engine::Stack prev_stack = t_ss.get_stack(); - Dispatch_Engine::Stack new_stack = t_ss.new_stack(); + Boxed_Value fn = this->children[0]->eval(t_ss); try { t_ss.set_stack(new_stack); - retval = t_ss.call_function(fun_name, plb); + const Boxed_Value &retval = (*boxed_cast(fn))(plb); t_ss.set_stack(prev_stack); + return retval; } - catch(const dispatch_error &e){ + catch(const exception::dispatch_error &e){ t_ss.set_stack(prev_stack); - throw Eval_Error(std::string(e.what())); + throw Eval_Error(std::string(e.what()) + " with function '" + this->children[0]->text + "'"); } catch(Return_Value &rv) { t_ss.set_stack(prev_stack); - retval = rv.retval; + return rv.retval; } catch(...) { t_ss.set_stack(prev_stack); throw; } - if (this->children[i]->identifier == AST_Node_Type::Array_Call) { - for (size_t j = 1; j < this->children[i]->children.size(); ++j) { + } + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + t_ss.set_stack(prev_stack); + throw Eval_Error(ee.reason); + } + + } + + }; + + struct Inplace_Fun_Call_AST_Node : public AST_Node { + public: + Inplace_Fun_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inplace_Fun_Call, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Inplace_Fun_Call_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Param_List_Builder plb; + + if ((this->children.size() > 1) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) { + for (size_t i = 0; i < this->children[1]->children.size(); ++i) { + try { + plb << this->children[1]->children[i]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[1]->children[i]); + throw; + } + } + } + + try { + Boxed_Value fn = this->children[0]->eval(t_ss); + + try { + return (*boxed_cast(fn))(plb); + } + catch(const exception::dispatch_error &e){ + throw Eval_Error(std::string(e.what()) + " with function '" + this->children[0]->text + "'"); + } + catch(Return_Value &rv) { + return rv.retval; + } + catch(...) { + throw; + } + } + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + throw Eval_Error(ee.reason); + } + + } + + }; + + struct Arg_List_AST_Node : public AST_Node { + public: + Arg_List_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Arg_List, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Arg_List_AST_Node() {} + }; + + struct Variable_AST_Node : public AST_Node { + public: + Variable_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Variable, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Variable_AST_Node() {} + }; + + struct Equation_AST_Node : public AST_Node { + public: + Equation_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Equation, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Equation_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; + try { + retval = this->children.back()->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children.back()); + throw; + } + + if (this->children.size() > 1) { + for (int i = static_cast(this->children.size())-3; i >= 0; i -= 2) { + if (this->children[i+1]->text == "=") { try { - retval = t_ss.call_function("[]", retval, this->children[i]->children[j]->eval(t_ss)); - } - catch(std::out_of_range &) { - throw Eval_Error("Out of bounds exception"); - } - catch(const dispatch_error &){ - throw Eval_Error("Can not find appropriate array lookup '[]' "); + Boxed_Value lhs = this->children[i]->eval(t_ss); + + try { + if (lhs.is_undef()) { + retval = t_ss.call_function("clone", retval); + retval.clear_dependencies(); + } + + try { + retval = t_ss.call_function(this->children[i+1]->text, lhs, retval); + } + catch(const exception::dispatch_error &){ + throw Eval_Error(std::string("Mismatched types in equation") + (lhs.is_const()?", lhs is const.":".")); + } + } + catch(const exception::dispatch_error &){ + throw Eval_Error("Can not clone right hand side of equation"); + } } catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]->children[j]); + ee.call_stack.push_back(this->children[i]); + throw; + } + } + else if (this->children[i+1]->text == ":=") { + try { + Boxed_Value lhs = this->children[i]->eval(t_ss); + if (lhs.is_undef() || type_match(lhs, retval)) { + lhs.assign(retval); + } + else { + throw Eval_Error("Mismatched types in equation"); + } + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i]); + throw; + } + } + else { + try { + retval = t_ss.call_function(this->children[i+1]->text, this->children[i]->eval(t_ss), retval); + } + catch(const exception::dispatch_error &){ + throw Eval_Error("Can not find appropriate '" + this->children[i+1]->text + "'"); + } + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[i]); throw; } } } } + return retval; } + }; - return retval; - } - - }; - - struct Quoted_String_AST_Node : public AST_Node { - public: - Quoted_String_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Quoted_String, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Quoted_String_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &){ - return const_var(this->text); - } - - }; - - struct Single_Quoted_String_AST_Node : public AST_Node { - public: - Single_Quoted_String_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Single_Quoted_String, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Single_Quoted_String_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &){ - return const_var(char(this->text[0])); - } - - }; - - struct Lambda_AST_Node : public AST_Node { - public: - Lambda_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Lambda, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Lambda_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - std::vector t_param_names; - size_t numparams = 0; - - if ((this->children.size() > 0) && (this->children[0]->identifier == AST_Node_Type::Arg_List)) { - numparams = this->children[0]->children.size(); - for (size_t i = 0; i < numparams; ++i) { - t_param_names.push_back(this->children[0]->children[i]->text); - } - - } - else { - //no parameters - numparams = 0; - } - - return Boxed_Value(Proxy_Function(new Dynamic_Proxy_Function - (boost::bind(&eval_function, boost::ref(t_ss), this->children.back(), t_param_names, _1), - static_cast(numparams), this->children.back()))); - } - - }; - - struct Block_AST_Node : public AST_Node { - public: - Block_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Block, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Block_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - size_t num_children = this->children.size(); - - t_ss.new_scope(); - for (size_t i = 0; i < num_children; ++i) { + struct Var_Decl_AST_Node : public AST_Node { + public: + Var_Decl_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Var_Decl, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Var_Decl_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ try { - const Boxed_Value &retval = this->children[i]->eval(t_ss); - - if (i + 1 == num_children) - { - t_ss.pop_scope(); - return retval; - } + t_ss.add_object(this->children[0]->text, Boxed_Value()); } - catch (const chaiscript::Return_Value &) { - t_ss.pop_scope(); - throw; + catch (const exception::reserved_word_error &) { + throw Eval_Error("Reserved word used as variable '" + this->children[0]->text + "'"); + } + return t_ss.get_object(this->children[0]->text); + } + + }; + + struct Comparison_AST_Node : public Binary_Operator_AST_Node { + public: + Comparison_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Comparison, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Comparison_AST_Node() {} + }; + + struct Additive_AST_Node : public Binary_Operator_AST_Node { + public: + Additive_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Additive, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Additive_AST_Node() {} + }; + + struct Multiplicative_AST_Node : public Binary_Operator_AST_Node { + public: + Multiplicative_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Multiplicative, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Multiplicative_AST_Node() {} + }; + + struct Array_Call_AST_Node : public AST_Node { + public: + Array_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Array_Call, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Array_Call_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; + + try { + retval = this->children[0]->eval(t_ss); } catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]); - t_ss.pop_scope(); + ee.call_stack.push_back(this->children[0]); throw; } - catch (...) { - t_ss.pop_scope(); - throw; - } - } - t_ss.pop_scope(); - return Boxed_Value(); - } - - }; - - struct Def_AST_Node : public AST_Node { - public: - Def_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Def, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Def_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - std::vector t_param_names; - size_t numparams = 0; - AST_NodePtr guardnode; - - if ((this->children.size() > 2) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) { - numparams = this->children[1]->children.size(); - for (size_t i = 0; i < numparams; ++i) { - t_param_names.push_back(this->children[1]->children[i]->text); - } - - if (this->children.size() > 3) { - guardnode = this->children[2]; - } - } - else { - //no parameters - numparams = 0; - - if (this->children.size() > 2) { - guardnode = this->children[1]; - } - } - - boost::shared_ptr guard; - if (guardnode) { - guard = boost::shared_ptr - (new Dynamic_Proxy_Function(boost::bind(&eval_function, - boost::ref(t_ss), guardnode, - t_param_names, _1), static_cast(numparams), guardnode)); - } - - try { - const std::string & l_function_name = this->children[0]->text; - const std::string & l_annotation = this->annotation?this->annotation->text:""; - t_ss.add(Proxy_Function - (new Dynamic_Proxy_Function(boost::bind(&eval_function, - boost::ref(t_ss), this->children.back(), - t_param_names, _1), static_cast(numparams), this->children.back(), - l_annotation, guard)), l_function_name); - } - catch (reserved_word_error &e) { - throw Eval_Error("Reserved word used as function name '" + e.word + "'"); - } - return Boxed_Value(); - } - - }; - - struct While_AST_Node : public AST_Node { - public: - While_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::While, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~While_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - bool cond; - - t_ss.new_scope(); - - try { - cond = boxed_cast(this->children[0]->eval(t_ss)); - } - catch (const exception::bad_boxed_cast &) { - t_ss.pop_scope(); - throw Eval_Error("While condition not boolean"); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - t_ss.pop_scope(); - throw; - } - while (cond) { - try { + for (size_t i = 1; i < this->children.size(); ++i) { try { - this->children[1]->eval(t_ss); + retval = t_ss.call_function("[]", retval, this->children[i]->eval(t_ss)); } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[1]); - throw; + catch(std::out_of_range &) { + throw Eval_Error("Out of bounds exception"); } - - try { - cond = boxed_cast(this->children[0]->eval(t_ss)); + catch(const exception::dispatch_error &){ + throw Eval_Error("Can not find appropriate array lookup '[]' "); } - catch (const exception::bad_boxed_cast &) { - t_ss.pop_scope(); - throw Eval_Error("While condition not boolean"); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - t_ss.pop_scope(); + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[i]); throw; } } - catch (Break_Loop &) { - cond = false; - } - } - t_ss.pop_scope(); - return Boxed_Value(); - } - ; - }; - struct If_AST_Node : public AST_Node { - public: - If_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::If, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~If_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - bool cond; - try { - cond = boxed_cast(this->children[0]->eval(t_ss)); - } - catch (const exception::bad_boxed_cast &) { - throw Eval_Error("If condition not boolean"); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; + return retval; } + }; - if (cond) { + struct Dot_Access_AST_Node : public AST_Node { + public: + Dot_Access_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Dot_Access, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Dot_Access_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; try { - return this->children[1]->eval(t_ss); + retval = this->children[0]->eval(t_ss); } catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[1]); + ee.call_stack.push_back(this->children[0]); throw; } - } - else { - if (this->children.size() > 2) { - size_t i = 2; - while ((!cond) && (i < this->children.size())) { - if (this->children[i]->text == "else") { - try { - return this->children[i+1]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i+1]); - throw; - } - } - else if (this->children[i]->text == "else if") { - try { - cond = boxed_cast(this->children[i+1]->eval(t_ss)); - } - catch (const exception::bad_boxed_cast &) { - throw Eval_Error("'else if' condition not boolean"); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i+1]); - throw; - } - if (cond) { + + if (this->children.size() > 1) { + for (size_t i = 2; i < this->children.size(); i+=2) { + Param_List_Builder plb; + plb << retval; + + if (this->children[i]->children.size() > 1) { + for (size_t j = 0; j < this->children[i]->children[1]->children.size(); ++j) { try { - return this->children[i+2]->eval(t_ss); + plb << this->children[i]->children[1]->children[j]->eval(t_ss); } catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i+2]); + ee.call_stack.push_back(this->children[i]->children[1]->children[j]); + throw; + } + } + } + + std::string fun_name; + if ((this->children[i]->identifier == AST_Node_Type::Fun_Call) || (this->children[i]->identifier == AST_Node_Type::Array_Call)) { + fun_name = this->children[i]->children[0]->text; + } + else { + fun_name = this->children[i]->text; + } + + Dispatch_Engine::Stack prev_stack = t_ss.get_stack(); + Dispatch_Engine::Stack new_stack = t_ss.new_stack(); + + try { + t_ss.set_stack(new_stack); + retval = t_ss.call_function(fun_name, plb); + t_ss.set_stack(prev_stack); + } + catch(const exception::dispatch_error &e){ + t_ss.set_stack(prev_stack); + throw Eval_Error(std::string(e.what())); + } + catch(Return_Value &rv) { + t_ss.set_stack(prev_stack); + retval = rv.retval; + } + catch(...) { + t_ss.set_stack(prev_stack); + throw; + } + if (this->children[i]->identifier == AST_Node_Type::Array_Call) { + for (size_t j = 1; j < this->children[i]->children.size(); ++j) { + try { + retval = t_ss.call_function("[]", retval, this->children[i]->children[j]->eval(t_ss)); + } + catch(std::out_of_range &) { + throw Eval_Error("Out of bounds exception"); + } + catch(const exception::dispatch_error &){ + throw Eval_Error("Can not find appropriate array lookup '[]' "); + } + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[i]->children[j]); throw; } } } - i = i + 3; } } + + return retval; } - return Boxed_Value(false); - } + }; - }; + struct Quoted_String_AST_Node : public AST_Node { + public: + Quoted_String_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Quoted_String, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Quoted_String_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &){ + return const_var(this->text); + } - struct For_AST_Node : public AST_Node { - public: - For_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::For, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~For_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - bool cond; + }; - t_ss.new_scope(); + struct Single_Quoted_String_AST_Node : public AST_Node { + public: + Single_Quoted_String_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Single_Quoted_String, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Single_Quoted_String_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &){ + return const_var(char(this->text[0])); + } - try { - if (this->children.size() == 4) { - try { - this->children[0]->eval(t_ss); + }; + + struct Lambda_AST_Node : public AST_Node { + public: + Lambda_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Lambda, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Lambda_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + std::vector t_param_names; + size_t numparams = 0; + + if ((this->children.size() > 0) && (this->children[0]->identifier == AST_Node_Type::Arg_List)) { + numparams = this->children[0]->children.size(); + for (size_t i = 0; i < numparams; ++i) { + t_param_names.push_back(this->children[0]->children[i]->text); } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); + + } + else { + //no parameters + numparams = 0; + } + + return Boxed_Value(Proxy_Function(new Dynamic_Proxy_Function + (boost::bind(&eval_function, boost::ref(t_ss), this->children.back(), t_param_names, _1), + static_cast(numparams), this->children.back()))); + } + + }; + + struct Block_AST_Node : public AST_Node { + public: + Block_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Block, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Block_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + size_t num_children = this->children.size(); + + t_ss.new_scope(); + for (size_t i = 0; i < num_children; ++i) { + try { + const Boxed_Value &retval = this->children[i]->eval(t_ss); + + if (i + 1 == num_children) + { + t_ss.pop_scope(); + return retval; + } + } + catch (const chaiscript::Return_Value &) { + t_ss.pop_scope(); throw; } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i]); + t_ss.pop_scope(); + throw; + } + catch (...) { + t_ss.pop_scope(); + throw; + } + } + t_ss.pop_scope(); + return Boxed_Value(); + } + + }; + + struct Def_AST_Node : public AST_Node { + public: + Def_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Def, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Def_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + std::vector t_param_names; + size_t numparams = 0; + AST_NodePtr guardnode; + + if ((this->children.size() > 2) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) { + numparams = this->children[1]->children.size(); + for (size_t i = 0; i < numparams; ++i) { + t_param_names.push_back(this->children[1]->children[i]->text); + } + + if (this->children.size() > 3) { + guardnode = this->children[2]; + } + } + else { + //no parameters + numparams = 0; + + if (this->children.size() > 2) { + guardnode = this->children[1]; + } + } + + boost::shared_ptr guard; + if (guardnode) { + guard = boost::shared_ptr + (new Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(t_ss), guardnode, + t_param_names, _1), static_cast(numparams), guardnode)); + } + + try { + const std::string & l_function_name = this->children[0]->text; + const std::string & l_annotation = this->annotation?this->annotation->text:""; + t_ss.add(Proxy_Function + (new Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(t_ss), this->children.back(), + t_param_names, _1), static_cast(numparams), this->children.back(), + l_annotation, guard)), l_function_name); + } + catch (const exception::reserved_word_error &e) { + throw Eval_Error("Reserved word used as function name '" + e.word() + "'"); + } + return Boxed_Value(); + } + + }; + + struct While_AST_Node : public AST_Node { + public: + While_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::While, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~While_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + bool cond; + + t_ss.new_scope(); + + try { + cond = boxed_cast(this->children[0]->eval(t_ss)); + } + catch (const exception::bad_boxed_cast &) { + t_ss.pop_scope(); + throw Eval_Error("While condition not boolean"); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + t_ss.pop_scope(); + throw; + } + while (cond) { try { - cond = boxed_cast(this->children[1]->eval(t_ss)); + try { + this->children[1]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[1]); + throw; + } + + try { + cond = boxed_cast(this->children[0]->eval(t_ss)); + } + catch (const exception::bad_boxed_cast &) { + t_ss.pop_scope(); + throw Eval_Error("While condition not boolean"); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + t_ss.pop_scope(); + throw; + } + } + catch (Break_Loop &) { + cond = false; + } + } + t_ss.pop_scope(); + return Boxed_Value(); + } + ; + }; + + struct If_AST_Node : public AST_Node { + public: + If_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::If, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~If_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + bool cond; + try { + cond = boxed_cast(this->children[0]->eval(t_ss)); + } + catch (const exception::bad_boxed_cast &) { + throw Eval_Error("If condition not boolean"); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + throw; + } + + if (cond) { + try { + return this->children[1]->eval(t_ss); } catch (Eval_Error &ee) { ee.call_stack.push_back(this->children[1]); @@ -804,36 +734,66 @@ namespace chaiscript } } else { - try { - cond = boxed_cast(this->children[0]->eval(t_ss)); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - t_ss.pop_scope(); - throw; + if (this->children.size() > 2) { + size_t i = 2; + while ((!cond) && (i < this->children.size())) { + if (this->children[i]->text == "else") { + try { + return this->children[i+1]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i+1]); + throw; + } + } + else if (this->children[i]->text == "else if") { + try { + cond = boxed_cast(this->children[i+1]->eval(t_ss)); + } + catch (const exception::bad_boxed_cast &) { + throw Eval_Error("'else if' condition not boolean"); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i+1]); + throw; + } + if (cond) { + try { + return this->children[i+2]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i+2]); + throw; + } + } + } + i = i + 3; + } } } + + return Boxed_Value(false); } - catch (const exception::bad_boxed_cast &) { - t_ss.pop_scope(); - throw Eval_Error("For condition not boolean"); - } - while (cond) { + + }; + + struct For_AST_Node : public AST_Node { + public: + For_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::For, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~For_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + bool cond; + + t_ss.new_scope(); + try { if (this->children.size() == 4) { try { - this->children[3]->eval(t_ss); + this->children[0]->eval(t_ss); } catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[3]); - throw; - } - - try { - this->children[2]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[2]); + ee.call_stack.push_back(this->children[0]); throw; } @@ -846,22 +806,6 @@ namespace chaiscript } } else { - try { - this->children[2]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[2]); - throw; - } - - try { - this->children[1]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[1]); - throw; - } - try { cond = boxed_cast(this->children[0]->eval(t_ss)); } @@ -876,251 +820,335 @@ namespace chaiscript t_ss.pop_scope(); throw Eval_Error("For condition not boolean"); } - catch (Break_Loop &) { - cond = false; - } - } - t_ss.pop_scope(); - return Boxed_Value(); - } - - }; - - struct Inline_Array_AST_Node : public AST_Node { - public: - Inline_Array_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Array, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Inline_Array_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - std::vector vec; - if (this->children.size() > 0) { - for (size_t i = 0; i < this->children[0]->children.size(); ++i) { + while (cond) { try { - vec.push_back(this->children[0]->children[i]->eval(t_ss)); + if (this->children.size() == 4) { + try { + this->children[3]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[3]); + throw; + } + + try { + this->children[2]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[2]); + throw; + } + + try { + cond = boxed_cast(this->children[1]->eval(t_ss)); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[1]); + throw; + } + } + else { + try { + this->children[2]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[2]); + throw; + } + + try { + this->children[1]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[1]); + throw; + } + + try { + cond = boxed_cast(this->children[0]->eval(t_ss)); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + t_ss.pop_scope(); + throw; + } + } } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]->children[i]); - throw; - } - } - } - - return const_var(vec); - } - - }; - - struct Inline_Map_AST_Node : public AST_Node { - public: - Inline_Map_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Map, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Inline_Map_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - try { - std::map retval; - for (size_t i = 0; i < this->children[0]->children.size(); ++i) { - try { - retval[boxed_cast(this->children[0]->children[i]->children[0]->eval(t_ss))] - = t_ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(t_ss)); - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]->children[i]); - throw; - } - } - return const_var(retval); - } - catch (const dispatch_error &) { - throw Eval_Error("Can not find appropriate 'Map()'"); - } - } - - }; - - struct Return_AST_Node : public AST_Node { - public: - Return_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Return, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Return_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - if (this->children.size() > 0) { - try { - throw Return_Value(this->children[0]->eval(t_ss)); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; - } - } - else { - throw Return_Value(Boxed_Value()); - } - } - - }; - - struct File_AST_Node : public AST_Node { - public: - File_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::File, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~File_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss) { - const size_t size = this->children.size(); - for (size_t i = 0; i < size; ++i) { - try { - const Boxed_Value &retval = this->children[i]->eval(t_ss); - if (i + 1 == size) { - return retval; - } - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i]); - throw; - } - } - return Boxed_Value(); - } - }; - - struct Prefix_AST_Node : public AST_Node { - public: - Prefix_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Prefix, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Prefix_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - try { - return t_ss.call_function(this->children[0]->text, this->children[1]->eval(t_ss)); - } - catch(std::exception &){ - throw Eval_Error("Can not find appropriate unary '" + this->children[0]->text + "'"); - } - } - - }; - - struct Break_AST_Node : public AST_Node { - public: - Break_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Break, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Break_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &){ - throw Break_Loop(); - } - }; - - struct Map_Pair_AST_Node : public AST_Node { - public: - Map_Pair_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Map_Pair, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Map_Pair_AST_Node() {} - }; - - struct Value_Range_AST_Node : public AST_Node { - public: - Value_Range_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Value_Range, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Value_Range_AST_Node() {} - }; - - struct Inline_Range_AST_Node : public AST_Node { - public: - Inline_Range_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Range, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Inline_Range_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - try { - return t_ss.call_function("generate_range", - this->children[0]->children[0]->children[0]->eval(t_ss), - this->children[0]->children[0]->children[1]->eval(t_ss)); - } - catch (const dispatch_error &) { - throw Eval_Error("Unable to generate range vector"); - } - catch(Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]->children[0]); - throw; - } - } - - }; - - struct Annotation_AST_Node : public AST_Node { - public: - Annotation_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Annotation, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Annotation_AST_Node() {} - }; - - struct Try_AST_Node : public AST_Node { - public: - Try_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Try, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Try_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; - - t_ss.new_scope(); - try { - retval = this->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - if (this->children.back()->identifier == AST_Node_Type::Finally) { - try { - this->children.back()->children[0]->eval(t_ss); - } - catch (Eval_Error &ee2) { - ee2.call_stack.push_back(this->children.back()->children[0]); + catch (const exception::bad_boxed_cast &) { t_ss.pop_scope(); - throw; + throw Eval_Error("For condition not boolean"); + } + catch (Break_Loop &) { + cond = false; } } t_ss.pop_scope(); - throw; + return Boxed_Value(); } - catch (const std::exception &e) { - Boxed_Value except = Boxed_Value(boost::ref(e)); - size_t end_point = this->children.size(); - if (this->children.back()->identifier == AST_Node_Type::Finally) { - assert(end_point > 0); - end_point = this->children.size() - 1; + }; + + struct Inline_Array_AST_Node : public AST_Node { + public: + Inline_Array_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Array, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Inline_Array_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + std::vector vec; + if (this->children.size() > 0) { + for (size_t i = 0; i < this->children[0]->children.size(); ++i) { + try { + vec.push_back(this->children[0]->children[i]->eval(t_ss)); + } + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]->children[i]); + throw; + } + } } - for (unsigned int i = 1; i < end_point; ++i) { - AST_NodePtr catch_block = this->children[i]; - if (catch_block->children.size() == 1) { - //No variable capture, no guards + return const_var(vec); + } + + }; + + struct Inline_Map_AST_Node : public AST_Node { + public: + Inline_Map_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Map, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Inline_Map_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + try { + std::map retval; + for (size_t i = 0; i < this->children[0]->children.size(); ++i) { try { - retval = catch_block->children[0]->eval(t_ss); + retval[boxed_cast(this->children[0]->children[i]->children[0]->eval(t_ss))] + = t_ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(t_ss)); } - catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[0]); + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]->children[i]); throw; } - break; } - else if (catch_block->children.size() == 2) { - //Variable capture, no guards - t_ss.add_object(catch_block->children[0]->text, except); - try { - retval = catch_block->children[1]->eval(t_ss); + return const_var(retval); + } + catch (const exception::dispatch_error &) { + throw Eval_Error("Can not find appropriate 'Map()'"); + } + } + + }; + + struct Return_AST_Node : public AST_Node { + public: + Return_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Return, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Return_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + if (this->children.size() > 0) { + try { + throw Return_Value(this->children[0]->eval(t_ss)); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + throw; + } + } + else { + throw Return_Value(Boxed_Value()); + } + } + + }; + + struct File_AST_Node : public AST_Node { + public: + File_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::File, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~File_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss) { + const size_t size = this->children.size(); + for (size_t i = 0; i < size; ++i) { + try { + const Boxed_Value &retval = this->children[i]->eval(t_ss); + if (i + 1 == size) { + return retval; } - catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[1]); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i]); + throw; + } + } + return Boxed_Value(); + } + }; + + struct Prefix_AST_Node : public AST_Node { + public: + Prefix_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Prefix, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Prefix_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + try { + return t_ss.call_function(this->children[0]->text, this->children[1]->eval(t_ss)); + } + catch(std::exception &){ + throw Eval_Error("Can not find appropriate unary '" + this->children[0]->text + "'"); + } + } + + }; + + struct Break_AST_Node : public AST_Node { + public: + Break_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Break, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Break_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &){ + throw Break_Loop(); + } + }; + + struct Map_Pair_AST_Node : public AST_Node { + public: + Map_Pair_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Map_Pair, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Map_Pair_AST_Node() {} + }; + + struct Value_Range_AST_Node : public AST_Node { + public: + Value_Range_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Value_Range, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Value_Range_AST_Node() {} + }; + + struct Inline_Range_AST_Node : public AST_Node { + public: + Inline_Range_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Range, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Inline_Range_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + try { + return t_ss.call_function("generate_range", + this->children[0]->children[0]->children[0]->eval(t_ss), + this->children[0]->children[0]->children[1]->eval(t_ss)); + } + catch (const exception::dispatch_error &) { + throw Eval_Error("Unable to generate range vector"); + } + catch(Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]->children[0]); + throw; + } + } + + }; + + struct Annotation_AST_Node : public AST_Node { + public: + Annotation_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Annotation, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Annotation_AST_Node() {} + }; + + struct Try_AST_Node : public AST_Node { + public: + Try_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Try, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Try_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; + + t_ss.new_scope(); + try { + retval = this->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + if (this->children.back()->identifier == AST_Node_Type::Finally) { + try { + this->children.back()->children[0]->eval(t_ss); + } + catch (Eval_Error &ee2) { + ee2.call_stack.push_back(this->children.back()->children[0]); + t_ss.pop_scope(); throw; } - - break; } - else if (catch_block->children.size() == 3) { - //Variable capture, no guards - t_ss.add_object(catch_block->children[0]->text, except); + t_ss.pop_scope(); + throw; + } + catch (const std::exception &e) { + Boxed_Value except = Boxed_Value(boost::ref(e)); - bool guard; - try { - guard = boxed_cast(catch_block->children[1]->eval(t_ss)); - } catch (const exception::bad_boxed_cast &) { + size_t end_point = this->children.size(); + if (this->children.back()->identifier == AST_Node_Type::Finally) { + assert(end_point > 0); + end_point = this->children.size() - 1; + } + for (unsigned int i = 1; i < end_point; ++i) { + AST_NodePtr catch_block = this->children[i]; + + if (catch_block->children.size() == 1) { + //No variable capture, no guards + try { + retval = catch_block->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(catch_block->children[0]); + throw; + } + break; + } + else if (catch_block->children.size() == 2) { + //Variable capture, no guards + t_ss.add_object(catch_block->children[0]->text, except); + try { + retval = catch_block->children[1]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(catch_block->children[1]); + throw; + } + + break; + } + else if (catch_block->children.size() == 3) { + //Variable capture, no guards + t_ss.add_object(catch_block->children[0]->text, except); + + bool guard; + try { + guard = boxed_cast(catch_block->children[1]->eval(t_ss)); + } catch (const exception::bad_boxed_cast &) { + if (this->children.back()->identifier == AST_Node_Type::Finally) { + try { + this->children.back()->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children.back()->children[0]); + t_ss.pop_scope(); + throw; + } + } + t_ss.pop_scope(); + throw Eval_Error("Guard condition not boolean"); + } + if (guard) { + try { + retval = catch_block->children[2]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(catch_block->children[2]); + throw; + } + + break; + } + } + else { if (this->children.back()->identifier == AST_Node_Type::Finally) { try { this->children.back()->children[0]->eval(t_ss); @@ -1132,75 +1160,79 @@ namespace chaiscript } } t_ss.pop_scope(); - throw Eval_Error("Guard condition not boolean"); + throw Eval_Error("Internal error: catch block size unrecognized"); } - if (guard) { + } + } + catch (Boxed_Value &bv) { + Boxed_Value except = bv; + for (size_t i = 1; i < this->children.size(); ++i) { + AST_NodePtr catch_block = this->children[i]; + + if (catch_block->children.size() == 1) { + //No variable capture, no guards try { - retval = catch_block->children[2]->eval(t_ss); + retval = catch_block->children[0]->eval(t_ss); } catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[2]); + ee.call_stack.push_back(catch_block->children[0]); throw; } break; } - } - else { - if (this->children.back()->identifier == AST_Node_Type::Finally) { + else if (catch_block->children.size() == 2) { + //Variable capture, no guards + t_ss.add_object(catch_block->children[0]->text, except); try { - this->children.back()->children[0]->eval(t_ss); + retval = catch_block->children[1]->eval(t_ss); } catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children.back()->children[0]); - t_ss.pop_scope(); + ee.call_stack.push_back(catch_block->children[1]); throw; } - } - t_ss.pop_scope(); - throw Eval_Error("Internal error: catch block size unrecognized"); - } - } - } - catch (Boxed_Value &bv) { - Boxed_Value except = bv; - for (size_t i = 1; i < this->children.size(); ++i) { - AST_NodePtr catch_block = this->children[i]; - if (catch_block->children.size() == 1) { - //No variable capture, no guards - try { - retval = catch_block->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[0]); - throw; + break; } + else if (catch_block->children.size() == 3) { + //Variable capture, no guards + t_ss.add_object(catch_block->children[0]->text, except); - break; - } - else if (catch_block->children.size() == 2) { - //Variable capture, no guards - t_ss.add_object(catch_block->children[0]->text, except); - try { - retval = catch_block->children[1]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[1]); - throw; - } + bool guard; + try { + guard = boxed_cast(catch_block->children[1]->eval(t_ss)); + } + catch (const exception::bad_boxed_cast &) { + if (this->children.back()->identifier == AST_Node_Type::Finally) { + try { + this->children.back()->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children.back()->children[0]); + t_ss.pop_scope(); + throw; + } + } - break; - } - else if (catch_block->children.size() == 3) { - //Variable capture, no guards - t_ss.add_object(catch_block->children[0]->text, except); - - bool guard; - try { - guard = boxed_cast(catch_block->children[1]->eval(t_ss)); + t_ss.pop_scope(); + throw Eval_Error("Guard condition not boolean"); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(catch_block->children[1]); + throw; + } + if (guard) { + try { + retval = catch_block->children[2]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(catch_block->children[2]); + throw; + } + break; + } } - catch (const exception::bad_boxed_cast &) { + else { if (this->children.back()->identifier == AST_Node_Type::Finally) { try { this->children.back()->children[0]->eval(t_ss); @@ -1211,45 +1243,29 @@ namespace chaiscript throw; } } - t_ss.pop_scope(); - throw Eval_Error("Guard condition not boolean"); + throw Eval_Error("Internal error: catch block size unrecognized"); } - catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[1]); - throw; - } - if (guard) { - try { - retval = catch_block->children[2]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(catch_block->children[2]); - throw; - } - break; - } - } - else { - if (this->children.back()->identifier == AST_Node_Type::Finally) { - try { - this->children.back()->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children.back()->children[0]); - t_ss.pop_scope(); - throw; - } - } - t_ss.pop_scope(); - throw Eval_Error("Internal error: catch block size unrecognized"); } } - } - catch (...) { + catch (...) { + if (this->children.back()->identifier == AST_Node_Type::Finally) { + try { + this->children.back()->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children.back()->children[0]); + t_ss.pop_scope(); + throw; + } + } + t_ss.pop_scope(); + throw; + } + if (this->children.back()->identifier == AST_Node_Type::Finally) { try { - this->children.back()->children[0]->eval(t_ss); + retval = this->children.back()->children[0]->eval(t_ss); } catch (Eval_Error &ee) { ee.call_stack.push_back(this->children.back()->children[0]); @@ -1257,258 +1273,245 @@ namespace chaiscript throw; } } + t_ss.pop_scope(); - throw; + + return retval; } - if (this->children.back()->identifier == AST_Node_Type::Finally) { - try { - retval = this->children.back()->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children.back()->children[0]); - t_ss.pop_scope(); - throw; - } - } + }; - t_ss.pop_scope(); + struct Catch_AST_Node : public AST_Node { + public: + Catch_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Catch, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Catch_AST_Node() {} + }; - return retval; - } + struct Finally_AST_Node : public AST_Node { + public: + Finally_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Finally, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Finally_AST_Node() {} + }; - }; + struct Method_AST_Node : public AST_Node { + public: + Method_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Method, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Method_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - struct Catch_AST_Node : public AST_Node { - public: - Catch_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Catch, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Catch_AST_Node() {} - }; + std::vector t_param_names; + AST_NodePtr guardnode; - struct Finally_AST_Node : public AST_Node { - public: - Finally_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Finally, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Finally_AST_Node() {} - }; + //The first param of a method is always the implied this ptr. + t_param_names.push_back("this"); - struct Method_AST_Node : public AST_Node { - public: - Method_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Method, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Method_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - - std::vector t_param_names; - AST_NodePtr guardnode; - - //The first param of a method is always the implied this ptr. - t_param_names.push_back("this"); - - if ((this->children.size() > 3) && (this->children[2]->identifier == AST_Node_Type::Arg_List)) { - for (size_t i = 0; i < this->children[2]->children.size(); ++i) { - t_param_names.push_back(this->children[2]->children[i]->text); - } - - if (this->children.size() > 4) { - guardnode = this->children[3]; - } - } - else { - //no parameters - - if (this->children.size() > 3) { - guardnode = this->children[2]; - } - } - - size_t numparams = t_param_names.size(); - - boost::shared_ptr guard; - if (guardnode) { - guard = boost::shared_ptr - (new Dynamic_Proxy_Function(boost::bind(&eval_function, - boost::ref(t_ss), guardnode, - t_param_names, _1), static_cast(numparams), guardnode)); - } - - try { - const std::string & l_annotation = this->annotation?this->annotation->text:""; - const std::string & class_name = this->children[0]->text; - const std::string & function_name = this->children[1]->text; - if (function_name == class_name) { - t_ss.add(Proxy_Function - (new detail::Dynamic_Object_Constructor(class_name, Proxy_Function - (new Dynamic_Proxy_Function(boost::bind(&eval_function, - boost::ref(t_ss), this->children.back(), - t_param_names, _1), static_cast(numparams), this->children.back(), - l_annotation, guard)))), function_name); + if ((this->children.size() > 3) && (this->children[2]->identifier == AST_Node_Type::Arg_List)) { + for (size_t i = 0; i < this->children[2]->children.size(); ++i) { + t_param_names.push_back(this->children[2]->children[i]->text); + } + if (this->children.size() > 4) { + guardnode = this->children[3]; + } } else { - boost::optional ti; - try { - ti = t_ss.get_type(class_name); - } catch (const std::range_error &) { - // No biggie, the type name is just not known - } - t_ss.add(Proxy_Function - (new detail::Dynamic_Object_Function(class_name, Proxy_Function - (new Dynamic_Proxy_Function(boost::bind(&eval_function, - boost::ref(t_ss), this->children.back(), - t_param_names, _1), static_cast(numparams), this->children.back(), - l_annotation, guard)), ti)), function_name); + //no parameters + if (this->children.size() > 3) { + guardnode = this->children[2]; + } } - } - catch (reserved_word_error &e) { - throw Eval_Error("Reserved word used as method name '" + e.word + "'"); - } - return Boxed_Value(); - } - }; + size_t numparams = t_param_names.size(); - struct Attr_Decl_AST_Node : public AST_Node { - public: - Attr_Decl_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Attr_Decl, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Attr_Decl_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - try { - t_ss.add(fun(boost::function(boost::bind(&detail::Dynamic_Object_Attribute::func, this->children[0]->text, - this->children[1]->text, _1))), this->children[1]->text); + boost::shared_ptr guard; + if (guardnode) { + guard = boost::shared_ptr + (new Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(t_ss), guardnode, + t_param_names, _1), static_cast(numparams), guardnode)); + } - } - catch (reserved_word_error &) { - throw Eval_Error("Reserved word used as attribute '" + this->children[1]->text + "'"); - } - return Boxed_Value(); - } + try { + const std::string & l_annotation = this->annotation?this->annotation->text:""; + const std::string & class_name = this->children[0]->text; + const std::string & function_name = this->children[1]->text; + if (function_name == class_name) { + t_ss.add(Proxy_Function + (new detail::Dynamic_Object_Constructor(class_name, Proxy_Function + (new Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(t_ss), this->children.back(), + t_param_names, _1), static_cast(numparams), this->children.back(), + l_annotation, guard)))), function_name); - }; - - struct Shift_AST_Node : public Binary_Operator_AST_Node { - public: - Shift_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Shift, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Shift_AST_Node() {} - }; - - struct Equality_AST_Node : public Binary_Operator_AST_Node { - public: - Equality_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Equality, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Equality_AST_Node() {} - }; - - struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node { - public: - Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_And, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Bitwise_And_AST_Node() {} - }; - - struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node { - public: - Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Xor, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Bitwise_Xor_AST_Node() {} - }; - - struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node { - public: - Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Or, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Bitwise_Or_AST_Node() {} - }; - - struct Logical_And_AST_Node : public AST_Node { - public: - Logical_And_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Logical_And, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Logical_And_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; - try { - retval = this->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; - } - - if (this->children.size() > 1) { - for (size_t i = 1; i < this->children.size(); i += 2) { - bool lhs; - try { - lhs = boxed_cast(retval); - } - catch (const exception::bad_boxed_cast &) { - throw Eval_Error("Condition not boolean"); - } - if (lhs) { - try { - retval = this->children[i+1]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i+1]); - throw; - } - } - else { - retval = Boxed_Value(false); - } - } - } - return retval; - } - }; - - struct Logical_Or_AST_Node : public AST_Node { - public: - Logical_Or_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Logical_Or, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : - AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } - virtual ~Logical_Or_AST_Node() {} - virtual Boxed_Value eval(Dispatch_Engine &t_ss){ - Boxed_Value retval; - - try { - retval = this->children[0]->eval(t_ss); - } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[0]); - throw; - } - - if (this->children.size() > 1) { - for (size_t i = 1; i < this->children.size(); i += 2) { - bool lhs; - try { - lhs = boxed_cast(retval); - } - catch (const exception::bad_boxed_cast &) { - throw Eval_Error("Condition not boolean"); - } - if (lhs) { - retval = Boxed_Value(true); } else { + boost::optional ti; try { - retval = this->children[i+1]->eval(t_ss); + ti = t_ss.get_type(class_name); + } catch (const std::range_error &) { + // No biggie, the type name is just not known } - catch (Eval_Error &ee) { - ee.call_stack.push_back(this->children[i+1]); - throw; + t_ss.add(Proxy_Function + (new detail::Dynamic_Object_Function(class_name, Proxy_Function + (new Dynamic_Proxy_Function(boost::bind(&eval_function, + boost::ref(t_ss), this->children.back(), + t_param_names, _1), static_cast(numparams), this->children.back(), + l_annotation, guard)), ti)), function_name); + + } + } + catch (const exception::reserved_word_error &e) { + throw Eval_Error("Reserved word used as method name '" + e.word() + "'"); + } + return Boxed_Value(); + } + + }; + + struct Attr_Decl_AST_Node : public AST_Node { + public: + Attr_Decl_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Attr_Decl, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Attr_Decl_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + try { + t_ss.add(fun(boost::function(boost::bind(&detail::Dynamic_Object_Attribute::func, this->children[0]->text, + this->children[1]->text, _1))), this->children[1]->text); + + } + catch (const exception::reserved_word_error &) { + throw Eval_Error("Reserved word used as attribute '" + this->children[1]->text + "'"); + } + return Boxed_Value(); + } + + }; + + struct Shift_AST_Node : public Binary_Operator_AST_Node { + public: + Shift_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Shift, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Shift_AST_Node() {} + }; + + struct Equality_AST_Node : public Binary_Operator_AST_Node { + public: + Equality_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Equality, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Equality_AST_Node() {} + }; + + struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node { + public: + Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_And, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Bitwise_And_AST_Node() {} + }; + + struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node { + public: + Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Xor, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Bitwise_Xor_AST_Node() {} + }; + + struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node { + public: + Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Or, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Binary_Operator_AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Bitwise_Or_AST_Node() {} + }; + + struct Logical_And_AST_Node : public AST_Node { + public: + Logical_And_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Logical_And, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Logical_And_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; + try { + retval = this->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + throw; + } + + if (this->children.size() > 1) { + for (size_t i = 1; i < this->children.size(); i += 2) { + bool lhs; + try { + lhs = boxed_cast(retval); + } + catch (const exception::bad_boxed_cast &) { + throw Eval_Error("Condition not boolean"); + } + if (lhs) { + try { + retval = this->children[i+1]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i+1]); + throw; + } + } + else { + retval = Boxed_Value(false); } } } + return retval; } - return retval; - } + }; - }; + struct Logical_Or_AST_Node : public AST_Node { + public: + Logical_Or_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Logical_Or, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } + virtual ~Logical_Or_AST_Node() {} + virtual Boxed_Value eval(Dispatch_Engine &t_ss){ + Boxed_Value retval; + + try { + retval = this->children[0]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[0]); + throw; + } + + if (this->children.size() > 1) { + for (size_t i = 1; i < this->children.size(); i += 2) { + bool lhs; + try { + lhs = boxed_cast(retval); + } + catch (const exception::bad_boxed_cast &) { + throw Eval_Error("Condition not boolean"); + } + if (lhs) { + retval = Boxed_Value(true); + } + else { + try { + retval = this->children[i+1]->eval(t_ss); + } + catch (Eval_Error &ee) { + ee.call_stack.push_back(this->children[i+1]); + throw; + } + } + } + } + return retval; + } + + }; + } } diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 9d18c29..de8b393 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -16,20 +16,23 @@ namespace chaiscript { - enum Alphabet - { symbol_alphabet = 0 - , keyword_alphabet - , int_alphabet - , float_alphabet - , x_alphabet - , hex_alphabet - , b_alphabet - , bin_alphabet - , id_alphabet - , white_alphabet - , max_alphabet - , lengthof_alphabet = 256 - }; + namespace detail + { + enum Alphabet + { symbol_alphabet = 0 + , keyword_alphabet + , int_alphabet + , float_alphabet + , x_alphabet + , hex_alphabet + , b_alphabet + , bin_alphabet + , id_alphabet + , white_alphabet + , max_alphabet + , lengthof_alphabet = 256 + }; + } class ChaiScript_Parser { @@ -40,7 +43,7 @@ namespace chaiscript std::string m_singleline_comment; boost::shared_ptr m_filename; std::vector m_match_stack; - bool m_alphabet[max_alphabet][lengthof_alphabet]; + bool m_alphabet[detail::max_alphabet][detail::lengthof_alphabet]; std::vector > m_operator_matches; std::vector m_operators; @@ -122,55 +125,55 @@ namespace chaiscript dot_access.push_back("."); m_operator_matches.push_back(dot_access); - for ( int c = 0 ; c < lengthof_alphabet ; ++c ) { - for ( int a = 0 ; a < max_alphabet ; a ++ ) { + for ( int c = 0 ; c < detail::lengthof_alphabet ; ++c ) { + for ( int a = 0 ; a < detail::max_alphabet ; a ++ ) { m_alphabet[a][c]=false; } } - m_alphabet[symbol_alphabet]['+']=true; - m_alphabet[symbol_alphabet]['-']=true; - m_alphabet[symbol_alphabet]['*']=true; - m_alphabet[symbol_alphabet]['/']=true; - m_alphabet[symbol_alphabet]['|']=true; - m_alphabet[symbol_alphabet]['&']=true; - m_alphabet[symbol_alphabet]['^']=true; - m_alphabet[symbol_alphabet]['=']=true; - m_alphabet[symbol_alphabet]['.']=true; - m_alphabet[symbol_alphabet]['<']=true; - m_alphabet[symbol_alphabet]['>']=true; + m_alphabet[detail::symbol_alphabet][static_cast('+')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('-')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('*')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('/')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('|')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('&')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('^')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('=')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('.')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('<')]=true; + m_alphabet[detail::symbol_alphabet][static_cast('>')]=true; - for ( int c = 'a' ; c <= 'z' ; ++c ) { m_alphabet[keyword_alphabet][c]=true; } - for ( int c = 'A' ; c <= 'Z' ; ++c ) { m_alphabet[keyword_alphabet][c]=true; } - for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[keyword_alphabet][c]=true; } - m_alphabet[keyword_alphabet]['_']=true; + for ( int c = 'a' ; c <= 'z' ; ++c ) { m_alphabet[detail::keyword_alphabet][c]=true; } + for ( int c = 'A' ; c <= 'Z' ; ++c ) { m_alphabet[detail::keyword_alphabet][c]=true; } + for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[detail::keyword_alphabet][c]=true; } + m_alphabet[detail::keyword_alphabet][static_cast('_')]=true; - for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[int_alphabet][c]=true; } - for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[float_alphabet][c]=true; } - m_alphabet[float_alphabet]['.']=true; + for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[detail::int_alphabet][c]=true; } + for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[detail::float_alphabet][c]=true; } + m_alphabet[detail::float_alphabet][static_cast('.')]=true; - for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[hex_alphabet][c]=true; } - for ( int c = 'a' ; c <= 'f' ; ++c ) { m_alphabet[hex_alphabet][c]=true; } - for ( int c = 'A' ; c <= 'F' ; ++c ) { m_alphabet[hex_alphabet][c]=true; } + for ( int c = '0' ; c <= '9' ; ++c ) { m_alphabet[detail::hex_alphabet][c]=true; } + for ( int c = 'a' ; c <= 'f' ; ++c ) { m_alphabet[detail::hex_alphabet][c]=true; } + for ( int c = 'A' ; c <= 'F' ; ++c ) { m_alphabet[detail::hex_alphabet][c]=true; } - m_alphabet[x_alphabet]['x']=true; - m_alphabet[x_alphabet]['X']=true; + m_alphabet[detail::x_alphabet][static_cast('x')]=true; + m_alphabet[detail::x_alphabet][static_cast('X')]=true; - for ( int c = '0' ; c <= '1' ; ++c ) { m_alphabet[bin_alphabet][c]=true; } - m_alphabet[b_alphabet]['b']=true; - m_alphabet[b_alphabet]['B']=true; + for ( int c = '0' ; c <= '1' ; ++c ) { m_alphabet[detail::bin_alphabet][c]=true; } + m_alphabet[detail::b_alphabet][static_cast('b')]=true; + m_alphabet[detail::b_alphabet][static_cast('B')]=true; - for ( int c = 'a' ; c <= 'z' ; ++c ) { m_alphabet[id_alphabet][c]=true; } - for ( int c = 'A' ; c <= 'Z' ; ++c ) { m_alphabet[id_alphabet][c]=true; } - m_alphabet[id_alphabet]['_'] = true; + for ( int c = 'a' ; c <= 'z' ; ++c ) { m_alphabet[detail::id_alphabet][c]=true; } + for ( int c = 'A' ; c <= 'Z' ; ++c ) { m_alphabet[detail::id_alphabet][c]=true; } + m_alphabet[detail::id_alphabet][static_cast('_')] = true; - m_alphabet[white_alphabet][' ']=true; - m_alphabet[white_alphabet]['\t']=true; + m_alphabet[detail::white_alphabet][static_cast(' ')]=true; + m_alphabet[detail::white_alphabet][static_cast('\t')]=true; } /** * test a char in an m_alphabet */ - bool char_in_alphabet(unsigned char c,Alphabet a) { return m_alphabet[a][c]; } + bool char_in_alphabet(unsigned char c, detail::Alphabet a) { return m_alphabet[a][c]; } /** * Prints the parsed ast_nodes as a tree @@ -298,7 +301,7 @@ namespace chaiscript bool SkipWS() { bool retval = false; while (has_more_input()) { - if ( char_in_alphabet(*m_input_pos,white_alphabet) ) { + if ( char_in_alphabet(*m_input_pos,detail::white_alphabet) ) { ++m_input_pos; ++m_col; retval = true; @@ -320,17 +323,17 @@ namespace chaiscript bool retval = false; std::string::const_iterator start = m_input_pos; - if (has_more_input() && char_in_alphabet(*m_input_pos,float_alphabet) ) { - while (has_more_input() && char_in_alphabet(*m_input_pos,int_alphabet) ) { + if (has_more_input() && char_in_alphabet(*m_input_pos,detail::float_alphabet) ) { + while (has_more_input() && char_in_alphabet(*m_input_pos,detail::int_alphabet) ) { ++m_input_pos; ++m_col; } if (has_more_input() && (*m_input_pos == '.')) { ++m_input_pos; ++m_col; - if (has_more_input() && char_in_alphabet(*m_input_pos,int_alphabet)) { + if (has_more_input() && char_in_alphabet(*m_input_pos,detail::int_alphabet)) { retval = true; - while (has_more_input() && char_in_alphabet(*m_input_pos,int_alphabet) ) { + while (has_more_input() && char_in_alphabet(*m_input_pos,detail::int_alphabet) ) { ++m_input_pos; ++m_col; } @@ -353,12 +356,12 @@ namespace chaiscript ++m_input_pos; ++m_col; - if (has_more_input() && char_in_alphabet(*m_input_pos,x_alphabet) ) { + if (has_more_input() && char_in_alphabet(*m_input_pos, detail::x_alphabet) ) { ++m_input_pos; ++m_col; - if (has_more_input() && char_in_alphabet(*m_input_pos,hex_alphabet)) { + if (has_more_input() && char_in_alphabet(*m_input_pos, detail::hex_alphabet)) { retval = true; - while (has_more_input() && char_in_alphabet(*m_input_pos,hex_alphabet) ) { + while (has_more_input() && char_in_alphabet(*m_input_pos, detail::hex_alphabet) ) { ++m_input_pos; ++m_col; } @@ -386,12 +389,12 @@ namespace chaiscript ++m_input_pos; ++m_col; - if (has_more_input() && char_in_alphabet(*m_input_pos,b_alphabet) ) { + if (has_more_input() && char_in_alphabet(*m_input_pos, detail::b_alphabet) ) { ++m_input_pos; ++m_col; - if (has_more_input() && char_in_alphabet(*m_input_pos,bin_alphabet) ) { + if (has_more_input() && char_in_alphabet(*m_input_pos, detail::bin_alphabet) ) { retval = true; - while (has_more_input() && char_in_alphabet(*m_input_pos,bin_alphabet) ) { + while (has_more_input() && char_in_alphabet(*m_input_pos, detail::bin_alphabet) ) { ++m_input_pos; ++m_col; } @@ -423,7 +426,7 @@ namespace chaiscript std::string::const_iterator start = m_input_pos; int prev_col = m_col; int prev_line = m_line; - if (has_more_input() && char_in_alphabet(*m_input_pos,float_alphabet) ) { + if (has_more_input() && char_in_alphabet(*m_input_pos, detail::float_alphabet) ) { if (Hex_()) { std::string match(start, m_input_pos); std::stringstream ss(match); @@ -432,7 +435,7 @@ namespace chaiscript std::ostringstream out_int; out_int << static_cast(temp_int); - AST_NodePtr t(new Int_AST_Node(out_int.str(), AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Int_AST_Node(out_int.str(), AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -451,13 +454,13 @@ namespace chaiscript std::ostringstream out_int; out_int << temp_int; - AST_NodePtr t(new Int_AST_Node(out_int.str(), AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Int_AST_Node(out_int.str(), AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } if (Float_()) { std::string match(start, m_input_pos); - AST_NodePtr t(new Float_AST_Node(match, AST_Node_Type::Float, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Float_AST_Node(match, AST_Node_Type::Float, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -470,11 +473,11 @@ namespace chaiscript std::ostringstream out_int; out_int << int(temp_int); - AST_NodePtr t(new Int_AST_Node(out_int.str(), AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Int_AST_Node(out_int.str(), AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); } else { - AST_NodePtr t(new Int_AST_Node(match, AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Int_AST_Node(match, AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); } return true; @@ -491,9 +494,9 @@ namespace chaiscript */ bool Id_() { bool retval = false; - if (has_more_input() && char_in_alphabet(*m_input_pos,id_alphabet)) { + if (has_more_input() && char_in_alphabet(*m_input_pos, detail::id_alphabet)) { retval = true; - while (has_more_input() && char_in_alphabet(*m_input_pos,keyword_alphabet) ) { + while (has_more_input() && char_in_alphabet(*m_input_pos, detail::keyword_alphabet) ) { ++m_input_pos; ++m_col; } @@ -544,13 +547,13 @@ namespace chaiscript if (*start == '`') { //Id Literal std::string match(start+1, m_input_pos-1); - AST_NodePtr t(new Id_AST_Node(match, AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Id_AST_Node(match, AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } else { std::string match(start, m_input_pos); - AST_NodePtr t(new Id_AST_Node(match, AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Id_AST_Node(match, AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -583,7 +586,7 @@ namespace chaiscript } while (Symbol("#")); std::string match(start, m_input_pos); - AST_NodePtr t(new Annotation_AST_Node(match, AST_Node_Type::Annotation, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Annotation_AST_Node(match, AST_Node_Type::Annotation, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -658,23 +661,23 @@ namespace chaiscript if (is_interpolated) { //If we've seen previous interpolation, add on instead of making a new one - AST_NodePtr plus(new Str_AST_Node("+", AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr plus(new eval::Str_AST_Node("+", AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(plus); - AST_NodePtr t(new Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); - build_match(AST_NodePtr(new Additive_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Additive_AST_Node()), prev_stack_top); } else { - AST_NodePtr t(new Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); } //We've finished with the part of the string up to this point, so clear it match = ""; - AST_NodePtr plus(new Str_AST_Node("+", AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr plus(new eval::Str_AST_Node("+", AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(plus); std::string eval_match; @@ -690,28 +693,28 @@ namespace chaiscript size_t tostr_stack_top = m_match_stack.size(); - AST_NodePtr tostr(new Id_AST_Node("to_string", AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr tostr(new eval::Id_AST_Node("to_string", AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(tostr); size_t ev_stack_top = m_match_stack.size(); - AST_NodePtr ev(new Id_AST_Node("eval", AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr ev(new eval::Id_AST_Node("eval", AST_Node_Type::Id, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(ev); size_t arg_stack_top = m_match_stack.size(); - AST_NodePtr t(new Quoted_String_AST_Node(eval_match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Quoted_String_AST_Node(eval_match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); - build_match(AST_NodePtr(new Arg_List_AST_Node()), arg_stack_top); + build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), arg_stack_top); - build_match(AST_NodePtr(new Inplace_Fun_Call_AST_Node()), ev_stack_top); + build_match(AST_NodePtr(new eval::Inplace_Fun_Call_AST_Node()), ev_stack_top); - build_match(AST_NodePtr(new Arg_List_AST_Node()), ev_stack_top); + build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), ev_stack_top); - build_match(AST_NodePtr(new Fun_Call_AST_Node()), tostr_stack_top); + build_match(AST_NodePtr(new eval::Fun_Call_AST_Node()), tostr_stack_top); - build_match(AST_NodePtr(new Additive_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Additive_AST_Node()), prev_stack_top); } else { throw Eval_Error("Unclosed in-string eval", File_Position(prev_line, prev_col), *m_filename); @@ -758,16 +761,16 @@ namespace chaiscript } } if (is_interpolated) { - AST_NodePtr plus(new Str_AST_Node("+", AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr plus(new eval::Str_AST_Node("+", AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(plus); - AST_NodePtr t(new Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); - build_match(AST_NodePtr(new Additive_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Additive_AST_Node()), prev_stack_top); } else { - AST_NodePtr t(new Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Quoted_String_AST_Node(match, AST_Node_Type::Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); } return true; @@ -859,7 +862,7 @@ namespace chaiscript is_escaped = false; } } - AST_NodePtr t(new Single_Quoted_String_AST_Node(match, AST_Node_Type::Single_Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Single_Quoted_String_AST_Node(match, AST_Node_Type::Single_Quoted_String, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -898,7 +901,7 @@ namespace chaiscript int prev_line = m_line; if (Char_(t_c)) { std::string match(start, m_input_pos); - AST_NodePtr t(new Char_AST_Node(match, AST_Node_Type::Char, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Char_AST_Node(match, AST_Node_Type::Char, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -941,7 +944,7 @@ namespace chaiscript int prev_line = m_line; bool retval = Keyword_(t_s); // ignore substring matches - if ( retval && has_more_input() && char_in_alphabet(*m_input_pos,keyword_alphabet) ) { + if ( retval && has_more_input() && char_in_alphabet(*m_input_pos, detail::keyword_alphabet) ) { m_input_pos = start; m_col = prev_col; m_line = prev_line; @@ -950,7 +953,7 @@ namespace chaiscript if ( t_capture && retval ) { std::string match(start, m_input_pos); - AST_NodePtr t(new Str_AST_Node(match, AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Str_AST_Node(match, AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); } return retval; @@ -989,7 +992,7 @@ namespace chaiscript int prev_line = m_line; bool retval = Symbol_(t_s); // ignore substring matches - if (retval && has_more_input() && (t_disallow_prevention == false) && char_in_alphabet(*m_input_pos,symbol_alphabet)) { + if (retval && has_more_input() && (t_disallow_prevention == false) && char_in_alphabet(*m_input_pos,detail::symbol_alphabet)) { m_input_pos = start; m_col = prev_col; m_line = prev_line; @@ -998,7 +1001,7 @@ namespace chaiscript if ( t_capture && retval ) { std::string match(start, m_input_pos); - AST_NodePtr t(new Str_AST_Node(match, AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Str_AST_Node(match, AST_Node_Type::Str, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); } @@ -1038,7 +1041,7 @@ namespace chaiscript int prev_line = m_line; if (Eol_()) { std::string match(start, m_input_pos); - AST_NodePtr t(new Eol_AST_Node(match, AST_Node_Type::Eol, m_filename, prev_line, prev_col, m_line, m_col)); + AST_NodePtr t(new eval::Eol_AST_Node(match, AST_Node_Type::Eol, m_filename, prev_line, prev_col, m_line, m_col)); m_match_stack.push_back(t); return true; } @@ -1067,7 +1070,7 @@ namespace chaiscript } } while (retval && Char(',')); } - build_match(AST_NodePtr(new Arg_List_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), prev_stack_top); } return retval; @@ -1083,7 +1086,7 @@ namespace chaiscript if (Value_Range()) { retval = true; - build_match(AST_NodePtr(new Arg_List_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), prev_stack_top); } else if (Map_Pair()) { retval = true; @@ -1096,7 +1099,7 @@ namespace chaiscript } } while (retval && Char(',')); } - build_match(AST_NodePtr(new Arg_List_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), prev_stack_top); } else if (Operator()) { retval = true; @@ -1109,7 +1112,7 @@ namespace chaiscript } } while (retval && Char(',')); } - build_match(AST_NodePtr(new Arg_List_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), prev_stack_top); } return retval; @@ -1140,7 +1143,7 @@ namespace chaiscript throw Eval_Error("Incomplete anonymous function", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Lambda_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Lambda_AST_Node()), prev_stack_top); } return retval; @@ -1201,10 +1204,10 @@ namespace chaiscript } if (is_method) { - build_match(AST_NodePtr(new Method_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Method_AST_Node()), prev_stack_top); } else { - build_match(AST_NodePtr(new Def_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Def_AST_Node()), prev_stack_top); } if (is_annotated) { @@ -1254,7 +1257,7 @@ namespace chaiscript if (!Block()) { throw Eval_Error("Incomplete 'catch' block", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Catch_AST_Node()), catch_stack_top); + build_match(AST_NodePtr(new eval::Catch_AST_Node()), catch_stack_top); has_matches = true; } } @@ -1267,10 +1270,10 @@ namespace chaiscript if (!Block()) { throw Eval_Error("Incomplete 'finally' block", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Finally_AST_Node()), finally_stack_top); + build_match(AST_NodePtr(new eval::Finally_AST_Node()), finally_stack_top); } - build_match(AST_NodePtr(new Try_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Try_AST_Node()), prev_stack_top); } return retval; @@ -1334,7 +1337,7 @@ namespace chaiscript } } - build_match(AST_NodePtr(new If_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::If_AST_Node()), prev_stack_top); } return retval; @@ -1365,7 +1368,7 @@ namespace chaiscript throw Eval_Error("Incomplete 'while' block", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new While_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::While_AST_Node()), prev_stack_top); } return retval; @@ -1410,7 +1413,7 @@ namespace chaiscript throw Eval_Error("Incomplete 'for' block", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new For_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::For_AST_Node()), prev_stack_top); } return retval; @@ -1432,7 +1435,7 @@ namespace chaiscript throw Eval_Error("Incomplete block", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Block_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Block_AST_Node()), prev_stack_top); } return retval; @@ -1450,7 +1453,7 @@ namespace chaiscript retval = true; Operator(); - build_match(AST_NodePtr(new Return_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Return_AST_Node()), prev_stack_top); } return retval; @@ -1467,7 +1470,7 @@ namespace chaiscript if (Keyword("break")) { retval = true; - build_match(AST_NodePtr(new Break_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Break_AST_Node()), prev_stack_top); } return retval; @@ -1496,7 +1499,7 @@ namespace chaiscript throw Eval_Error("Incomplete function call", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Fun_Call_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Fun_Call_AST_Node()), prev_stack_top); } else if (Char('[')) { has_more = true; @@ -1505,7 +1508,7 @@ namespace chaiscript throw Eval_Error("Incomplete array access", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Array_Call_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Array_Call_AST_Node()), prev_stack_top); } } } @@ -1528,7 +1531,7 @@ namespace chaiscript throw Eval_Error("Incomplete variable declaration", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Var_Decl_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Var_Decl_AST_Node()), prev_stack_top); } else if (Keyword("attr")) { retval = true; @@ -1544,7 +1547,7 @@ namespace chaiscript } - build_match(AST_NodePtr(new Attr_Decl_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Attr_Decl_AST_Node()), prev_stack_top); } return retval; @@ -1584,17 +1587,17 @@ namespace chaiscript } if ((prev_stack_top != m_match_stack.size()) && (m_match_stack.back()->children.size() > 0)) { if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Value_Range) { - build_match(AST_NodePtr(new Inline_Range_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Inline_Range_AST_Node()), prev_stack_top); } else if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Map_Pair) { - build_match(AST_NodePtr(new Inline_Map_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Inline_Map_AST_Node()), prev_stack_top); } else { - build_match(AST_NodePtr(new Inline_Array_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Inline_Array_AST_Node()), prev_stack_top); } } else { - build_match(AST_NodePtr(new Inline_Array_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Inline_Array_AST_Node()), prev_stack_top); } } @@ -1616,7 +1619,7 @@ namespace chaiscript throw Eval_Error("Incomplete '++' expression", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Prefix_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Prefix_AST_Node()), prev_stack_top); } else if (Symbol("--", true)) { retval = true; @@ -1625,7 +1628,7 @@ namespace chaiscript throw Eval_Error("Incomplete '--' expression", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Prefix_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Prefix_AST_Node()), prev_stack_top); } else if (Char('-', true)) { retval = true; @@ -1634,7 +1637,7 @@ namespace chaiscript throw Eval_Error("Incomplete unary '-' expression", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Prefix_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Prefix_AST_Node()), prev_stack_top); } else if (Char('+', true)) { retval = true; @@ -1643,7 +1646,7 @@ namespace chaiscript throw Eval_Error("Incomplete unary '+' expression", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Prefix_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Prefix_AST_Node()), prev_stack_top); } else if (Char('!', true)) { retval = true; @@ -1652,7 +1655,7 @@ namespace chaiscript throw Eval_Error("Incomplete '!' expression", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Prefix_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Prefix_AST_Node()), prev_stack_top); } else if (Char('~', true)) { retval = true; @@ -1661,7 +1664,7 @@ namespace chaiscript throw Eval_Error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Prefix_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Prefix_AST_Node()), prev_stack_top); } return retval; @@ -1708,37 +1711,37 @@ namespace chaiscript switch (m_operators[t_precedence]) { case(AST_Node_Type::Comparison) : - build_match(AST_NodePtr(new Comparison_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Comparison_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Dot_Access) : - build_match(AST_NodePtr(new Dot_Access_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Dot_Access_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Additive) : - build_match(AST_NodePtr(new Additive_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Additive_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Multiplicative) : - build_match(AST_NodePtr(new Multiplicative_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Multiplicative_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Shift) : - build_match(AST_NodePtr(new Shift_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Shift_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Equality) : - build_match(AST_NodePtr(new Equality_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Equality_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Bitwise_And) : - build_match(AST_NodePtr(new Bitwise_And_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Bitwise_And_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Bitwise_Xor) : - build_match(AST_NodePtr(new Bitwise_Xor_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Bitwise_Xor_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Bitwise_Or) : - build_match(AST_NodePtr(new Bitwise_Or_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Bitwise_Or_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Logical_And) : - build_match(AST_NodePtr(new Logical_And_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Logical_And_AST_Node()), prev_stack_top); break; case(AST_Node_Type::Logical_Or) : - build_match(AST_NodePtr(new Logical_Or_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Logical_Or_AST_Node()), prev_stack_top); break; default: throw Eval_Error("Internal error: unhandled ast_node", File_Position(m_line, m_col), *m_filename); @@ -1770,7 +1773,7 @@ namespace chaiscript throw Eval_Error("Incomplete map pair", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Map_Pair_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Map_Pair_AST_Node()), prev_stack_top); } else { m_input_pos = prev_pos; @@ -1801,7 +1804,7 @@ namespace chaiscript throw Eval_Error("Incomplete value range", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Value_Range_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Value_Range_AST_Node()), prev_stack_top); } else { m_input_pos = prev_pos; @@ -1833,7 +1836,7 @@ namespace chaiscript throw Eval_Error("Incomplete equation", File_Position(m_line, m_col), *m_filename); } - build_match(AST_NodePtr(new Equation_AST_Node()), prev_stack_top); + build_match(AST_NodePtr(new eval::Equation_AST_Node()), prev_stack_top); } } @@ -1957,7 +1960,7 @@ namespace chaiscript throw Eval_Error("Unparsed input", File_Position(m_line, m_col), t_fname); } else { - build_match(AST_NodePtr(new File_AST_Node()), 0); + build_match(AST_NodePtr(new eval::File_AST_Node()), 0); return true; } } diff --git a/src/main.cpp b/src/main.cpp index 5acdf2a..dbffe03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,7 +102,7 @@ void interactive(chaiscript::ChaiScript& chai) //Then, we try to print the result of the evaluation to the user if (!val.get_type_info().bare_equal(chaiscript::user_type())) { try { - chaiscript::dispatch(chai.get_eval_engine().get_function("print"), chaiscript::Param_List_Builder() << val); + std::cout << chai.functor("to_string")(val) << std::endl; } catch (...) {} //If we can't, do nothing } diff --git a/unittests/functor_creation_test.cpp b/unittests/functor_creation_test.cpp index ada7369..432ea55 100644 --- a/unittests/functor_creation_test.cpp +++ b/unittests/functor_creation_test.cpp @@ -8,9 +8,19 @@ int main() chai.eval("def func() { print(\"Hello World\"); } "); boost::function f = chai.functor("func"); - f(); - return EXIT_SUCCESS; + if (chai.functor("to_string")(6) != "6") + { + return EXIT_FAILURE; + } + + if (chai.functor("to_string")(chaiscript::var(6)) == "6") + { + return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; + } + }