From 0c31d81711785fd73cfac5b07083f7f075416e1d Mon Sep 17 00:00:00 2001 From: Mike Bogdanov Date: Thu, 13 Jul 2017 12:41:23 +0300 Subject: [PATCH 01/14] fixed PVS-Studio warnings V728 --- include/chaiscript/language/chaiscript_parser.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 163d2bb3..97cbca2f 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -995,7 +995,7 @@ namespace chaiscript int in_interpolation = 0; bool in_quote = false; - while (m_position.has_more() && ((*m_position != '\"') || ((*m_position == '\"') && (in_interpolation > 0)) || ((*m_position == '\"') && (prev_char == '\\')))) { + while (m_position.has_more() && ((*m_position != '\"') || (in_interpolation > 0) || (prev_char == '\\'))) { if (!Eol_()) { if (prev_char == '$' && *m_position == '{') { @@ -1281,7 +1281,7 @@ namespace chaiscript char prev_char = *m_position; ++m_position; - while (m_position.has_more() && ((*m_position != '\'') || ((*m_position == '\'') && (prev_char == '\\')))) { + while (m_position.has_more() && ((*m_position != '\'') || (prev_char == '\\'))) { if (!Eol_()) { if (prev_char == '\\') { prev_char = 0; From a87147a12dc726501c2e63ef2fc6473f7d1b9676 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Thu, 14 Sep 2017 17:41:11 +0200 Subject: [PATCH 02/14] Upgrade samples where it improves readability --- samples/example.cpp | 28 ++++++++++++++-------------- samples/inheritance.cpp | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/samples/example.cpp b/samples/example.cpp index 9a0f0070..4810a127 100644 --- a/samples/example.cpp +++ b/samples/example.cpp @@ -51,11 +51,9 @@ struct System void do_callbacks(const std::string &inp) { log("Running Callbacks: " + inp); - for (std::map >::iterator itr = m_callbacks.begin(); - itr != m_callbacks.end(); - ++itr) + for (auto & m_callback : m_callbacks) { - log("Callback: " + itr->first, itr->second(inp)); + log("Callback: " + m_callback.first, m_callback.second(inp)); } } }; @@ -88,25 +86,25 @@ int main(int /*argc*/, char * /*argv*/[]) { // The function "{ 'Callback1' + x }" is created in chaiscript and passed into our C++ application // in the "add_callback" function of struct System the chaiscript function is converted into a // std::function, so it can be handled and called easily and type-safely - chai.eval("system.add_callback(\"#1\", fun(x) { \"Callback1 \" + x });"); + chai.eval(R"(system.add_callback("#1", fun(x) { "Callback1 " + x });)"); // Because we are sharing the "system" object with the chaiscript engine we have equal // access to it both from within chaiscript and from C++ code system.do_callbacks("TestString"); - chai.eval("system.do_callbacks(\"TestString\");"); + chai.eval(R"(system.do_callbacks("TestString");)"); // The log function is overloaded, therefore we have to give the C++ compiler a hint as to which // version we want to register. One way to do this is to create a typedef of the function pointer // then cast your function to that typedef. - typedef void (*PlainLog)(const std::string &); - typedef void (*ModuleLog)(const std::string &, const std::string &); + using PlainLog = void (*)(const std::string &); + using ModuleLog = void (*)(const std::string &, const std::string &); chai.add(fun(PlainLog(&log)), "log"); chai.add(fun(ModuleLog(&log)), "log"); - chai.eval("log(\"Test Message\")"); + chai.eval(R"(log("Test Message"))"); // A shortcut to using eval is just to use the chai operator() - chai("log(\"Test Module\", \"Test Message\");"); + chai(R"(log("Test Module", "Test Message");)"); //Finally, it is possible to register a lambda as a system function, in this //way, we can, for instance add a bound member function to the system @@ -115,7 +113,9 @@ int main(int /*argc*/, char * /*argv*/[]) { //Call bound version of do_callbacks chai("do_callbacks()"); - std::function caller = chai.eval >("fun() { system.do_callbacks(\"From Functor\"); }"); + std::function caller = chai.eval >( + R"(fun() { system.do_callbacks("From Functor"); })" + ); caller(); @@ -134,7 +134,7 @@ int main(int /*argc*/, char * /*argv*/[]) { std::cout << "scripti: " << scripti << '\n'; scripti *= 2; std::cout << "scripti (updated): " << scripti << '\n'; - chai("print(\"Scripti from chai: \" + to_string(scripti))"); + chai(R"(print("Scripti from chai: " + to_string(scripti)))"); //To do: Add examples of handling Boxed_Values directly when needed @@ -146,7 +146,7 @@ int main(int /*argc*/, char * /*argv*/[]) { log("Functor test output", ss.str()); chai.add(var(std::shared_ptr()), "nullvar"); - chai("print(\"This should be true.\"); print(nullvar.is_var_null())"); + chai(R"(print("This should be true."); print(nullvar.is_var_null()))"); // test the global const action chai.add_global_const(const_var(1), "constvar"); @@ -160,7 +160,7 @@ int main(int /*argc*/, char * /*argv*/[]) { // Test ability to register a function that excepts a shared_ptr version of a type - chai("take_shared_ptr(\"Hello World as a shared_ptr\");"); + chai(R"(take_shared_ptr("Hello World as a shared_ptr");)"); chai.add(fun(&bound_log, std::string("Msg")), "BoundFun"); diff --git a/samples/inheritance.cpp b/samples/inheritance.cpp index aba619a4..1c9ddb63 100644 --- a/samples/inheritance.cpp +++ b/samples/inheritance.cpp @@ -122,7 +122,7 @@ int main() assert(myderived.getValue() == "1234"); - chai.eval("myderived.setValue(\"new\")"); // set the value via chaiscript + chai.eval(R"(myderived.setValue("new"))"); // set the value via chaiscript assert(myderived.getValue() == "new"); // call the other derived method via chaiscript and return the value to c++ land: From ee3f828b8c33fc161613897da851cd6da1e56aea Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Fri, 15 Sep 2017 10:12:47 +0200 Subject: [PATCH 03/14] Allow bootstrapping hpx::lcos::future Which has an overloaded get(error_code &). Use a lambda in standard_library::future_type to disambiguate. --- include/chaiscript/dispatchkit/bootstrap_stl.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 7f409a3a..eca3cf38 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -733,7 +733,7 @@ namespace chaiscript m.add(user_type(), type); m.add(fun([](const FutureType &t) { return t.valid(); }), "valid"); - m.add(fun(&FutureType::get), "get"); + m.add(fun([](FutureType &t) { return t.get(); }), "get"); m.add(fun(&FutureType::wait), "wait"); } template From 0fa0def11270d9403f6fc83a946af31629d7c6ac Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Mon, 18 Sep 2017 15:00:52 +0200 Subject: [PATCH 04/14] Use range-based for --- include/chaiscript/dispatchkit/dispatchkit.hpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index d8bfb25b..2e71e543 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -1143,24 +1143,17 @@ namespace chaiscript { std::cout << "Registered Types: \n"; std::vector > types = get_types(); - for (std::vector >::const_iterator itr = types.begin(); - itr != types.end(); - ++itr) + for (auto const &type: get_types()) { - std::cout << itr->first << ": "; - std::cout << itr->second.bare_name(); - std::cout << '\n'; + std::cout << type.first << ": " << type.second.bare_name() << '\n'; } std::cout << '\n'; - std::vector > funcs = get_functions(); std::cout << "Functions: \n"; - for (std::vector >::const_iterator itr = funcs.begin(); - itr != funcs.end(); - ++itr) + for (auto const &func: get_functions()) { - dump_function(*itr); + dump_function(func); } std::cout << '\n'; } From 3e521d29522016e75960ba7da3af71c652c0675b Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Mon, 18 Sep 2017 17:00:04 +0200 Subject: [PATCH 05/14] Delete now useless local copy --- include/chaiscript/dispatchkit/dispatchkit.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 2e71e543..ae729819 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -1142,7 +1142,6 @@ namespace chaiscript void dump_system() const { std::cout << "Registered Types: \n"; - std::vector > types = get_types(); for (auto const &type: get_types()) { std::cout << type.first << ": " << type.second.bare_name() << '\n'; From c6237cc528c2d378dca9f74d8d455a88756b1fad Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 21 Sep 2017 08:55:32 -0600 Subject: [PATCH 06/14] Add `+= char` for string type --- include/chaiscript/dispatchkit/bootstrap_stl.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 7f409a3a..7785f9fb 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -706,6 +706,8 @@ namespace chaiscript m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of"); m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of"); + + m.add(fun([](String *s, typename String::value_type c) -> decltype(auto) { return (*s += c); } ), "+="); m.add(fun([](String *s) { s->clear(); } ), "clear"); m.add(fun([](const String *s) { return s->empty(); } ), "empty"); From 15196af5d6b5712575ba49aa908600b8d43da050 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 2 Oct 2017 09:52:51 -0600 Subject: [PATCH 07/14] Remove one case of UB union work --- include/chaiscript/language/chaiscript_posix.hpp | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/include/chaiscript/language/chaiscript_posix.hpp b/include/chaiscript/language/chaiscript_posix.hpp index 62f1c117..87b66881 100644 --- a/include/chaiscript/language/chaiscript_posix.hpp +++ b/include/chaiscript/language/chaiscript_posix.hpp @@ -41,7 +41,7 @@ namespace chaiscript struct DLSym { DLSym(DLModule &t_mod, const std::string &t_symbol) - : m_symbol(cast_symbol(dlsym(t_mod.m_data, t_symbol.c_str()))) + : m_symbol(reinterpret_cast(dlsym(t_mod.m_data, t_symbol.c_str()))) { if (!m_symbol) { @@ -49,19 +49,6 @@ namespace chaiscript } } - static T cast_symbol(void *p) - { - union cast_union - { - T func_ptr; - void *in_ptr; - }; - - cast_union c; - c.in_ptr = p; - return c.func_ptr; - } - T m_symbol; }; From 1541cce1d91df027dad2612dbd034de14df5fd36 Mon Sep 17 00:00:00 2001 From: dinghram Date: Tue, 3 Oct 2017 08:43:01 -0600 Subject: [PATCH 08/14] Change long to int64_t to remove OS ambiguity Linux compilers interpret "long" as 64 bit, Visual Studio on Windows interprets "long" as 32 bit. In order to remove ambiguity, from_json should use int64_t rather than long when parsing integers. --- include/chaiscript/utility/json.hpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index 693f19da..2912a50e 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -131,7 +131,7 @@ class JSON } Internal( double d ) : Float( d ), Type(Class::Floating) {} - Internal( long l ) : Int( l ), Type(Class::Integral) {} + Internal( int64_t l ) : Int( l ), Type(Class::Integral) {} Internal( bool b ) : Bool( b ), Type(Class::Boolean) {} Internal( std::string s ) : String(std::make_unique(std::move(s))), Type(Class::String) {} Internal() : Type(Class::Null) {} @@ -192,7 +192,7 @@ class JSON std::unique_ptr Map; std::unique_ptr String; double Float = 0; - long Int = 0; + int64_t Int = 0; bool Bool = false; Class Type = Class::Null; @@ -248,7 +248,7 @@ class JSON explicit JSON( T b, typename enable_if::value>::type* = nullptr ) : internal( static_cast(b) ) {} template - explicit JSON( T i, typename enable_if::value && !is_same::value>::type* = nullptr ) : internal( static_cast(i) ) {} + explicit JSON( T i, typename enable_if::value && !is_same::value>::type* = nullptr ) : internal( static_cast(i) ) {} template explicit JSON( T f, typename enable_if::value>::type* = nullptr ) : internal( static_cast(f) ) {} @@ -335,8 +335,8 @@ class JSON return ok ? internal.Float : 0.0; } - long to_int() const { bool b; return to_int( b ); } - long to_int( bool &ok ) const { + int64_t to_int() const { bool b; return to_int( b ); } + int64_t to_int( bool &ok ) const { ok = (internal.Type == Class::Integral); return ok ? internal.Int : 0; } @@ -568,7 +568,7 @@ struct JSONParser { char c = '\0'; bool isDouble = false; bool isNegative = false; - long exp = 0; + int64_t exp = 0; if( offset < str.size() && str.at(offset) == '-' ) { isNegative = true; ++offset; @@ -605,7 +605,7 @@ struct JSONParser { break; } } - exp = chaiscript::parse_num( exp_str ); + exp = chaiscript::parse_num( exp_str ); } else if( offset < str.size() && (!isspace( c ) && c != ',' && c != ']' && c != '}' )) { throw std::runtime_error(std::string("JSON ERROR: Number: unexpected character '") + c + "'"); @@ -616,9 +616,9 @@ struct JSONParser { return JSON((isNegative?-1:1) * chaiscript::parse_num( val ) * std::pow( 10, exp )); } else { if( !exp_str.empty() ) { - return JSON((isNegative?-1:1) * static_cast(chaiscript::parse_num( val )) * std::pow( 10, exp )); + return JSON((isNegative?-1:1) * static_cast(chaiscript::parse_num( val )) * std::pow( 10, exp )); } else { - return JSON((isNegative?-1:1) * chaiscript::parse_num( val )); + return JSON((isNegative?-1:1) * chaiscript::parse_num( val )); } } } From 2b735d1b3acb6c346f55beb9125324b0b37fe997 Mon Sep 17 00:00:00 2001 From: superfunc Date: Sat, 21 Oct 2017 15:14:27 -0700 Subject: [PATCH 09/14] Fix link in readme --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 8bf4deec..8323a128 100644 --- a/readme.md +++ b/readme.md @@ -25,7 +25,7 @@ Release under the BSD license, see "license.txt" for details. Introduction ============ -[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/ChaiScript/ChaiScript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![Gitter](https://badges.gitter.im/JoinChat.svg)](https://gitter.im/ChaiScript/ChaiScript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) ChaiScript is one of the only embedded scripting language designed from the ground up to directly target C++ and take advantage of modern C++ development From 79d985d6ffdde4a42e40e07a2ad72f97788be78a Mon Sep 17 00:00:00 2001 From: Glen Fraser Date: Tue, 24 Oct 2017 20:10:50 +0200 Subject: [PATCH 10/14] Fix JSON parsing for floats with negative exponents - also add unit tests to cover some broken (now fixed) cases. --- include/chaiscript/utility/json.hpp | 7 ++++--- unittests/json_14.chai | 5 +++++ 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 unittests/json_14.chai diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index 693f19da..838bd5b9 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -568,6 +568,7 @@ struct JSONParser { char c = '\0'; bool isDouble = false; bool isNegative = false; + bool isExpNegative = false; long exp = 0; if( offset < str.size() && str.at(offset) == '-' ) { isNegative = true; @@ -587,7 +588,7 @@ struct JSONParser { if( offset < str.size() && (c == 'E' || c == 'e' )) { c = str.at(offset++); if( c == '-' ) { - exp_str += '-'; + isExpNegative = true; } else if( c == '+' ) { // do nothing } else { @@ -603,9 +604,9 @@ struct JSONParser { } else { break; -} + } } - exp = chaiscript::parse_num( exp_str ); + exp = chaiscript::parse_num( exp_str ) * (isExpNegative?-1:1); } else if( offset < str.size() && (!isspace( c ) && c != ',' && c != ']' && c != '}' )) { throw std::runtime_error(std::string("JSON ERROR: Number: unexpected character '") + c + "'"); diff --git a/unittests/json_14.chai b/unittests/json_14.chai new file mode 100644 index 00000000..e6490a1d --- /dev/null +++ b/unittests/json_14.chai @@ -0,0 +1,5 @@ +assert_equal(from_json("9.9e-02"), 9.9e-02) +assert_equal(from_json("-13.57e+3"), -13570.0) +assert_equal(from_json("1E-01"), 0.1) +assert_equal(from_json("-314159e-5"), -3.14159) +assert_equal(from_json("5e+04"), 50000) From 784c3a9720fbb148fd45adfc2919ed54937c3fea Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 21 Nov 2017 07:12:17 -0700 Subject: [PATCH 11/14] Add slow test for creating variables References #356 --- performance_tests/create_variables.chai | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 performance_tests/create_variables.chai diff --git a/performance_tests/create_variables.chai b/performance_tests/create_variables.chai new file mode 100644 index 00000000..e63e1a46 --- /dev/null +++ b/performance_tests/create_variables.chai @@ -0,0 +1,11 @@ +def var_test(int n) +{ + for (var i = 0; i < n; ++i) { + var j = 0 + } +} + +var n = 500000 +var_test(n) // takes 2.6 s + + From 50a2773081131cbb15e8d43e2b5aebdf18975fbf Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 21 Nov 2017 09:01:17 -0700 Subject: [PATCH 12/14] Reduce cost of cloning common built in types Re: #356 --- .../chaiscript/dispatchkit/boxed_number.hpp | 4 ++ .../chaiscript/language/chaiscript_eval.hpp | 39 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 8f1e871f..dafc1234 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -521,6 +521,10 @@ namespace chaiscript validate_boxed_number(bv); } + static Boxed_Value clone(const Boxed_Value &t_bv) { + return Boxed_Number(t_bv).get_as(t_bv.get_type_info()).bv; + } + static bool is_floating_point(const Boxed_Value &t_bv) { const Type_Info &inp_ = t_bv.get_type_info(); diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index a9819440..508f4d3a 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -88,6 +88,23 @@ namespace chaiscript return std::move(rv.retval); } } + + inline Boxed_Value clone_if_necessary(Boxed_Value incoming, std::atomic_uint_fast32_t &t_loc, const chaiscript::detail::Dispatch_State &t_ss) + { + if (!incoming.is_return_value()) + { + if (incoming.get_type_info().is_arithmetic()) { + return Boxed_Number::clone(incoming); + } else if (incoming.get_type_info().bare_equal_type_info(typeid(bool))) { + return Boxed_Value(*static_cast(incoming.get_const_ptr())); + } else { + return t_ss->call_function("clone", t_loc, {incoming}, t_ss.conversions()); + } + } else { + incoming.reset_return_value(); + return incoming; + } + } } template @@ -459,11 +476,7 @@ namespace chaiscript lhs.reset_return_value(); return rhs; } else { - if (!rhs.is_return_value()) - { - rhs = t_ss->call_function("clone", m_clone_loc, {rhs}, t_ss.conversions()); - } - rhs.reset_return_value(); + rhs = detail::clone_if_necessary(std::move(rhs), m_clone_loc, t_ss); } } @@ -1056,12 +1069,7 @@ namespace chaiscript if (!this->children.empty()) { vec.reserve(this->children[0]->children.size()); for (const auto &child : this->children[0]->children) { - auto obj = child->eval(t_ss); - if (!obj.is_return_value()) { - vec.push_back(t_ss->call_function("clone", m_loc, {obj}, t_ss.conversions())); - } else { - vec.push_back(std::move(obj)); - } + vec.push_back(detail::clone_if_necessary(child->eval(t_ss), m_loc, t_ss)); } } return const_var(std::move(vec)); @@ -1086,12 +1094,8 @@ namespace chaiscript std::map retval; for (const auto &child : this->children[0]->children) { - auto obj = child->children[1]->eval(t_ss); - if (!obj.is_return_value()) { - obj = t_ss->call_function("clone", m_loc, {obj}, t_ss.conversions()); - } - - retval[t_ss->boxed_cast(child->children[0]->eval(t_ss))] = std::move(obj); + retval.insert(std::make_pair(t_ss->boxed_cast(child->children[0]->eval(t_ss)), + detail::clone_if_necessary(child->children[1]->eval(t_ss), m_loc, t_ss))); } return const_var(std::move(retval)); @@ -1450,7 +1454,6 @@ namespace chaiscript function_name); } } catch (const exception::name_conflict_error &e) { - std::cout << "Method!!" << std::endl; throw exception::eval_error("Method redefined '" + e.name() + "'"); } return void_var(); From 830766393895501fc0da6212f5b13e2d367ed9bc Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 21 Nov 2017 10:26:58 -0700 Subject: [PATCH 13/14] Look for and optimize decl assignments Re: #356 --- .../chaiscript/language/chaiscript_common.hpp | 4 +-- .../chaiscript/language/chaiscript_eval.hpp | 21 ++++++++++++++++ .../language/chaiscript_optimizer.hpp | 25 +++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index b157cac2..b1e16688 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -62,7 +62,7 @@ namespace chaiscript /// Types of AST nodes available to the parser and eval - enum class AST_Node_Type { Id, Fun_Call, Unused_Return_Fun_Call, Arg_List, Equation, Var_Decl, + enum class AST_Node_Type { Id, Fun_Call, Unused_Return_Fun_Call, Arg_List, Equation, Var_Decl, Assign_Decl, Array_Call, Dot_Access, Lambda, Block, Scopeless_Block, Def, While, If, For, Ranged_For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range, Inline_Range, Try, Catch, Finally, Method, Attr_Decl, @@ -77,7 +77,7 @@ namespace chaiscript { /// Helper lookup to get the name of each node type inline const char *ast_node_type_to_string(AST_Node_Type ast_node_type) { - static const char * const ast_node_types[] = { "Id", "Fun_Call", "Unused_Return_Fun_Call", "Arg_List", "Equation", "Var_Decl", + static const char * const ast_node_types[] = { "Id", "Fun_Call", "Unused_Return_Fun_Call", "Arg_List", "Equation", "Var_Decl", "Assign_Decl", "Array_Call", "Dot_Access", "Lambda", "Block", "Scopeless_Block", "Def", "While", "If", "For", "Ranged_For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range", "Inline_Range", "Try", "Catch", "Finally", "Method", "Attr_Decl", diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 508f4d3a..e0933b71 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -555,6 +555,27 @@ namespace chaiscript } }; + template + struct Assign_Decl_AST_Node final : AST_Node_Impl { + Assign_Decl_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector> t_children) : + AST_Node_Impl(std::move(t_ast_node_text), AST_Node_Type::Assign_Decl, std::move(t_loc), std::move(t_children)) { } + + Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { + const std::string &idname = this->children[0]->text; + + try { + Boxed_Value bv(detail::clone_if_necessary(this->children[1]->eval(t_ss), m_loc, t_ss)); + bv.reset_return_value(); + t_ss.add_object(idname, bv); + return bv; + } catch (const exception::name_conflict_error &e) { + throw exception::eval_error("Variable redefined '" + e.name() + "'"); + } + } + private: + mutable std::atomic_uint_fast32_t m_loc = {0}; + }; + template struct Array_Call_AST_Node final : AST_Node_Impl { diff --git a/include/chaiscript/language/chaiscript_optimizer.hpp b/include/chaiscript/language/chaiscript_optimizer.hpp index 675d092c..55a19c58 100644 --- a/include/chaiscript/language/chaiscript_optimizer.hpp +++ b/include/chaiscript/language/chaiscript_optimizer.hpp @@ -97,7 +97,7 @@ namespace chaiscript { template bool contains_var_decl_in_scope(const eval::AST_Node_Impl &node) { - if (node.identifier == AST_Node_Type::Var_Decl) { + if (node.identifier == AST_Node_Type::Var_Decl || node.identifier == AST_Node_Type::Assign_Decl) { return true; } @@ -208,6 +208,27 @@ namespace chaiscript { } }; + struct Assign_Decl { + template + auto optimize(eval::AST_Node_Impl_Ptr node) { + if ((node->identifier == AST_Node_Type::Equation) + && node->text == "=" + && node->children.size() == 2 + && node->children[0]->identifier == AST_Node_Type::Var_Decl + ) + { + std::vector> new_children; + new_children.push_back(std::move(node->children[0]->children[0])); + new_children.push_back(std::move(node->children[1])); + return chaiscript::make_unique, eval::Assign_Decl_AST_Node>(node->text, + node->location, std::move(new_children) ); + } + + return node; + } + }; + + struct If { template auto optimize(eval::AST_Node_Impl_Ptr node) { @@ -440,7 +461,7 @@ namespace chaiscript { }; typedef Optimizer Optimizer_Default; + optimizer::If, optimizer::Return, optimizer::Dead_Code, optimizer::Block, optimizer::For_Loop, optimizer::Assign_Decl> Optimizer_Default; } } From 680f9b92429515dc83bf400ed774bb117abde1e3 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 21 Nov 2017 14:06:42 -0700 Subject: [PATCH 14/14] Fix variable redeclaration from last merge conflict fix. --- include/chaiscript/utility/json.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/chaiscript/utility/json.hpp b/include/chaiscript/utility/json.hpp index 121b9390..d7c632c6 100644 --- a/include/chaiscript/utility/json.hpp +++ b/include/chaiscript/utility/json.hpp @@ -570,7 +570,6 @@ struct JSONParser { bool isNegative = false; int64_t exp = 0; bool isExpNegative = false; - long exp = 0; if( offset < str.size() && str.at(offset) == '-' ) { isNegative = true; ++offset;