Compare commits

...

35 Commits

Author SHA1 Message Date
Jason Turner
8c70fff02b Update version to 4.3.0 2014-02-23 07:10:03 -07:00
Jason Turner
af44da916a Fix warnings and errors on VisualStudio 2013
Interestingly, VS2013 with Boost 1.55 exhibited the issues complained
about in issue #92, so I was able to provide an appropriate fix. It would
appear to be bugs in both compilers, which seems very odd.
2014-02-22 16:09:34 -07:00
Jason Turner
04131d208b Fix type on range.chai 2014-02-22 14:44:07 -07:00
Jason Turner
9c35ede59a Merge branch 'master' of https://github.com/ChaiScript/ChaiScript 2014-02-18 13:02:28 -07:00
Jason Turner
71348b7967 Update copyrights to 2014 and some comment formatting 2014-02-17 16:24:29 -07:00
Jason Turner
eb1a1c0275 Port windows unicode fix from @da2ce7 to 4.x branch 2014-01-23 12:28:33 -07:00
Jason Turner
8724e0cb80 Fix build error on 4.x branch if readline is found 2014-01-23 12:17:06 -07:00
Jason Turner
b872a50acc Add module search path relative to current exe #94 2014-01-05 14:59:31 -07:00
Jason Turner
36466a4ac5 Add unit tests for string size() fixes #93 #95 2014-01-05 12:22:44 -07:00
Jason Turner
bf83969723 Add unit test for calling 'count' on const map object. #90 2013-11-27 09:28:23 -07:00
Jason Turner
41f6ca18ea Prevent the user from naming an object with "::" #91 2013-11-27 08:53:34 -07:00
Jason Turner
a26d628e5c Cleanup (indentation, comments) for non-const global support
@Zoomulator - I pulled in your non-const global commits.
2013-11-27 08:19:26 -07:00
Jason Turner
d5fef3121a Merge remote-tracking branch 'zoomulator/master'
Adding zoomulator's patches for allowing of non-const globals if the user
wants to.
2013-11-27 08:06:39 -07:00
Jason Turner
16f09794cf Fixes #88: Linking error on MacOS Mavericks
The c++ library implementation on MacOS is broken, it does not allow you to
correctly use points to members of std::string. We work around this by not
directly using member pointers and instead wrapping the method calls
with our own functions.
2013-11-27 08:00:23 -07:00
Jason Turner
d44de49fb1 Add explicit tests for accessing data members of const objects 2013-09-27 20:19:16 -06:00
Jason Turner
101225aa68 - Fix legit threading issue which shows itself on clang / macos mostly
- Fix all warnings that I can / ignore those caused by boost
2013-05-26 22:47:23 -06:00
Jason Turner
feb344e744 Fix various warnings found by MSVC 2013-04-25 15:39:03 -06:00
Jason Turner
12d842ca5a Fix some additional warnings on MacOS 2013-04-25 12:21:41 -06:00
Jason Turner
e68599920a Move to pointers for Dynamic_Cast_Conversions to avoid problems with using the class before it's defined 2013-04-25 09:15:09 -06:00
Jason Turner
1e1385bc52 Do not link dl if on FreeBSD 2013-03-28 21:59:36 -06:00
Jason Turner
ae1221d46d Merge branch 'master' of https://github.com/ChaiScript/ChaiScript 2013-03-28 21:18:44 -06:00
Jason Turner
4fc51dfe05 Merge pull request #82 from mgee/master
Fix pretty_print for inline arrays.
2013-03-28 19:25:50 -07:00
Markus Groß
241ca75204 Correct pretty_print for inline arrays. 2013-02-28 17:39:30 +01:00
Jason Turner
2afc09dad4 Eradicate global base_class registrations to prevent problems with threading and general memory management issues with knowing how and when to clean them up. 2013-02-25 11:00:14 -07:00
Jason Turner
1858885010 Add example of wrapping data members of a class #79 2013-02-24 15:11:06 -07:00
Jason Turner
d068ce472c Clean up constuctors for AST_Node types 2013-02-24 13:55:20 -07:00
Jason Turner
380b94a8d2 Clean up implementation of for and while loops to make them easier to read and reduce code copying 2013-02-24 09:01:26 -07:00
Jason Turner
fd72b2951a Correct broken unit test for vector of suffixed numbers 2013-02-23 21:27:01 -07:00
Jason Turner
48f538438d Get all for loop related unit tests passing and expand the types of expressions
that can exist in a for loop
2013-02-23 21:14:37 -07:00
Jason Turner
c9995480e6 Add 'continue' command for loops. Also enhance for() unit tests which are now breaking and need to be fixed 2013-02-23 14:49:20 -07:00
Jason Turner
e298333ac6 Add unit test of suffixed number inside of vector initialization 2013-02-23 08:53:25 -07:00
Jason Turner
d225e09d5d Add profile for calling functions on members of a heterogeneous array 2013-02-23 08:49:31 -07:00
Jason Turner
59df213e66 Update version to 4.2.0 2012-11-30 20:38:17 -07:00
Jason Turner
0ea8931b21 Add ability to call functions requiring arithmetic value conversions
- Conversions are only attempted on a dispatch
 - Conversions are only attempted after a normal dispatch has failed
 - Conversions are only attempted if exactly one function matches
   the signature of the parameters passed in - excluding the mismatched
   arithmetic parameters
 - This feature should not be relied on in performance critical code
   overhead is added for each function call that requires a conversion
   to execute, see the tests performed above.
2012-11-27 21:21:37 -07:00
Kim Simmons
abcc6c9e3e Chai::add_global was added
Just a modification of the add_global_const. I don't see the point in
limiting the user to binding only constants to the global scope if the user
wishes to have a thread safe mutable object as a global. This was my case
anyway.
2012-11-06 17:20:56 +01:00
55 changed files with 1453 additions and 730 deletions

View File

@@ -17,8 +17,8 @@ set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/readme.md")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/description.txt") set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/description.txt")
set(CPACK_PACKAGE_VERSION_MAJOR 4) set(CPACK_PACKAGE_VERSION_MAJOR 4)
set(CPACK_PACKAGE_VERSION_MINOR 1) set(CPACK_PACKAGE_VERSION_MINOR 3)
set(CPACK_PACKAGE_VERSION_PATCH 1) set(CPACK_PACKAGE_VERSION_PATCH 0)
set(CPACK_PACKAGE_EXECUTABLES "chai;ChaiScript Eval") set(CPACK_PACKAGE_EXECUTABLES "chai;ChaiScript Eval")
set(CPACK_PACKAGE_VENDOR "ChaiScript.com") set(CPACK_PACKAGE_VENDOR "ChaiScript.com")
set(CPACK_PACKAGE_CONTACT "contact@chaiscript.com") set(CPACK_PACKAGE_CONTACT "contact@chaiscript.com")
@@ -95,9 +95,9 @@ else()
add_definitions(-DCHAISCRIPT_NO_THREADS) add_definitions(-DCHAISCRIPT_NO_THREADS)
endif() endif()
if (CMAKE_HOST_UNIX) if (CMAKE_HOST_UNIX AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
list(APPEND LIBS "dl") list(APPEND LIBS "dl")
endif(CMAKE_HOST_UNIX) endif()
list(APPEND LIBS ${READLINE_LIB}) list(APPEND LIBS ${READLINE_LIB})
@@ -212,6 +212,10 @@ if(BUILD_TESTING)
target_link_libraries(integer_literal_test ${LIBS}) target_link_libraries(integer_literal_test ${LIBS})
add_test(NAME Integer_Literal_Test COMMAND integer_literal_test) add_test(NAME Integer_Literal_Test COMMAND integer_literal_test)
add_executable(arithmetic_conversions_test unittests/arithmetic_conversions_test.cpp)
target_link_libraries(arithmetic_conversions_test ${LIBS})
add_test(NAME Arithmetic_Conversions_Test COMMAND arithmetic_conversions_test)
if (MULTITHREAD_SUPPORT_ENABLED) if (MULTITHREAD_SUPPORT_ENABLED)
add_executable(multithreaded_test unittests/multithreaded_test.cpp) add_executable(multithreaded_test unittests/multithreaded_test.cpp)
target_link_libraries(multithreaded_test ${LIBS}) target_link_libraries(multithreaded_test ${LIBS})
@@ -238,6 +242,7 @@ if(BUILD_TESTING)
endif(BUILD_TESTING) endif(BUILD_TESTING)
install(TARGETS chai ${MODULES} RUNTIME DESTINATION bin LIBRARY DESTINATION lib/chaiscript ) install(TARGETS chai ${MODULES} RUNTIME DESTINATION bin LIBRARY DESTINATION lib/chaiscript )
install(DIRECTORY include/chaiscript DESTINATION include install(DIRECTORY include/chaiscript DESTINATION include
PATTERN "*.hpp" PATTERN "*.hpp"
PATTERN "*/.svn*" EXCLUDE PATTERN "*/.svn*" EXCLUDE

View File

@@ -0,0 +1,10 @@
var my_array=["1", 4, 6.6l, 10ul, "1000", 100, 10.9f ];
for (var j = 0; j < 10000; ++j)
{
for (var i = 0; i < 6; ++i)
{
to_string(my_array[i]);
}
}

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_HPP_ #ifndef CHAISCRIPT_HPP_

View File

@@ -1,14 +1,23 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_THREADING_HPP_ #ifndef CHAISCRIPT_THREADING_HPP_
#define CHAISCRIPT_THREADING_HPP_ #define CHAISCRIPT_THREADING_HPP_
#ifndef CHAISCRIPT_NO_THREADS #ifndef CHAISCRIPT_NO_THREADS
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++11-long-long"
#pragma clang diagnostic ignored "-Wshadow"
#endif
#include <boost/thread.hpp> #include <boost/thread.hpp>
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
#else #else
#pragma message ("ChaiScript is compiling without thread safety.") #pragma message ("ChaiScript is compiling without thread safety.")
#endif #endif
@@ -75,6 +84,8 @@ namespace chaiscript
{ {
public: public:
unique_lock(T &) {} unique_lock(T &) {}
void lock() {}
void unlock() {}
}; };
template<typename T> template<typename T>
@@ -82,6 +93,7 @@ namespace chaiscript
{ {
public: public:
shared_lock(T &) {} shared_lock(T &) {}
void lock() {}
void unlock() {} void unlock() {}
}; };

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_BAD_BOXED_CAST_HPP_ #ifndef CHAISCRIPT_BAD_BOXED_CAST_HPP_

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#include <boost/preprocessor.hpp> #include <boost/preprocessor.hpp>

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_BOOTSTRAP_HPP_ #ifndef CHAISCRIPT_BOOTSTRAP_HPP_
@@ -273,28 +273,17 @@ namespace chaiscript
std::vector<Boxed_Value>(params.begin() + 1, params.end())))); std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
} }
/**
* Returns true if a call can be made that consists of the first parameter
* (the function) with the remaining parameters as its arguments.
*/
static Boxed_Value call_exists(const std::vector<Boxed_Value> &params)
{
if (params.size() < 1)
{
throw exception::arity_error(static_cast<int>(params.size()), 1);
}
Const_Proxy_Function f = boxed_cast<Const_Proxy_Function>(params[0]);
return Boxed_Value(f->call_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
}
static bool has_guard(const Const_Proxy_Function &t_pf) static bool has_guard(const Const_Proxy_Function &t_pf)
{ {
boost::shared_ptr<const dispatch::Dynamic_Proxy_Function> pf = boost::dynamic_pointer_cast<const dispatch::Dynamic_Proxy_Function>(t_pf); boost::shared_ptr<const dispatch::Dynamic_Proxy_Function> pf = boost::dynamic_pointer_cast<const dispatch::Dynamic_Proxy_Function>(t_pf);
if (pf) if (pf)
{ {
return pf->get_guard(); if (pf->get_guard()) {
return true;
} else {
return false;
}
} else { } else {
return false; return false;
} }
@@ -383,7 +372,6 @@ namespace chaiscript
m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity"); m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation"); m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation");
m->add(fun(&dispatch::Proxy_Function_Base::operator()), "call");
m->add(fun(&dispatch::Proxy_Function_Base::operator==), "=="); m->add(fun(&dispatch::Proxy_Function_Base::operator==), "==");
@@ -476,9 +464,6 @@ namespace chaiscript
m->add(fun(&ptr_assign<boost::remove_const<dispatch::Proxy_Function_Base>::type>), "="); m->add(fun(&ptr_assign<boost::remove_const<dispatch::Proxy_Function_Base>::type>), "=");
m->add(fun(&ptr_assign<boost::add_const<dispatch::Proxy_Function_Base>::type>), "="); m->add(fun(&ptr_assign<boost::add_const<dispatch::Proxy_Function_Base>::type>), "=");
m->add(Proxy_Function(new dispatch::Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
"call_exists");
m->add(fun(&type_match), "type_match"); m->add(fun(&type_match), "type_match");
return m; return m;

View File

@@ -1,16 +1,13 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
/** /// \file
* \file /// This file contains utility functions for registration of STL container
* This file contains utility functions for registration of STL container /// classes. The methodology used is based on the SGI STL concepts.
* classes. The methodology used is based on the SGI STL concepts. /// http://www.sgi.com/tech/stl/table_of_contents.html
* http://www.sgi.com/tech/stl/table_of_contents.html
*/
#ifndef CHAISCRIPT_BOOTSTRAP_STL_HPP_ #ifndef CHAISCRIPT_BOOTSTRAP_STL_HPP_
@@ -25,11 +22,10 @@ namespace chaiscript
{ {
namespace standard_library namespace standard_library
{ {
/**
* Bidir_Range, based on the D concept of ranges. /// Bidir_Range, based on the D concept of ranges.
* \todo Update the Range code to base its capabilities on /// \todo Update the Range code to base its capabilities on
* the user_typetraits of the iterator passed in /// the user_typetraits of the iterator passed in
*/
template<typename Container> template<typename Container>
struct Bidir_Range struct Bidir_Range
{ {
@@ -245,9 +241,8 @@ namespace chaiscript
/**
* Add Bidir_Range support for the given ContainerType /// Add Bidir_Range support for the given ContainerType
*/
template<typename Bidir_Type> template<typename Bidir_Type>
ModulePtr input_range_type_impl(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr input_range_type_impl(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -266,9 +261,8 @@ namespace chaiscript
return m; return m;
} }
/**
* Algorithm for inserting at a specific position into a container /// Algorithm for inserting at a specific position into a container
*/
template<typename Type> template<typename Type>
void insert_at(Type &container, int pos, const typename Type::value_type &v) void insert_at(Type &container, int pos, const typename Type::value_type &v)
{ {
@@ -284,9 +278,8 @@ namespace chaiscript
container.insert(itr, v); container.insert(itr, v);
} }
/**
* Algorithm for erasing a specific position from a container /// Algorithm for erasing a specific position from a container
*/
template<typename Type> template<typename Type>
void erase_at(Type &container, int pos) void erase_at(Type &container, int pos)
{ {
@@ -312,10 +305,9 @@ namespace chaiscript
return m; return m;
} }
/**
* Add random_access_container concept to the given ContainerType /// Add random_access_container concept to the given ContainerType
* http://www.sgi.com/tech/stl/RandomAccessContainer.html /// http://www.sgi.com/tech/stl/RandomAccessContainer.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -334,10 +326,9 @@ namespace chaiscript
return m; return m;
} }
/**
* Add assignable concept to the given ContainerType /// Add assignable concept to the given ContainerType
* http://www.sgi.com/tech/stl/Assignable.html /// http://www.sgi.com/tech/stl/Assignable.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr assignable_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr assignable_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -346,26 +337,22 @@ namespace chaiscript
return m; return m;
} }
/**
* Add container concept to the given ContainerType /// Add container concept to the given ContainerType
* http://www.sgi.com/tech/stl/Container.html /// http://www.sgi.com/tech/stl/Container.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
boost::function<int (const ContainerType *)> f = detail::return_int(&ContainerType::size); boost::function<int (const ContainerType *)> f = detail::return_int(&ContainerType::size);
m->add(fun(f), "size"); m->add(fun(f), "size");
// m->add(fun(boost::function<int (const ContainerType *)>(boost::mem_fn(&ContainerType::size))), "size");
m->add(fun<bool (ContainerType::*)() const>(&ContainerType::empty), "empty"); m->add(fun<bool (ContainerType::*)() const>(&ContainerType::empty), "empty");
m->add(fun<void (ContainerType::*)()>(&ContainerType::clear), "clear"); m->add(fun<void (ContainerType::*)()>(&ContainerType::clear), "clear");
return m; return m;
} }
/**
* Add default constructable concept to the given Type /// Add default constructable concept to the given Type
* http://www.sgi.com/tech/stl/DefaultConstructible.html /// http://www.sgi.com/tech/stl/DefaultConstructible.html
*/
template<typename Type> template<typename Type>
ModulePtr default_constructible_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr default_constructible_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -375,10 +362,9 @@ namespace chaiscript
/**
* Add sequence concept to the given ContainerType /// Add sequence concept to the given ContainerType
* http://www.sgi.com/tech/stl/Sequence.html /// http://www.sgi.com/tech/stl/Sequence.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -396,10 +382,9 @@ namespace chaiscript
return m; return m;
} }
/**
* Add back insertion sequence concept to the given ContainerType /// Add back insertion sequence concept to the given ContainerType
* http://www.sgi.com/tech/stl/BackInsertionSequence.html /// http://www.sgi.com/tech/stl/BackInsertionSequence.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr back_insertion_sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr back_insertion_sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -422,10 +407,9 @@ namespace chaiscript
} }
/**
*Front insertion sequence /// Front insertion sequence
*http://www.sgi.com/tech/stl/FrontInsertionSequence.html /// http://www.sgi.com/tech/stl/FrontInsertionSequence.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr front_insertion_sequence_type(const std::string &, ModulePtr m = ModulePtr(new Module())) ModulePtr front_insertion_sequence_type(const std::string &, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -448,10 +432,9 @@ namespace chaiscript
return m; return m;
} }
/**
* bootstrap a given PairType /// bootstrap a given PairType
* http://www.sgi.com/tech/stl/pair.html /// http://www.sgi.com/tech/stl/pair.html
*/
template<typename PairType> template<typename PairType>
ModulePtr pair_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr pair_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -471,10 +454,10 @@ namespace chaiscript
} }
/**
* Add pair associative container concept to the given ContainerType /// Add pair associative container concept to the given ContainerType
* http://www.sgi.com/tech/stl/PairAssociativeContainer.html /// http://www.sgi.com/tech/stl/PairAssociativeContainer.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -483,10 +466,9 @@ namespace chaiscript
return m; return m;
} }
/**
* Add unique associative container concept to the given ContainerType /// Add unique associative container concept to the given ContainerType
* http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html /// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
*/
template<typename ContainerType> template<typename ContainerType>
ModulePtr unique_associative_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr unique_associative_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -512,10 +494,9 @@ namespace chaiscript
return m; return m;
} }
/**
* Add a MapType container /// Add a MapType container
* http://www.sgi.com/tech/stl/Map.html /// http://www.sgi.com/tech/stl/Map.html
*/
template<typename MapType> template<typename MapType>
ModulePtr map_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr map_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -534,10 +515,9 @@ namespace chaiscript
return m; return m;
} }
/**
* hopefully working List type /// hopefully working List type
* http://www.sgi.com/tech/stl/List.html /// http://www.sgi.com/tech/stl/List.html
*/
template<typename ListType> template<typename ListType>
ModulePtr list_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr list_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -554,10 +534,9 @@ namespace chaiscript
return m; return m;
} }
/**
* Create a vector type with associated concepts /// Create a vector type with associated concepts
* http://www.sgi.com/tech/stl/Vector.html /// http://www.sgi.com/tech/stl/Vector.html
*/
template<typename VectorType> template<typename VectorType>
ModulePtr vector_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr vector_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -600,10 +579,34 @@ namespace chaiscript
return m; return m;
} }
/** namespace detail {
* Add a String container template<typename String>
* http://www.sgi.com/tech/stl/basic_string.html struct apple_string_workarounds
*/ {
/// The latest version of MacOS has a broken std::string implementation which will not allow
/// us to take pointers to the members. Code compiles, but does not link
/// \todo re-evaluate at some point
static size_t find(const String *s, const String &w, int pos) { return s->find(w, pos); }
static size_t rfind(const String *s, const String &w, size_t pos) { return s->rfind(w, pos); }
static size_t find_first_of(const String *s, const String &w, size_t pos) { return s->find_first_of(w, pos); }
static size_t find_last_of(const String *s, const String &w, size_t pos) { return s->find_last_of(w, pos); }
static size_t find_first_not_of(const String *s, const String &w, size_t pos) { return s->find_first_not_of(w, pos); }
static size_t find_last_not_of(const String *s, const String &w, size_t pos) { return s->find_last_not_of(w, pos); }
static void clear(String *s) { s->clear(); }
static bool empty(const String *s) { return s->empty(); }
static size_t size(const String *s) { return s->size(); }
static std::string substr(const String *s, size_t pos, size_t len) { return s->substr(pos,len); }
static const char *c_str(const String *s) { return s->c_str(); }
static const char *data(const String *s) { return s->data(); }
};
}
/// Add a String container
/// http://www.sgi.com/tech/stl/basic_string.html
template<typename String> template<typename String>
ModulePtr string_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) ModulePtr string_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{ {
@@ -614,7 +617,7 @@ namespace chaiscript
random_access_container_type<String>(type, m); random_access_container_type<String>(type, m);
sequence_type<String>(type, m); sequence_type<String>(type, m);
default_constructible_type<String>(type, m); default_constructible_type<String>(type, m);
container_type<String>(type, m); // container_type<String>(type, m);
assignable_type<String>(type, m); assignable_type<String>(type, m);
input_range_type<String>(type, m); input_range_type<String>(type, m);
@@ -628,20 +631,19 @@ namespace chaiscript
} }
m->add(fun(&String::push_back), push_back_name); m->add(fun(&String::push_back), push_back_name);
typedef typename String::size_type (String::*find_func_ptr)(const String &, typename String::size_type) const;
typedef boost::function<int (const String *, const String &, int)> find_func; m->add(fun(&detail::apple_string_workarounds<String>::find), "find");
m->add(fun(&detail::apple_string_workarounds<String>::rfind), "rfind");
m->add(fun(find_func(detail::return_int(static_cast<find_func_ptr>(&String::find)))), "find"); m->add(fun(&detail::apple_string_workarounds<String>::find_first_of), "find_first_of");
m->add(fun(find_func(detail::return_int(static_cast<find_func_ptr>(&String::rfind)))), "rfind"); m->add(fun(&detail::apple_string_workarounds<String>::find_last_of), "find_last_of");
m->add(fun(find_func(detail::return_int(static_cast<find_func_ptr>(&String::find_first_of)))), "find_first_of"); m->add(fun(&detail::apple_string_workarounds<String>::find_first_not_of), "find_first_not_of");
m->add(fun(find_func(detail::return_int(static_cast<find_func_ptr>(&String::find_last_of)))), "find_last_of"); m->add(fun(&detail::apple_string_workarounds<String>::find_last_not_of), "find_last_not_of");
m->add(fun(find_func(detail::return_int(static_cast<find_func_ptr>(&String::find_first_not_of)))), "find_first_not_of"); m->add(fun(&detail::apple_string_workarounds<String>::clear), "clear");
m->add(fun(find_func(detail::return_int(static_cast<find_func_ptr>(&String::find_last_not_of)))), "find_last_not_of"); m->add(fun(&detail::apple_string_workarounds<String>::size), "size");
m->add(fun(&detail::apple_string_workarounds<String>::empty), "empty");
m->add(fun(&detail::substr_helper<String, &String::substr>), "substr"); m->add(fun(&detail::apple_string_workarounds<String>::substr), "substr");
m->add(fun(&detail::apple_string_workarounds<String>::c_str), "c_str");
m->add(fun(&String::c_str), "c_str"); m->add(fun(&detail::apple_string_workarounds<String>::data), "data");
m->add(fun(&String::data), "data");
return m; return m;

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_BOXED_CAST_HPP_ #ifndef CHAISCRIPT_BOXED_CAST_HPP_
@@ -68,10 +68,10 @@ namespace chaiscript
/// assert(i == 5); /// assert(i == 5);
/// \endcode /// \endcode
template<typename Type> template<typename Type>
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv, const Dynamic_Cast_Conversions *t_conversions = 0)
{ {
try { try {
return detail::Cast_Helper<Type>::cast(bv); return detail::Cast_Helper<Type>::cast(bv, t_conversions);
} catch (const boost::bad_any_cast &) { } catch (const boost::bad_any_cast &) {
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
@@ -81,12 +81,12 @@ namespace chaiscript
#pragma warning(disable : 4127) #pragma warning(disable : 4127)
#endif #endif
if (boost::is_polymorphic<typename detail::Stripped_Type<Type>::type>::value) if (boost::is_polymorphic<typename detail::Stripped_Type<Type>::type>::value && t_conversions)
{ {
try { try {
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it // We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
// either way, we are not responsible if it doesn't work // either way, we are not responsible if it doesn't work
return detail::Cast_Helper<Type>::cast(detail::boxed_dynamic_cast<Type>(bv)); return detail::Cast_Helper<Type>::cast(t_conversions->boxed_dynamic_cast<Type>(bv), t_conversions);
} catch (const boost::bad_any_cast &) { } catch (const boost::bad_any_cast &) {
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type)); throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_BOXED_CAST_HELPER_HPP_ #ifndef CHAISCRIPT_BOXED_CAST_HELPER_HPP_
@@ -17,6 +17,8 @@
namespace chaiscript namespace chaiscript
{ {
class Dynamic_Cast_Conversions;
namespace detail namespace detail
{ {
// Cast_Helper_Inner helper classes // Cast_Helper_Inner helper classes
@@ -29,7 +31,7 @@ namespace chaiscript
{ {
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type; typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@@ -71,7 +73,7 @@ namespace chaiscript
{ {
typedef const Result * Result_Type; typedef const Result * Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@@ -100,7 +102,7 @@ namespace chaiscript
{ {
typedef Result * Result_Type; typedef Result * Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@@ -119,7 +121,7 @@ namespace chaiscript
{ {
typedef typename boost::reference_wrapper<Result> Result_Type; typedef typename boost::reference_wrapper<Result> Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
if (ob.is_ref()) if (ob.is_ref())
{ {
@@ -138,7 +140,7 @@ namespace chaiscript
{ {
typedef typename boost::shared_ptr<Result> Result_Type; typedef typename boost::shared_ptr<Result> Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
return boost::any_cast<boost::shared_ptr<Result> >(ob.get()); return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
} }
@@ -152,7 +154,7 @@ namespace chaiscript
{ {
typedef typename boost::shared_ptr<const Result> Result_Type; typedef typename boost::shared_ptr<const Result> Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
if (!ob.get_type_info().is_const()) if (!ob.get_type_info().is_const())
{ {
@@ -200,7 +202,7 @@ namespace chaiscript
{ {
typedef const Boxed_Value & Result_Type; typedef const Boxed_Value & Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
return ob; return ob;
} }
@@ -261,9 +263,9 @@ namespace chaiscript
{ {
typedef typename Cast_Helper_Inner<T>::Result_Type Result_Type; typedef typename Cast_Helper_Inner<T>::Result_Type Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *t_conversions)
{ {
return Cast_Helper_Inner<T>::cast(ob); return Cast_Helper_Inner<T>::cast(ob, t_conversions);
} }
}; };
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_BOXED_NUMERIC_HPP_ #ifndef CHAISCRIPT_BOXED_NUMERIC_HPP_
@@ -10,6 +10,7 @@
#include "boxed_value.hpp" #include "boxed_value.hpp"
#include "../language/chaiscript_algebraic.hpp" #include "../language/chaiscript_algebraic.hpp"
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#include <sstream>
namespace chaiscript namespace chaiscript
{ {
@@ -332,6 +333,46 @@ namespace chaiscript
validate_boxed_number(bv); validate_boxed_number(bv);
} }
Boxed_Number get_as(const Type_Info &inp_) const
{
if (inp_.bare_equal_type_info(typeid(int))) {
return Boxed_Number(get_as<int>());
} else if (inp_.bare_equal_type_info(typeid(double))) {
return Boxed_Number(get_as<double>());
} else if (inp_.bare_equal_type_info(typeid(float))) {
return Boxed_Number(get_as<float>());
} else if (inp_.bare_equal_type_info(typeid(long double))) {
return Boxed_Number(get_as<long double>());
} else if (inp_.bare_equal_type_info(typeid(char))) {
return Boxed_Number(get_as<char>());
} else if (inp_.bare_equal_type_info(typeid(unsigned int))) {
return Boxed_Number(get_as<unsigned int>());
} else if (inp_.bare_equal_type_info(typeid(long))) {
return Boxed_Number(get_as<long>());
} else if (inp_.bare_equal_type_info(typeid(unsigned long))) {
return Boxed_Number(get_as<unsigned long>());
} else if (inp_.bare_equal_type_info(typeid(boost::int8_t))) {
return Boxed_Number(get_as<boost::int8_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::int16_t))) {
return Boxed_Number(get_as<boost::int16_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::int32_t))) {
return Boxed_Number(get_as<boost::int32_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::int64_t))) {
return Boxed_Number(get_as<boost::int64_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::uint8_t))) {
return Boxed_Number(get_as<boost::uint8_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::uint16_t))) {
return Boxed_Number(get_as<boost::uint16_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::uint32_t))) {
return Boxed_Number(get_as<boost::uint32_t>());
} else if (inp_.bare_equal_type_info(typeid(boost::uint64_t))) {
return Boxed_Number(get_as<boost::uint64_t>());
} else {
throw boost::bad_any_cast();
}
}
template<typename Target> Target get_as() const template<typename Target> Target get_as() const
{ {
const Type_Info &inp_ = bv.get_type_info(); const Type_Info &inp_ = bv.get_type_info();
@@ -780,7 +821,7 @@ namespace chaiscript
{ {
typedef Boxed_Number Result_Type; typedef Boxed_Number Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *)
{ {
return Boxed_Number(ob); return Boxed_Number(ob);
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_BOXED_VALUE_HPP_ #ifndef CHAISCRIPT_BOXED_VALUE_HPP_
@@ -13,7 +13,19 @@
#include <map> #include <map>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include <boost/make_shared.hpp> #include <boost/make_shared.hpp>
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
#include <boost/any.hpp> #include <boost/any.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
#include <boost/ref.hpp> #include <boost/ref.hpp>

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_DISPATCHKIT_HPP_ #ifndef CHAISCRIPT_DISPATCHKIT_HPP_
@@ -53,9 +53,31 @@ namespace chaiscript
private: private:
std::string m_word; std::string m_word;
}; };
/**
* Exception thrown in the case that an object name is invalid because it contains illegal characters
*/
class illegal_name_error : public std::runtime_error
{
public:
illegal_name_error(const std::string &t_name) throw()
: std::runtime_error("Reserved name not allowed in object name: " + t_name), m_name(t_name)
{
}
virtual ~illegal_name_error() throw() {}
std::string name() const
{
return m_name;
}
private:
std::string m_name;
};
/** /**
* Exception thrown in the case that an object name is invalid because it already exists in current context * Exception thrown in the case that an object name is invalid because it already exists in current context
*/ */
@@ -222,9 +244,9 @@ namespace chaiscript
public: public:
Dispatch_Function(const std::vector<Proxy_Function> &t_funcs) Dispatch_Function(const std::vector<Proxy_Function> &t_funcs)
: Proxy_Function_Base(build_type_infos(t_funcs)), : Proxy_Function_Base(build_type_infos(t_funcs)),
m_funcs(t_funcs) m_funcs(t_funcs)
{ {
} }
virtual bool operator==(const dispatch::Proxy_Function_Base &rhs) const virtual bool operator==(const dispatch::Proxy_Function_Base &rhs) const
{ {
@@ -274,7 +296,7 @@ namespace chaiscript
return -1; // unknown arity return -1; // unknown arity
} }
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
typedef std::vector<Proxy_Function> function_vec; typedef std::vector<Proxy_Function> function_vec;
@@ -283,7 +305,7 @@ namespace chaiscript
while (begin != end) while (begin != end)
{ {
if ((*begin)->call_match(vals)) if ((*begin)->call_match(vals, t_conversions))
{ {
return true; return true;
} else { } else {
@@ -300,9 +322,9 @@ namespace chaiscript
} }
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
return dispatch::dispatch(m_funcs.begin(), m_funcs.end(), params); return dispatch::dispatch(m_funcs.begin(), m_funcs.end(), params, t_conversions);
} }
private: private:
@@ -389,16 +411,21 @@ namespace chaiscript
~Dispatch_Engine() ~Dispatch_Engine()
{ {
detail::Dynamic_Conversions::get().cleanup(m_conversions.begin(), m_conversions.end());
} }
/// \brief casts an object while applying any Dynamic_Conversion available
template<typename Type>
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
{
return chaiscript::boxed_cast<Type>(bv, &m_conversions);
}
/** /**
* Add a new conversion for upcasting to a base class * Add a new conversion for upcasting to a base class
*/ */
void add(const Dynamic_Cast_Conversion &d) void add(const Dynamic_Cast_Conversion &d)
{ {
m_conversions.push_back(d); m_conversions.add_conversion(d);
return detail::Dynamic_Conversions::get().add_conversion(d);
} }
/** /**
@@ -472,6 +499,25 @@ namespace chaiscript
} }
} }
/**
* Adds a new global (non-const) shared object, between all the threads
*/
void add_global(const Boxed_Value &obj, const std::string &name)
{
validate_object_name(name);
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_global_object_mutex);
if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end())
{
throw exception::name_conflict_error(name);
} else {
m_state.m_global_objects.insert(std::make_pair(name, obj));
}
}
/** /**
* Adds a new scope to the stack * Adds a new scope to the stack
*/ */
@@ -771,11 +817,16 @@ namespace chaiscript
m_state.m_reserved_words.insert(name); m_state.m_reserved_words.insert(name);
} }
const Dynamic_Cast_Conversions &conversions() const
{
return m_conversions;
}
Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> &params) const Boxed_Value call_function(const std::string &t_name, const std::vector<Boxed_Value> &params) const
{ {
std::vector<Proxy_Function> functions = get_function(t_name); std::vector<Proxy_Function> functions = get_function(t_name);
return dispatch::dispatch(functions.begin(), functions.end(), params); return dispatch::dispatch(functions.begin(), functions.end(), params, m_conversions);
} }
Boxed_Value call_function(const std::string &t_name) const Boxed_Value call_function(const std::string &t_name) const
@@ -844,6 +895,22 @@ namespace chaiscript
std::cout << ") " << std::endl; std::cout << ") " << std::endl;
} }
/**
* Returns true if a call can be made that consists of the first parameter
* (the function) with the remaining parameters as its arguments.
*/
Boxed_Value call_exists(const std::vector<Boxed_Value> &params)
{
if (params.size() < 1)
{
throw exception::arity_error(static_cast<int>(params.size()), 1);
}
Const_Proxy_Function f = this->boxed_cast<Const_Proxy_Function>(params[0]);
return Boxed_Value(f->call_match(std::vector<Boxed_Value>(params.begin() + 1, params.end()), m_conversions));
}
/** /**
* Dump all system info to stdout * Dump all system info to stdout
*/ */
@@ -1070,6 +1137,10 @@ namespace chaiscript
*/ */
void validate_object_name(const std::string &name) const void validate_object_name(const std::string &name) const
{ {
if (name.find("::") != std::string::npos) {
throw exception::illegal_name_error(name);
}
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex); chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
if (m_state.m_reserved_words.find(name) != m_state.m_reserved_words.end()) if (m_state.m_reserved_words.find(name) != m_state.m_reserved_words.end())
@@ -1109,6 +1180,14 @@ namespace chaiscript
vec.push_back(t_f); vec.push_back(t_f);
std::stable_sort(vec.begin(), vec.end(), &function_less_than); std::stable_sort(vec.begin(), vec.end(), &function_less_than);
func_objs[t_name] = Proxy_Function(new Dispatch_Function(vec)); func_objs[t_name] = Proxy_Function(new Dispatch_Function(vec));
} else if (t_f->has_arithmetic_param()) {
// if the function is the only function but it also contains
// arithmetic operators, we must wrap it in a dispatch function
// to allow for automatic arithmetic type conversions
std::vector<Proxy_Function> vec;
vec.push_back(t_f);
funcs.insert(std::make_pair(t_name, vec));
func_objs[t_name] = Proxy_Function(new Dispatch_Function(vec));
} else { } else {
std::vector<Proxy_Function> vec; std::vector<Proxy_Function> vec;
vec.push_back(t_f); vec.push_back(t_f);
@@ -1138,7 +1217,7 @@ namespace chaiscript
int call_depth; int call_depth;
}; };
std::vector<Dynamic_Cast_Conversion> m_conversions; Dynamic_Cast_Conversions m_conversions;
chaiscript::detail::threading::Thread_Storage<Stack_Holder> m_stack_holder; chaiscript::detail::threading::Thread_Storage<Stack_Holder> m_stack_holder;

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_ #ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
@@ -47,7 +47,7 @@ namespace chaiscript
class Dynamic_Conversion class Dynamic_Conversion
{ {
public: public:
virtual Boxed_Value convert(const Boxed_Value &derived) = 0; virtual Boxed_Value convert(const Boxed_Value &derived) const = 0;
const Type_Info &base() const Type_Info &base()
{ {
@@ -73,7 +73,7 @@ namespace chaiscript
}; };
template<typename Base, typename Derived> template<typename Base, typename Derived>
class Dynamic_Conversion_Impl : public Dynamic_Conversion class Dynamic_Conversion_Impl : public Dynamic_Conversion
{ {
public: public:
Dynamic_Conversion_Impl() Dynamic_Conversion_Impl()
@@ -81,7 +81,7 @@ namespace chaiscript
{ {
} }
virtual Boxed_Value convert(const Boxed_Value &t_derived) virtual Boxed_Value convert(const Boxed_Value &t_derived) const
{ {
if (t_derived.get_type_info().bare_equal(user_type<Derived>())) if (t_derived.get_type_info().bare_equal(user_type<Derived>()))
{ {
@@ -91,7 +91,7 @@ namespace chaiscript
if (t_derived.is_const()) if (t_derived.is_const())
{ {
boost::shared_ptr<const Base> data boost::shared_ptr<const Base> data
= boost::dynamic_pointer_cast<const Base>(detail::Cast_Helper<boost::shared_ptr<const Derived> >::cast(t_derived)); = boost::dynamic_pointer_cast<const Base>(detail::Cast_Helper<boost::shared_ptr<const Derived> >::cast(t_derived, 0));
if (!data) if (!data)
{ {
throw std::bad_cast(); throw std::bad_cast();
@@ -100,7 +100,7 @@ namespace chaiscript
return Boxed_Value(data); return Boxed_Value(data);
} else { } else {
boost::shared_ptr<Base> data boost::shared_ptr<Base> data
= boost::dynamic_pointer_cast<Base>(detail::Cast_Helper<boost::shared_ptr<Derived> >::cast(t_derived)); = boost::dynamic_pointer_cast<Base>(detail::Cast_Helper<boost::shared_ptr<Derived> >::cast(t_derived, 0));
if (!data) if (!data)
{ {
@@ -113,11 +113,11 @@ namespace chaiscript
// Pull the reference out of the contained boxed value, which we know is the type we want // Pull the reference out of the contained boxed value, which we know is the type we want
if (t_derived.is_const()) if (t_derived.is_const())
{ {
const Derived &d = detail::Cast_Helper<const Derived &>::cast(t_derived); const Derived &d = detail::Cast_Helper<const Derived &>::cast(t_derived, 0);
const Base &data = dynamic_cast<const Base &>(d); const Base &data = dynamic_cast<const Base &>(d);
return Boxed_Value(boost::cref(data)); return Boxed_Value(boost::cref(data));
} else { } else {
Derived &d = detail::Cast_Helper<Derived &>::cast(t_derived); Derived &d = detail::Cast_Helper<Derived &>::cast(t_derived, 0);
Base &data = dynamic_cast<Base &>(d); Base &data = dynamic_cast<Base &>(d);
return Boxed_Value(boost::ref(data)); return Boxed_Value(boost::ref(data));
} }
@@ -127,101 +127,98 @@ namespace chaiscript
} }
} }
}; };
class Dynamic_Conversions
{
public:
static inline Dynamic_Conversions &get()
{
static Dynamic_Conversions obj;
return obj;
}
template<typename Base, typename Derived>
static boost::shared_ptr<Dynamic_Conversion> create()
{
boost::shared_ptr<Dynamic_Conversion> conversion(new Dynamic_Conversion_Impl<Base, Derived>());
/// \todo this is a hack and a kludge. The idea is to make sure that
/// the conversion is registered both in the module's notion of the static conversion object
/// and in the global notion of the static conversion object
/// someday this will almost certainly have to change. Maybe it is time for ChaiScript
/// to become a library?
Dynamic_Conversions::get().add_conversion(conversion);
return conversion;
}
template<typename InItr>
void cleanup(InItr begin, const InItr &end)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
while (begin != end)
{
if (begin->unique())
{
m_conversions.erase(begin->get());
}
++begin;
}
}
void add_conversion(const boost::shared_ptr<Dynamic_Conversion> &conversion)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
m_conversions.insert(conversion.get());
}
bool has_conversion(const Type_Info &base, const Type_Info &derived) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return find(base, derived) != m_conversions.end();
}
Dynamic_Conversion *get_conversion(const Type_Info &base, const Type_Info &derived) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
std::set<Dynamic_Conversion *>::const_iterator itr =
find(base, derived);
if (itr != m_conversions.end())
{
return *itr;
} else {
throw std::out_of_range("No such conversion exists from " + derived.bare_name() + " to " + base.bare_name());
}
}
private:
Dynamic_Conversions() {}
std::set<Dynamic_Conversion *>::const_iterator find(
const Type_Info &base, const Type_Info &derived) const
{
for (std::set<Dynamic_Conversion *>::const_iterator itr = m_conversions.begin();
itr != m_conversions.end();
++itr)
{
if ((*itr)->base().bare_equal(base) && (*itr)->derived().bare_equal(derived))
{
return itr;
}
}
return m_conversions.end();
}
mutable chaiscript::detail::threading::shared_mutex m_mutex;
std::set<Dynamic_Conversion *> m_conversions;
};
} }
class Dynamic_Cast_Conversions
{
public:
Dynamic_Cast_Conversions()
{
}
Dynamic_Cast_Conversions(const Dynamic_Cast_Conversions &t_other)
: m_conversions(t_other.get_conversions())
{
}
void add_conversion(const boost::shared_ptr<detail::Dynamic_Conversion> &conversion)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
m_conversions.insert(conversion);
}
template<typename Base, typename Derived>
bool dynamic_cast_converts() const
{
return dynamic_cast_converts(user_type<Base>(), user_type<Derived>());
}
bool dynamic_cast_converts(const Type_Info &base, const Type_Info &derived) const
{
return has_conversion(base, derived);
}
template<typename Base>
Boxed_Value boxed_dynamic_cast(const Boxed_Value &derived) const
{
try {
return get_conversion(user_type<Base>(), derived.get_type_info())->convert(derived);
} catch (const std::out_of_range &) {
throw exception::bad_boxed_dynamic_cast(derived.get_type_info(), typeid(Base), "No known conversion");
} catch (const std::bad_cast &) {
throw exception::bad_boxed_dynamic_cast(derived.get_type_info(), typeid(Base), "Unable to perform dynamic_cast operation");
}
}
bool has_conversion(const Type_Info &base, const Type_Info &derived) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return find(base, derived) != m_conversions.end();
}
boost::shared_ptr<detail::Dynamic_Conversion> get_conversion(const Type_Info &base, const Type_Info &derived) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
std::set<boost::shared_ptr<detail::Dynamic_Conversion> >::const_iterator itr =
find(base, derived);
if (itr != m_conversions.end())
{
return *itr;
} else {
throw std::out_of_range("No such conversion exists from " + derived.bare_name() + " to " + base.bare_name());
}
}
private:
std::set<boost::shared_ptr<detail::Dynamic_Conversion> >::const_iterator find(
const Type_Info &base, const Type_Info &derived) const
{
for (std::set<boost::shared_ptr<detail::Dynamic_Conversion> >::const_iterator itr = m_conversions.begin();
itr != m_conversions.end();
++itr)
{
if ((*itr)->base().bare_equal(base) && (*itr)->derived().bare_equal(derived))
{
return itr;
}
}
return m_conversions.end();
}
std::set<boost::shared_ptr<detail::Dynamic_Conversion> > get_conversions() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return m_conversions;
}
mutable chaiscript::detail::threading::shared_mutex m_mutex;
std::set<boost::shared_ptr<detail::Dynamic_Conversion> > m_conversions;
};
typedef boost::shared_ptr<chaiscript::detail::Dynamic_Conversion> Dynamic_Cast_Conversion; typedef boost::shared_ptr<chaiscript::detail::Dynamic_Conversion> Dynamic_Cast_Conversion;
/// \brief Used to register a base / parent class relationship with ChaiScript. Necessary if you /// \brief Used to register a base / parent class relationship with ChaiScript. Necessary if you
@@ -248,42 +245,17 @@ namespace chaiscript
/// \todo Move share static type registration code into a mechanism that allows it to be properly /// \todo Move share static type registration code into a mechanism that allows it to be properly
/// shared by all modules /// shared by all modules
template<typename Base, typename Derived> template<typename Base, typename Derived>
Dynamic_Cast_Conversion base_class() Dynamic_Cast_Conversion base_class()
{
//Can only be used with related polymorphic types
//may be expanded some day to support conversions other than child -> parent
BOOST_STATIC_ASSERT((boost::is_base_of<Base,Derived>::value));
BOOST_STATIC_ASSERT(boost::is_polymorphic<Base>::value);
BOOST_STATIC_ASSERT(boost::is_polymorphic<Derived>::value);
return detail::Dynamic_Conversions::create<Base, Derived>();
}
namespace detail
{
template<typename Base, typename Derived>
bool dynamic_cast_converts()
{
return dynamic_cast_converts(user_type<Base>(), user_type<Derived>());
}
static bool dynamic_cast_converts(const Type_Info &base, const Type_Info &derived)
{ {
return detail::Dynamic_Conversions::get().has_conversion(base, derived); //Can only be used with related polymorphic types
//may be expanded some day to support conversions other than child -> parent
BOOST_STATIC_ASSERT((boost::is_base_of<Base,Derived>::value));
BOOST_STATIC_ASSERT(boost::is_polymorphic<Base>::value);
BOOST_STATIC_ASSERT(boost::is_polymorphic<Derived>::value);
return boost::shared_ptr<detail::Dynamic_Conversion>(new detail::Dynamic_Conversion_Impl<Base, Derived>());
} }
template<typename Base>
Boxed_Value boxed_dynamic_cast(const Boxed_Value &derived)
{
try {
return detail::Dynamic_Conversions::get().get_conversion(user_type<Base>(), derived.get_type_info())->convert(derived);
} catch (const std::out_of_range &) {
throw exception::bad_boxed_dynamic_cast(derived.get_type_info(), typeid(Base), "No known conversion");
} catch (const std::bad_cast &) {
throw exception::bad_boxed_dynamic_cast(derived.get_type_info(), typeid(Base), "Unable to perform dynamic_cast operation");
}
}
}
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_ #ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_
@@ -57,11 +57,11 @@ namespace chaiscript
const Proxy_Function &t_func, const Proxy_Function &t_func,
const boost::optional<Type_Info> &t_ti = boost::optional<Type_Info>()) const boost::optional<Type_Info> &t_ti = boost::optional<Type_Info>())
: Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti)), : Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti)),
m_type_name(t_type_name), m_func(t_func), m_ti(t_ti) m_type_name(t_type_name), m_func(t_func), m_ti(t_ti)
{ {
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0) assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)"); && "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
} }
virtual ~Dynamic_Object_Function() {} virtual ~Dynamic_Object_Function() {}
@@ -76,11 +76,11 @@ namespace chaiscript
} }
} }
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
if (dynamic_object_typename_match(vals, m_type_name, m_ti)) if (dynamic_object_typename_match(vals, m_type_name, m_ti, t_conversions))
{ {
return m_func->call_match(vals); return m_func->call_match(vals, t_conversions);
} else { } else {
return false; return false;
} }
@@ -106,19 +106,19 @@ namespace chaiscript
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
if (dynamic_object_typename_match(params, m_type_name, m_ti)) if (dynamic_object_typename_match(params, m_type_name, m_ti, t_conversions))
{ {
return (*m_func)(params); return (*m_func)(params, t_conversions);
} else { } else {
throw exception::guard_error(); throw exception::guard_error();
} }
} }
virtual bool compare_first_type(const Boxed_Value &bv) const virtual bool compare_first_type(const Boxed_Value &bv, const Dynamic_Cast_Conversions &t_conversions) const
{ {
return dynamic_object_typename_match(bv, m_type_name, m_ti); return dynamic_object_typename_match(bv, m_type_name, m_ti, t_conversions);
} }
private: private:
@@ -139,13 +139,13 @@ namespace chaiscript
} }
static bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name, static bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name,
const boost::optional<Type_Info> &ti) const boost::optional<Type_Info> &ti, const Dynamic_Cast_Conversions &t_conversions)
{ {
static Type_Info doti = user_type<Dynamic_Object>(); static Type_Info doti = user_type<Dynamic_Object>();
if (bv.get_type_info().bare_equal(doti)) if (bv.get_type_info().bare_equal(doti))
{ {
try { try {
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv); const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
return name == "Dynamic_Object" || d.get_type_name() == name; return name == "Dynamic_Object" || d.get_type_name() == name;
} catch (const std::bad_cast &) { } catch (const std::bad_cast &) {
return false; return false;
@@ -162,11 +162,11 @@ namespace chaiscript
} }
static bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name, static bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
const boost::optional<Type_Info> &ti) const boost::optional<Type_Info> &ti, const Dynamic_Cast_Conversions &t_conversions)
{ {
if (bvs.size() > 0) if (bvs.size() > 0)
{ {
return dynamic_object_typename_match(bvs[0], name, ti); return dynamic_object_typename_match(bvs[0], name, ti, t_conversions);
} else { } else {
return false; return false;
} }
@@ -192,11 +192,11 @@ namespace chaiscript
const std::string &t_type_name, const std::string &t_type_name,
const Proxy_Function &t_func) const Proxy_Function &t_func)
: Proxy_Function_Base(build_type_list(t_func->get_param_types())), : Proxy_Function_Base(build_type_list(t_func->get_param_types())),
m_type_name(t_type_name), m_func(t_func) m_type_name(t_type_name), m_func(t_func)
{ {
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0) assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)"); && "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
} }
static std::vector<Type_Info> build_type_list(const std::vector<Type_Info> &tl) static std::vector<Type_Info> build_type_list(const std::vector<Type_Info> &tl)
{ {
@@ -224,13 +224,13 @@ namespace chaiscript
} }
} }
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
std::vector<Boxed_Value> new_vals; std::vector<Boxed_Value> new_vals;
new_vals.push_back(Boxed_Value(Dynamic_Object(m_type_name))); new_vals.push_back(Boxed_Value(Dynamic_Object(m_type_name)));
new_vals.insert(new_vals.end(), vals.begin(), vals.end()); new_vals.insert(new_vals.end(), vals.begin(), vals.end());
return m_func->call_match(new_vals); return m_func->call_match(new_vals, t_conversions);
} }
@@ -246,14 +246,14 @@ namespace chaiscript
} }
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
std::vector<Boxed_Value> new_params; std::vector<Boxed_Value> new_params;
chaiscript::Boxed_Value bv = var(Dynamic_Object(m_type_name)); chaiscript::Boxed_Value bv = var(Dynamic_Object(m_type_name));
new_params.push_back(bv); new_params.push_back(bv);
new_params.insert(new_params.end(), params.begin(), params.end()); new_params.insert(new_params.end(), params.begin(), params.end());
(*m_func)(new_params); (*m_func)(new_params, t_conversions);
return bv; return bv;
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_ #ifndef CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_
@@ -15,65 +15,77 @@ namespace chaiscript
{ {
struct Exception_Handler_Base struct Exception_Handler_Base
{ {
virtual void handle(const Boxed_Value &bv) = 0; virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine) = 0;
virtual ~Exception_Handler_Base() {}
protected: protected:
template<typename T> template<typename T>
void throw_type(const Boxed_Value &bv) void throw_type(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
try { T t = boxed_cast<T>(bv); throw t; } catch (const exception::bad_boxed_cast &) {} try { T t = t_engine.boxed_cast<T>(bv); throw t; } catch (const exception::bad_boxed_cast &) {}
} }
}; };
template<typename T1> template<typename T1>
struct Exception_Handler_Impl1 : Exception_Handler_Base struct Exception_Handler_Impl1 : Exception_Handler_Base
{ {
virtual void handle(const Boxed_Value &bv) virtual ~Exception_Handler_Impl1() {}
virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
throw_type<T1>(bv); throw_type<T1>(bv, t_engine);
} }
}; };
template<typename T1, typename T2> template<typename T1, typename T2>
struct Exception_Handler_Impl2 : Exception_Handler_Base struct Exception_Handler_Impl2 : Exception_Handler_Base
{ {
virtual void handle(const Boxed_Value &bv) virtual ~Exception_Handler_Impl2() {}
virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
throw_type<T1>(bv); throw_type<T1>(bv, t_engine);
throw_type<T2>(bv); throw_type<T2>(bv, t_engine);
} }
}; };
template<typename T1, typename T2, typename T3> template<typename T1, typename T2, typename T3>
struct Exception_Handler_Impl3 : Exception_Handler_Base struct Exception_Handler_Impl3 : Exception_Handler_Base
{ {
virtual void handle(const Boxed_Value &bv) virtual ~Exception_Handler_Impl3() {}
virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
throw_type<T1>(bv); throw_type<T1>(bv, t_engine);
throw_type<T2>(bv); throw_type<T2>(bv, t_engine);
throw_type<T3>(bv); throw_type<T3>(bv, t_engine);
} }
}; };
template<typename T1, typename T2, typename T3, typename T4> template<typename T1, typename T2, typename T3, typename T4>
struct Exception_Handler_Impl4 : Exception_Handler_Base struct Exception_Handler_Impl4 : Exception_Handler_Base
{ {
virtual void handle(const Boxed_Value &bv) virtual ~Exception_Handler_Impl4() {}
virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
throw_type<T1>(bv); throw_type<T1>(bv, t_engine);
throw_type<T2>(bv); throw_type<T2>(bv, t_engine);
throw_type<T3>(bv); throw_type<T3>(bv, t_engine);
throw_type<T4>(bv); throw_type<T4>(bv, t_engine);
} }
}; };
template<typename T1, typename T2, typename T3, typename T4, typename T5> template<typename T1, typename T2, typename T3, typename T4, typename T5>
struct Exception_Handler_Impl5 : Exception_Handler_Base struct Exception_Handler_Impl5 : Exception_Handler_Base
{ {
virtual void handle(const Boxed_Value &bv) virtual ~Exception_Handler_Impl5() {}
virtual void handle(const Boxed_Value &bv, const Dispatch_Engine &t_engine)
{ {
throw_type<T1>(bv); throw_type<T1>(bv, t_engine);
throw_type<T2>(bv); throw_type<T2>(bv, t_engine);
throw_type<T3>(bv); throw_type<T3>(bv, t_engine);
throw_type<T4>(bv); throw_type<T4>(bv, t_engine);
throw_type<T5>(bv); throw_type<T5>(bv, t_engine);
} }
}; };
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_FUNCTION_CALL_HPP_ #ifndef CHAISCRIPT_FUNCTION_CALL_HPP_
@@ -32,10 +32,10 @@ namespace chaiscript
*/ */
template<typename FunctionType> template<typename FunctionType>
boost::function<FunctionType> boost::function<FunctionType>
functor(const std::vector<Const_Proxy_Function> &funcs) functor(const std::vector<Const_Proxy_Function> &funcs, const Dynamic_Cast_Conversions *t_conversions)
{ {
FunctionType *p=0; FunctionType *p=0;
return detail::build_function_caller_helper(p, funcs); return detail::build_function_caller_helper(p, funcs, t_conversions);
} }
/** /**
@@ -53,11 +53,11 @@ namespace chaiscript
*/ */
template<typename FunctionType> template<typename FunctionType>
boost::function<FunctionType> boost::function<FunctionType>
functor(Const_Proxy_Function func) functor(Const_Proxy_Function func, const Dynamic_Cast_Conversions *t_conversions)
{ {
std::vector<Const_Proxy_Function> funcs; std::vector<Const_Proxy_Function> funcs;
funcs.push_back(func); funcs.push_back(func);
return functor<FunctionType>(funcs); return functor<FunctionType>(funcs, t_conversions);
} }
/** /**
@@ -66,9 +66,9 @@ namespace chaiscript
*/ */
template<typename FunctionType> template<typename FunctionType>
boost::function<FunctionType> boost::function<FunctionType>
functor(const Boxed_Value &bv) functor(const Boxed_Value &bv, const Dynamic_Cast_Conversions *t_conversions)
{ {
return functor<FunctionType>(boxed_cast<Const_Proxy_Function >(bv)); return functor<FunctionType>(boxed_cast<Const_Proxy_Function >(bv, t_conversions), t_conversions);
} }
} }
@@ -81,13 +81,13 @@ namespace chaiscript
{ {
typedef boost::function<Signature> Result_Type; typedef boost::function<Signature> Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *t_conversions)
{ {
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>())) if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
{ {
return dispatch::functor<Signature>(ob); return dispatch::functor<Signature>(ob, t_conversions);
} else { } else {
return Cast_Helper_Inner<const boost::function<Signature> &>::cast(ob); return Cast_Helper_Inner<const boost::function<Signature> &>::cast(ob, t_conversions);
} }
} }
}; };
@@ -100,13 +100,13 @@ namespace chaiscript
{ {
typedef boost::function<Signature> Result_Type; typedef boost::function<Signature> Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *t_conversions)
{ {
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>())) if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
{ {
return dispatch::functor<Signature>(ob); return dispatch::functor<Signature>(ob, t_conversions);
} else { } else {
return Cast_Helper_Inner<boost::function<Signature> >::cast(ob); return Cast_Helper_Inner<boost::function<Signature> >::cast(ob, t_conversions);
} }
} }
}; };
@@ -119,13 +119,13 @@ namespace chaiscript
{ {
typedef boost::function<Signature> Result_Type; typedef boost::function<Signature> Result_Type;
static Result_Type cast(const Boxed_Value &ob) static Result_Type cast(const Boxed_Value &ob, const Dynamic_Cast_Conversions *t_conversions)
{ {
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>())) if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
{ {
return dispatch::functor<Signature>(ob); return dispatch::functor<Signature>(ob, t_conversions);
} else { } else {
return Cast_Helper_Inner<const boost::function<Signature> >::cast(ob); return Cast_Helper_Inner<const boost::function<Signature> >::cast(ob, t_conversions);
} }
} }
}; };

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#include <boost/preprocessor.hpp> #include <boost/preprocessor.hpp>
@@ -36,9 +36,9 @@ namespace chaiscript
struct Function_Caller_Ret struct Function_Caller_Ret
{ {
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs, static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions)
{ {
return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params)); return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, t_conversions));
} }
}; };
@@ -49,9 +49,9 @@ namespace chaiscript
struct Function_Caller_Ret<void> struct Function_Caller_Ret<void>
{ {
static void call(const std::vector<Const_Proxy_Function> &t_funcs, static void call(const std::vector<Const_Proxy_Function> &t_funcs,
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions)
{ {
dispatch::dispatch(t_funcs, params); dispatch::dispatch(t_funcs, params, t_conversions);
} }
}; };
} }
@@ -76,14 +76,14 @@ namespace chaiscript
* used internally for unwrapping a function call's types * used internally for unwrapping a function call's types
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
Ret function_caller(const std::vector<Const_Proxy_Function> &funcs Ret function_caller(const std::vector<Const_Proxy_Function> &funcs, const Dynamic_Cast_Conversions &t_conversions
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) ) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
{ {
std::vector<Boxed_Value> params; std::vector<Boxed_Value> params;
BOOST_PP_REPEAT(n, addparam, ~) BOOST_PP_REPEAT(n, addparam, ~);
return Function_Caller_Ret<Ret>::call(funcs, params); return Function_Caller_Ret<Ret>::call(funcs, params, t_conversions);
} }
/** /**
@@ -91,7 +91,8 @@ namespace chaiscript
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) > template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) > boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<Const_Proxy_Function> &funcs) build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<Const_Proxy_Function> &funcs,
const Dynamic_Cast_Conversions *t_conversions)
{ {
if (funcs.size() == 1) if (funcs.size() == 1)
{ {
@@ -107,7 +108,7 @@ namespace chaiscript
// we cannot make any other guesses or assumptions really, so continuing // we cannot make any other guesses or assumptions really, so continuing
} }
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs, (t_conversions?*t_conversions:Dynamic_Cast_Conversions())
BOOST_PP_ENUM_TRAILING(n, curry, ~)); BOOST_PP_ENUM_TRAILING(n, curry, ~));
} }
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_HANDLE_RETURN_HPP_ #ifndef CHAISCRIPT_HANDLE_RETURN_HPP_

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_OPERATORS_HPP_ #ifndef CHAISCRIPT_OPERATORS_HPP_

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#include <boost/preprocessor.hpp> #include <boost/preprocessor.hpp>

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
@@ -70,9 +70,9 @@ namespace chaiscript
{ {
public: public:
virtual ~Proxy_Function_Base() {} virtual ~Proxy_Function_Base() {}
Boxed_Value operator()(const std::vector<Boxed_Value> &params) const Boxed_Value operator()(const std::vector<Boxed_Value> &params, const chaiscript::Dynamic_Cast_Conversions &t_conversions) const
{ {
Boxed_Value bv = do_call(params); Boxed_Value bv = do_call(params, t_conversions);
return bv; return bv;
} }
@@ -83,7 +83,12 @@ namespace chaiscript
std::vector<Type_Info> get_param_types() const { return m_types; } std::vector<Type_Info> get_param_types() const { return m_types; }
virtual bool operator==(const Proxy_Function_Base &) const = 0; virtual bool operator==(const Proxy_Function_Base &) const = 0;
virtual bool call_match(const std::vector<Boxed_Value> &vals) const = 0; virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const = 0;
bool has_arithmetic_param() const
{
return m_has_arithmetic_param;
}
virtual std::vector<boost::shared_ptr<const Proxy_Function_Base> > get_contained_functions() const virtual std::vector<boost::shared_ptr<const Proxy_Function_Base> > get_contained_functions() const
{ {
@@ -93,7 +98,7 @@ namespace chaiscript
//! Return true if the function is a possible match //! Return true if the function is a possible match
//! to the passed in values //! to the passed in values
bool filter(const std::vector<Boxed_Value> &vals) const bool filter(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
int arity = get_arity(); int arity = get_arity();
@@ -105,7 +110,7 @@ namespace chaiscript
{ {
return true; return true;
} else { } else {
return compare_first_type(vals[0]); return compare_first_type(vals[0], t_conversions);
} }
} else { } else {
return false; return false;
@@ -117,30 +122,15 @@ namespace chaiscript
virtual std::string annotation() const = 0; virtual std::string annotation() const = 0;
protected: static bool compare_type_to_param(const Type_Info &ti, const Boxed_Value &bv, const Dynamic_Cast_Conversions &t_conversions)
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const = 0;
Proxy_Function_Base(const std::vector<Type_Info> &t_types)
: m_types(t_types)
{ {
}
virtual bool compare_first_type(const Boxed_Value &bv) const
{
const std::vector<Type_Info> &types = get_param_types();
if (types.size() < 2)
{
return true;
}
const Type_Info &ti = types[1];
if (ti.is_undef() if (ti.is_undef()
|| ti.bare_equal(user_type<Boxed_Value>()) || ti.bare_equal(user_type<Boxed_Value>())
|| (!bv.get_type_info().is_undef() || (!bv.get_type_info().is_undef()
&& (ti.bare_equal(user_type<Boxed_Number>()) && (ti.bare_equal(user_type<Boxed_Number>())
|| ti.bare_equal(bv.get_type_info()) || ti.bare_equal(bv.get_type_info())
|| chaiscript::detail::dynamic_cast_converts(ti, bv.get_type_info())
|| bv.get_type_info().bare_equal(user_type<boost::shared_ptr<const Proxy_Function_Base> >()) || bv.get_type_info().bare_equal(user_type<boost::shared_ptr<const Proxy_Function_Base> >())
|| t_conversions.dynamic_cast_converts(ti, bv.get_type_info())
) )
) )
) )
@@ -150,8 +140,38 @@ namespace chaiscript
return false; return false;
} }
} }
protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const = 0;
bool compare_types(const std::vector<Type_Info> &tis, const std::vector<Boxed_Value> &bvs) const Proxy_Function_Base(const std::vector<Type_Info> &t_types)
: m_types(t_types), m_has_arithmetic_param(false)
{
for (size_t i = 1; i < m_types.size(); ++i)
{
if (m_types[i].is_arithmetic())
{
m_has_arithmetic_param = true;
return;
}
}
}
virtual bool compare_first_type(const Boxed_Value &bv, const Dynamic_Cast_Conversions &t_conversions) const
{
const std::vector<Type_Info> &types = get_param_types();
if (types.size() < 2)
{
return true;
}
const Type_Info &ti = types[1];
return compare_type_to_param(ti, bv, t_conversions);
}
static bool compare_types(const std::vector<Type_Info> &tis, const std::vector<Boxed_Value> &bvs)
{ {
if (tis.size() - 1 != bvs.size()) if (tis.size() - 1 != bvs.size())
{ {
@@ -170,6 +190,8 @@ namespace chaiscript
} }
std::vector<Type_Info> m_types; std::vector<Type_Info> m_types;
bool m_has_arithmetic_param;
}; };
} }
@@ -225,10 +247,10 @@ namespace chaiscript
&& !this->m_guard && !prhs->m_guard); && !this->m_guard && !prhs->m_guard);
} }
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
return (m_arity < 0 || vals.size() == size_t(m_arity)) return (m_arity < 0 || vals.size() == size_t(m_arity))
&& test_guard(vals); && test_guard(vals, t_conversions);
} }
virtual ~Dynamic_Proxy_Function() {} virtual ~Dynamic_Proxy_Function() {}
@@ -255,12 +277,12 @@ namespace chaiscript
} }
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
if (m_arity < 0 || params.size() == size_t(m_arity)) if (m_arity < 0 || params.size() == size_t(m_arity))
{ {
if (test_guard(params)) if (test_guard(params, t_conversions))
{ {
return m_f(params); return m_f(params);
} else { } else {
@@ -273,12 +295,12 @@ namespace chaiscript
} }
private: private:
bool test_guard(const std::vector<Boxed_Value> &params) const bool test_guard(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
if (m_guard) if (m_guard)
{ {
try { try {
return boxed_cast<bool>((*m_guard)(params)); return boxed_cast<bool>((*m_guard)(params, t_conversions));
} catch (const exception::arity_error &) { } catch (const exception::arity_error &) {
return false; return false;
} catch (const exception::bad_boxed_cast &) { } catch (const exception::bad_boxed_cast &) {
@@ -334,7 +356,7 @@ namespace chaiscript
Bound_Function(const Const_Proxy_Function &t_f, Bound_Function(const Const_Proxy_Function &t_f,
const std::vector<Boxed_Value> &t_args) const std::vector<Boxed_Value> &t_args)
: Proxy_Function_Base(build_param_type_info(t_f, 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:static_cast<int>(get_param_types().size())-1) m_f(t_f), m_args(t_args), m_arity(t_f->get_arity()<0?-1:static_cast<int>(get_param_types().size())-1)
{ {
assert(m_f->get_arity() < 0 || m_f->get_arity() == static_cast<int>(m_args.size())); assert(m_f->get_arity() < 0 || m_f->get_arity() == static_cast<int>(m_args.size()));
} }
@@ -346,14 +368,9 @@ namespace chaiscript
virtual ~Bound_Function() {} virtual ~Bound_Function() {}
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
return m_f->call_match(build_param_list(vals)); return m_f->call_match(build_param_list(vals), t_conversions);
}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) const
{
return (*m_f)(build_param_list(params));
} }
virtual std::vector<Const_Proxy_Function> get_contained_functions() const virtual std::vector<Const_Proxy_Function> get_contained_functions() const
@@ -430,9 +447,9 @@ namespace chaiscript
return retval; return retval;
} }
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
return (*m_f)(build_param_list(params)); return (*m_f)(build_param_list(params), t_conversions);
} }
private: private:
@@ -453,8 +470,8 @@ namespace chaiscript
Proxy_Function_Impl(const boost::function<Func> &f) Proxy_Function_Impl(const boost::function<Func> &f)
: Proxy_Function_Base(detail::build_param_type_list(static_cast<Func *>(0))), : Proxy_Function_Base(detail::build_param_type_list(static_cast<Func *>(0))),
m_f(f), m_dummy_func(0) m_f(f), m_dummy_func(0)
{ {
} }
virtual ~Proxy_Function_Impl() {} virtual ~Proxy_Function_Impl() {}
@@ -471,14 +488,14 @@ namespace chaiscript
} }
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &t_conversions) const
{ {
if (int(vals.size()) != get_arity()) if (int(vals.size()) != get_arity())
{ {
return false; return false;
} }
return compare_types(m_types, vals) || detail::compare_types_cast(m_dummy_func, vals); return compare_types(m_types, vals) || detail::compare_types_cast(m_dummy_func, vals, t_conversions);
} }
virtual std::string annotation() const virtual std::string annotation() const
@@ -492,9 +509,9 @@ namespace chaiscript
} }
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
return detail::Do_Call<typename boost::function<Func>::result_type>::go(m_f, params); return detail::Do_Call<typename boost::function<Func>::result_type>::go(m_f, params, t_conversions);
} }
private: private:
@@ -512,8 +529,8 @@ namespace chaiscript
Attribute_Access(T Class::* t_attr) Attribute_Access(T Class::* t_attr)
: Proxy_Function_Base(param_types()), : Proxy_Function_Base(param_types()),
m_attr(t_attr) m_attr(t_attr)
{ {
} }
virtual ~Attribute_Access() {} virtual ~Attribute_Access() {}
@@ -534,7 +551,7 @@ namespace chaiscript
return 1; return 1;
} }
virtual bool call_match(const std::vector<Boxed_Value> &vals) const virtual bool call_match(const std::vector<Boxed_Value> &vals, const Dynamic_Cast_Conversions &) const
{ {
if (vals.size() != 1) if (vals.size() != 1)
{ {
@@ -550,17 +567,17 @@ namespace chaiscript
} }
protected: protected:
virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params) const virtual Boxed_Value do_call(const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions) const
{ {
if (params.size() == 1) if (params.size() == 1)
{ {
const Boxed_Value &bv = params[0]; const Boxed_Value &bv = params[0];
if (bv.is_const()) if (bv.is_const())
{ {
const Class *o = boxed_cast<const Class *>(bv); const Class *o = boxed_cast<const Class *>(bv, &t_conversions);
return detail::Handle_Return<typename boost::add_reference<T>::type>::handle(o->*m_attr); return detail::Handle_Return<typename boost::add_reference<T>::type>::handle(o->*m_attr);
} else { } else {
Class *o = boxed_cast<Class *>(bv); Class *o = boxed_cast<Class *>(bv, &t_conversions);
return detail::Handle_Return<typename boost::add_reference<T>::type>::handle(o->*m_attr); return detail::Handle_Return<typename boost::add_reference<T>::type>::handle(o->*m_attr);
} }
} else { } else {
@@ -605,6 +622,94 @@ namespace chaiscript
namespace dispatch namespace dispatch
{ {
namespace detail
{
template<typename FuncType>
bool types_match_except_for_arithmetic(const FuncType &t_func, const std::vector<Boxed_Value> &plist,
const Dynamic_Cast_Conversions &t_conversions)
{
if (t_func->get_arity() != static_cast<int>(plist.size()))
{
return false;
}
const std::vector<Type_Info> &types = t_func->get_param_types();
assert(plist.size() == types.size() - 1);
for (size_t i = 0; i < plist.size(); ++i)
{
if (Proxy_Function_Base::compare_type_to_param(types[i+1], plist[i], t_conversions)
|| (types[i+1].is_arithmetic() && plist[i].get_type_info().is_arithmetic()))
{
// types continue to match
} else {
return false;
}
}
// all types match
return true;
}
template<typename InItr>
Boxed_Value dispatch_with_conversions(InItr begin, const InItr &end, const std::vector<Boxed_Value> &plist,
const Dynamic_Cast_Conversions &t_conversions)
{
InItr orig(begin);
InItr matching_func(end);
while (begin != end)
{
if (types_match_except_for_arithmetic(*begin, plist, t_conversions))
{
if (matching_func == end)
{
matching_func = begin;
} else {
// More than one function matches, not attempting
throw exception::dispatch_error(plist, std::vector<Const_Proxy_Function>(orig, end));
}
}
++begin;
}
if (matching_func == end)
{
// no appropriate function to attempt arithmetic type conversion on
throw exception::dispatch_error(plist, std::vector<Const_Proxy_Function>(orig, end));
}
std::vector<Boxed_Value> newplist;
const std::vector<Type_Info> &tis = (*matching_func)->get_param_types();
for (size_t i = 0; i < plist.size(); ++i)
{
if (tis[i+1].is_arithmetic()
&& plist[i].get_type_info().is_arithmetic()) {
newplist.push_back(Boxed_Number(plist[i]).get_as(tis[i+1]).bv);
} else {
newplist.push_back(plist[i]);
}
}
try {
return (*(*matching_func))(newplist, t_conversions);
} catch (const exception::bad_boxed_cast &) {
//parameter failed to cast
} catch (const exception::arity_error &) {
//invalid num params
} catch (const exception::guard_error &) {
//guard failed to allow the function to execute
}
throw exception::dispatch_error(plist, std::vector<Const_Proxy_Function>(orig, end));
}
}
/** /**
* Take a vector of functions and a vector of parameters. Attempt to execute * Take a vector of functions and a vector of parameters. Attempt to execute
@@ -613,15 +718,15 @@ namespace chaiscript
*/ */
template<typename InItr> template<typename InItr>
Boxed_Value dispatch(InItr begin, const InItr &end, Boxed_Value dispatch(InItr begin, const InItr &end,
const std::vector<Boxed_Value> &plist) const std::vector<Boxed_Value> &plist, const Dynamic_Cast_Conversions &t_conversions)
{ {
InItr orig(begin); InItr orig(begin);
while (begin != end) while (begin != end)
{ {
try { try {
if ((*begin)->filter(plist)) if ((*begin)->filter(plist, t_conversions))
{ {
return (*(*begin))(plist); return (*(*begin))(plist, t_conversions);
} }
} catch (const exception::bad_boxed_cast &) { } catch (const exception::bad_boxed_cast &) {
//parameter failed to cast, try again //parameter failed to cast, try again
@@ -634,7 +739,7 @@ namespace chaiscript
++begin; ++begin;
} }
throw exception::dispatch_error(plist, std::vector<Const_Proxy_Function>(orig, end)); return detail::dispatch_with_conversions(orig, end, plist, t_conversions);
} }
/** /**
@@ -644,9 +749,9 @@ namespace chaiscript
*/ */
template<typename Funcs> template<typename Funcs>
Boxed_Value dispatch(const Funcs &funcs, Boxed_Value dispatch(const Funcs &funcs,
const std::vector<Boxed_Value> &plist) const std::vector<Boxed_Value> &plist, const Dynamic_Cast_Conversions &t_conversions)
{ {
return dispatch::dispatch(funcs.begin(), funcs.end(), plist); return dispatch::dispatch(funcs.begin(), funcs.end(), plist, t_conversions);
} }
} }
} }

View File

@@ -1,14 +1,14 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#include <boost/preprocessor.hpp> #include <boost/preprocessor.hpp>
#define gettypeinfo(z,n,text) ti.push_back(chaiscript::detail::Get_Type_Info<Param ## n>::get()); #define gettypeinfo(z,n,text) ti.push_back(chaiscript::detail::Get_Type_Info<Param ## n>::get());
#define casthelper(z,n,text) BOOST_PP_COMMA_IF(n) chaiscript::boxed_cast< Param ## n >(params[n]) #define casthelper(z,n,text) BOOST_PP_COMMA_IF(n) chaiscript::boxed_cast< Param ## n >(params[n], &t_conversions)
#define trycast(z,n,text) chaiscript::boxed_cast<Param ## n>(params[n]); #define trycast(z,n,text) chaiscript::boxed_cast<Param ## n>(params[n], &t_conversions);
#ifndef BOOST_PP_IS_ITERATING #ifndef BOOST_PP_IS_ITERATING
#ifndef CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_ #ifndef CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_
@@ -86,9 +86,23 @@ namespace chaiscript
* if any unboxing fails the execution of the function fails and * if any unboxing fails the execution of the function fails and
* the bad_boxed_cast is passed up to the caller. * the bad_boxed_cast is passed up to the caller.
*/ */
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)> template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
Ret call_func(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f, Ret call_func(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f,
const std::vector<Boxed_Value> &params) const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions & BOOST_PP_IF(n, t_conversions, BOOST_PP_EMPTY))
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
{ {
if (params.size() != n) if (params.size() != n)
{ {
@@ -98,16 +112,37 @@ namespace chaiscript
} }
} }
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
/** /**
* Used by Proxy_Function_Impl to determine if it is equivalent to another * Used by Proxy_Function_Impl to determine if it is equivalent to another
* Proxy_Function_Impl object. This function is primarly used to prevent * Proxy_Function_Impl object. This function is primarly used to prevent
* registration of two functions with the exact same signatures * registration of two functions with the exact same signatures
*/ */
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4100)
#endif
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
bool compare_types_cast(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)), bool compare_types_cast(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)),
const std::vector<Boxed_Value> & BOOST_PP_IF(n, params, )) const std::vector<Boxed_Value> & BOOST_PP_IF(n, params, BOOST_PP_EMPTY), const Dynamic_Cast_Conversions &t_conversions)
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
{ {
try { try {
(void)t_conversions;
BOOST_PP_REPEAT(n, trycast, ~); BOOST_PP_REPEAT(n, trycast, ~);
} catch (const exception::bad_boxed_cast &) { } catch (const exception::bad_boxed_cast &) {
return false; return false;
@@ -115,6 +150,11 @@ namespace chaiscript
return true; return true;
} }
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
} }
} }
} }
@@ -140,9 +180,9 @@ namespace chaiscript
struct Do_Call struct Do_Call
{ {
template<typename Fun> template<typename Fun>
static Boxed_Value go(const boost::function<Fun> &fun, const std::vector<Boxed_Value> &params) static Boxed_Value go(const boost::function<Fun> &fun, const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions)
{ {
return Handle_Return<Ret>::handle(call_func(fun, params)); return Handle_Return<Ret>::handle(call_func(fun, params, t_conversions));
} }
}; };
@@ -150,9 +190,9 @@ namespace chaiscript
struct Do_Call<void> struct Do_Call<void>
{ {
template<typename Fun> template<typename Fun>
static Boxed_Value go(const boost::function<Fun> &fun, const std::vector<Boxed_Value> &params) static Boxed_Value go(const boost::function<Fun> &fun, const std::vector<Boxed_Value> &params, const Dynamic_Cast_Conversions &t_conversions)
{ {
call_func(fun, params); call_func(fun, params, t_conversions);
return Handle_Return<void>::handle(); return Handle_Return<void>::handle();
} }
}; };

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_ #ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_TYPE_INFO_HPP_ #ifndef CHAISCRIPT_TYPE_INFO_HPP_
@@ -99,6 +99,12 @@ namespace chaiscript
|| (ti.m_bare_type_info && m_bare_type_info && *ti.m_bare_type_info == *m_bare_type_info); || (ti.m_bare_type_info && m_bare_type_info && *ti.m_bare_type_info == *m_bare_type_info);
} }
bool bare_equal_type_info(const std::type_info &ti) const
{
return m_bare_type_info != 0
&& (*m_bare_type_info) == ti;
}
bool is_const() const { return m_is_const; } bool is_const() const { return m_is_const; }
bool is_reference() const { return m_is_reference; } bool is_reference() const { return m_is_reference; }
bool is_void() const { return m_is_void; } bool is_void() const { return m_is_void; }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_ALGEBRAIC_HPP_ #ifndef CHAISCRIPT_ALGEBRAIC_HPP_

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_COMMON_HPP_ #ifndef CHAISCRIPT_COMMON_HPP_
@@ -21,23 +21,22 @@ namespace chaiscript
public: public:
enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl, enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl,
Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String, Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range, Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range,
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or, Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
Logical_And, Logical_Or, Switch, Case, Default, Ternary_Cond Logical_And, Logical_Or, Switch, Case, Default, Ternary_Cond, Noop
}; };
}; };
namespace namespace
{ {
/**
* Helper lookup to get the name of each node type /// Helper lookup to get the name of each node type
*/
const char *ast_node_type_to_string(int ast_node_type) { const char *ast_node_type_to_string(int ast_node_type) {
const char *ast_node_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", const char *ast_node_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl",
"Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", "Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String",
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range", "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range",
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or", "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or",
"Logical_And", "Logical_Or", "Switch", "Case", "Default", "Ternary Condition"}; "Logical_And", "Logical_Or", "Switch", "Case", "Default", "Ternary Condition", "Noop"};
return ast_node_types[ast_node_type]; return ast_node_types[ast_node_type];
} }
@@ -61,9 +60,8 @@ namespace chaiscript
/// \brief Classes which may be thrown during error cases when ChaiScript is executing. /// \brief Classes which may be thrown during error cases when ChaiScript is executing.
namespace exception namespace exception
{ {
/**
* Errors generated during parsing or evaluation /// Errors generated during parsing or evaluation
*/
struct eval_error : public std::runtime_error { struct eval_error : public std::runtime_error {
std::string reason; std::string reason;
File_Position start_position; File_Position start_position;
@@ -377,9 +375,8 @@ namespace chaiscript
} }
}; };
/**
* Errors generated when loading a file /// Errors generated when loading a file
*/
struct file_not_found_error : public std::runtime_error { struct file_not_found_error : public std::runtime_error {
file_not_found_error(const std::string &t_filename) throw() file_not_found_error(const std::string &t_filename) throw()
: std::runtime_error("File Not Found: " + t_filename) : std::runtime_error("File Not Found: " + t_filename)
@@ -414,9 +411,8 @@ namespace chaiscript
return oss.str(); return oss.str();
} }
/**
* Prints the contents of an AST node, including its children, recursively /// Prints the contents of an AST node, including its children, recursively
*/
std::string to_string(std::string t_prepend = "") { std::string to_string(std::string t_prepend = "") {
std::ostringstream oss; std::ostringstream oss;
@@ -478,22 +474,27 @@ namespace chaiscript
{ {
namespace detail namespace detail
{ {
/**
* Special type for returned values /// Special type for returned values
*/
struct Return_Value { struct Return_Value {
Boxed_Value retval; Boxed_Value retval;
Return_Value(const Boxed_Value &t_return_value) : retval(t_return_value) { } Return_Value(const Boxed_Value &t_return_value) : retval(t_return_value) { }
}; };
/**
* Special type indicating a call to 'break' /// Special type indicating a call to 'break'
*/
struct Break_Loop { struct Break_Loop {
Break_Loop() { } Break_Loop() { }
}; };
/// Special type indicating a call to 'continue'
struct Continue_Loop {
Continue_Loop() { }
};
/// Creates a new scope then pops it on destruction /// Creates a new scope then pops it on destruction
struct Scope_Push_Pop struct Scope_Push_Pop
{ {

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_ENGINE_HPP_ #ifndef CHAISCRIPT_ENGINE_HPP_
@@ -251,9 +251,8 @@ namespace chaiscript
chaiscript::detail::Dispatch_Engine m_engine; chaiscript::detail::Dispatch_Engine m_engine;
/**
* Evaluates the given string in by parsing it and running the results through the evaluator /// Evaluates the given string in by parsing it and running the results through the evaluator
*/
Boxed_Value do_eval(const std::string &t_input, const std::string &t_filename = "__EVAL__", bool /* t_internal*/ = false) Boxed_Value do_eval(const std::string &t_input, const std::string &t_filename = "__EVAL__", bool /* t_internal*/ = false)
{ {
try { try {
@@ -282,9 +281,8 @@ namespace chaiscript
} }
/**
* Evaluates the given string, used during eval() inside of a script /// Evaluates the given string, used during eval() inside of a script
*/
const Boxed_Value internal_eval(const std::string &t_e) { const Boxed_Value internal_eval(const std::string &t_e) {
try { try {
return do_eval(t_e, "__EVAL__", true); return do_eval(t_e, "__EVAL__", true);
@@ -294,16 +292,14 @@ namespace chaiscript
} }
/**
* Returns the current evaluation m_engine /// Returns the current evaluation m_engine
*/
chaiscript::detail::Dispatch_Engine &get_eval_engine() { chaiscript::detail::Dispatch_Engine &get_eval_engine() {
return m_engine; return m_engine;
} }
/**
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude. /// Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
*/
void build_eval_system() { void build_eval_system() {
using namespace bootstrap; using namespace bootstrap;
m_engine.add_reserved_word("auto"); m_engine.add_reserved_word("auto");
@@ -334,6 +330,13 @@ namespace chaiscript
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_function_objects, boost::ref(m_engine)), "get_functions"); m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_function_objects, boost::ref(m_engine)), "get_functions");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_scripting_objects, boost::ref(m_engine)), "get_objects"); m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_scripting_objects, boost::ref(m_engine)), "get_objects");
m_engine.add(Proxy_Function(new dispatch::Dynamic_Proxy_Function(boost::bind(&chaiscript::detail::Dispatch_Engine::call_exists, boost::ref(m_engine), _1))),
"call_exists");
m_engine.add(fun<Boxed_Value (const dispatch::Proxy_Function_Base *, const std::vector<Boxed_Value> &)>(boost::bind(&chaiscript::dispatch::Proxy_Function_Base::operator(), _1, _2, boost::ref(m_engine.conversions()))), "call");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_type_name, boost::ref(m_engine)), "name"); m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_type_name, boost::ref(m_engine)), "name");
@@ -355,9 +358,8 @@ namespace chaiscript
do_eval(chaiscript_prelude, "standard prelude"); do_eval(chaiscript_prelude, "standard prelude");
} }
/**
* Helper function for loading a file /// Helper function for loading a file
*/
std::string load_file(const std::string &t_filename) { std::string load_file(const std::string &t_filename) {
std::ifstream infile(t_filename.c_str(), std::ios::in | std::ios::ate | std::ios::binary ); std::ifstream infile(t_filename.c_str(), std::ios::in | std::ios::ate | std::ios::binary );
@@ -413,19 +415,20 @@ namespace chaiscript
try { try {
const std::string appendedpath = m_usepaths[i] + t_filename; const std::string appendedpath = m_usepaths[i] + t_filename;
chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex); chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex); chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
if (m_used_files.count(appendedpath) == 0) if (m_used_files.count(appendedpath) == 0)
{ {
m_used_files.insert(appendedpath);
l2.unlock(); l2.unlock();
eval_file(appendedpath); eval_file(appendedpath);
} l2.lock();
m_used_files.insert(appendedpath);
}
return; // return, we loaded it, or it was already loaded return; // return, we loaded it, or it was already loaded
} catch (const exception::file_not_found_error &) { } catch (const exception::file_not_found_error &) {
if (i == m_usepaths.size() - 1) if (i == m_usepaths.size() - 1)
{ {
throw exception::file_not_found_error(t_filename); throw exception::file_not_found_error(t_filename);
} }
@@ -446,6 +449,17 @@ namespace chaiscript
return *this; return *this;
} }
/// \brief Adds a mutable object that is available in all contexts and to all threads
/// \param[in] t_bv Boxed_Value to add as a global
/// \param[in] t_name Name of the value to add
/// \warning The user is responsible for making sure the object is thread-safe if necessary
/// ChaiScript is thread-safe but provides no threading locking mechanism to the script
ChaiScript &add_global(const Boxed_Value &t_bv, const std::string &t_name)
{
m_engine.add_global(t_bv, t_name);
return *this;
}
/// \brief Represents the current state of the ChaiScript system. State and be saved and restored /// \brief Represents the current state of the ChaiScript system. State and be saved and restored
/// \sa ChaiScript::get_state /// \sa ChaiScript::get_state
/// \sa ChaiScript::set_state /// \sa ChaiScript::set_state
@@ -665,7 +679,7 @@ namespace chaiscript
return do_eval(t_script); return do_eval(t_script);
} catch (Boxed_Value &bv) { } catch (Boxed_Value &bv) {
if (t_handler) { if (t_handler) {
t_handler->handle(bv); t_handler->handle(bv, m_engine);
} }
throw bv; throw bv;
} }
@@ -688,15 +702,23 @@ namespace chaiscript
T eval(const std::string &t_input, const Exception_Handler &t_handler = Exception_Handler(), const std::string &t_filename="__EVAL__") T eval(const std::string &t_input, const Exception_Handler &t_handler = Exception_Handler(), const std::string &t_filename="__EVAL__")
{ {
try { try {
return boxed_cast<T>(do_eval(t_input, t_filename)); return m_engine.boxed_cast<T>(do_eval(t_input, t_filename));
} catch (Boxed_Value &bv) { } catch (Boxed_Value &bv) {
if (t_handler) { if (t_handler) {
t_handler->handle(bv); t_handler->handle(bv, m_engine);
} }
throw bv; throw bv;
} }
} }
/// \brief casts an object while applying any Dynamic_Conversion available
template<typename Type>
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
{
return m_engine.boxed_cast<Type>(bv);
}
/// \brief Evaluates a string. /// \brief Evaluates a string.
/// ///
/// \param[in] t_input Script to execute /// \param[in] t_input Script to execute
@@ -713,7 +735,7 @@ namespace chaiscript
return do_eval(t_input, t_filename); return do_eval(t_input, t_filename);
} catch (Boxed_Value &bv) { } catch (Boxed_Value &bv) {
if (t_handler) { if (t_handler) {
t_handler->handle(bv); t_handler->handle(bv, m_engine);
} }
throw bv; throw bv;
} }
@@ -729,7 +751,7 @@ namespace chaiscript
return do_eval(load_file(t_filename), t_filename); return do_eval(load_file(t_filename), t_filename);
} catch (Boxed_Value &bv) { } catch (Boxed_Value &bv) {
if (t_handler) { if (t_handler) {
t_handler->handle(bv); t_handler->handle(bv, m_engine);
} }
throw bv; throw bv;
} }
@@ -746,10 +768,10 @@ namespace chaiscript
template<typename T> template<typename T>
T eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) { T eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
try { try {
return boxed_cast<T>(do_eval(load_file(t_filename), t_filename)); return m_engine.boxed_cast<T>(do_eval(load_file(t_filename), t_filename));
} catch (Boxed_Value &bv) { } catch (Boxed_Value &bv) {
if (t_handler) { if (t_handler) {
t_handler->handle(bv); t_handler->handle(bv, m_engine);
} }
throw bv; throw bv;
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_EVAL_HPP_ #ifndef CHAISCRIPT_EVAL_HPP_
@@ -38,7 +38,7 @@ namespace chaiscript
struct Binary_Operator_AST_Node : public AST_Node { struct Binary_Operator_AST_Node : public AST_Node {
public: public:
Binary_Operator_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Xor, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Binary_Operator_AST_Node(const std::string &t_ast_node_text, int t_id, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
{ } { }
@@ -92,19 +92,19 @@ namespace chaiscript
struct Error_AST_Node : public AST_Node { struct Error_AST_Node : public AST_Node {
public: public:
Error_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Error, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Error_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Error, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Error_AST_Node() {} virtual ~Error_AST_Node() {}
}; };
struct Int_AST_Node : public AST_Node { struct Int_AST_Node : public AST_Node {
public: public:
Int_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Int, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Int_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Int, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(const_var(int(atoi(t_ast_node_text.c_str())))) { } m_value(const_var(int(atoi(t_ast_node_text.c_str())))) { }
Int_AST_Node(const std::string &t_ast_node_text, const Boxed_Value &t_bv, int t_id = AST_Node_Type::Int, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Int_AST_Node(const std::string &t_ast_node_text, const Boxed_Value &t_bv, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Int, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(t_bv) { } m_value(t_bv) { }
virtual ~Int_AST_Node() {} virtual ~Int_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){
@@ -118,11 +118,11 @@ namespace chaiscript
struct Float_AST_Node : public AST_Node { struct Float_AST_Node : public AST_Node {
public: public:
Float_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Float, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Float_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Float, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(const_var(double(atof(t_ast_node_text.c_str())))) { } m_value(const_var(double(atof(t_ast_node_text.c_str())))) { }
Float_AST_Node(const std::string &t_ast_node_text, const Boxed_Value &t_bv, int t_id = AST_Node_Type::Float, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Float_AST_Node(const std::string &t_ast_node_text, const Boxed_Value &t_bv, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Float, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(t_bv) { } m_value(t_bv) { }
virtual ~Float_AST_Node() {} virtual ~Float_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){
@@ -136,8 +136,8 @@ namespace chaiscript
struct Id_AST_Node : public AST_Node { struct Id_AST_Node : public AST_Node {
public: public:
Id_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Id, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Id_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(get_value(t_ast_node_text)) m_value(get_value(t_ast_node_text))
{ } { }
@@ -180,22 +180,22 @@ namespace chaiscript
struct Char_AST_Node : public AST_Node { struct Char_AST_Node : public AST_Node {
public: public:
Char_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Char, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Char_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Char, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Char_AST_Node() {} virtual ~Char_AST_Node() {}
}; };
struct Str_AST_Node : public AST_Node { struct Str_AST_Node : public AST_Node {
public: public:
Str_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Str, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Str_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Str, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Str_AST_Node() {} virtual ~Str_AST_Node() {}
}; };
struct Eol_AST_Node : public AST_Node { struct Eol_AST_Node : public AST_Node {
public: public:
Eol_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Eol, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Eol_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Eol, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Eol_AST_Node() {} virtual ~Eol_AST_Node() {}
virtual std::string pretty_print() const virtual std::string pretty_print() const
@@ -206,8 +206,8 @@ namespace chaiscript
struct Fun_Call_AST_Node : public AST_Node { struct Fun_Call_AST_Node : public AST_Node {
public: public:
Fun_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Fun_Call, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Fun_Call_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Fun_Call, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Fun_Call_AST_Node() {} virtual ~Fun_Call_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
@@ -225,7 +225,7 @@ namespace chaiscript
try { try {
chaiscript::eval::detail::Stack_Push_Pop spp(t_ss); chaiscript::eval::detail::Stack_Push_Pop spp(t_ss);
const Boxed_Value &retval = (*boxed_cast<const Const_Proxy_Function &>(fn))(plb); const Boxed_Value &retval = (*t_ss.boxed_cast<const Const_Proxy_Function &>(fn))(plb, t_ss.conversions());
return retval; return retval;
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
@@ -233,7 +233,7 @@ namespace chaiscript
} }
catch(const exception::bad_boxed_cast &){ catch(const exception::bad_boxed_cast &){
try { try {
Const_Proxy_Function f = boxed_cast<const Const_Proxy_Function &>(fn); Const_Proxy_Function f = t_ss.boxed_cast<const Const_Proxy_Function &>(fn);
// handle the case where there is only 1 function to try to call and dispatch fails on it // handle the case where there is only 1 function to try to call and dispatch fails on it
std::vector<Const_Proxy_Function> funcs; std::vector<Const_Proxy_Function> funcs;
funcs.push_back(f); funcs.push_back(f);
@@ -275,8 +275,8 @@ namespace chaiscript
struct Inplace_Fun_Call_AST_Node : public AST_Node { struct Inplace_Fun_Call_AST_Node : public AST_Node {
public: 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<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Inplace_Fun_Call_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Inplace_Fun_Call, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Inplace_Fun_Call_AST_Node() {} virtual ~Inplace_Fun_Call_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
dispatch::Param_List_Builder plb; dispatch::Param_List_Builder plb;
@@ -293,11 +293,11 @@ namespace chaiscript
try { try {
bv = this->children[0]->eval(t_ss); bv = this->children[0]->eval(t_ss);
try { try {
fn = boxed_cast<const Const_Proxy_Function &>(bv); fn = t_ss.boxed_cast<const Const_Proxy_Function &>(bv);
} catch (const exception::bad_boxed_cast &) { } catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("'" + this->children[0]->pretty_print() + "' does not evaluate to a function."); throw exception::eval_error("'" + this->children[0]->pretty_print() + "' does not evaluate to a function.");
} }
return (*fn)(plb); return (*fn)(plb, t_ss.conversions());
} }
catch(const exception::dispatch_error &e){ catch(const exception::dispatch_error &e){
throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'", e.parameters, e.functions, false, t_ss); throw exception::eval_error(std::string(e.what()) + " with function '" + this->children[0]->text + "'", e.parameters, e.functions, false, t_ss);
@@ -340,8 +340,8 @@ namespace chaiscript
struct Arg_List_AST_Node : public AST_Node { struct Arg_List_AST_Node : public AST_Node {
public: public:
Arg_List_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Arg_List, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Arg_List_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Arg_List, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Arg_List_AST_Node() {} virtual ~Arg_List_AST_Node() {}
virtual std::string pretty_print() const virtual std::string pretty_print() const
@@ -362,15 +362,15 @@ namespace chaiscript
struct Variable_AST_Node : public AST_Node { struct Variable_AST_Node : public AST_Node {
public: public:
Variable_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Variable, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Variable_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Variable, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Variable_AST_Node() {} virtual ~Variable_AST_Node() {}
}; };
struct Equation_AST_Node : public AST_Node { struct Equation_AST_Node : public AST_Node {
public: public:
Equation_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Equation, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Equation_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) AST_Node(t_ast_node_text, AST_Node_Type::Equation, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
{} {}
virtual ~Equation_AST_Node() {} virtual ~Equation_AST_Node() {}
@@ -429,8 +429,8 @@ namespace chaiscript
struct Var_Decl_AST_Node : public AST_Node { struct Var_Decl_AST_Node : public AST_Node {
public: public:
Var_Decl_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Var_Decl, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Var_Decl_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Var_Decl, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Var_Decl_AST_Node() {} virtual ~Var_Decl_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
try { try {
@@ -453,17 +453,17 @@ namespace chaiscript
struct Comparison_AST_Node : public Binary_Operator_AST_Node { struct Comparison_AST_Node : public Binary_Operator_AST_Node {
public: public:
Comparison_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Comparison, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Comparison_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Comparison, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Comparison_AST_Node() {} virtual ~Comparison_AST_Node() {}
}; };
struct Addition_AST_Node : public Binary_Operator_AST_Node { struct Addition_AST_Node : public Binary_Operator_AST_Node {
public: public:
Addition_AST_Node(const std::string &t_ast_node_text = "+", int t_id = AST_Node_Type::Addition, Addition_AST_Node(const std::string &t_ast_node_text = "+",
const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(),
int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Addition, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Addition_AST_Node() {} virtual ~Addition_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
return do_oper(t_ss, Operators::sum, "+", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); return do_oper(t_ss, Operators::sum, "+", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
@@ -472,10 +472,10 @@ namespace chaiscript
struct Subtraction_AST_Node : public Binary_Operator_AST_Node { struct Subtraction_AST_Node : public Binary_Operator_AST_Node {
public: public:
Subtraction_AST_Node(const std::string &t_ast_node_text = "-", int t_id = AST_Node_Type::Subtraction, Subtraction_AST_Node(const std::string &t_ast_node_text = "-",
const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0,
int t_start_col = 0, int t_end_line = 0, int t_end_col = 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Subtraction, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Subtraction_AST_Node() {} virtual ~Subtraction_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
return do_oper(t_ss, Operators::difference, "-", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); return do_oper(t_ss, Operators::difference, "-", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
@@ -484,10 +484,10 @@ namespace chaiscript
struct Multiplication_AST_Node : public Binary_Operator_AST_Node { struct Multiplication_AST_Node : public Binary_Operator_AST_Node {
public: public:
Multiplication_AST_Node(const std::string &t_ast_node_text = "*", int t_id = AST_Node_Type::Multiplication, Multiplication_AST_Node(const std::string &t_ast_node_text = "*",
const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0,
int t_start_col = 0, int t_end_line = 0, int t_end_col = 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Multiplication, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Multiplication_AST_Node() {} virtual ~Multiplication_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
return do_oper(t_ss, Operators::product, "*", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); return do_oper(t_ss, Operators::product, "*", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
@@ -496,10 +496,10 @@ namespace chaiscript
struct Division_AST_Node : public Binary_Operator_AST_Node { struct Division_AST_Node : public Binary_Operator_AST_Node {
public: public:
Division_AST_Node(const std::string &t_ast_node_text = "/", int t_id = AST_Node_Type::Division, Division_AST_Node(const std::string &t_ast_node_text = "/",
const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0,
int t_start_col = 0, int t_end_line = 0, int t_end_col = 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Division, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Division_AST_Node() {} virtual ~Division_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
return do_oper(t_ss, Operators::quotient, "/", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); return do_oper(t_ss, Operators::quotient, "/", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
@@ -508,10 +508,10 @@ namespace chaiscript
struct Modulus_AST_Node : public Binary_Operator_AST_Node { struct Modulus_AST_Node : public Binary_Operator_AST_Node {
public: public:
Modulus_AST_Node(const std::string &t_ast_node_text = "%", int t_id = AST_Node_Type::Modulus, Modulus_AST_Node(const std::string &t_ast_node_text = "%",
const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0,
int t_start_col = 0, int t_end_line = 0, int t_end_col = 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Modulus, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Modulus_AST_Node() {} virtual ~Modulus_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
return do_oper(t_ss, Operators::remainder, "%", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss)); return do_oper(t_ss, Operators::remainder, "%", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
@@ -520,8 +520,8 @@ namespace chaiscript
struct Array_Call_AST_Node : public AST_Node { struct Array_Call_AST_Node : public AST_Node {
public: public:
Array_Call_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Array_Call, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Array_Call_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Array_Call, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Array_Call_AST_Node() {} virtual ~Array_Call_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss); chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
@@ -568,8 +568,8 @@ namespace chaiscript
struct Dot_Access_AST_Node : public AST_Node { struct Dot_Access_AST_Node : public AST_Node {
public: public:
Dot_Access_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Dot_Access, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Dot_Access_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Dot_Access, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Dot_Access_AST_Node() {} virtual ~Dot_Access_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
Boxed_Value retval = this->children[0]->eval(t_ss); Boxed_Value retval = this->children[0]->eval(t_ss);
@@ -635,8 +635,8 @@ namespace chaiscript
struct Quoted_String_AST_Node : public AST_Node { struct Quoted_String_AST_Node : public AST_Node {
public: public:
Quoted_String_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Quoted_String, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Quoted_String_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Quoted_String, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(const_var(t_ast_node_text)) { } m_value(const_var(t_ast_node_text)) { }
virtual ~Quoted_String_AST_Node() {} virtual ~Quoted_String_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &) {
@@ -655,8 +655,8 @@ namespace chaiscript
struct Single_Quoted_String_AST_Node : public AST_Node { struct Single_Quoted_String_AST_Node : public AST_Node {
public: 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<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Single_Quoted_String_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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), AST_Node(t_ast_node_text, AST_Node_Type::Single_Quoted_String, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(const_var(char(t_ast_node_text.at(0)))) { } m_value(const_var(char(t_ast_node_text.at(0)))) { }
virtual ~Single_Quoted_String_AST_Node() {} virtual ~Single_Quoted_String_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){
@@ -674,8 +674,8 @@ namespace chaiscript
struct Lambda_AST_Node : public AST_Node { struct Lambda_AST_Node : public AST_Node {
public: public:
Lambda_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Lambda, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Lambda_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Lambda, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Lambda_AST_Node() {} virtual ~Lambda_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
std::vector<std::string> t_param_names; std::vector<std::string> t_param_names;
@@ -702,8 +702,8 @@ namespace chaiscript
struct Block_AST_Node : public AST_Node { struct Block_AST_Node : public AST_Node {
public: public:
Block_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Block, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Block_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Block, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Block_AST_Node() {} virtual ~Block_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
const size_t num_children = this->children.size(); const size_t num_children = this->children.size();
@@ -731,8 +731,8 @@ namespace chaiscript
struct Def_AST_Node : public AST_Node { struct Def_AST_Node : public AST_Node {
public: public:
Def_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Def, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Def_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Def, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Def_AST_Node() {} virtual ~Def_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
std::vector<std::string> t_param_names; std::vector<std::string> t_param_names;
@@ -787,35 +787,28 @@ namespace chaiscript
struct While_AST_Node : public AST_Node { struct While_AST_Node : public AST_Node {
public: public:
While_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::While, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : While_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::While, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~While_AST_Node() {} virtual ~While_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
bool cond;
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
try { try {
cond = boxed_cast<bool>(this->children[0]->eval(t_ss)); while (boxed_cast<bool>(this->children[0]->eval(t_ss))) {
}
catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("While condition not boolean");
}
while (cond) {
try {
this->children[1]->eval(t_ss);
try { try {
cond = boxed_cast<bool>(this->children[0]->eval(t_ss)); this->children[1]->eval(t_ss);
} catch (detail::Continue_Loop &) {
// we got a continue exception, which means all of the remaining
// loop implementation is skipped and we just need to continue to
// the next condition test
} }
catch (const exception::bad_boxed_cast &) { }
throw exception::eval_error("While condition not boolean"); } catch (const exception::bad_boxed_cast &) {
} throw exception::eval_error("While condition not boolean");
} } catch (detail::Break_Loop &) {
catch (detail::Break_Loop &) { // loop was broken intentionally
cond = false;
}
} }
return Boxed_Value(); return Boxed_Value();
} }
@@ -823,8 +816,8 @@ namespace chaiscript
struct Ternary_Cond_AST_Node : public AST_Node { struct Ternary_Cond_AST_Node : public AST_Node {
public: public:
Ternary_Cond_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::If, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Ternary_Cond_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::If, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Ternary_Cond_AST_Node() {} virtual ~Ternary_Cond_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
bool cond; bool cond;
@@ -832,7 +825,7 @@ namespace chaiscript
cond = boxed_cast<bool>(this->children[0]->eval(t_ss)); cond = boxed_cast<bool>(this->children[0]->eval(t_ss));
} }
catch (const exception::bad_boxed_cast &) { catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("If condition not boolean"); throw exception::eval_error("Ternary if condition not boolean");
} }
if (cond) { if (cond) {
@@ -846,8 +839,8 @@ namespace chaiscript
struct If_AST_Node : public AST_Node { struct If_AST_Node : public AST_Node {
public: public:
If_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::If, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : If_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::If, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~If_AST_Node() {} virtual ~If_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
bool cond; bool cond;
@@ -891,49 +884,38 @@ namespace chaiscript
struct For_AST_Node : public AST_Node { struct For_AST_Node : public AST_Node {
public: public:
For_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::For, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : For_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::For, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~For_AST_Node() {} virtual ~For_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
bool cond;
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
try { // initial expression
if (this->children.size() == 4) { this->children[0]->eval(t_ss);
this->children[0]->eval(t_ss);
cond = boxed_cast<bool>(this->children[1]->eval(t_ss)); try {
} else { // while condition evals to true
cond = boxed_cast<bool>(this->children[0]->eval(t_ss)); while (boxed_cast<bool>(this->children[1]->eval(t_ss))) {
try {
// Body of Loop
this->children[3]->eval(t_ss);
} catch (detail::Continue_Loop &) {
// we got a continue exception, which means all of the remaining
// loop implementation is skipped and we just need to continue to
// the next iteration step
}
// loop expression
this->children[2]->eval(t_ss);
} }
} }
catch (const exception::bad_boxed_cast &) { catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("For condition not boolean"); throw exception::eval_error("For condition not boolean");
} }
while (cond) { catch (detail::Break_Loop &) {
try { // loop broken
if (this->children.size() == 4) {
this->children[3]->eval(t_ss);
this->children[2]->eval(t_ss);
cond = boxed_cast<bool>(this->children[1]->eval(t_ss));
}
else {
this->children[2]->eval(t_ss);
this->children[1]->eval(t_ss);
cond = boxed_cast<bool>(this->children[0]->eval(t_ss));
}
}
catch (const exception::bad_boxed_cast &) {
throw exception::eval_error("For condition not boolean");
}
catch (detail::Break_Loop &) {
cond = false;
}
} }
return Boxed_Value(); return Boxed_Value();
} }
@@ -941,8 +923,8 @@ namespace chaiscript
struct Switch_AST_Node : public AST_Node { struct Switch_AST_Node : public AST_Node {
public: public:
Switch_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Switch, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Switch_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Switch, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Switch_AST_Node() {} virtual ~Switch_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
Boxed_Value match_value; Boxed_Value match_value;
@@ -984,8 +966,8 @@ namespace chaiscript
struct Case_AST_Node : public AST_Node { struct Case_AST_Node : public AST_Node {
public: public:
Case_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Case, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Case_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Case, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Case_AST_Node() {} virtual ~Case_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
@@ -998,8 +980,8 @@ namespace chaiscript
struct Default_AST_Node : public AST_Node { struct Default_AST_Node : public AST_Node {
public: public:
Default_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Default, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Default_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Default, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Default_AST_Node() {} virtual ~Default_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss); chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
@@ -1012,8 +994,8 @@ namespace chaiscript
struct Inline_Array_AST_Node : public AST_Node { struct Inline_Array_AST_Node : public AST_Node {
public: public:
Inline_Array_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Array, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Inline_Array_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Inline_Array, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Inline_Array_AST_Node() {} virtual ~Inline_Array_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
try { try {
@@ -1032,19 +1014,23 @@ namespace chaiscript
} }
virtual std::string pretty_print() const
{
return "[" + AST_Node::pretty_print() + "]";
}
}; };
struct Inline_Map_AST_Node : public AST_Node { struct Inline_Map_AST_Node : public AST_Node {
public: public:
Inline_Map_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Map, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Inline_Map_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Inline_Map, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Inline_Map_AST_Node() {} virtual ~Inline_Map_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
try { try {
std::map<std::string, Boxed_Value> retval; std::map<std::string, Boxed_Value> retval;
for (size_t i = 0; i < this->children[0]->children.size(); ++i) { for (size_t i = 0; i < this->children[0]->children.size(); ++i) {
Boxed_Value bv = t_ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(t_ss)); Boxed_Value bv = t_ss.call_function("clone", this->children[0]->children[i]->children[1]->eval(t_ss));
retval[boxed_cast<std::string>(this->children[0]->children[i]->children[0]->eval(t_ss))] retval[t_ss.boxed_cast<std::string>(this->children[0]->children[i]->children[0]->eval(t_ss))]
= bv; = bv;
} }
return const_var(retval); return const_var(retval);
@@ -1058,8 +1044,8 @@ namespace chaiscript
struct Return_AST_Node : public AST_Node { struct Return_AST_Node : public AST_Node {
public: public:
Return_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Return, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Return_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Return, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Return_AST_Node() {} virtual ~Return_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
if (this->children.size() > 0) { if (this->children.size() > 0) {
@@ -1074,8 +1060,8 @@ namespace chaiscript
struct File_AST_Node : public AST_Node { struct File_AST_Node : public AST_Node {
public: public:
File_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::File, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : File_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::File, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~File_AST_Node() {} virtual ~File_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) { virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) {
const size_t size = this->children.size(); const size_t size = this->children.size();
@@ -1091,8 +1077,8 @@ namespace chaiscript
struct Prefix_AST_Node : public AST_Node { struct Prefix_AST_Node : public AST_Node {
public: public:
Prefix_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Prefix, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Prefix_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) AST_Node(t_ast_node_text, AST_Node_Type::Prefix, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
{ } { }
virtual ~Prefix_AST_Node() {} virtual ~Prefix_AST_Node() {}
@@ -1102,6 +1088,7 @@ namespace chaiscript
Operators::Opers oper = Operators::to_operator(children[0]->text, true); Operators::Opers oper = Operators::to_operator(children[0]->text, true);
try { try {
// short circuit arithmetic operations
if (bv.get_type_info().is_arithmetic() && oper != Operators::invalid) if (bv.get_type_info().is_arithmetic() && oper != Operators::invalid)
{ {
return Boxed_Number::do_oper(oper, bv); return Boxed_Number::do_oper(oper, bv);
@@ -1121,32 +1108,59 @@ namespace chaiscript
struct Break_AST_Node : public AST_Node { struct Break_AST_Node : public AST_Node {
public: public:
Break_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Break, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Break_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Break, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Break_AST_Node() {} virtual ~Break_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){
throw detail::Break_Loop(); throw detail::Break_Loop();
} }
}; };
struct Continue_AST_Node : public AST_Node {
public:
Continue_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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, AST_Node_Type::Continue, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Continue_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){
throw detail::Continue_Loop();
}
};
struct Noop_AST_Node : public AST_Node {
public:
Noop_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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, AST_Node_Type::Noop, t_fname, t_start_line, t_start_col, t_end_line, t_end_col),
m_value(const_var(true))
{ }
virtual ~Noop_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){
// It's a no-op, that evaluates to "true"
return m_value;
}
private:
Boxed_Value m_value;
};
struct Map_Pair_AST_Node : public AST_Node { struct Map_Pair_AST_Node : public AST_Node {
public: public:
Map_Pair_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Map_Pair, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Map_Pair_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Map_Pair, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Map_Pair_AST_Node() {} virtual ~Map_Pair_AST_Node() {}
}; };
struct Value_Range_AST_Node : public AST_Node { struct Value_Range_AST_Node : public AST_Node {
public: public:
Value_Range_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Value_Range, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Value_Range_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Value_Range, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Value_Range_AST_Node() {} virtual ~Value_Range_AST_Node() {}
}; };
struct Inline_Range_AST_Node : public AST_Node { struct Inline_Range_AST_Node : public AST_Node {
public: public:
Inline_Range_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Inline_Range, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Inline_Range_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Inline_Range, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Inline_Range_AST_Node() {} virtual ~Inline_Range_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
try { try {
@@ -1163,15 +1177,15 @@ namespace chaiscript
struct Annotation_AST_Node : public AST_Node { struct Annotation_AST_Node : public AST_Node {
public: public:
Annotation_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Annotation, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Annotation_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Annotation, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Annotation_AST_Node() {} virtual ~Annotation_AST_Node() {}
}; };
struct Try_AST_Node : public AST_Node { struct Try_AST_Node : public AST_Node {
public: public:
Try_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Try, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Try_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Try, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Try_AST_Node() {} virtual ~Try_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
Boxed_Value retval; Boxed_Value retval;
@@ -1299,22 +1313,22 @@ namespace chaiscript
struct Catch_AST_Node : public AST_Node { struct Catch_AST_Node : public AST_Node {
public: public:
Catch_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Catch, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Catch_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Catch, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Catch_AST_Node() {} virtual ~Catch_AST_Node() {}
}; };
struct Finally_AST_Node : public AST_Node { struct Finally_AST_Node : public AST_Node {
public: public:
Finally_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Finally, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Finally_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Finally, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Finally_AST_Node() {} virtual ~Finally_AST_Node() {}
}; };
struct Method_AST_Node : public AST_Node { struct Method_AST_Node : public AST_Node {
public: public:
Method_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Method, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Method_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Method, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Method_AST_Node() {} virtual ~Method_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
@@ -1392,8 +1406,8 @@ namespace chaiscript
struct Attr_Decl_AST_Node : public AST_Node { struct Attr_Decl_AST_Node : public AST_Node {
public: public:
Attr_Decl_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Attr_Decl, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Attr_Decl_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Attr_Decl, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Attr_Decl_AST_Node() {} virtual ~Attr_Decl_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
try { try {
@@ -1421,43 +1435,43 @@ namespace chaiscript
struct Shift_AST_Node : public Binary_Operator_AST_Node { struct Shift_AST_Node : public Binary_Operator_AST_Node {
public: public:
Shift_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Shift, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Shift_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Shift, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Shift_AST_Node() {} virtual ~Shift_AST_Node() {}
}; };
struct Equality_AST_Node : public Binary_Operator_AST_Node { struct Equality_AST_Node : public Binary_Operator_AST_Node {
public: public:
Equality_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Equality, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Equality_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Equality, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Equality_AST_Node() {} virtual ~Equality_AST_Node() {}
}; };
struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node { struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node {
public: public:
Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_And, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_And, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Bitwise_And_AST_Node() {} virtual ~Bitwise_And_AST_Node() {}
}; };
struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node { struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node {
public: public:
Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Xor, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_Xor, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Bitwise_Xor_AST_Node() {} virtual ~Bitwise_Xor_AST_Node() {}
}; };
struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node { struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node {
public: public:
Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Bitwise_Or, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_Or, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Bitwise_Or_AST_Node() {} virtual ~Bitwise_Or_AST_Node() {}
}; };
struct Logical_And_AST_Node : public AST_Node { struct Logical_And_AST_Node : public AST_Node {
public: public:
Logical_And_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Logical_And, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Logical_And_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Logical_And, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Logical_And_AST_Node() {} virtual ~Logical_And_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
Boxed_Value retval = this->children[0]->eval(t_ss); Boxed_Value retval = this->children[0]->eval(t_ss);
@@ -1490,8 +1504,8 @@ namespace chaiscript
struct Logical_Or_AST_Node : public AST_Node { struct Logical_Or_AST_Node : public AST_Node {
public: public:
Logical_Or_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Logical_Or, const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : Logical_Or_AST_Node(const std::string &t_ast_node_text = "", const boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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) { } AST_Node(t_ast_node_text, AST_Node_Type::Logical_Or, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
virtual ~Logical_Or_AST_Node() {} virtual ~Logical_Or_AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){ virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss){
Boxed_Value retval; Boxed_Value retval;

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_PARSER_HPP_ #ifndef CHAISCRIPT_PARSER_HPP_
@@ -635,7 +635,7 @@ namespace chaiscript
if (Hex_()) { if (Hex_()) {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
Boxed_Value i = buildInt(std::hex, match); Boxed_Value i = buildInt(std::hex, match);
AST_NodePtr t(new eval::Int_AST_Node(match, i, AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); AST_NodePtr t(new eval::Int_AST_Node(match, i, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -660,14 +660,14 @@ namespace chaiscript
i = Boxed_Value(const_var(temp_int)); i = Boxed_Value(const_var(temp_int));
} }
AST_NodePtr t(new eval::Int_AST_Node(match, i, AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); AST_NodePtr t(new eval::Int_AST_Node(match, i, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
if (Float_()) { if (Float_()) {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
Boxed_Value f = buildFloat(match); Boxed_Value f = buildFloat(match);
AST_NodePtr t(new eval::Float_AST_Node(match, f, AST_Node_Type::Float, m_filename, prev_line, prev_col, m_line, m_col)); AST_NodePtr t(new eval::Float_AST_Node(match, f, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -676,12 +676,12 @@ namespace chaiscript
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
if ((match.size() > 0) && (match[0] == '0')) { if ((match.size() > 0) && (match[0] == '0')) {
Boxed_Value i = buildInt(std::oct, match); Boxed_Value i = buildInt(std::oct, match);
AST_NodePtr t(new eval::Int_AST_Node(match, i, AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); AST_NodePtr t(new eval::Int_AST_Node(match, i, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
} }
else { else {
Boxed_Value i = buildInt(std::dec, match); Boxed_Value i = buildInt(std::dec, match);
AST_NodePtr t(new eval::Int_AST_Node(match, i, AST_Node_Type::Int, m_filename, prev_line, prev_col, m_line, m_col)); AST_NodePtr t(new eval::Int_AST_Node(match, i, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
} }
return true; return true;
@@ -751,13 +751,13 @@ namespace chaiscript
if (*start == '`') { if (*start == '`') {
//Id Literal //Id Literal
std::string match(start+1, m_input_pos-1); std::string match(start+1, m_input_pos-1);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
else { else {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -790,7 +790,7 @@ namespace chaiscript
} while (Symbol("#")); } while (Symbol("#"));
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -865,13 +865,13 @@ namespace chaiscript
if (is_interpolated) { if (is_interpolated) {
//If we've seen previous interpolation, add on instead of making a new one //If we've seen previous interpolation, add on instead of making a new one
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)); AST_NodePtr t(new eval::Quoted_String_AST_Node(match, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
build_match(AST_NodePtr(new eval::Addition_AST_Node()), prev_stack_top); build_match(AST_NodePtr(new eval::Addition_AST_Node()), prev_stack_top);
} }
else { else {
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)); AST_NodePtr t(new eval::Quoted_String_AST_Node(match, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
} }
@@ -891,17 +891,17 @@ namespace chaiscript
size_t tostr_stack_top = m_match_stack.size(); size_t tostr_stack_top = m_match_stack.size();
AST_NodePtr tostr(new eval::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", m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(tostr); m_match_stack.push_back(tostr);
size_t ev_stack_top = m_match_stack.size(); size_t ev_stack_top = m_match_stack.size();
AST_NodePtr ev(new eval::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", m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(ev); m_match_stack.push_back(ev);
size_t arg_stack_top = m_match_stack.size(); size_t arg_stack_top = m_match_stack.size();
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)); AST_NodePtr t(new eval::Quoted_String_AST_Node(eval_match, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), arg_stack_top); build_match(AST_NodePtr(new eval::Arg_List_AST_Node()), arg_stack_top);
@@ -959,13 +959,13 @@ namespace chaiscript
} }
} }
if (is_interpolated) { if (is_interpolated) {
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)); AST_NodePtr t(new eval::Quoted_String_AST_Node(match, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
build_match(AST_NodePtr(new eval::Addition_AST_Node()), prev_stack_top); build_match(AST_NodePtr(new eval::Addition_AST_Node()), prev_stack_top);
} }
else { else {
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)); AST_NodePtr t(new eval::Quoted_String_AST_Node(match, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
} }
return true; return true;
@@ -1057,7 +1057,7 @@ namespace chaiscript
is_escaped = false; is_escaped = false;
} }
} }
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)); AST_NodePtr t(new eval::Single_Quoted_String_AST_Node(match, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -1096,7 +1096,7 @@ namespace chaiscript
int prev_line = m_line; int prev_line = m_line;
if (Char_(t_c)) { if (Char_(t_c)) {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -1148,7 +1148,7 @@ namespace chaiscript
if ( t_capture && retval ) { if ( t_capture && retval ) {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
} }
return retval; return retval;
@@ -1196,7 +1196,7 @@ namespace chaiscript
if ( t_capture && retval ) { if ( t_capture && retval ) {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
} }
@@ -1236,7 +1236,7 @@ namespace chaiscript
int prev_line = m_line; int prev_line = m_line;
if (Eol_()) { if (Eol_()) {
std::string match(start, m_input_pos); std::string match(start, m_input_pos);
AST_NodePtr t(new eval::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, m_filename, prev_line, prev_col, m_line, m_col));
m_match_stack.push_back(t); m_match_stack.push_back(t);
return true; return true;
} }
@@ -1506,7 +1506,7 @@ namespace chaiscript
if (Keyword("else", true)) { if (Keyword("else", true)) {
if (Keyword("if")) { if (Keyword("if")) {
AST_NodePtr back(m_match_stack.back()); AST_NodePtr back(m_match_stack.back());
m_match_stack.back() = AST_NodePtr(new eval::If_AST_Node("else if", back->identifier)); m_match_stack.back() = AST_NodePtr(new eval::If_AST_Node("else if"));
m_match_stack.back()->start = back->start; m_match_stack.back()->start = back->start;
m_match_stack.back()->end = back->end; m_match_stack.back()->end = back->end;
m_match_stack.back()->children = back->children; m_match_stack.back()->children = back->children;
@@ -1574,18 +1574,40 @@ namespace chaiscript
return retval; return retval;
} }
/** /**
* Reads the C-style for conditions from input * Reads the C-style for conditions from input
*/ */
bool For_Guards() { bool For_Guards() {
Equation(); if (!(Equation() && Eol()))
{
if (!Eol())
{
throw exception::eval_error("'for' loop initial statment missing", File_Position(m_line, m_col), *m_filename);
} else {
AST_NodePtr t(new eval::Noop_AST_Node());
m_match_stack.push_back(t);
}
}
if (Char(';') && Operator() && Char(';') && Equation()) { if (!(Equation() && Eol()))
return true; {
if (!Eol())
{
throw exception::eval_error("'for' loop condition missing", File_Position(m_line, m_col), *m_filename);
} else {
AST_NodePtr t(new eval::Noop_AST_Node());
m_match_stack.push_back(t);
}
} }
else {
throw exception::eval_error("Incomplete conditions in 'for' loop", File_Position(m_line, m_col), *m_filename); if (!Equation())
{
AST_NodePtr t(new eval::Noop_AST_Node());
m_match_stack.push_back(t);
} }
return true;
} }
/** /**
@@ -1760,6 +1782,23 @@ namespace chaiscript
return retval; return retval;
} }
/**
* Reads a continue statement from input
*/
bool Continue() {
bool retval = false;
size_t prev_stack_top = m_match_stack.size();
if (Keyword("continue")) {
retval = true;
build_match(AST_NodePtr(new eval::Continue_AST_Node()), prev_stack_top);
}
return retval;
}
/** /**
* Reads a dot expression(member access), then proceeds to check if it's a function or array call * Reads a dot expression(member access), then proceeds to check if it's a function or array call
*/ */
@@ -2257,6 +2296,14 @@ namespace chaiscript
retval = true; retval = true;
saw_eol = false; saw_eol = false;
} }
else if (Continue()) {
if (!saw_eol) {
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
}
has_more = true;
retval = true;
saw_eol = false;
}
else if (Equation()) { else if (Equation()) {
if (!saw_eol) { if (!saw_eol) {
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_PRELUDE_HPP_ #ifndef CHAISCRIPT_PRELUDE_HPP_

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#ifndef CHAISCRIPT_UTILITY_UTILITY_HPP_ #ifndef CHAISCRIPT_UTILITY_UTILITY_HPP_

View File

@@ -1,4 +1,7 @@
Copyright 2009-2012 Jason Turner and Jonathan Turner. All Rights Reserved. Copyright 2009-2014 Jason Turner
Copyright 2009-2012 Jonathan Turner.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are modification, are permitted provided that the following conditions are
met: met:

View File

@@ -2,7 +2,8 @@ ChaiScript
http://www.chaiscript.com http://www.chaiscript.com
(c) 2009-2012 Jason Turner and Jonathan Turner (c) 2009-2012 Jonathan Turner
(c) 2009-2014 Jason Turner
Release under the BSD license, see "license.txt" for details. Release under the BSD license, see "license.txt" for details.

View File

@@ -1,4 +1,29 @@
Changes since 4.1.0 Current Version: 4.3.0
Note: this is scheduled to be the last release that requires boost, new releases after this will require a C++11 compiler.
### Changes since 4.2.0
* Enhanced unit tests
* Add `continue` statement, fix various use cases for `for` loops
* Fix use of suffixed numbers in vector initialization
* Code cleanups
* Eliminate global data, which makes code more portable and thread safe
* Fix issue #79
* Merge pretty_print fixes from @mgee #82
* Compiler warning fixes for latest compiler releases
* Fix threading problems
* Fix linking error on MacOS Mavericks #88
* Allow non-const globals
* Make sure user cannot name a variable with `::` in it #91
* Fix various string / map / vector `size` and `count` calls for compilers which have weird overloads for them. #90 #93 #95
* Make module search path relative to the currently running executable
* Build and work with wstring windows builds
### Changes since 4.1.1
* Add support for automatic conversion of arithmetic types when possible
and when no ambiguous method dispatch exists.
### Changes since 4.1.0
* Fix missed gcc build error in 4.1.0 * Fix missed gcc build error in 4.1.0
Changes since 4.0.0 Changes since 4.0.0
@@ -25,7 +50,7 @@ Changes since 3.1.0
* Increased unit tests to 161 * Increased unit tests to 161
* Performance enhancements * Performance enhancements
Changes since 3.0.0 ### Changes since 3.0.0
* Numeric operations performance increased approximately 10x * Numeric operations performance increased approximately 10x
* Looping operations performance increased up to 2x * Looping operations performance increased up to 2x
* Engine start up time decreased * Engine start up time decreased
@@ -34,8 +59,7 @@ Changes since 3.0.0
uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t
* Enhanced support for capturing of exceptions thrown from ChaiScript in C++ * Enhanced support for capturing of exceptions thrown from ChaiScript in C++
Changes since 2.3.3 ### Changes since 2.3.3
* Code simplifications * Code simplifications
* Fully integrate documentation with source code in doxygen style comments * Fully integrate documentation with source code in doxygen style comments
* Unit tests increased from 114 to 137 * Unit tests increased from 114 to 137

View File

@@ -9,7 +9,18 @@
#include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript.hpp>
#include <chaiscript/dispatchkit/function_call.hpp> #include <chaiscript/dispatchkit/function_call.hpp>
#include <boost/function.hpp> #include <boost/function.hpp>
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
#include <boost/regex.hpp> #include <boost/regex.hpp>
void log(const std::string &msg) void log(const std::string &msg)
@@ -43,9 +54,9 @@ struct System
std::map<std::string, boost::function<std::string (const std::string &) > > m_callbacks; std::map<std::string, boost::function<std::string (const std::string &) > > m_callbacks;
void add_callback(const std::string &t_name, void add_callback(const std::string &t_name,
const chaiscript::Proxy_Function &t_func) const boost::function<std::string (const std::string &)> &t_func)
{ {
m_callbacks[t_name] = chaiscript::dispatch::functor<std::string (const std::string &)>(t_func); m_callbacks[t_name] = t_func;
} }

View File

@@ -5,7 +5,7 @@ def for_each(container, function)
while (!range.empty()) while (!range.empty())
{ {
function(range.front()); function(range.front());
range.popFront(); range.pop_front();
} }
} }

View File

@@ -1,7 +1,7 @@
// This file is distributed under the BSD License. // This file is distributed under the BSD License.
// See "license.txt" for details. // See "license.txt" for details.
// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com) // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com) // Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com // http://www.chaiscript.com
#include <iostream> #include <iostream>
@@ -30,6 +30,86 @@ void add_history(const char*){}
void using_history(){} void using_history(){}
#endif #endif
void *cast_module_symbol(std::string (*t_path)())
{
union cast_union
{
std::string (*in_ptr)();
void *out_ptr;
};
cast_union c;
c.in_ptr = t_path;
return c.out_ptr;
}
std::string default_search_path()
{
#ifdef BOOST_WINDOWS // force no unicode
CHAR path[4096];
int size = GetModuleFileNameA(0, path, sizeof(path)-1);
std::string exepath(path, size);
size_t secondtolastslash = exepath.rfind('\\', exepath.rfind('\\') - 1);
if (secondtolastslash != std::string::npos)
{
return exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\";
} else {
return "";
}
#else
std::string exepath;
std::vector<char> buf(2048);
ssize_t size = -1;
if ((size = readlink("/proc/self/exe", &buf.front(), buf.size())) != -1)
{
exepath = std::string(&buf.front(), size);
}
if (exepath.empty())
{
if ((size = readlink("/proc/curproc/file", &buf.front(), buf.size())) != -1)
{
exepath = std::string(&buf.front(), size);
}
}
if (exepath.empty())
{
if ((size = readlink("/proc/self/path/a.out", &buf.front(), buf.size())) != -1)
{
exepath = std::string(&buf.front(), size);
}
}
if (exepath.empty())
{
Dl_info rInfo;
memset( &rInfo, 0, sizeof(rInfo) );
if ( !dladdr(cast_module_symbol(&default_search_path), &rInfo) || !rInfo.dli_fname ) {
return "";
}
exepath = std::string(rInfo.dli_fname);
}
size_t secondtolastslash = exepath.rfind('/', exepath.rfind('/') - 1);
if (secondtolastslash != std::string::npos)
{
return exepath.substr(0, secondtolastslash) + "/lib/chaiscript/";
} else {
return "";
}
#endif
}
void help(int n) { void help(int n) {
if ( n >= 0 ) { if ( n >= 0 ) {
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl; std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl;
@@ -167,6 +247,8 @@ int main(int argc, char *argv[])
usepaths.push_back(usepath); usepaths.push_back(usepath);
} }
std::string searchpath = default_search_path();
modulepaths.push_back(searchpath);
modulepaths.push_back(""); modulepaths.push_back("");
if (modulepath) if (modulepath)
{ {

View File

@@ -1,3 +1,9 @@
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
#include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript.hpp>
#include <chaiscript/utility/utility.hpp> #include <chaiscript/utility/utility.hpp>
@@ -18,7 +24,12 @@ bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf)
= boost::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf); = boost::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf);
if (pf) if (pf)
{ {
return pf->get_parse_tree(); if (pf->get_parse_tree())
{
return true;
} else {
return false;
}
} else { } else {
return false; return false;
} }
@@ -42,6 +53,12 @@ chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t
} }
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
#endif
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_reflection() CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_reflection()
{ {
chaiscript::ModulePtr m(new chaiscript::Module()); chaiscript::ModulePtr m(new chaiscript::Module());
@@ -90,6 +107,11 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_reflect
return m; return m;
} }
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning(pop) #pragma warning(pop)

View File

@@ -1,3 +1,8 @@
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
#include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript.hpp>
#include <list> #include <list>
@@ -10,11 +15,19 @@
#pragma warning(disable : 4190) #pragma warning(disable : 4190)
#endif #endif
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
#endif
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra() CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra()
{ {
return chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List"); return chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List");
} }
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning(pop) #pragma warning(pop)

View File

@@ -5,12 +5,19 @@
class TestBaseType class TestBaseType
{ {
public: public:
TestBaseType() {} TestBaseType() : val(10), const_val(15) { }
TestBaseType(int) {} TestBaseType(int) : val(10), const_val(15) {}
TestBaseType(int *) {} TestBaseType(int *) : val(10), const_val(15) {}
virtual ~TestBaseType() {} virtual ~TestBaseType() {}
virtual int func() { return 0; } virtual int func() { return 0; }
const TestBaseType &constMe() const { return *this; }
int val;
const int const_val;
private:
TestBaseType &operator=(const TestBaseType &);
}; };
enum TestEnum enum TestEnum
@@ -28,6 +35,9 @@ class TestDerivedType : public TestBaseType
public: public:
virtual ~TestDerivedType() {} virtual ~TestDerivedType() {}
virtual int func() { return 1; } virtual int func() { return 1; }
private:
TestDerivedType &operator=(const TestDerivedType &);
}; };
std::string hello_world() std::string hello_world()
@@ -47,6 +57,10 @@ int *get_new_int()
#pragma warning(disable : 4190) #pragma warning(disable : 4190)
#endif #endif
#ifdef __llvm__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
#endif
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_module() CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_module()
{ {
@@ -68,6 +82,8 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
m->add(chaiscript::base_class<TestBaseType, TestDerivedType>()); m->add(chaiscript::base_class<TestBaseType, TestDerivedType>());
m->add(chaiscript::fun(&TestBaseType::func), "func"); m->add(chaiscript::fun(&TestBaseType::func), "func");
m->add(chaiscript::fun(&TestBaseType::val), "val");
m->add(chaiscript::fun(&TestBaseType::const_val), "const_val");
m->add(chaiscript::fun(&get_new_int), "get_new_int"); m->add(chaiscript::fun(&get_new_int), "get_new_int");
@@ -76,12 +92,17 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
m->add(chaiscript::user_type<TestEnum>(), "TestEnum"); m->add(chaiscript::user_type<TestEnum>(), "TestEnum");
m->add(chaiscript::fun(&to_int), "to_int"); m->add(chaiscript::fun(&to_int), "to_int");
m->add(chaiscript::fun(&TestBaseType::constMe), "constMe");
return m; return m;
} }
#ifdef __llvm__
#pragma clang diagnostic pop
#endif
#ifdef BOOST_MSVC #ifdef BOOST_MSVC
#pragma warning(pop) #pragma warning(pop)
#endif #endif

View File

@@ -0,0 +1,58 @@
// Tests to make sure that type conversions happen only when they should
#include <chaiscript/chaiscript.hpp>
void f1(int)
{
}
void f4(std::string)
{
}
void f2(int)
{
}
void f3(double)
{
}
int main()
{
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&f1), "f1");
chai.add(chaiscript::fun(&f2), "f2");
chai.add(chaiscript::fun(&f3), "f2");
chai.add(chaiscript::fun(&f1), "f3");
chai.add(chaiscript::fun(&f4), "f3");
// no overloads
chai.eval("f1(0)");
chai.eval("f1(0l)");
chai.eval("f1(0ul)");
chai.eval("f1(0ll)");
chai.eval("f1(0ull)");
chai.eval("f1(0.0)");
chai.eval("f1(0.0f)");
chai.eval("f1(0.0l)");
// expected overloads
chai.eval("f2(1)");
chai.eval("f2(1.0)");
// 1 non-arithmetic overload
chai.eval("f2(1.0)");
// this is the one call we expect to fail
try {
chai.eval("f2(1.0l)");
} catch (const std::exception &) {
return EXIT_SUCCESS;
}
// if the last one did not throw, we failed
return EXIT_FAILURE;
}

11
unittests/break_for.chai Normal file
View File

@@ -0,0 +1,11 @@
var j = 0;
for (var i = 0; i < 10; ++i) {
if (i == 5) {
break
}
j = i
}
assert_equal(4, j);

View File

@@ -0,0 +1,16 @@
var j = 0;
var k = 0;
for (var i = 0; i < 10; ++i)
{
j = i
if (i > 5)
{
continue
}
k = i
}
assert_equal(5, k);
assert_equal(9, j);

View File

@@ -0,0 +1,14 @@
var i = 0
var j = 0
while (i < 10) {
if (++i > 5)
{
continue
}
j = i;
}
assert_equal(10, i);
assert_equal(5, j);

View File

@@ -9,7 +9,7 @@ int test_generic()
try { try {
chai.eval("throw(runtime_error(\"error\"));"); chai.eval("throw(runtime_error(\"error\"));");
} catch (const chaiscript::Boxed_Value &bv) { } catch (const chaiscript::Boxed_Value &bv) {
const std::exception &e = chaiscript::boxed_cast<const std::exception &>(bv); const std::exception &e = chai.boxed_cast<const std::exception &>(bv);
if (e.what() == std::string("error")) if (e.what() == std::string("error"))
{ {
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@@ -5,3 +5,57 @@ for (var i = 0; i < 5; ++i) {
} }
assert_equal([0,1,2,3,4], ret); assert_equal([0,1,2,3,4], ret);
var j = 0;
for (;j<10; ++j)
{
}
assert_equal(10, j);
var k = 0;
for (;k<10; )
{
++k;
}
assert_equal(10, k);
for (;;)
{
break;
}
var l = 0;
for (;;l = 1)
{
break;
}
assert_equal(0, l)
def isNotFive(x)
{
if (x != 5)
{
return true
} else {
return false
}
}
var m;
for (m = 0; isNotFive(m); m = m + 1)
{
}
assert_equal(5, m);

View File

@@ -6,3 +6,12 @@ var t = TestDerivedType();
assert_equal(t0.func(), 0); assert_equal(t0.func(), 0);
assert_equal(t.func(), 1); assert_equal(t.func(), 1);
assert_equal(10, t0.val);
assert_equal(15, t0.const_val);
assert_equal(10, t.val);
assert_equal(15, t.const_val);
t.val = 23;
assert_equal(23, t.val)

View File

@@ -1,5 +1,3 @@
// Tests to make sure that the order in which function dispatches occur is correct
#include <chaiscript/chaiscript.hpp> #include <chaiscript/chaiscript.hpp>
#define TEST_LITERAL(v) test_literal(v, #v) #define TEST_LITERAL(v) test_literal(v, #v)

View File

@@ -1,2 +1,4 @@
load_module("test_module") load_module("test_module")
assert_equal("Hello World", hello_world()); assert_equal("Hello World", hello_world());

5
unittests/map_count.chai Normal file
View File

@@ -0,0 +1,5 @@
assert_equal(1, ["a":1].count("a"))
assert_equal(0, ["A":1].count("a"))

View File

@@ -0,0 +1,11 @@
load_module("test_module")
var t0 = TestBaseType()
t0.val = 13
assert_equal(15, t0.const_val)
assert_equal(13, t0.val)
assert_equal(15, t0.constMe().const_val)
assert_equal(13, t0.constMe().val)

View File

@@ -1,9 +1,3 @@
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
#include <iostream> #include <iostream>
#include <list> #include <list>
@@ -25,13 +19,17 @@ int expected_value(int num_iters)
void do_work(chaiscript::ChaiScript &c, int id) void do_work(chaiscript::ChaiScript &c, int id)
{ {
std::stringstream ss; try{
ss << "MyVar" << rand(); std::stringstream ss;
c.add(chaiscript::var(5), ss.str()); ss << "MyVar" << rand();
ss.str(""); c.add(chaiscript::var(5), ss.str());
ss << id; ss.str("");
c.use("multithreaded_work.inc"); ss << id;
c("do_chai_work(4000, " + ss.str() + ");"); c.use("multithreaded_work.inc");
c("do_chai_work(4000, " + ss.str() + ");");
} catch (const std::exception &e) {
std::cout << "exception: " << e.what() << " thread: " << id;
}
} }
int main() int main()
@@ -65,20 +63,17 @@ int main()
chaiscript::ChaiScript chai(modulepaths,usepaths); chaiscript::ChaiScript chai(modulepaths,usepaths);
std::vector<boost::shared_ptr<boost::thread> > threads; boost::thread_group threads;
// Ensure at least two, but say only 7 on an 8 core processor // Ensure at least two, but say only 7 on an 8 core processor
int num_threads = std::max<unsigned int>(boost::thread::hardware_concurrency() - 1, 2u); int num_threads = std::max<unsigned int>(boost::thread::hardware_concurrency() - 1, 2u);
for (int i = 0; i < num_threads; ++i) for (int i = 0; i < num_threads; ++i)
{ {
threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(do_work, boost::ref(chai), i)))); threads.create_thread(boost::bind(&do_work, boost::ref(chai), i));
} }
for (int i = 0; i < num_threads; ++i) threads.join_all();
{
threads[i]->join();
}
for (int i = 0; i < num_threads; ++i) for (int i = 0; i < num_threads; ++i)

View File

@@ -0,0 +1,2 @@
assert_equal(true, "".empty())
assert_equal(3, "123".size())

View File

@@ -0,0 +1,3 @@
var x = [1, 2u, 3.0l]
assert_equal(3.0l, x[2])
assert_equal(2u, x[1])