Compare commits
69 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
343264944a | ||
![]() |
b436791272 | ||
![]() |
9b19aa3b6e | ||
![]() |
e86fc96b2f | ||
![]() |
66801349a8 | ||
![]() |
bde4eb04b6 | ||
![]() |
c3f343450d | ||
![]() |
8dc2c55acd | ||
![]() |
adfc56db8b | ||
![]() |
c7b2b3095a | ||
![]() |
9449fca22f | ||
![]() |
759d6fc42f | ||
![]() |
f95ca75aca | ||
![]() |
41a45ce8b5 | ||
![]() |
5b6e6042f3 | ||
![]() |
1552d36d7a | ||
![]() |
26bf531cab | ||
![]() |
7761ceb736 | ||
![]() |
497dd89046 | ||
![]() |
ef69e4a2f1 | ||
![]() |
3f23e57a3d | ||
![]() |
f66b4aafc1 | ||
![]() |
3d1edbf38f | ||
![]() |
c1f47cbc16 | ||
![]() |
4761a68d06 | ||
![]() |
31ef683ced | ||
![]() |
9b3bb493e9 | ||
![]() |
2f90b3ae6b | ||
![]() |
420ba68b94 | ||
![]() |
576816e3b1 | ||
![]() |
25b15a3449 | ||
![]() |
8746a9eea5 | ||
![]() |
0695eec3ca | ||
![]() |
1a4dec0df0 | ||
![]() |
de09489355 | ||
![]() |
440ceeebbb | ||
![]() |
12533ce3e1 | ||
![]() |
2e02273673 | ||
![]() |
d91294b989 | ||
![]() |
52d03a66b1 | ||
![]() |
a32a180a06 | ||
![]() |
e61612e416 | ||
![]() |
7478d57264 | ||
![]() |
bab3701c2f | ||
![]() |
e225654289 | ||
![]() |
019ea57cb6 | ||
![]() |
60fe242fb6 | ||
![]() |
6c10f18e4c | ||
![]() |
d5a221a468 | ||
![]() |
6ec3afc687 | ||
![]() |
049cd12127 | ||
![]() |
8b34066dd5 | ||
![]() |
9fb74762ad | ||
![]() |
7b7e7176f5 | ||
![]() |
81146d6b0f | ||
![]() |
423e872720 | ||
![]() |
1e8c0ab93e | ||
![]() |
c90fe16858 | ||
![]() |
161652b5d9 | ||
![]() |
cc5bf45b3b | ||
![]() |
28124e4b33 | ||
![]() |
db207b345b | ||
![]() |
684d724103 | ||
![]() |
283152a880 | ||
![]() |
dc3ef087e2 | ||
![]() |
032ba63b8a | ||
![]() |
cf49b1b30c | ||
![]() |
63a083b47b | ||
![]() |
b41c0f432b |
@@ -1,13 +1,25 @@
|
||||
compilers:
|
||||
- name: "clang"
|
||||
version: "3.5"
|
||||
skip_packaging: true
|
||||
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
||||
- name: "clang"
|
||||
build_tag: AddressSanitizer
|
||||
version: "3.5"
|
||||
skip_packaging: true
|
||||
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DENABLE_ADDRESS_SANITIZER:BOOL=ON
|
||||
- name: "clang"
|
||||
build_tag: ThreadSanitizer
|
||||
version: "3.5"
|
||||
skip_packaging: true
|
||||
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DENABLE_THREAD_SANITIZER:BOOL=ON
|
||||
- name: "gcc"
|
||||
version: "4.8"
|
||||
cmake_extra_flags: -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
||||
- name: "gcc"
|
||||
version: "4.6"
|
||||
skip_packaging: true
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
||||
- name: cppcheck
|
||||
compiler_extra_flags: --enable=all -I include --inline-suppr
|
||||
compiler_extra_flags: --enable=all -I include --inline-suppr -Umax --suppress="*:cmake*"
|
||||
|
||||
|
@@ -1,7 +1,9 @@
|
||||
compilers:
|
||||
- name: clang
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=$COMMIT_SHA
|
||||
build_package_generator: TBZ2
|
||||
- name: clang
|
||||
build_type: Debug
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=$COMMIT_SHA
|
||||
skip_packaging: true
|
||||
|
||||
|
@@ -2,19 +2,21 @@ compilers:
|
||||
- name: Visual Studio
|
||||
version: 14
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||
compiler_extra_flags: /ANALYZE
|
||||
compiler_extra_flags: /analyze
|
||||
skip_packaging: true
|
||||
- name: Visual Studio
|
||||
version: 14
|
||||
architecture: Win64
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||
compiler_extra_flags: /ANALYZE
|
||||
compiler_extra_flags: /analyze
|
||||
skip_packaging: true
|
||||
- name: Visual Studio
|
||||
version: 12
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||
compiler_extra_flags: /ANALYZE
|
||||
compiler_extra_flags: /analyze
|
||||
- name: Visual Studio
|
||||
version: 12
|
||||
architecture: Win64
|
||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||
compiler_extra_flags: /ANALYZE
|
||||
compiler_extra_flags: /analyze
|
||||
|
||||
|
@@ -81,7 +81,7 @@ 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_VERSION_MAJOR 5)
|
||||
set(CPACK_PACKAGE_VERSION_MINOR 5)
|
||||
set(CPACK_PACKAGE_VERSION_MINOR 6)
|
||||
set(CPACK_PACKAGE_VERSION_PATCH 0)
|
||||
|
||||
set(CPACK_PACKAGE_EXECUTABLES "chai;ChaiScript Eval")
|
||||
@@ -152,7 +152,13 @@ if(MSVC)
|
||||
# how to workaround or fix the error. So I'm disabling it globally.
|
||||
add_definitions(/wd4503)
|
||||
else()
|
||||
add_definitions(-Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic ${CPP11_FLAG})
|
||||
add_definitions(-Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Woverloaded-virtual -pedantic ${CPP11_FLAG})
|
||||
|
||||
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
add_definitions(-Weverything -Wno-c++98-compat -Wno-documentation -Wno-switch-enum -Wno-weak-vtables -Wno-sign-conversion -Wno-missing-prototypes -Wno-padded -Wno-missing-noreturn)
|
||||
else()
|
||||
add_definitions(-Wnoexcept)
|
||||
endif()
|
||||
|
||||
if(APPLE)
|
||||
add_definitions(-Wno-sign-compare)
|
||||
@@ -232,6 +238,8 @@ if(BUILD_SAMPLES)
|
||||
target_link_libraries(memory_leak_test ${LIBS})
|
||||
add_executable(inheritance samples/inheritance.cpp)
|
||||
target_link_libraries(inheritance ${LIBS})
|
||||
add_executable(fun_call_performance samples/fun_call_performance.cpp)
|
||||
target_link_libraries(fun_call_performance ${LIBS})
|
||||
endif()
|
||||
|
||||
|
||||
@@ -294,6 +302,10 @@ if(BUILD_TESTING)
|
||||
target_link_libraries(object_lifetime_test ${LIBS})
|
||||
add_test(NAME Object_Lifetime_Test COMMAND object_lifetime_test)
|
||||
|
||||
add_executable(object_lifetime_test2 unittests/object_lifetime_test2.cpp)
|
||||
target_link_libraries(object_lifetime_test2 ${LIBS})
|
||||
add_test(NAME Object_Lifetime_Test2 COMMAND object_lifetime_test2)
|
||||
|
||||
add_executable(function_ordering_test unittests/function_ordering_test.cpp)
|
||||
target_link_libraries(function_ordering_test ${LIBS})
|
||||
add_test(NAME Function_Ordering_Test COMMAND function_ordering_test)
|
||||
|
@@ -40,7 +40,7 @@ PROJECT_NUMBER = ${CHAI_VERSION}
|
||||
# for a project that appears at the top of each page and should give viewer
|
||||
# a quick idea about the purpose of the project. Keep the description short.
|
||||
|
||||
PROJECT_BRIEF = ${CMAKE_BINARY_DIR}/docs
|
||||
PROJECT_BRIEF = "An easy to use embedded scripting language for C++."
|
||||
|
||||
# With the PROJECT_LOGO tag one can specify an logo or icon that is
|
||||
# included in the documentation. The maximum height of the logo should not
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_DEFINES_HPP_
|
||||
@@ -17,7 +17,7 @@
|
||||
#endif
|
||||
|
||||
#if (defined(__GNUC__) && __GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
|
||||
/// Currently only g++>=4.8supports this natively
|
||||
/// Currently only g++>=4.8 supports this natively
|
||||
/// \todo Make this support other compilers when possible
|
||||
#define CHAISCRIPT_HAS_THREAD_LOCAL
|
||||
#endif
|
||||
@@ -45,7 +45,7 @@
|
||||
|
||||
namespace chaiscript {
|
||||
static const int version_major = 5;
|
||||
static const int version_minor = 5;
|
||||
static const int version_minor = 6;
|
||||
static const int version_patch = 0;
|
||||
}
|
||||
|
||||
|
@@ -19,6 +19,11 @@
|
||||
#include "dispatchkit/bootstrap_stl.hpp"
|
||||
#include "dispatchkit/boxed_value.hpp"
|
||||
|
||||
#ifndef CHAISCRIPT_NO_THREADS
|
||||
#include <future>
|
||||
#endif
|
||||
|
||||
|
||||
/// @file
|
||||
///
|
||||
/// This file generates the standard library that normal ChaiScript usage requires.
|
||||
@@ -40,6 +45,11 @@ namespace chaiscript
|
||||
lib->add(standard_library::map_type<std::map<std::string, Boxed_Value> >("Map"));
|
||||
lib->add(standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
|
||||
|
||||
#ifndef CHAISCRIPT_NO_THREADS
|
||||
lib->add(standard_library::future_type<std::future<chaiscript::Boxed_Value>>("future"));
|
||||
lib->add(chaiscript::fun<std::future<Boxed_Value> (const std::function<chaiscript::Boxed_Value ()> &)>([](const std::function<chaiscript::Boxed_Value ()> &t_func){ return std::async(std::launch::async, t_func);}), "async");
|
||||
#endif
|
||||
|
||||
return lib;
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_THREADING_HPP_
|
||||
@@ -82,16 +82,27 @@ namespace chaiscript
|
||||
t().erase(m_key);
|
||||
}
|
||||
|
||||
inline T *operator->() const
|
||||
inline const T *operator->() const
|
||||
{
|
||||
return &(t()[m_key]);
|
||||
}
|
||||
|
||||
inline T &operator*() const
|
||||
inline const T &operator*() const
|
||||
{
|
||||
return t()[m_key];
|
||||
}
|
||||
|
||||
inline T *operator->()
|
||||
{
|
||||
return &(t()[m_key]);
|
||||
}
|
||||
|
||||
inline T &operator*()
|
||||
{
|
||||
return t()[m_key];
|
||||
}
|
||||
|
||||
|
||||
void *m_key;
|
||||
|
||||
private:
|
||||
@@ -117,12 +128,22 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
inline T *operator->() const
|
||||
inline const T *operator->() const
|
||||
{
|
||||
return get_tls().get();
|
||||
}
|
||||
|
||||
inline T &operator*() const
|
||||
inline const T &operator*() const
|
||||
{
|
||||
return *get_tls();
|
||||
}
|
||||
|
||||
inline T *operator->()
|
||||
{
|
||||
return get_tls().get();
|
||||
}
|
||||
|
||||
inline T &operator*()
|
||||
{
|
||||
return *get_tls();
|
||||
}
|
||||
|
@@ -26,6 +26,8 @@ namespace chaiscript {
|
||||
{
|
||||
}
|
||||
|
||||
bad_any_cast(const bad_any_cast &) = default;
|
||||
|
||||
virtual ~bad_any_cast() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
/// \brief Description of what error occurred
|
||||
@@ -105,7 +107,7 @@ namespace chaiscript {
|
||||
}
|
||||
}
|
||||
|
||||
#if _MSC_VER != 1800
|
||||
#if !defined(_MSC_VER) || _MSC_VER != 1800
|
||||
Any(Any &&) = default;
|
||||
Any &operator=(Any &&t_any) = default;
|
||||
#endif
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BAD_BOXED_CAST_HPP_
|
||||
@@ -45,7 +45,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~bad_boxed_cast() CHAISCRIPT_NOEXCEPT {}
|
||||
bad_boxed_cast(const bad_boxed_cast &) = default;
|
||||
virtual ~bad_boxed_cast() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
/// \brief Description of what error occurred
|
||||
virtual const char * what() const CHAISCRIPT_NOEXCEPT CHAISCRIPT_OVERRIDE
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BIND_FIRST_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BOOTSTRAP_HPP_
|
||||
@@ -202,12 +202,12 @@ namespace chaiscript
|
||||
|
||||
static void print(const std::string &s)
|
||||
{
|
||||
std::cout << s;
|
||||
fwrite(s.c_str(), 1, s.size(), stdout);
|
||||
}
|
||||
|
||||
static void println(const std::string &s)
|
||||
{
|
||||
std::cout << s << std::endl;
|
||||
puts(s.c_str());
|
||||
}
|
||||
|
||||
|
||||
@@ -396,10 +396,9 @@ namespace chaiscript
|
||||
|
||||
m->add(user_type<std::runtime_error>(), "runtime_error");
|
||||
m->add(chaiscript::base_class<std::exception, std::runtime_error>());
|
||||
|
||||
|
||||
m->add(constructor<std::runtime_error (const std::string &)>(), "runtime_error");
|
||||
m->add(fun(std::function<std::string (const std::runtime_error &)>(&what)), "what");
|
||||
m->add(fun(std::function<std::string (const std::runtime_error &)>(&what)), "what");
|
||||
|
||||
m->add(user_type<dispatch::Dynamic_Object>(), "Dynamic_Object");
|
||||
m->add(constructor<dispatch::Dynamic_Object (const std::string &)>(), "Dynamic_Object");
|
||||
@@ -443,7 +442,7 @@ namespace chaiscript
|
||||
operators::assign<bool>(m);
|
||||
operators::equal<bool>(m);
|
||||
|
||||
m->add(fun(&to_string<const std::string &>), "to_string");
|
||||
m->add(fun<std::string (const std::string &t_ss)>([](const std::string &s) -> std::string { return s; }), "to_string");
|
||||
m->add(fun(&Bootstrap::bool_to_string), "to_string");
|
||||
m->add(fun(&unknown_assign), "=");
|
||||
m->add(fun(&throw_exception), "throw");
|
||||
@@ -487,7 +486,12 @@ namespace chaiscript
|
||||
m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree");
|
||||
m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree");
|
||||
|
||||
m->add(chaiscript::base_class<std::exception, chaiscript::exception::eval_error>());
|
||||
m->add(chaiscript::user_type<chaiscript::exception::eval_error>(), "eval_error");
|
||||
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>());
|
||||
|
||||
m->add(chaiscript::user_type<chaiscript::exception::arithmetic_error>(), "arithmetic_error");
|
||||
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>());
|
||||
|
||||
|
||||
// chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m);
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
/// \file
|
||||
@@ -246,6 +246,7 @@ namespace chaiscript
|
||||
template<typename ContainerType>
|
||||
ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
// cppcheck-suppress syntaxError
|
||||
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
|
||||
typedef typename ContainerType::const_reference(ContainerType::*constindexoper)(size_t) const;
|
||||
|
||||
@@ -564,6 +565,25 @@ namespace chaiscript
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Add a MapType container
|
||||
/// http://www.sgi.com/tech/stl/Map.html
|
||||
template<typename FutureType>
|
||||
ModulePtr future_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
m->add(user_type<FutureType>(), type);
|
||||
|
||||
m->add(fun<bool (const FutureType &)>([](const FutureType &t) { return t.valid(); }), "valid");
|
||||
m->add(fun(&FutureType::get), "get");
|
||||
m->add(fun(&FutureType::wait), "wait");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BOXED_CAST_HPP_
|
||||
@@ -89,13 +89,13 @@ namespace chaiscript
|
||||
if (t_conversions && t_conversions->convertable_type<Type>())
|
||||
{
|
||||
try {
|
||||
// std::cout << "trying an up conversion " << typeid(Type).name() << std::endl;
|
||||
// std::cout << "trying an up conversion " << typeid(Type).name() << '\n';
|
||||
// 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
|
||||
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_conversion<Type>(bv), t_conversions);
|
||||
} catch (...) {
|
||||
try {
|
||||
// std::cout << "trying a down conversion " << typeid(Type).name() << std::endl;
|
||||
// std::cout << "trying a down conversion " << typeid(Type).name() << '\n';
|
||||
// try going the other way - down the inheritance graph
|
||||
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_down_conversion<Type>(bv), t_conversions);
|
||||
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BOXED_CAST_HELPER_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BOXED_NUMERIC_HPP_
|
||||
@@ -22,6 +22,19 @@ namespace chaiscript {
|
||||
class Type_Conversions;
|
||||
} // namespace chaiscript
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
namespace exception
|
||||
{
|
||||
struct arithmetic_error : public std::runtime_error
|
||||
{
|
||||
arithmetic_error(const std::string& reason) : std::runtime_error("Arithmetic error: " + reason) {}
|
||||
arithmetic_error(const arithmetic_error &) = default;
|
||||
virtual ~arithmetic_error() CHAISCRIPT_NOEXCEPT {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
|
||||
@@ -33,16 +46,37 @@ namespace chaiscript
|
||||
#pragma warning(disable : 4244 4018 4389 4146 4365)
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsign-compare"
|
||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
||||
#pragma GCC diagnostic ignored "-Wconversion"
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
#endif
|
||||
|
||||
/// \brief Represents any numeric type, generically. Used internally for generic operations between POD values
|
||||
class Boxed_Number
|
||||
{
|
||||
private:
|
||||
template<typename T>
|
||||
static void check_divide_by_zero(T t, typename std::enable_if<std::is_integral<T>::value>::type* = 0)
|
||||
{
|
||||
#ifndef CHAISCRIPT_NO_PROTECT_DIVIDEBYZERO
|
||||
if (t == 0) {
|
||||
throw chaiscript::exception::arithmetic_error("divide by zero");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static void check_divide_by_zero(T, typename std::enable_if<std::is_floating_point<T>::value>::type* = 0)
|
||||
{
|
||||
}
|
||||
|
||||
struct boolean
|
||||
{
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic ignored "-Wsign-compare"
|
||||
#endif
|
||||
template<typename T, typename U>
|
||||
static Boxed_Value go(Operators::Opers t_oper, const T &t, const U &u, const Boxed_Value &)
|
||||
{
|
||||
@@ -61,7 +95,7 @@ namespace chaiscript
|
||||
case Operators::not_equal:
|
||||
return const_var(t != u);
|
||||
default:
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -89,13 +123,14 @@ namespace chaiscript
|
||||
t += u;
|
||||
break;
|
||||
case Operators::assign_quotient:
|
||||
check_divide_by_zero(u);
|
||||
t /= u;
|
||||
break;
|
||||
case Operators::assign_difference:
|
||||
t -= u;
|
||||
break;
|
||||
default:
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
}
|
||||
|
||||
return t_lhs;
|
||||
@@ -122,13 +157,14 @@ namespace chaiscript
|
||||
t >>= u;
|
||||
break;
|
||||
case Operators::assign_remainder:
|
||||
check_divide_by_zero(u);
|
||||
t %= u;
|
||||
break;
|
||||
case Operators::assign_bitwise_xor:
|
||||
t ^= u;
|
||||
break;
|
||||
default:
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
}
|
||||
return t_lhs;
|
||||
}
|
||||
@@ -146,6 +182,7 @@ namespace chaiscript
|
||||
case Operators::shift_right:
|
||||
return const_var(t >> u);
|
||||
case Operators::remainder:
|
||||
check_divide_by_zero(u);
|
||||
return const_var(t % u);
|
||||
case Operators::bitwise_and:
|
||||
return const_var(t & u);
|
||||
@@ -156,7 +193,7 @@ namespace chaiscript
|
||||
case Operators::bitwise_complement:
|
||||
return const_var(~t);
|
||||
default:
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -171,6 +208,7 @@ namespace chaiscript
|
||||
case Operators::sum:
|
||||
return const_var(t + u);
|
||||
case Operators::quotient:
|
||||
check_divide_by_zero(u);
|
||||
return const_var(t / u);
|
||||
case Operators::product:
|
||||
return const_var(t * u);
|
||||
@@ -181,7 +219,7 @@ namespace chaiscript
|
||||
case Operators::unary_plus:
|
||||
return const_var(+t);
|
||||
default:
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
throw chaiscript::detail::exception::bad_any_cast();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -327,7 +365,6 @@ namespace chaiscript
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
Boxed_Number()
|
||||
: bv(Boxed_Value(0))
|
||||
@@ -340,6 +377,13 @@ namespace chaiscript
|
||||
validate_boxed_number(bv);
|
||||
}
|
||||
|
||||
Boxed_Number(const Boxed_Number &) = default;
|
||||
|
||||
#if !defined(_MSC_VER) || _MSC_VER != 1800
|
||||
Boxed_Number(Boxed_Number &&) = default;
|
||||
Boxed_Number& operator=(Boxed_Number &&) = default;
|
||||
#endif
|
||||
|
||||
template<typename T> explicit Boxed_Number(T t)
|
||||
: bv(Boxed_Value(t))
|
||||
{
|
||||
@@ -547,6 +591,7 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
// cppcheck-suppress operatorEq
|
||||
Boxed_Number operator=(const Boxed_Value &v)
|
||||
{
|
||||
validate_boxed_number(v);
|
||||
@@ -554,6 +599,7 @@ namespace chaiscript
|
||||
return *this;
|
||||
}
|
||||
|
||||
// cppcheck-suppress operatorEq
|
||||
Boxed_Number operator=(const Boxed_Number &t_rhs) const
|
||||
{
|
||||
return oper(Operators::assign, this->bv, t_rhs.bv);
|
||||
@@ -843,14 +889,18 @@ namespace chaiscript
|
||||
struct Cast_Helper<const Boxed_Number &> : Cast_Helper<Boxed_Number>
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
/// Cast_Helper for converting from Boxed_Value to Boxed_Number
|
||||
template<>
|
||||
struct Cast_Helper<const Boxed_Number> : Cast_Helper<Boxed_Number>
|
||||
{
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#ifdef __GNUC__
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BOXED_VALUE_HPP_
|
||||
@@ -72,8 +72,8 @@ namespace chaiscript
|
||||
chaiscript::detail::Any m_obj;
|
||||
void *m_data_ptr;
|
||||
const void *m_const_data_ptr;
|
||||
bool m_is_ref;
|
||||
std::unique_ptr<std::map<std::string, Boxed_Value>> m_attrs;
|
||||
bool m_is_ref;
|
||||
};
|
||||
|
||||
struct Object_Data
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_DISPATCHKIT_HPP_
|
||||
@@ -62,6 +62,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
reserved_word_error(const reserved_word_error &) = default;
|
||||
|
||||
virtual ~reserved_word_error() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
std::string word() const
|
||||
@@ -82,6 +84,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
illegal_name_error(const illegal_name_error &) = default;
|
||||
|
||||
virtual ~illegal_name_error() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
std::string name() const
|
||||
@@ -103,6 +107,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
name_conflict_error(const name_conflict_error &) = default;
|
||||
|
||||
virtual ~name_conflict_error() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
std::string name() const
|
||||
@@ -125,6 +131,7 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
global_non_const(const global_non_const &) = default;
|
||||
virtual ~global_non_const() CHAISCRIPT_NOEXCEPT {}
|
||||
};
|
||||
}
|
||||
@@ -389,6 +396,8 @@ namespace chaiscript
|
||||
std::set<std::string> m_reserved_words;
|
||||
|
||||
State &operator=(const State &) = default;
|
||||
State() = default;
|
||||
State(const State &) = default;
|
||||
};
|
||||
|
||||
Dispatch_Engine()
|
||||
@@ -443,17 +452,13 @@ namespace chaiscript
|
||||
|
||||
|
||||
/// Adds a named object to the current scope
|
||||
void add_object(const std::string &name, const Boxed_Value &obj) const
|
||||
/// \warning This version does not check the validity of the name
|
||||
/// it is meant for internal use only
|
||||
void add_object(const std::string &name, const Boxed_Value &obj)
|
||||
{
|
||||
auto &stack = get_stack_data();
|
||||
validate_object_name(name);
|
||||
|
||||
auto &scope = stack.back();
|
||||
if (scope.find(name) != scope.end())
|
||||
if (!get_stack_data().back().insert(std::make_pair(name, obj)).second)
|
||||
{
|
||||
throw chaiscript::exception::name_conflict_error(name);
|
||||
} else {
|
||||
scope.insert(std::make_pair(name, obj));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,11 +502,13 @@ namespace chaiscript
|
||||
void new_scope()
|
||||
{
|
||||
get_stack_data().emplace_back();
|
||||
m_stack_holder->call_params.emplace_back();
|
||||
}
|
||||
|
||||
/// Pops the current scope from the stack
|
||||
void pop_scope()
|
||||
{
|
||||
m_stack_holder->call_params.pop_back();
|
||||
StackData &stack = get_stack_data();
|
||||
if (stack.size() > 1)
|
||||
{
|
||||
@@ -697,10 +704,10 @@ namespace chaiscript
|
||||
///
|
||||
std::map<std::string, Boxed_Value> get_scripting_objects() const
|
||||
{
|
||||
Stack_Holder &s = *m_stack_holder;
|
||||
const Stack_Holder &s = *m_stack_holder;
|
||||
|
||||
// We don't want the current context, but one up if it exists
|
||||
StackData &stack = (s.stacks.size()==1)?(s.stacks.back()):(s.stacks[s.stacks.size()-2]);
|
||||
const StackData &stack = (s.stacks.size()==1)?(s.stacks.back()):(s.stacks[s.stacks.size()-2]);
|
||||
|
||||
std::map<std::string, Boxed_Value> retval;
|
||||
|
||||
@@ -796,7 +803,7 @@ namespace chaiscript
|
||||
/// Dump object info to stdout
|
||||
void dump_object(const Boxed_Value &o) const
|
||||
{
|
||||
std::cout << (o.is_const()?"const ":"") << type_name(o) << std::endl;
|
||||
std::cout << (o.is_const()?"const ":"") << type_name(o) << '\n';
|
||||
}
|
||||
|
||||
/// Dump type info to stdout
|
||||
@@ -830,7 +837,7 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ") " << std::endl;
|
||||
std::cout << ") \n";
|
||||
}
|
||||
|
||||
/// Returns true if a call can be made that consists of the first parameter
|
||||
@@ -850,7 +857,7 @@ namespace chaiscript
|
||||
/// Dump all system info to stdout
|
||||
void dump_system() const
|
||||
{
|
||||
std::cout << "Registered Types: " << std::endl;
|
||||
std::cout << "Registered Types: \n";
|
||||
std::vector<std::pair<std::string, Type_Info> > types = get_types();
|
||||
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
|
||||
itr != types.end();
|
||||
@@ -858,20 +865,20 @@ namespace chaiscript
|
||||
{
|
||||
std::cout << itr->first << ": ";
|
||||
std::cout << itr->second.bare_name();
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
std::vector<std::pair<std::string, Proxy_Function > > funcs = get_functions();
|
||||
|
||||
std::cout << "Functions: " << std::endl;
|
||||
std::cout << "Functions: \n";
|
||||
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
dump_function(*itr);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
|
||||
/// return true if the Boxed_Value matches the registered type by name
|
||||
@@ -918,7 +925,7 @@ namespace chaiscript
|
||||
void save_function_params(std::initializer_list<Boxed_Value> t_params)
|
||||
{
|
||||
Stack_Holder &s = *m_stack_holder;
|
||||
s.call_params.insert(s.call_params.begin(), std::move(t_params));
|
||||
s.call_params.back().insert(s.call_params.back().begin(), std::move(t_params));
|
||||
}
|
||||
|
||||
void save_function_params(std::vector<Boxed_Value> &&t_params)
|
||||
@@ -927,14 +934,14 @@ namespace chaiscript
|
||||
|
||||
for (auto &¶m : t_params)
|
||||
{
|
||||
s.call_params.insert(s.call_params.begin(), std::move(param));
|
||||
s.call_params.back().insert(s.call_params.back().begin(), std::move(param));
|
||||
}
|
||||
}
|
||||
|
||||
void save_function_params(const std::vector<Boxed_Value> &t_params)
|
||||
{
|
||||
Stack_Holder &s = *m_stack_holder;
|
||||
s.call_params.insert(s.call_params.begin(), t_params.begin(), t_params.end());
|
||||
s.call_params.back().insert(s.call_params.back().begin(), t_params.begin(), t_params.end());
|
||||
}
|
||||
|
||||
void new_function_call()
|
||||
@@ -945,7 +952,7 @@ namespace chaiscript
|
||||
m_conversions.enable_conversion_saves(true);
|
||||
}
|
||||
|
||||
++m_stack_holder->call_depth;
|
||||
++s.call_depth;
|
||||
|
||||
save_function_params(m_conversions.take_saves());
|
||||
}
|
||||
@@ -959,9 +966,7 @@ namespace chaiscript
|
||||
|
||||
if (s.call_depth == 0)
|
||||
{
|
||||
/// \todo Critical: this needs to be smarter, memory can expand quickly
|
||||
/// in tight loops involving function calls
|
||||
s.call_params.clear();
|
||||
s.call_params.back().clear();
|
||||
m_conversions.enable_conversion_saves(false);
|
||||
}
|
||||
}
|
||||
@@ -969,7 +974,12 @@ namespace chaiscript
|
||||
private:
|
||||
/// Returns the current stack
|
||||
/// make const/non const versions
|
||||
StackData &get_stack_data() const
|
||||
const StackData &get_stack_data() const
|
||||
{
|
||||
return m_stack_holder->stacks.back();
|
||||
}
|
||||
|
||||
StackData &get_stack_data()
|
||||
{
|
||||
return m_stack_holder->stacks.back();
|
||||
}
|
||||
@@ -1153,11 +1163,12 @@ namespace chaiscript
|
||||
: call_depth(0)
|
||||
{
|
||||
stacks.emplace_back(1);
|
||||
call_params.emplace_back();
|
||||
}
|
||||
|
||||
std::deque<StackData> stacks;
|
||||
|
||||
std::list<Boxed_Value> call_params;
|
||||
std::deque<std::list<Boxed_Value>> call_params;
|
||||
int call_depth;
|
||||
};
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_
|
||||
@@ -19,7 +19,6 @@
|
||||
#include "boxed_cast.hpp"
|
||||
#include "boxed_cast_helper.hpp"
|
||||
#include "boxed_value.hpp"
|
||||
#include "proxy_functions.hpp"
|
||||
#include "type_info.hpp"
|
||||
|
||||
namespace chaiscript {
|
||||
@@ -62,217 +61,6 @@ namespace chaiscript
|
||||
std::map<std::string, Boxed_Value> m_attrs;
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
/// A Proxy_Function implementation designed for calling a function
|
||||
/// that is automatically guarded based on the first param based on the
|
||||
/// param's type name
|
||||
class Dynamic_Object_Function : public Proxy_Function_Base
|
||||
{
|
||||
public:
|
||||
Dynamic_Object_Function(
|
||||
std::string t_type_name,
|
||||
const Proxy_Function &t_func)
|
||||
: Proxy_Function_Base(t_func->get_param_types(), t_func->get_arity()),
|
||||
m_type_name(std::move(t_type_name)), m_func(t_func), m_doti(user_type<Dynamic_Object>())
|
||||
{
|
||||
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
|
||||
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
|
||||
}
|
||||
|
||||
Dynamic_Object_Function(
|
||||
std::string t_type_name,
|
||||
const Proxy_Function &t_func,
|
||||
const Type_Info &t_ti)
|
||||
: Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti), t_func->get_arity()),
|
||||
m_type_name(std::move(t_type_name)), m_func(t_func), m_ti(new Type_Info(t_ti)), m_doti(user_type<Dynamic_Object>())
|
||||
{
|
||||
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
|
||||
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Object_Function() {}
|
||||
|
||||
Dynamic_Object_Function &operator=(const Dynamic_Object_Function) = delete;
|
||||
Dynamic_Object_Function(Dynamic_Object_Function &) = delete;
|
||||
|
||||
virtual bool operator==(const Proxy_Function_Base &f) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (const auto *df = dynamic_cast<const Dynamic_Object_Function *>(&f))
|
||||
{
|
||||
return df->m_type_name == m_type_name && (*df->m_func) == (*m_func);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (dynamic_object_typename_match(vals, m_type_name, m_ti, t_conversions))
|
||||
{
|
||||
return m_func->call_match(vals, t_conversions);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual std::vector<Const_Proxy_Function> get_contained_functions() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return {m_func};
|
||||
}
|
||||
|
||||
virtual std::string annotation() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return m_func->annotation();
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (dynamic_object_typename_match(params, m_type_name, m_ti, t_conversions))
|
||||
{
|
||||
return (*m_func)(params, t_conversions);
|
||||
} else {
|
||||
throw exception::guard_error();
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool compare_first_type(const Boxed_Value &bv, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return dynamic_object_typename_match(bv, m_type_name, m_ti, t_conversions);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::vector<Type_Info> build_param_types(
|
||||
const std::vector<Type_Info> &t_inner_types, const Type_Info& t_objectti)
|
||||
{
|
||||
std::vector<Type_Info> types(t_inner_types);
|
||||
|
||||
assert(types.size() > 1);
|
||||
assert(types[1].bare_equal(user_type<Boxed_Value>()));
|
||||
types[1] = t_objectti;
|
||||
return types;
|
||||
}
|
||||
|
||||
bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name,
|
||||
const std::unique_ptr<Type_Info> &ti, const Type_Conversions &t_conversions) const
|
||||
{
|
||||
if (bv.get_type_info().bare_equal(m_doti))
|
||||
{
|
||||
try {
|
||||
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
|
||||
return name == "Dynamic_Object" || d.get_type_name() == name;
|
||||
} catch (const std::bad_cast &) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (ti)
|
||||
{
|
||||
return bv.get_type_info().bare_equal(*ti);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
|
||||
const std::unique_ptr<Type_Info> &ti, const Type_Conversions &t_conversions) const
|
||||
{
|
||||
if (bvs.size() > 0)
|
||||
{
|
||||
return dynamic_object_typename_match(bvs[0], name, ti, t_conversions);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string m_type_name;
|
||||
Proxy_Function m_func;
|
||||
std::unique_ptr<Type_Info> m_ti;
|
||||
const Type_Info m_doti;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A Proxy_Function implementation designed for creating a new
|
||||
* Dynamic_Object
|
||||
* that is automatically guarded based on the first param based on the
|
||||
* param's type name
|
||||
*/
|
||||
class Dynamic_Object_Constructor : public Proxy_Function_Base
|
||||
{
|
||||
public:
|
||||
Dynamic_Object_Constructor(
|
||||
std::string t_type_name,
|
||||
const Proxy_Function &t_func)
|
||||
: Proxy_Function_Base(build_type_list(t_func->get_param_types()), t_func->get_arity() - 1),
|
||||
m_type_name(std::move(t_type_name)), m_func(t_func)
|
||||
{
|
||||
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
|
||||
&& "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)
|
||||
{
|
||||
auto begin = tl.begin();
|
||||
auto end = tl.end();
|
||||
|
||||
if (begin != end)
|
||||
{
|
||||
++begin;
|
||||
}
|
||||
|
||||
return std::vector<Type_Info>(begin, end);
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Object_Constructor() {}
|
||||
|
||||
virtual bool operator==(const Proxy_Function_Base &f) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
const Dynamic_Object_Constructor *dc = dynamic_cast<const Dynamic_Object_Constructor*>(&f);
|
||||
if (dc)
|
||||
{
|
||||
return dc->m_type_name == m_type_name && (*dc->m_func) == (*m_func);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
std::vector<Boxed_Value> new_vals{Boxed_Value(Dynamic_Object(m_type_name))};
|
||||
new_vals.insert(new_vals.end(), vals.begin(), vals.end());
|
||||
|
||||
return m_func->call_match(new_vals, t_conversions);
|
||||
}
|
||||
|
||||
virtual std::string annotation() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return m_func->annotation();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
auto bv = var(Dynamic_Object(m_type_name));
|
||||
std::vector<Boxed_Value> new_params{bv};
|
||||
new_params.insert(new_params.end(), params.begin(), params.end());
|
||||
|
||||
(*m_func)(new_params, t_conversions);
|
||||
|
||||
return bv;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_type_name;
|
||||
Proxy_Function m_func;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
251
include/chaiscript/dispatchkit/dynamic_object_detail.hpp
Normal file
251
include/chaiscript/dispatchkit/dynamic_object_detail.hpp
Normal file
@@ -0,0 +1,251 @@
|
||||
// This file is distributed under the BSD License.
|
||||
// See "license.txt" for details.
|
||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_DETAIL_HPP_
|
||||
#define CHAISCRIPT_DYNAMIC_OBJECT_DETAIL_HPP_
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../chaiscript_defines.hpp"
|
||||
#include "boxed_cast.hpp"
|
||||
#include "boxed_cast_helper.hpp"
|
||||
#include "boxed_value.hpp"
|
||||
#include "proxy_functions.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include "dynamic_object.hpp"
|
||||
|
||||
namespace chaiscript {
|
||||
class Type_Conversions;
|
||||
namespace dispatch {
|
||||
class Proxy_Function_Base;
|
||||
} // namespace dispatch
|
||||
} // namespace chaiscript
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
namespace dispatch
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
/// A Proxy_Function implementation designed for calling a function
|
||||
/// that is automatically guarded based on the first param based on the
|
||||
/// param's type name
|
||||
class Dynamic_Object_Function : public Proxy_Function_Base
|
||||
{
|
||||
public:
|
||||
Dynamic_Object_Function(
|
||||
std::string t_type_name,
|
||||
const Proxy_Function &t_func)
|
||||
: Proxy_Function_Base(t_func->get_param_types(), t_func->get_arity()),
|
||||
m_type_name(std::move(t_type_name)), m_func(t_func), m_doti(user_type<Dynamic_Object>())
|
||||
{
|
||||
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
|
||||
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
|
||||
}
|
||||
|
||||
Dynamic_Object_Function(
|
||||
std::string t_type_name,
|
||||
const Proxy_Function &t_func,
|
||||
const Type_Info &t_ti)
|
||||
: Proxy_Function_Base(build_param_types(t_func->get_param_types(), t_ti), t_func->get_arity()),
|
||||
m_type_name(std::move(t_type_name)), m_func(t_func), m_ti(new Type_Info(t_ti)), m_doti(user_type<Dynamic_Object>())
|
||||
{
|
||||
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
|
||||
&& "Programming error, Dynamic_Object_Function must have at least one parameter (this)");
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Object_Function() {}
|
||||
|
||||
Dynamic_Object_Function &operator=(const Dynamic_Object_Function) = delete;
|
||||
Dynamic_Object_Function(Dynamic_Object_Function &) = delete;
|
||||
|
||||
virtual bool operator==(const Proxy_Function_Base &f) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (const auto *df = dynamic_cast<const Dynamic_Object_Function *>(&f))
|
||||
{
|
||||
return df->m_type_name == m_type_name && (*df->m_func) == (*m_func);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (dynamic_object_typename_match(vals, m_type_name, m_ti, t_conversions))
|
||||
{
|
||||
return m_func->call_match(vals, t_conversions);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual std::vector<Const_Proxy_Function> get_contained_functions() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return {m_func};
|
||||
}
|
||||
|
||||
virtual std::string annotation() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return m_func->annotation();
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (dynamic_object_typename_match(params, m_type_name, m_ti, t_conversions))
|
||||
{
|
||||
return (*m_func)(params, t_conversions);
|
||||
} else {
|
||||
throw exception::guard_error();
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool compare_first_type(const Boxed_Value &bv, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return dynamic_object_typename_match(bv, m_type_name, m_ti, t_conversions);
|
||||
}
|
||||
|
||||
private:
|
||||
static std::vector<Type_Info> build_param_types(
|
||||
const std::vector<Type_Info> &t_inner_types, const Type_Info& t_objectti)
|
||||
{
|
||||
std::vector<Type_Info> types(t_inner_types);
|
||||
|
||||
assert(types.size() > 1);
|
||||
//assert(types[1].bare_equal(user_type<Boxed_Value>()));
|
||||
types[1] = t_objectti;
|
||||
return types;
|
||||
}
|
||||
|
||||
bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name,
|
||||
const std::unique_ptr<Type_Info> &ti, const Type_Conversions &t_conversions) const
|
||||
{
|
||||
if (bv.get_type_info().bare_equal(m_doti))
|
||||
{
|
||||
try {
|
||||
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
|
||||
return name == "Dynamic_Object" || d.get_type_name() == name;
|
||||
} catch (const std::bad_cast &) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (ti)
|
||||
{
|
||||
return bv.get_type_info().bare_equal(*ti);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
|
||||
const std::unique_ptr<Type_Info> &ti, const Type_Conversions &t_conversions) const
|
||||
{
|
||||
if (bvs.size() > 0)
|
||||
{
|
||||
return dynamic_object_typename_match(bvs[0], name, ti, t_conversions);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string m_type_name;
|
||||
Proxy_Function m_func;
|
||||
std::unique_ptr<Type_Info> m_ti;
|
||||
const Type_Info m_doti;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A Proxy_Function implementation designed for creating a new
|
||||
* Dynamic_Object
|
||||
* that is automatically guarded based on the first param based on the
|
||||
* param's type name
|
||||
*/
|
||||
class Dynamic_Object_Constructor : public Proxy_Function_Base
|
||||
{
|
||||
public:
|
||||
Dynamic_Object_Constructor(
|
||||
std::string t_type_name,
|
||||
const Proxy_Function &t_func)
|
||||
: Proxy_Function_Base(build_type_list(t_func->get_param_types()), t_func->get_arity() - 1),
|
||||
m_type_name(std::move(t_type_name)), m_func(t_func)
|
||||
{
|
||||
assert( (t_func->get_arity() > 0 || t_func->get_arity() < 0)
|
||||
&& "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)
|
||||
{
|
||||
auto begin = tl.begin();
|
||||
auto end = tl.end();
|
||||
|
||||
if (begin != end)
|
||||
{
|
||||
++begin;
|
||||
}
|
||||
|
||||
return std::vector<Type_Info>(begin, end);
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Object_Constructor() {}
|
||||
|
||||
virtual bool operator==(const Proxy_Function_Base &f) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
const Dynamic_Object_Constructor *dc = dynamic_cast<const Dynamic_Object_Constructor*>(&f);
|
||||
if (dc)
|
||||
{
|
||||
return dc->m_type_name == m_type_name && (*dc->m_func) == (*m_func);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
std::vector<Boxed_Value> new_vals{Boxed_Value(Dynamic_Object(m_type_name))};
|
||||
new_vals.insert(new_vals.end(), vals.begin(), vals.end());
|
||||
|
||||
return m_func->call_match(new_vals, t_conversions);
|
||||
}
|
||||
|
||||
virtual std::string annotation() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return m_func->annotation();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
auto bv = var(Dynamic_Object(m_type_name));
|
||||
std::vector<Boxed_Value> new_params{bv};
|
||||
new_params.insert(new_params.end(), params.begin(), params.end());
|
||||
|
||||
(*m_func)(new_params, t_conversions);
|
||||
|
||||
return bv;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_type_name;
|
||||
Proxy_Function m_func;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_FUNCTION_CALL_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_FUNCTION_CALL_DETAIL_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_HANDLE_RETURN_HPP_
|
||||
@@ -37,6 +37,11 @@ namespace chaiscript
|
||||
{
|
||||
return const_var(r);
|
||||
}
|
||||
|
||||
static Boxed_Value handle(Ret &&r)
|
||||
{
|
||||
return Boxed_Value(std::move(r));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_OPERATORS_HPP_
|
||||
|
@@ -1,13 +1,15 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
|
||||
#ifndef CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
|
||||
#define CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
|
||||
|
||||
#include "proxy_functions.hpp"
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
namespace dispatch
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "boxed_value.hpp"
|
||||
#include "proxy_functions_detail.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include "dynamic_object.hpp"
|
||||
|
||||
namespace chaiscript {
|
||||
class Type_Conversions;
|
||||
@@ -42,6 +43,95 @@ namespace chaiscript
|
||||
|
||||
namespace dispatch
|
||||
{
|
||||
class Param_Types
|
||||
{
|
||||
public:
|
||||
Param_Types()
|
||||
: m_has_types(false),
|
||||
m_doti(user_type<Dynamic_Object>())
|
||||
{}
|
||||
|
||||
Param_Types(std::vector<std::pair<std::string, Type_Info>> t_types)
|
||||
: m_types(std::move(t_types)),
|
||||
m_has_types(false),
|
||||
m_doti(user_type<Dynamic_Object>())
|
||||
{
|
||||
update_has_types();
|
||||
}
|
||||
|
||||
void push_front(std::string t_name, Type_Info t_ti)
|
||||
{
|
||||
m_types.emplace(m_types.begin(), std::move(t_name), std::move(t_ti));
|
||||
update_has_types();
|
||||
}
|
||||
|
||||
bool operator==(const Param_Types &t_rhs) const
|
||||
{
|
||||
return m_types == t_rhs.m_types;
|
||||
}
|
||||
|
||||
bool match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const
|
||||
{
|
||||
if (!m_has_types) return true;
|
||||
if (vals.size() != m_types.size()) return false;
|
||||
|
||||
for (size_t i = 0; i < vals.size(); ++i)
|
||||
{
|
||||
const auto &name = m_types[i].first;
|
||||
if (!name.empty()) {
|
||||
const auto &bv = vals[i];
|
||||
|
||||
if (bv.get_type_info().bare_equal(m_doti))
|
||||
{
|
||||
try {
|
||||
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
|
||||
return name == "Dynamic_Object" || d.get_type_name() == name;
|
||||
} catch (const std::bad_cast &) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
const auto &ti = m_types[i].second;
|
||||
if (!ti.is_undef())
|
||||
{
|
||||
if (!bv.get_type_info().bare_equal(ti)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::vector<std::pair<std::string, Type_Info>> &types() const
|
||||
{
|
||||
return m_types;
|
||||
}
|
||||
|
||||
private:
|
||||
void update_has_types()
|
||||
{
|
||||
for (const auto &type : m_types)
|
||||
{
|
||||
if (!type.first.empty())
|
||||
{
|
||||
m_has_types = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_has_types = false;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, Type_Info>> m_types;
|
||||
bool m_has_types;
|
||||
Type_Info m_doti;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Pure virtual base class for all Proxy_Function implementations
|
||||
* Proxy_Functions are a type erasure of type safe C++
|
||||
@@ -129,7 +219,7 @@ namespace chaiscript
|
||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions &t_conversions) const = 0;
|
||||
|
||||
Proxy_Function_Base(std::vector<Type_Info> t_types, int t_arity)
|
||||
: m_types(std::move(t_types)), m_has_arithmetic_param(false), m_arity(t_arity)
|
||||
: m_types(std::move(t_types)), m_arity(t_arity), m_has_arithmetic_param(false)
|
||||
{
|
||||
for (size_t i = 1; i < m_types.size(); ++i)
|
||||
{
|
||||
@@ -175,8 +265,8 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
std::vector<Type_Info> m_types;
|
||||
bool m_has_arithmetic_param;
|
||||
int m_arity;
|
||||
bool m_has_arithmetic_param;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -197,6 +287,8 @@ namespace chaiscript
|
||||
: std::runtime_error("Guard evaluation failed")
|
||||
{ }
|
||||
|
||||
guard_error(const guard_error &) = default;
|
||||
|
||||
virtual ~guard_error() CHAISCRIPT_NOEXCEPT
|
||||
{ }
|
||||
};
|
||||
@@ -215,10 +307,13 @@ namespace chaiscript
|
||||
std::function<Boxed_Value (const std::vector<Boxed_Value> &)> t_f,
|
||||
int t_arity=-1,
|
||||
AST_NodePtr t_parsenode = AST_NodePtr(),
|
||||
Param_Types t_param_types = Param_Types(),
|
||||
std::string t_description = "",
|
||||
Proxy_Function t_guard = Proxy_Function())
|
||||
: Proxy_Function_Base(build_param_type_list(t_arity), t_arity),
|
||||
m_f(std::move(t_f)), m_arity(t_arity), m_description(std::move(t_description)), m_guard(std::move(t_guard)), m_parsenode(std::move(t_parsenode))
|
||||
: Proxy_Function_Base(build_param_type_list(t_param_types), t_arity),
|
||||
m_param_types(std::move(t_param_types)),
|
||||
m_guard(std::move(t_guard)), m_parsenode(std::move(t_parsenode)), m_description(std::move(t_description)),
|
||||
m_f(std::move(t_f))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -231,14 +326,15 @@ namespace chaiscript
|
||||
return this == &rhs
|
||||
|| (prhs
|
||||
&& this->m_arity == prhs->m_arity
|
||||
&& !this->m_guard && !prhs->m_guard);
|
||||
&& !this->m_guard && !prhs->m_guard
|
||||
&& this->m_param_types == prhs->m_param_types);
|
||||
}
|
||||
|
||||
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
return (m_arity < 0 || vals.size() == size_t(m_arity))
|
||||
return (m_arity < 0 || (vals.size() == size_t(m_arity) && m_param_types.match(vals, t_conversions)))
|
||||
&& test_guard(vals, t_conversions);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Proxy_Function get_guard() const
|
||||
@@ -261,8 +357,7 @@ namespace chaiscript
|
||||
{
|
||||
if (m_arity < 0 || params.size() == size_t(m_arity))
|
||||
{
|
||||
|
||||
if (test_guard(params, t_conversions))
|
||||
if (call_match(params, t_conversions) && test_guard(params, t_conversions))
|
||||
{
|
||||
return m_f(params);
|
||||
} else {
|
||||
@@ -291,29 +386,30 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<Type_Info> build_param_type_list(int arity)
|
||||
static std::vector<Type_Info> build_param_type_list(const Param_Types &t_types)
|
||||
{
|
||||
std::vector<Type_Info> types;
|
||||
|
||||
// For the return type
|
||||
types.push_back(chaiscript::detail::Get_Type_Info<Boxed_Value>::get());
|
||||
|
||||
if (arity > 0)
|
||||
for (const auto &t : t_types.types())
|
||||
{
|
||||
for (int i = 0; i < arity; ++i)
|
||||
{
|
||||
if (t.second.is_undef()) {
|
||||
types.push_back(chaiscript::detail::Get_Type_Info<Boxed_Value>::get());
|
||||
} else {
|
||||
types.push_back(t.second);
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
std::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
||||
int m_arity;
|
||||
std::string m_description;
|
||||
Param_Types m_param_types;
|
||||
Proxy_Function m_guard;
|
||||
AST_NodePtr m_parsenode;
|
||||
std::string m_description;
|
||||
std::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -560,7 +656,7 @@ namespace chaiscript
|
||||
}
|
||||
} else {
|
||||
throw exception::arity_error(static_cast<int>(params.size()), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -589,6 +685,7 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
dispatch_error(const dispatch_error &) = default;
|
||||
virtual ~dispatch_error() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
std::vector<Boxed_Value> parameters;
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_
|
||||
@@ -42,6 +42,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
arity_error(const arity_error &) = default;
|
||||
|
||||
virtual ~arity_error() CHAISCRIPT_NOEXCEPT {}
|
||||
|
||||
int got;
|
||||
@@ -72,7 +74,7 @@ namespace chaiscript
|
||||
template<typename Param, typename ... Rest>
|
||||
struct Try_Cast<Param, Rest...>
|
||||
{
|
||||
static void do_try(const std::vector<Boxed_Value> ¶ms, int generation, const Type_Conversions &t_conversions)
|
||||
static void do_try(const std::vector<Boxed_Value> ¶ms, size_t generation, const Type_Conversions &t_conversions)
|
||||
{
|
||||
boxed_cast<Param>(params[generation], &t_conversions);
|
||||
Try_Cast<Rest...>::do_try(params, generation+1, t_conversions);
|
||||
@@ -83,7 +85,7 @@ namespace chaiscript
|
||||
template<>
|
||||
struct Try_Cast<>
|
||||
{
|
||||
static void do_try(const std::vector<Boxed_Value> &, int, const Type_Conversions &)
|
||||
static void do_try(const std::vector<Boxed_Value> &, size_t, const Type_Conversions &)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
|
||||
@@ -44,6 +44,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
bad_boxed_dynamic_cast(const bad_boxed_dynamic_cast &) = default;
|
||||
|
||||
virtual ~bad_boxed_dynamic_cast() CHAISCRIPT_NOEXCEPT {}
|
||||
};
|
||||
|
||||
@@ -66,6 +68,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
bad_boxed_type_cast(const bad_boxed_type_cast &) = default;
|
||||
|
||||
virtual ~bad_boxed_type_cast() CHAISCRIPT_NOEXCEPT {}
|
||||
};
|
||||
}
|
||||
@@ -213,14 +217,20 @@ namespace chaiscript
|
||||
};
|
||||
|
||||
Type_Conversions()
|
||||
: m_num_types(0),
|
||||
: m_mutex(),
|
||||
m_conversions(),
|
||||
m_convertableTypes(),
|
||||
m_num_types(0),
|
||||
m_thread_cache(this),
|
||||
m_conversion_saves(this)
|
||||
{
|
||||
}
|
||||
|
||||
Type_Conversions(const Type_Conversions &t_other)
|
||||
: m_conversions(t_other.get_conversions()), m_num_types(m_conversions.size()),
|
||||
: m_mutex(),
|
||||
m_conversions(t_other.get_conversions()),
|
||||
m_convertableTypes(),
|
||||
m_num_types(m_conversions.size()),
|
||||
m_thread_cache(this),
|
||||
m_conversion_saves(this)
|
||||
|
||||
@@ -366,8 +376,8 @@ namespace chaiscript
|
||||
std::set<std::shared_ptr<detail::Type_Conversion_Base>> m_conversions;
|
||||
std::set<const std::type_info *, Less_Than> m_convertableTypes;
|
||||
std::atomic_size_t m_num_types;
|
||||
chaiscript::detail::threading::Thread_Storage<std::set<const std::type_info *, Less_Than>> m_thread_cache;
|
||||
chaiscript::detail::threading::Thread_Storage<Conversion_Saves> m_conversion_saves;
|
||||
mutable chaiscript::detail::threading::Thread_Storage<std::set<const std::type_info *, Less_Than>> m_thread_cache;
|
||||
mutable chaiscript::detail::threading::Thread_Storage<Conversion_Saves> m_conversion_saves;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<chaiscript::detail::Type_Conversion_Base> Type_Conversion;
|
||||
@@ -429,11 +439,7 @@ namespace chaiscript
|
||||
static_assert(std::is_convertible<From, To>::value, "Types are not automatically convertible");
|
||||
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
|
||||
// not even attempting to call boxed_cast so that we don't get caught in some call recursion
|
||||
std::cout << " Type conversion to : " << typeid(To).name() << " from " << typeid(From).name() << std::endl;
|
||||
auto &&from = detail::Cast_Helper<From>::cast(t_bv, nullptr);
|
||||
std::cout << "Ptr" << static_cast<const void *>(from) << std::endl;
|
||||
std::cout << "Ptr" << from << std::endl;
|
||||
|
||||
To to(from);
|
||||
return chaiscript::Boxed_Value(to);
|
||||
};
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_TYPE_INFO_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_ALGEBRAIC_HPP_
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_COMMON_HPP_
|
||||
@@ -37,7 +37,7 @@ namespace chaiscript
|
||||
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, Continue, Map_Pair, Value_Range,
|
||||
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
|
||||
Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class
|
||||
Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class, Binary, Arg
|
||||
};
|
||||
};
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace chaiscript
|
||||
"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", "Continue", "Map_Pair", "Value_Range",
|
||||
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or",
|
||||
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop"};
|
||||
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary", "Arg"};
|
||||
|
||||
return ast_node_types[ast_node_type];
|
||||
}
|
||||
@@ -112,25 +112,27 @@ namespace chaiscript
|
||||
reason(t_why)
|
||||
{}
|
||||
|
||||
eval_error(const eval_error &) = default;
|
||||
|
||||
std::string pretty_print() const
|
||||
{
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << what();
|
||||
if (call_stack.size() > 0) {
|
||||
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")" << std::endl;
|
||||
ss << std::endl << detail << std::endl;
|
||||
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")\n";
|
||||
ss << '\n' << detail << '\n';
|
||||
ss << " " << fname(call_stack[0]) << " (" << startpos(call_stack[0]) << ") '" << pretty(call_stack[0]) << "'";
|
||||
for (size_t j = 1; j < call_stack.size(); ++j) {
|
||||
if (id(call_stack[j]) != chaiscript::AST_Node_Type::Block
|
||||
&& id(call_stack[j]) != chaiscript::AST_Node_Type::File)
|
||||
{
|
||||
ss << std::endl;
|
||||
ss << '\n';
|
||||
ss << " from " << fname(call_stack[j]) << " (" << startpos(call_stack[j]) << ") '" << pretty(call_stack[j]) << "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
ss << std::endl;
|
||||
ss << '\n';
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
@@ -264,13 +266,13 @@ namespace chaiscript
|
||||
std::stringstream ss;
|
||||
if (t_functions.size() == 1)
|
||||
{
|
||||
ss << " Expected: " << format_types(t_functions[0], t_dot_notation, t_ss) << std::endl;
|
||||
ss << " Expected: " << format_types(t_functions[0], t_dot_notation, t_ss) << '\n';
|
||||
} else {
|
||||
ss << " " << t_functions.size() << " overloads available:" << std::endl;
|
||||
ss << " " << t_functions.size() << " overloads available:\n";
|
||||
|
||||
for (const auto & t_function : t_functions)
|
||||
{
|
||||
ss << " " << format_types((t_function), t_dot_notation, t_ss) << std::endl;
|
||||
ss << " " << format_types((t_function), t_dot_notation, t_ss) << '\n';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -395,6 +397,7 @@ namespace chaiscript
|
||||
: std::runtime_error("File Not Found: " + t_filename)
|
||||
{ }
|
||||
|
||||
file_not_found_error(const file_not_found_error &) = default;
|
||||
virtual ~file_not_found_error() CHAISCRIPT_NOEXCEPT {}
|
||||
};
|
||||
|
||||
@@ -430,7 +433,7 @@ namespace chaiscript
|
||||
std::ostringstream oss;
|
||||
|
||||
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
|
||||
<< this->text << " : " << this->start.line << ", " << this->start.column << std::endl;
|
||||
<< this->text << " : " << this->start.line << ", " << this->start.column << '\n';
|
||||
|
||||
for (size_t j = 0; j < this->children.size(); ++j) {
|
||||
oss << this->children[j]->to_string(t_prepend + " ");
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_ENGINE_HPP_
|
||||
@@ -61,9 +61,8 @@ namespace chaiscript
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~load_module_error() CHAISCRIPT_NOEXCEPT
|
||||
{
|
||||
}
|
||||
load_module_error(const load_module_error &) = default;
|
||||
virtual ~load_module_error() CHAISCRIPT_NOEXCEPT {}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -371,10 +370,10 @@ namespace chaiscript
|
||||
m_engine.add(fun(&ChaiScript::internal_eval, this), "eval");
|
||||
m_engine.add(fun(&ChaiScript::internal_eval_ast, this), "eval");
|
||||
|
||||
m_engine.add(fun(&ChaiScript::version_major, this), "version_major");
|
||||
m_engine.add(fun(&ChaiScript::version_minor, this), "version_minor");
|
||||
m_engine.add(fun(&ChaiScript::version_patch, this), "version_patch");
|
||||
m_engine.add(fun(&ChaiScript::version, this), "version");
|
||||
m_engine.add(fun(&ChaiScript::version_major), "version_major");
|
||||
m_engine.add(fun(&ChaiScript::version_minor), "version_minor");
|
||||
m_engine.add(fun(&ChaiScript::version_patch), "version_patch");
|
||||
m_engine.add(fun(&ChaiScript::version), "version");
|
||||
|
||||
m_engine.add(fun(&ChaiScript::add_global_const, this), "add_global_const");
|
||||
m_engine.add(fun(&ChaiScript::add_global, this), "add_global");
|
||||
@@ -464,7 +463,7 @@ namespace chaiscript
|
||||
memset( &rInfo, 0, sizeof(rInfo) );
|
||||
cast_union u;
|
||||
u.in_ptr = &ChaiScript::use;
|
||||
if ( dladdr((void*)(u.out_ptr), &rInfo) && rInfo.dli_fname ) {
|
||||
if ( dladdr(static_cast<void*>(u.out_ptr), &rInfo) && rInfo.dli_fname ) {
|
||||
std::string dllpath(rInfo.dli_fname);
|
||||
const size_t lastslash = dllpath.rfind('/');
|
||||
if (lastslash != std::string::npos)
|
||||
@@ -491,22 +490,22 @@ namespace chaiscript
|
||||
build_eval_system(ModulePtr());
|
||||
}
|
||||
|
||||
int version_major() const
|
||||
static int version_major()
|
||||
{
|
||||
return chaiscript::version_major;
|
||||
}
|
||||
|
||||
int version_minor() const
|
||||
static int version_minor()
|
||||
{
|
||||
return chaiscript::version_minor;
|
||||
}
|
||||
|
||||
int version_patch() const
|
||||
static int version_patch()
|
||||
{
|
||||
return chaiscript::version_patch;
|
||||
}
|
||||
|
||||
std::string version() const
|
||||
static std::string version()
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << version_major() << "." << version_minor() << "." << version_patch();
|
||||
@@ -581,6 +580,8 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
/// \brief Represents the current state of the ChaiScript system. State and be saved and restored
|
||||
/// \warning State object does not contain the user defined type conversions of the engine. They
|
||||
/// are left out due to performance considerations involved in tracking the state
|
||||
/// \sa ChaiScript::get_state
|
||||
/// \sa ChaiScript::set_state
|
||||
struct State
|
||||
@@ -603,7 +604,7 @@ namespace chaiscript
|
||||
/// chaiscript::ChaiScript chai;
|
||||
/// chaiscript::ChaiScript::State s = chai.get_state(); // represents bootstrapped initial state
|
||||
/// \endcode
|
||||
State get_state()
|
||||
State get_state() const
|
||||
{
|
||||
chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
|
||||
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
|
||||
@@ -706,7 +707,7 @@ namespace chaiscript
|
||||
/// \param[in] t_module_name Name of the module to load
|
||||
///
|
||||
/// The module is searched for in the registered module path folders (chaiscript::ChaiScript::ChaiScript)
|
||||
/// and with standard prefixes and postfixes: ("lib"|"")\<t_module_name\>(".dll"|".so"|"").
|
||||
/// and with standard prefixes and postfixes: ("lib"|"")\<t_module_name\>(".dll"|".so"|".bundle"|"").
|
||||
///
|
||||
/// Once the file is located, the system looks for the symbol "create_chaiscript_module_\<t_module_name\>".
|
||||
/// If no file can be found matching the search criteria and containing the appropriate entry point
|
||||
@@ -725,7 +726,7 @@ namespace chaiscript
|
||||
|
||||
std::vector<std::string> prefixes{"lib", "cyg", ""};
|
||||
|
||||
std::vector<std::string> postfixes{".dll", ".so", ""};
|
||||
std::vector<std::string> postfixes{".dll", ".so", ".bundle", ""};
|
||||
|
||||
for (auto & elem : m_modulepaths)
|
||||
{
|
||||
@@ -735,11 +736,11 @@ namespace chaiscript
|
||||
{
|
||||
try {
|
||||
const auto name = elem + prefix + t_module_name + postfix;
|
||||
// std::cerr << "trying location: " << name << std::endl;
|
||||
// std::cerr << "trying location: " << name << '\n';
|
||||
load_module(version_stripped_name, name);
|
||||
return name;
|
||||
} catch (const chaiscript::exception::load_module_error &e) {
|
||||
// std::cerr << "error: " << e.what() << std::endl;
|
||||
// std::cerr << "error: " << e.what() << '\n';
|
||||
errors.push_back(e);
|
||||
// Try next set
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_EVAL_HPP_
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "../dispatchkit/boxed_number.hpp"
|
||||
#include "../dispatchkit/boxed_value.hpp"
|
||||
#include "../dispatchkit/dispatchkit.hpp"
|
||||
#include "../dispatchkit/dynamic_object_detail.hpp"
|
||||
#include "../dispatchkit/proxy_functions.hpp"
|
||||
#include "../dispatchkit/proxy_functions_detail.hpp"
|
||||
#include "../dispatchkit/register_function.hpp"
|
||||
@@ -63,25 +64,21 @@ namespace chaiscript
|
||||
|
||||
struct Binary_Operator_AST_Node : public AST_Node {
|
||||
public:
|
||||
Binary_Operator_AST_Node(std::string t_ast_node_text, int t_id, const std::shared_ptr<std::string> &t_fname, int t_start_line, int t_start_col, int t_end_line, int t_end_col) :
|
||||
AST_Node(std::move(t_ast_node_text), t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
|
||||
Binary_Operator_AST_Node(const std::string &t_oper) :
|
||||
AST_Node(t_oper, AST_Node_Type::Binary, std::make_shared<std::string>(""), 0, 0, 0, 0),
|
||||
m_oper(Operators::to_operator(t_oper))
|
||||
{ }
|
||||
|
||||
virtual ~Binary_Operator_AST_Node() {}
|
||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
return do_oper(t_ss, Operators::to_operator(children[1]->text), children[1]->text,
|
||||
return do_oper(t_ss, m_oper, text,
|
||||
this->children[0]->eval(t_ss),
|
||||
this->children[2]->eval(t_ss));
|
||||
this->children[1]->eval(t_ss));
|
||||
}
|
||||
|
||||
virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
if (children.size() == 3)
|
||||
{
|
||||
return "(" + this->children[0]->pretty_print() + " " + this->children[1]->text + " " + this->children[2]->pretty_print() + ")";
|
||||
} else {
|
||||
return "(" + this->children[0]->pretty_print() + " " + text + " " + this->children[1]->pretty_print() + ")";
|
||||
}
|
||||
return "(" + this->children[0]->pretty_print() + " " + text + " " + this->children[1]->pretty_print() + ")";
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -97,6 +94,8 @@ namespace chaiscript
|
||||
// If it's an arithmetic operation we want to short circuit dispatch
|
||||
try{
|
||||
return Boxed_Number::do_oper(t_oper, t_lhs, t_rhs);
|
||||
} catch (const chaiscript::exception::arithmetic_error &) {
|
||||
throw;
|
||||
} catch (...) {
|
||||
throw exception::eval_error("Error with numeric operator calling: " + t_oper_string);
|
||||
}
|
||||
@@ -110,6 +109,9 @@ namespace chaiscript
|
||||
throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, e.functions, false, t_ss);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Operators::Opers m_oper;
|
||||
};
|
||||
|
||||
struct Int_AST_Node : public AST_Node {
|
||||
@@ -316,8 +318,6 @@ namespace chaiscript
|
||||
}
|
||||
catch(const exception::bad_boxed_cast &){
|
||||
// handle the case where there is only 1 function to try to call and dispatch fails on it
|
||||
std::vector<Const_Proxy_Function> funcs;
|
||||
funcs.push_back(fn);
|
||||
throw exception::eval_error("Error calling function '" + this->children[0]->text + "'", params, {fn}, false, t_ss);
|
||||
}
|
||||
catch(const exception::arity_error &e){
|
||||
@@ -352,6 +352,28 @@ namespace chaiscript
|
||||
|
||||
};
|
||||
|
||||
struct Arg_AST_Node : public AST_Node {
|
||||
public:
|
||||
Arg_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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(std::move(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_AST_Node() {}
|
||||
|
||||
virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE
|
||||
{
|
||||
std::ostringstream oss;
|
||||
for (size_t j = 0; j < this->children.size(); ++j) {
|
||||
if (j != 0)
|
||||
{
|
||||
oss << " ";
|
||||
}
|
||||
|
||||
oss << this->children[j]->pretty_print();
|
||||
}
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
};
|
||||
|
||||
struct Arg_List_AST_Node : public AST_Node {
|
||||
public:
|
||||
Arg_List_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
||||
@@ -372,6 +394,53 @@ namespace chaiscript
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
static std::string get_arg_name(const AST_NodePtr &t_node) {
|
||||
if (t_node->children.empty())
|
||||
{
|
||||
return t_node->text;
|
||||
} else if (t_node->children.size() == 1) {
|
||||
return t_node->children[0]->text;
|
||||
} else {
|
||||
return t_node->children[1]->text;
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<std::string> get_arg_names(const AST_NodePtr &t_node) {
|
||||
std::vector<std::string> retval;
|
||||
|
||||
for (const auto &node : t_node->children)
|
||||
{
|
||||
retval.push_back(get_arg_name(node));
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static std::pair<std::string, Type_Info> get_arg_type(const AST_NodePtr &t_node, chaiscript::detail::Dispatch_Engine &t_ss)
|
||||
{
|
||||
if (t_node->children.size() < 2)
|
||||
{
|
||||
return std::pair<std::string, Type_Info>();
|
||||
} else {
|
||||
try {
|
||||
return std::pair<std::string, Type_Info>(t_node->children[0]->text, t_ss.get_type(t_node->children[0]->text));
|
||||
} catch (const std::range_error &) {
|
||||
return std::pair<std::string, Type_Info>(t_node->children[0]->text, Type_Info());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static dispatch::Param_Types get_arg_types(const AST_NodePtr &t_node, chaiscript::detail::Dispatch_Engine &t_ss) {
|
||||
std::vector<std::pair<std::string, Type_Info>> retval;
|
||||
|
||||
for (const auto &child : t_node->children)
|
||||
{
|
||||
retval.push_back(get_arg_type(child, t_ss));
|
||||
}
|
||||
|
||||
return dispatch::Param_Types(std::move(retval));
|
||||
}
|
||||
};
|
||||
|
||||
struct Equation_AST_Node : public AST_Node {
|
||||
@@ -382,6 +451,7 @@ namespace chaiscript
|
||||
|
||||
virtual ~Equation_AST_Node() {}
|
||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
||||
Boxed_Value retval = this->children.back()->eval(t_ss);
|
||||
|
||||
|
||||
@@ -454,7 +524,7 @@ namespace chaiscript
|
||||
{
|
||||
return this->children[0]->eval(t_ss);
|
||||
} else {
|
||||
std::string idname = this->children[0]->text;
|
||||
const std::string &idname = this->children[0]->text;
|
||||
|
||||
Boxed_Value bv;
|
||||
try {
|
||||
@@ -477,72 +547,6 @@ namespace chaiscript
|
||||
|
||||
};
|
||||
|
||||
struct Comparison_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Comparison_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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(std::move(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() {}
|
||||
};
|
||||
|
||||
struct Addition_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Addition_AST_Node(std::string t_ast_node_text = "+",
|
||||
const std::shared_ptr<std::string> &t_fname=std::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(std::move(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 Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
return do_oper(t_ss, Operators::sum, "+", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
||||
}
|
||||
};
|
||||
|
||||
struct Subtraction_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Subtraction_AST_Node(std::string t_ast_node_text = "-",
|
||||
const std::shared_ptr<std::string> &t_fname=std::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(std::move(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 Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
return do_oper(t_ss, Operators::difference, "-", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
||||
}
|
||||
};
|
||||
|
||||
struct Multiplication_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Multiplication_AST_Node(std::string t_ast_node_text = "*",
|
||||
const std::shared_ptr<std::string> &t_fname=std::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(std::move(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 Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
return do_oper(t_ss, Operators::product, "*", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
||||
}
|
||||
};
|
||||
|
||||
struct Division_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Division_AST_Node(std::string t_ast_node_text = "/",
|
||||
const std::shared_ptr<std::string> &t_fname=std::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(std::move(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 Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
return do_oper(t_ss, Operators::quotient, "/", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
||||
}
|
||||
};
|
||||
|
||||
struct Modulus_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Modulus_AST_Node(std::string t_ast_node_text = "%",
|
||||
const std::shared_ptr<std::string> &t_fname=std::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(std::move(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 Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||
return do_oper(t_ss, Operators::remainder, "%", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
||||
}
|
||||
};
|
||||
|
||||
struct Array_Call_AST_Node : public AST_Node {
|
||||
public:
|
||||
@@ -595,8 +599,8 @@ namespace chaiscript
|
||||
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 Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
||||
Boxed_Value retval = this->children[0]->eval(t_ss);
|
||||
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
||||
Boxed_Value retval = this->children[0]->eval(t_ss);
|
||||
|
||||
if (this->children.size() > 1) {
|
||||
for (size_t i = 2; i < this->children.size(); i+=2) {
|
||||
@@ -704,13 +708,12 @@ namespace chaiscript
|
||||
|
||||
size_t numparams = 0;
|
||||
|
||||
dispatch::Param_Types param_types;
|
||||
|
||||
if (!this->children.empty() && (this->children[0]->identifier == AST_Node_Type::Arg_List)) {
|
||||
numparams = this->children[0]->children.size();
|
||||
|
||||
for (const auto &child : this->children[0]->children)
|
||||
{
|
||||
t_param_names.push_back(child->text);
|
||||
}
|
||||
t_param_names = Arg_List_AST_Node::get_arg_names(this->children[0]);
|
||||
param_types = Arg_List_AST_Node::get_arg_types(this->children[0], t_ss);
|
||||
}
|
||||
|
||||
const auto &lambda_node = this->children.back();
|
||||
@@ -720,7 +723,7 @@ namespace chaiscript
|
||||
{
|
||||
return detail::eval_function(t_ss, lambda_node, t_param_names, t_params);
|
||||
},
|
||||
static_cast<int>(numparams), lambda_node)));
|
||||
static_cast<int>(numparams), lambda_node, param_types)));
|
||||
}
|
||||
|
||||
};
|
||||
@@ -764,13 +767,12 @@ namespace chaiscript
|
||||
size_t numparams = 0;
|
||||
AST_NodePtr guardnode;
|
||||
|
||||
dispatch::Param_Types param_types;
|
||||
|
||||
if ((this->children.size() > 2) && (this->children[1]->identifier == AST_Node_Type::Arg_List)) {
|
||||
numparams = this->children[1]->children.size();
|
||||
|
||||
for (const auto &child : this->children[1]->children)
|
||||
{
|
||||
t_param_names.push_back(child->text);
|
||||
}
|
||||
t_param_names = Arg_List_AST_Node::get_arg_names(this->children[1]);
|
||||
param_types = Arg_List_AST_Node::get_arg_types(this->children[1], t_ss);
|
||||
|
||||
if (this->children.size() > 3) {
|
||||
guardnode = this->children[2];
|
||||
@@ -803,7 +805,7 @@ namespace chaiscript
|
||||
{
|
||||
return detail::eval_function(t_ss, func_node, t_param_names, t_params);
|
||||
}, static_cast<int>(numparams), this->children.back(),
|
||||
l_annotation, guard)), l_function_name);
|
||||
param_types, l_annotation, guard)), l_function_name);
|
||||
}
|
||||
catch (const exception::reserved_word_error &e) {
|
||||
throw exception::eval_error("Reserved word used as function name '" + e.word() + "'");
|
||||
@@ -1144,8 +1146,9 @@ namespace chaiscript
|
||||
|
||||
struct Prefix_AST_Node : public AST_Node {
|
||||
public:
|
||||
Prefix_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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(std::move(t_ast_node_text), AST_Node_Type::Prefix, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
|
||||
Prefix_AST_Node(Operators::Opers t_oper) :
|
||||
AST_Node("", AST_Node_Type::Prefix, std::make_shared<std::string>(""), 0, 0, 0, 0),
|
||||
m_oper(t_oper)
|
||||
{ }
|
||||
|
||||
virtual ~Prefix_AST_Node() {}
|
||||
@@ -1153,12 +1156,11 @@ namespace chaiscript
|
||||
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
||||
Boxed_Value bv(this->children[1]->eval(t_ss));
|
||||
|
||||
Operators::Opers oper = Operators::to_operator(children[0]->text, true);
|
||||
try {
|
||||
// short circuit arithmetic operations
|
||||
if (bv.get_type_info().is_arithmetic() && oper != Operators::invalid)
|
||||
if (m_oper != Operators::invalid && bv.get_type_info().is_arithmetic())
|
||||
{
|
||||
return Boxed_Number::do_oper(oper, std::move(bv));
|
||||
return Boxed_Number::do_oper(m_oper, std::move(bv));
|
||||
} else {
|
||||
chaiscript::eval::detail::Stack_Push_Pop spp(t_ss);
|
||||
fpp.save_params({bv});
|
||||
@@ -1169,6 +1171,8 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Operators::Opers m_oper;
|
||||
};
|
||||
|
||||
struct Break_AST_Node : public AST_Node {
|
||||
@@ -1252,11 +1256,74 @@ namespace chaiscript
|
||||
Try_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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::Try, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||
virtual ~Try_AST_Node() {}
|
||||
|
||||
Boxed_Value handle_exception(chaiscript::detail::Dispatch_Engine &t_ss, const Boxed_Value &t_except) const
|
||||
{
|
||||
Boxed_Value retval;
|
||||
|
||||
size_t end_point = this->children.size();
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
assert(end_point > 0);
|
||||
end_point = this->children.size() - 1;
|
||||
}
|
||||
for (size_t i = 1; i < end_point; ++i) {
|
||||
chaiscript::eval::detail::Scope_Push_Pop catchscope(t_ss);
|
||||
AST_NodePtr catch_block = this->children[i];
|
||||
|
||||
if (catch_block->children.size() == 1) {
|
||||
//No variable capture, no guards
|
||||
retval = catch_block->children[0]->eval(t_ss);
|
||||
break;
|
||||
} else if (catch_block->children.size() == 2 || catch_block->children.size() == 3) {
|
||||
const auto name = Arg_List_AST_Node::get_arg_name(catch_block->children[0]);
|
||||
|
||||
if (dispatch::Param_Types(
|
||||
std::vector<std::pair<std::string, Type_Info>>{Arg_List_AST_Node::get_arg_type(catch_block->children[0], t_ss)}
|
||||
).match(std::vector<Boxed_Value>{t_except}, t_ss.conversions()))
|
||||
{
|
||||
t_ss.add_object(name, t_except);
|
||||
|
||||
if (catch_block->children.size() == 2) {
|
||||
//Variable capture, no guards
|
||||
retval = catch_block->children[1]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
else if (catch_block->children.size() == 3) {
|
||||
//Variable capture, guards
|
||||
|
||||
bool guard = false;
|
||||
try {
|
||||
guard = boxed_cast<bool>(catch_block->children[1]->eval(t_ss));
|
||||
} catch (const exception::bad_boxed_cast &) {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
throw exception::eval_error("Guard condition not boolean");
|
||||
}
|
||||
if (guard) {
|
||||
retval = catch_block->children[2]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
throw exception::eval_error("Internal error: catch block size unrecognized");
|
||||
}
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
||||
Boxed_Value retval;
|
||||
|
||||
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
|
||||
|
||||
|
||||
try {
|
||||
retval = this->children[0]->eval(t_ss);
|
||||
}
|
||||
@@ -1267,98 +1334,11 @@ namespace chaiscript
|
||||
throw;
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
Boxed_Value except(std::ref(e));
|
||||
retval = handle_exception(t_ss, Boxed_Value(std::ref(e)));
|
||||
|
||||
size_t end_point = this->children.size();
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
assert(end_point > 0);
|
||||
end_point = this->children.size() - 1;
|
||||
}
|
||||
for (size_t i = 1; i < end_point; ++i) {
|
||||
chaiscript::eval::detail::Scope_Push_Pop catchscope(t_ss);
|
||||
AST_NodePtr catch_block = this->children[i];
|
||||
|
||||
if (catch_block->children.size() == 1) {
|
||||
//No variable capture, no guards
|
||||
retval = catch_block->children[0]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
else if (catch_block->children.size() == 2) {
|
||||
//Variable capture, no guards
|
||||
t_ss.add_object(catch_block->children[0]->text, except);
|
||||
retval = catch_block->children[1]->eval(t_ss);
|
||||
|
||||
break;
|
||||
}
|
||||
else if (catch_block->children.size() == 3) {
|
||||
//Variable capture, no guards
|
||||
t_ss.add_object(catch_block->children[0]->text, except);
|
||||
|
||||
bool guard = false;
|
||||
try {
|
||||
guard = boxed_cast<bool>(catch_block->children[1]->eval(t_ss));
|
||||
} catch (const exception::bad_boxed_cast &) {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
throw exception::eval_error("Guard condition not boolean");
|
||||
}
|
||||
if (guard) {
|
||||
retval = catch_block->children[2]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
throw exception::eval_error("Internal error: catch block size unrecognized");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Boxed_Value &except) {
|
||||
for (size_t i = 1; i < this->children.size(); ++i) {
|
||||
chaiscript::eval::detail::Scope_Push_Pop catchscope(t_ss);
|
||||
const auto &catch_block = this->children[i];
|
||||
|
||||
if (catch_block->children.size() == 1) {
|
||||
//No variable capture, no guards
|
||||
retval = catch_block->children[0]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
else if (catch_block->children.size() == 2) {
|
||||
//Variable capture, no guards
|
||||
t_ss.add_object(catch_block->children[0]->text, except);
|
||||
retval = catch_block->children[1]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
else if (catch_block->children.size() == 3) {
|
||||
//Variable capture, guards
|
||||
t_ss.add_object(catch_block->children[0]->text, except);
|
||||
|
||||
bool guard;
|
||||
try {
|
||||
guard = boxed_cast<bool>(catch_block->children[1]->eval(t_ss));
|
||||
}
|
||||
catch (const exception::bad_boxed_cast &) {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
|
||||
throw exception::eval_error("Guard condition not boolean");
|
||||
}
|
||||
if (guard) {
|
||||
retval = catch_block->children[2]->eval(t_ss);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
throw exception::eval_error("Internal error: catch block size unrecognized");
|
||||
}
|
||||
}
|
||||
catch (Boxed_Value &e) {
|
||||
retval = handle_exception(t_ss, e);
|
||||
}
|
||||
catch (...) {
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
@@ -1367,6 +1347,7 @@ namespace chaiscript
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
if (this->children.back()->identifier == AST_Node_Type::Finally) {
|
||||
retval = this->children.back()->children[0]->eval(t_ss);
|
||||
}
|
||||
@@ -1399,29 +1380,29 @@ namespace chaiscript
|
||||
|
||||
AST_NodePtr guardnode;
|
||||
|
||||
auto d = t_ss.get_parent_locals();
|
||||
auto itr = d.find("_current_class_name");
|
||||
int class_offset = 0;
|
||||
if (itr != d.end()) class_offset = -1;
|
||||
const auto d = t_ss.get_parent_locals();
|
||||
const auto itr = d.find("_current_class_name");
|
||||
const auto class_offset = (itr != d.end())?-1:0;
|
||||
const std::string & class_name = (itr != d.end())?std::string(boxed_cast<std::string>(itr->second)):this->children[0]->text;
|
||||
|
||||
//The first param of a method is always the implied this ptr.
|
||||
std::vector<std::string> t_param_names{"this"};
|
||||
dispatch::Param_Types param_types;
|
||||
|
||||
if ((this->children.size() > static_cast<size_t>(3 + class_offset)) && (this->children[(2 + class_offset)]->identifier == AST_Node_Type::Arg_List)) {
|
||||
for (const auto &child : this->children[(2 + class_offset)]->children) {
|
||||
t_param_names.push_back(child->text);
|
||||
}
|
||||
if ((this->children.size() > static_cast<size_t>(3 + class_offset)) && (this->children[static_cast<size_t>(2 + class_offset)]->identifier == AST_Node_Type::Arg_List)) {
|
||||
auto args = Arg_List_AST_Node::get_arg_names(this->children[static_cast<size_t>(2 + class_offset)]);
|
||||
t_param_names.insert(t_param_names.end(), args.begin(), args.end());
|
||||
param_types = Arg_List_AST_Node::get_arg_types(this->children[static_cast<size_t>(2 + class_offset)], t_ss);
|
||||
|
||||
if (this->children.size() > static_cast<size_t>(4 + class_offset)) {
|
||||
guardnode = this->children[(3 + class_offset)];
|
||||
guardnode = this->children[static_cast<size_t>(3 + class_offset)];
|
||||
}
|
||||
}
|
||||
else {
|
||||
//no parameters
|
||||
|
||||
if (this->children.size() > static_cast<size_t>(3 + class_offset)) {
|
||||
guardnode = this->children[(2 + class_offset)];
|
||||
guardnode = this->children[static_cast<size_t>(2 + class_offset)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1438,32 +1419,38 @@ namespace chaiscript
|
||||
try {
|
||||
const std::string & l_annotation = this->annotation?this->annotation->text:"";
|
||||
|
||||
const std::string & function_name = this->children[(1 + class_offset)]->text;
|
||||
const std::string & function_name = this->children[static_cast<size_t>(1 + class_offset)]->text;
|
||||
|
||||
if (function_name == class_name) {
|
||||
param_types.push_front(class_name, Type_Info());
|
||||
t_ss.add(std::make_shared<dispatch::detail::Dynamic_Object_Constructor>(class_name, std::make_shared<dispatch::Dynamic_Proxy_Function>(std::bind(chaiscript::eval::detail::eval_function,
|
||||
std::ref(t_ss), this->children.back(), t_param_names, std::placeholders::_1),
|
||||
static_cast<int>(numparams), this->children.back(), l_annotation, guard)),
|
||||
static_cast<int>(numparams), this->children.back(), param_types, l_annotation, guard)),
|
||||
function_name);
|
||||
|
||||
}
|
||||
else {
|
||||
try {
|
||||
// Do know type name
|
||||
// Do know type name (if this line fails, the catch block is called and the
|
||||
// other version is called, with no Type_Info object known)
|
||||
auto type = t_ss.get_type(class_name);
|
||||
param_types.push_front(class_name, type);
|
||||
|
||||
t_ss.add(
|
||||
std::make_shared<dispatch::detail::Dynamic_Object_Function>(class_name,
|
||||
std::make_shared<dispatch::Dynamic_Proxy_Function>(std::bind(chaiscript::eval::detail::eval_function,
|
||||
std::ref(t_ss), this->children.back(),
|
||||
t_param_names, std::placeholders::_1), static_cast<int>(numparams), this->children.back(),
|
||||
l_annotation, guard), t_ss.get_type(class_name)), function_name);
|
||||
param_types, l_annotation, guard), type), function_name);
|
||||
} catch (const std::range_error &) {
|
||||
param_types.push_front(class_name, Type_Info());
|
||||
// Do not know type name
|
||||
t_ss.add(
|
||||
std::make_shared<dispatch::detail::Dynamic_Object_Function>(class_name,
|
||||
std::make_shared<dispatch::Dynamic_Proxy_Function>(std::bind(chaiscript::eval::detail::eval_function,
|
||||
std::ref(t_ss), this->children.back(),
|
||||
t_param_names, std::placeholders::_1), static_cast<int>(numparams), this->children.back(),
|
||||
l_annotation, guard)), function_name);
|
||||
param_types, l_annotation, guard)), function_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1486,24 +1473,23 @@ namespace chaiscript
|
||||
{
|
||||
const auto &d = t_ss.get_parent_locals();
|
||||
const auto itr = d.find("_current_class_name");
|
||||
int class_offset = 0;
|
||||
if (itr != d.end()) class_offset = -1;
|
||||
const auto class_offset = (itr != d.end())?-1:0;
|
||||
std::string class_name = (itr != d.end())?std::string(boxed_cast<std::string>(itr->second)):this->children[0]->text;
|
||||
|
||||
try {
|
||||
t_ss.add(
|
||||
std::make_shared<dispatch::detail::Dynamic_Object_Function>(
|
||||
class_name,
|
||||
std::move(class_name),
|
||||
fun(std::function<Boxed_Value (dispatch::Dynamic_Object &)>(std::bind(&dispatch::Dynamic_Object::get_attr,
|
||||
std::placeholders::_1,
|
||||
this->children[(1 + class_offset)]->text
|
||||
this->children[static_cast<size_t>(1 + class_offset)]->text
|
||||
))
|
||||
)
|
||||
), this->children[(1 + class_offset)]->text);
|
||||
), this->children[static_cast<size_t>(1 + class_offset)]->text);
|
||||
|
||||
}
|
||||
catch (const exception::reserved_word_error &) {
|
||||
throw exception::eval_error("Reserved word used as attribute '" + this->children[(1 + class_offset)]->text + "'");
|
||||
throw exception::eval_error("Reserved word used as attribute '" + this->children[static_cast<size_t>(1 + class_offset)]->text + "'");
|
||||
} catch (const exception::name_conflict_error &e) {
|
||||
throw exception::eval_error("Attribute redefined '" + e.name() + "'");
|
||||
}
|
||||
@@ -1512,40 +1498,6 @@ namespace chaiscript
|
||||
|
||||
};
|
||||
|
||||
struct Shift_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Shift_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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, AST_Node_Type::Shift, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||
virtual ~Shift_AST_Node() {}
|
||||
};
|
||||
|
||||
struct Equality_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Equality_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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, AST_Node_Type::Equality, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||
virtual ~Equality_AST_Node() {}
|
||||
};
|
||||
|
||||
struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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, AST_Node_Type::Bitwise_And, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||
virtual ~Bitwise_And_AST_Node() {}
|
||||
};
|
||||
|
||||
struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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, AST_Node_Type::Bitwise_Xor, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||
virtual ~Bitwise_Xor_AST_Node() {}
|
||||
};
|
||||
|
||||
struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node {
|
||||
public:
|
||||
Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::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, AST_Node_Type::Bitwise_Or, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||
virtual ~Bitwise_Or_AST_Node() {}
|
||||
};
|
||||
|
||||
struct Logical_And_AST_Node : public AST_Node {
|
||||
public:
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_PARSER_HPP_
|
||||
@@ -171,7 +171,7 @@ namespace chaiscript
|
||||
/// Prints the parsed ast_nodes as a tree
|
||||
/*
|
||||
void debug_print(AST_NodePtr t, std::string prepend = "") {
|
||||
std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << std::endl;
|
||||
std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << '\n';
|
||||
for (unsigned int j = 0; j < t->children.size(); ++j) {
|
||||
debug_print(t->children[j], prepend + " ");
|
||||
}
|
||||
@@ -223,8 +223,8 @@ namespace chaiscript
|
||||
t_t->end.column = pos_col_stop;
|
||||
|
||||
if (is_deep) {
|
||||
t_t->children.assign(m_match_stack.begin() + t_match_start, m_match_stack.end());
|
||||
m_match_stack.erase(m_match_stack.begin() + t_match_start, m_match_stack.end());
|
||||
t_t->children.assign(m_match_stack.begin() + static_cast<int>(t_match_start), m_match_stack.end());
|
||||
m_match_stack.erase(m_match_stack.begin() + static_cast<int>(t_match_start), m_match_stack.end());
|
||||
}
|
||||
|
||||
/// \todo fix the fact that a successful match that captured no ast_nodes doesn't have any real start position
|
||||
@@ -431,21 +431,13 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
std::stringstream ss(t_val.substr(0, i));
|
||||
|
||||
if (float_)
|
||||
{
|
||||
float f;
|
||||
ss >> f;
|
||||
return const_var(f);
|
||||
return const_var(std::stof(t_val.substr(0,i)));
|
||||
} else if (long_) {
|
||||
long double f;
|
||||
ss >> f;
|
||||
return const_var(f);
|
||||
return const_var(std::stold(t_val.substr(0,i)));
|
||||
} else {
|
||||
double f;
|
||||
ss >> f;
|
||||
return const_var(f);
|
||||
return const_var(std::stod(t_val.substr(0,i)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -699,6 +691,25 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads an argument from input
|
||||
bool Arg() {
|
||||
const auto prev_stack_top = m_match_stack.size();
|
||||
SkipWS();
|
||||
|
||||
if (!Id(true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SkipWS();
|
||||
Id(true);
|
||||
|
||||
build_match(std::make_shared<eval::Arg_AST_Node>(), prev_stack_top);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Checks for a node annotation of the form "#<annotation>"
|
||||
bool Annotation() {
|
||||
SkipWS();
|
||||
@@ -787,7 +798,7 @@ namespace chaiscript
|
||||
//If we've seen previous interpolation, add on instead of making a new one
|
||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||
|
||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
||||
build_match(std::make_shared<eval::Binary_Operator_AST_Node>("+"), prev_stack_top);
|
||||
} else {
|
||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||
}
|
||||
@@ -824,7 +835,7 @@ namespace chaiscript
|
||||
build_match(std::make_shared<eval::Inplace_Fun_Call_AST_Node>(), ev_stack_top);
|
||||
build_match(std::make_shared<eval::Arg_List_AST_Node>(), ev_stack_top);
|
||||
build_match(std::make_shared<eval::Fun_Call_AST_Node>(), tostr_stack_top);
|
||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
||||
build_match(std::make_shared<eval::Binary_Operator_AST_Node>("+"), prev_stack_top);
|
||||
} else {
|
||||
throw exception::eval_error("Unclosed in-string eval", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
@@ -867,7 +878,7 @@ namespace chaiscript
|
||||
if (is_interpolated) {
|
||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||
|
||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
||||
build_match(std::make_shared<eval::Binary_Operator_AST_Node>("+"), prev_stack_top);
|
||||
} else {
|
||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||
}
|
||||
@@ -1109,6 +1120,33 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads a comma-separated list of values from input, for function declarations
|
||||
bool Decl_Arg_List() {
|
||||
SkipWS(true);
|
||||
bool retval = false;
|
||||
|
||||
const auto prev_stack_top = m_match_stack.size();
|
||||
|
||||
if (Arg()) {
|
||||
retval = true;
|
||||
while (Eol()) {}
|
||||
if (Char(',')) {
|
||||
do {
|
||||
while (Eol()) {}
|
||||
if (!Arg()) {
|
||||
throw exception::eval_error("Unexpected value in parameter list", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
} while (Char(','));
|
||||
}
|
||||
build_match(std::make_shared<eval::Arg_List_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
|
||||
SkipWS(true);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/// Reads a comma-separated list of values from input
|
||||
bool Arg_List() {
|
||||
SkipWS(true);
|
||||
@@ -1186,7 +1224,7 @@ namespace chaiscript
|
||||
retval = true;
|
||||
|
||||
if (Char('(')) {
|
||||
Arg_List();
|
||||
Decl_Arg_List();
|
||||
if (!Char(')')) {
|
||||
throw exception::eval_error("Incomplete anonymous function", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
@@ -1236,7 +1274,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
if (Char('(')) {
|
||||
Arg_List();
|
||||
Decl_Arg_List();
|
||||
if (!Char(')')) {
|
||||
throw exception::eval_error("Incomplete function definition", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
@@ -1291,7 +1329,7 @@ namespace chaiscript
|
||||
if (Keyword("catch", false)) {
|
||||
const auto catch_stack_top = m_match_stack.size();
|
||||
if (Char('(')) {
|
||||
if (!(Id(true) && Char(')'))) {
|
||||
if (!(Arg() && Char(')'))) {
|
||||
throw exception::eval_error("Incomplete 'catch' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
if (Char(':')) {
|
||||
@@ -1835,72 +1873,24 @@ namespace chaiscript
|
||||
|
||||
/// Reads a unary prefixed expression from input
|
||||
bool Prefix() {
|
||||
bool retval = false;
|
||||
|
||||
const auto prev_stack_top = m_match_stack.size();
|
||||
const std::vector<std::string> prefix_opers{"++", "--", "-", "+", "!", "~", "&"};
|
||||
|
||||
if (Symbol("++", true)) {
|
||||
retval = true;
|
||||
for (const auto &oper : prefix_opers)
|
||||
{
|
||||
bool is_char = oper.size() == 1;
|
||||
if ((is_char && Char(oper[0], true)) || (!is_char && Symbol(oper.c_str(), true)))
|
||||
{
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete prefix '" + oper + "' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete '++' expression", File_Position(m_line, m_col), *m_filename);
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(Operators::to_operator(oper, true)), prev_stack_top);
|
||||
return true;
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
} else if (Symbol("--", true)) {
|
||||
retval = true;
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete '--' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
} else if (Char('-', true)) {
|
||||
retval = true;
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete unary '-' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
} else if (Char('+', true)) {
|
||||
retval = true;
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete unary '+' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
else if (Char('!', true)) {
|
||||
retval = true;
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete '!' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
else if (Char('~', true)) {
|
||||
retval = true;
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
else if (Char('&', true)) {
|
||||
retval = true;
|
||||
|
||||
if (!Operator(m_operators.size()-1)) {
|
||||
throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
|
||||
return retval;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Parses any of a group of 'value' style ast_node groups from input
|
||||
@@ -1923,7 +1913,6 @@ namespace chaiscript
|
||||
|
||||
bool Operator(const size_t t_precedence = 0) {
|
||||
bool retval = false;
|
||||
AST_NodePtr oper;
|
||||
const auto prev_stack_top = m_match_stack.size();
|
||||
|
||||
if (t_precedence < m_operators.size()) {
|
||||
@@ -1938,10 +1927,9 @@ namespace chaiscript
|
||||
File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
|
||||
AST_NodePtr oper = m_match_stack.at(m_match_stack.size()-2);
|
||||
|
||||
switch (m_operators[t_precedence]) {
|
||||
case(AST_Node_Type::Comparison) :
|
||||
build_match(std::make_shared<eval::Comparison_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
case(AST_Node_Type::Ternary_Cond) :
|
||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
||||
m_match_stack.begin() + m_match_stack.size() - 1);
|
||||
@@ -1959,52 +1947,27 @@ namespace chaiscript
|
||||
File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
break;
|
||||
|
||||
case(AST_Node_Type::Addition) :
|
||||
oper = m_match_stack.at(m_match_stack.size()-2);
|
||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
||||
m_match_stack.begin() + m_match_stack.size() - 1);
|
||||
if (oper->text == "+") {
|
||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
else if (oper->text == "-") {
|
||||
build_match(std::make_shared<eval::Subtraction_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
break;
|
||||
case(AST_Node_Type::Multiplication) :
|
||||
oper = m_match_stack.at(m_match_stack.size()-2);
|
||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
||||
m_match_stack.begin() + m_match_stack.size() - 1);
|
||||
if (oper->text == "*") {
|
||||
build_match(std::make_shared<eval::Multiplication_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
else if (oper->text == "/") {
|
||||
build_match(std::make_shared<eval::Division_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
else if (oper->text == "%") {
|
||||
build_match(std::make_shared<eval::Modulus_AST_Node>(), prev_stack_top);
|
||||
}
|
||||
break;
|
||||
case(AST_Node_Type::Shift) :
|
||||
build_match(std::make_shared<eval::Shift_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
case(AST_Node_Type::Equality) :
|
||||
build_match(std::make_shared<eval::Equality_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
case(AST_Node_Type::Bitwise_And) :
|
||||
build_match(std::make_shared<eval::Bitwise_And_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
case(AST_Node_Type::Bitwise_Xor) :
|
||||
build_match(std::make_shared<eval::Bitwise_Xor_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
case(AST_Node_Type::Bitwise_Or) :
|
||||
build_match(std::make_shared<eval::Bitwise_Or_AST_Node>(), prev_stack_top);
|
||||
case(AST_Node_Type::Comparison) :
|
||||
assert(m_match_stack.size() > 1);
|
||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2, m_match_stack.begin() + m_match_stack.size() - 1);
|
||||
build_match(std::make_shared<eval::Binary_Operator_AST_Node>(oper->text), prev_stack_top);
|
||||
break;
|
||||
|
||||
case(AST_Node_Type::Logical_And) :
|
||||
build_match(std::make_shared<eval::Logical_And_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
case(AST_Node_Type::Logical_Or) :
|
||||
build_match(std::make_shared<eval::Logical_Or_AST_Node>(), prev_stack_top);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_line, m_col), *m_filename);
|
||||
}
|
||||
@@ -2111,14 +2074,7 @@ namespace chaiscript
|
||||
while (has_more) {
|
||||
const auto prev_line = m_line;
|
||||
const auto prev_col = m_col;
|
||||
if (Def(true)) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
} else if (Var_Decl(true)) {
|
||||
if (Def(true) || Var_Decl(true)) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
@@ -2147,7 +2103,7 @@ namespace chaiscript
|
||||
while (has_more) {
|
||||
int prev_line = m_line;
|
||||
int prev_col = m_col;
|
||||
if (Def()) {
|
||||
if (Def() || Try() || If() || While() || Class() || For() || Switch()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
@@ -2155,55 +2111,7 @@ namespace chaiscript
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (Try()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (If()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (While()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (Class()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (For()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (Switch()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (Return()) {
|
||||
else if (Return() || Break() || Continue() || Equation()) {
|
||||
if (!saw_eol) {
|
||||
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||
}
|
||||
@@ -2211,36 +2119,7 @@ namespace chaiscript
|
||||
retval = true;
|
||||
saw_eol = false;
|
||||
}
|
||||
else if (Break()) {
|
||||
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 (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 (Block()) {
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
}
|
||||
else if (Equation()) {
|
||||
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 (Eol()) {
|
||||
else if (Block() || Eol()) {
|
||||
has_more = true;
|
||||
retval = true;
|
||||
saw_eol = true;
|
||||
|
@@ -455,37 +455,37 @@ def zip(x, y) {
|
||||
|
||||
|
||||
# Returns the position of the second value string in the first value string
|
||||
def string::find(substr) : is_type(substr, "string") {
|
||||
def string::find(string substr) {
|
||||
find(this, substr, size_t(0));
|
||||
}
|
||||
|
||||
|
||||
# Returns the position of last match of the second value string in the first value string
|
||||
def string::rfind(substr) : is_type(substr, "string") {
|
||||
def string::rfind(string substr) {
|
||||
rfind(this, substr, size_t(-1));
|
||||
}
|
||||
|
||||
|
||||
# Returns the position of the first match of elements in the second value string in the first value string
|
||||
def string::find_first_of(list) : is_type(list, "string") {
|
||||
def string::find_first_of(string list) {
|
||||
find_first_of(this, list, size_t(0));
|
||||
}
|
||||
|
||||
|
||||
# Returns the position of the last match of elements in the second value string in the first value string
|
||||
def string::find_last_of(list) : is_type(list, "string") {
|
||||
def string::find_last_of(string list) {
|
||||
find_last_of(this, list, size_t(-1));
|
||||
}
|
||||
|
||||
|
||||
# Returns the position of the first non-matching element in the second value string in the first value string
|
||||
def string::find_first_not_of(list) : is_type(list, "string") {
|
||||
def string::find_first_not_of(string list) {
|
||||
find_first_not_of(this, list, size_t(0));
|
||||
}
|
||||
|
||||
|
||||
# Returns the position of the last non-matching element in the second value string in the first value string
|
||||
def string::find_last_not_of(list) : is_type(list, "string") {
|
||||
def string::find_last_not_of(string list) {
|
||||
find_last_not_of(this, list, size_t(-1));
|
||||
}
|
||||
|
||||
@@ -505,7 +505,7 @@ def string::trim() {
|
||||
}
|
||||
|
||||
|
||||
def find(container, value, compare_func) : call_exists(range, container) && is_type(compare_func, "Function") {
|
||||
def find(container, value, Function compare_func) : call_exists(range, container) {
|
||||
auto range := range(container);
|
||||
while (!range.empty()) {
|
||||
if (compare_func(range.front(), value)) {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_UTILITY_UTILITY_HPP_
|
||||
|
@@ -1,4 +1,4 @@
|
||||
Copyright 2009-2014 Jason Turner
|
||||
Copyright 2009-2015 Jason Turner
|
||||
Copyright 2009-2012 Jonathan Turner.
|
||||
|
||||
All Rights Reserved.
|
||||
|
@@ -8,7 +8,7 @@ ChaiScript
|
||||
http://www.chaiscript.com
|
||||
|
||||
(c) 2009-2012 Jonathan Turner
|
||||
(c) 2009-2014 Jason Turner
|
||||
(c) 2009-2015 Jason Turner
|
||||
|
||||
Release under the BSD license, see "license.txt" for details.
|
||||
|
||||
@@ -16,6 +16,8 @@ Release under the BSD license, see "license.txt" for details.
|
||||
Introduction
|
||||
============
|
||||
|
||||
[](https://gitter.im/ChaiScript/ChaiScript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
ChaiScript is one of the only embedded scripting language designed from the
|
||||
ground up to directly target C++ and take advantage of modern C++ development
|
||||
techniques, working with the developer like he expects it to work. Being a
|
||||
@@ -33,9 +35,10 @@ Requirements
|
||||
============
|
||||
|
||||
ChaiScript requires a C++11 compiler to build with support for variadic
|
||||
templates. It has been tested with gcc 4.7 and clang 3.1 (with libcxx). MacOS
|
||||
templates. It has been tested with gcc 4.6 and clang 3.1 (with libcxx). MacOS
|
||||
10.8 (Mountain Lion) is also known to support the C++11 build with Apple's
|
||||
clang 4.0.
|
||||
clang 4.0. MSVC 2013 or newer is supports also. For more information see the build
|
||||
[dashboard](http://chaiscript.com/ChaiScript-BuildResults/index.html).
|
||||
|
||||
Usage
|
||||
=====
|
||||
|
@@ -1,6 +1,36 @@
|
||||
Notes:
|
||||
=======
|
||||
Current Version: 5.5.0
|
||||
Current Version: 5.6.0
|
||||
|
||||
### Changes since 5.5.1
|
||||
* Throw exception on integer divide by 0
|
||||
* Add optional type specification to function declarations
|
||||
|
||||
```
|
||||
def func(int i, j, double k) {
|
||||
// i must be an int.
|
||||
// j can be anything
|
||||
// k must be a double
|
||||
// normal conversion rules still apply
|
||||
}
|
||||
```
|
||||
* Many minor fixes for compiler warnings
|
||||
* Add support for `std::future` and `std::async`
|
||||
```
|
||||
var f := async(someFunction);
|
||||
var f2 := async(someFunction2);
|
||||
|
||||
// someFunction and someFunction2 are running in parallel now
|
||||
f.get();
|
||||
f2.get();
|
||||
```
|
||||
* Fully support r-value returns, supporting move-only objects and reducing object copies
|
||||
|
||||
|
||||
### Changes since 5.5.0
|
||||
* 30% performance increase
|
||||
* Fix handling of object stack, resulting in greatly reduced memory usage
|
||||
* Code cleanups
|
||||
|
||||
### Changes since 5.4.0
|
||||
* 2x performance increase
|
||||
|
@@ -13,12 +13,12 @@
|
||||
|
||||
void log(const std::string &msg)
|
||||
{
|
||||
std::cout << "[" << time(nullptr) << "] " << msg << std::endl;
|
||||
std::cout << "[" << time(nullptr) << "] " << msg << '\n';
|
||||
}
|
||||
|
||||
void log(const std::string &module, const std::string &msg)
|
||||
{
|
||||
std::cout << "[" << time(nullptr) << "] <" << module << "> " << msg << std::endl;
|
||||
std::cout << "[" << time(nullptr) << "] <" << module << "> " << msg << '\n';
|
||||
}
|
||||
|
||||
void bound_log(const std::string &msg)
|
||||
@@ -28,12 +28,12 @@ void bound_log(const std::string &msg)
|
||||
|
||||
void hello_world(const chaiscript::Boxed_Value & /*o*/)
|
||||
{
|
||||
std::cout << "Hello World" << std::endl;
|
||||
std::cout << "Hello World\n";
|
||||
}
|
||||
|
||||
void hello_constructor(const chaiscript::Boxed_Value & /*o*/)
|
||||
{
|
||||
std::cout << "Hello Constructor" << std::endl;
|
||||
std::cout << "Hello Constructor\n";
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ struct System
|
||||
|
||||
void take_shared_ptr(const std::shared_ptr<const std::string> &p)
|
||||
{
|
||||
std::cout << *p << std::endl;
|
||||
std::cout << *p << '\n';
|
||||
}
|
||||
|
||||
int main(int /*argc*/, char * /*argv*/[]) {
|
||||
@@ -122,7 +122,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
||||
//the templated version of eval:
|
||||
int i = chai.eval<int>("5+5");
|
||||
|
||||
std::cout << "5+5: " << i << std::endl;
|
||||
std::cout << "5+5: " << i << '\n';
|
||||
|
||||
//Add a new variable
|
||||
chai("var scripti = 15");
|
||||
@@ -130,9 +130,9 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
||||
//We can even get a handle to the variables in the system
|
||||
int &scripti = chai.eval<int &>("scripti");
|
||||
|
||||
std::cout << "scripti: " << scripti << std::endl;
|
||||
std::cout << "scripti: " << scripti << '\n';
|
||||
scripti *= 2;
|
||||
std::cout << "scripti (updated): " << scripti << std::endl;
|
||||
std::cout << "scripti (updated): " << scripti << '\n';
|
||||
chai("print(\"Scripti from chai: \" + to_string(scripti))");
|
||||
|
||||
//To do: Add examples of handling Boxed_Values directly when needed
|
||||
|
4
samples/fun_call_performance.chai
Normal file
4
samples/fun_call_performance.chai
Normal file
@@ -0,0 +1,4 @@
|
||||
for (var i = 0; i < 1000; ++i) {
|
||||
puts(helloWorld("Bob12345"))
|
||||
}
|
||||
|
405
samples/fun_call_performance.cpp
Normal file
405
samples/fun_call_performance.cpp
Normal file
@@ -0,0 +1,405 @@
|
||||
// This file is distributed under the BSD License.
|
||||
// See "license.txt" for details.
|
||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <regex>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
#ifdef READLINE_AVAILABLE
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#else
|
||||
|
||||
char *mystrdup(const char *s) {
|
||||
size_t len = strlen(s); // Space for length plus nul
|
||||
char *d = static_cast<char*>(malloc(len + 1));
|
||||
if (d == nullptr) return nullptr; // No memory
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
strcpy_s(d, len + 1, s); // Copy the characters
|
||||
#else
|
||||
strncpy(d, s, len); // Copy the characters
|
||||
#endif
|
||||
d[len] = '\0';
|
||||
return d; // Return the new string
|
||||
}
|
||||
|
||||
char* readline(const char* p)
|
||||
{
|
||||
std::string retval;
|
||||
std::cout << p;
|
||||
std::getline(std::cin, retval);
|
||||
return std::cin.eof() ? nullptr : mystrdup(retval.c_str());
|
||||
}
|
||||
|
||||
|
||||
void add_history(const char*){}
|
||||
void using_history(){}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void *cast_module_symbol(std::vector<std::string>(*t_path)())
|
||||
{
|
||||
union cast_union
|
||||
{
|
||||
std::vector<std::string>(*in_ptr)();
|
||||
void *out_ptr;
|
||||
};
|
||||
|
||||
cast_union c;
|
||||
c.in_ptr = t_path;
|
||||
return c.out_ptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> default_search_paths()
|
||||
{
|
||||
std::vector<std::string> paths;
|
||||
|
||||
#ifdef CHAISCRIPT_WINDOWS // force no unicode
|
||||
CHAR path[4096];
|
||||
int size = GetModuleFileNameA(0, path, sizeof(path) - 1);
|
||||
|
||||
std::string exepath(path, size);
|
||||
|
||||
size_t lastslash = exepath.rfind('\\');
|
||||
size_t secondtolastslash = exepath.rfind('\\', lastslash - 1);
|
||||
if (lastslash != std::string::npos)
|
||||
{
|
||||
paths.push_back(exepath.substr(0, lastslash));
|
||||
}
|
||||
|
||||
if (secondtolastslash != std::string::npos)
|
||||
{
|
||||
return{ exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\" };
|
||||
}
|
||||
#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_paths), &rInfo) || !rInfo.dli_fname) {
|
||||
return paths;
|
||||
}
|
||||
|
||||
exepath = std::string(rInfo.dli_fname);
|
||||
}
|
||||
|
||||
size_t lastslash = exepath.rfind('/');
|
||||
|
||||
size_t secondtolastslash = exepath.rfind('/', lastslash - 1);
|
||||
if (lastslash != std::string::npos)
|
||||
{
|
||||
paths.push_back(exepath.substr(0, lastslash));
|
||||
}
|
||||
|
||||
if (secondtolastslash != std::string::npos)
|
||||
{
|
||||
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
|
||||
}
|
||||
#endif
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
void help(int n) {
|
||||
if (n >= 0) {
|
||||
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl;
|
||||
std::cout << "Additionally, you can inspect the runtime system using:" << std::endl;
|
||||
std::cout << " dump_system() - outputs all functions registered to the system" << std::endl;
|
||||
std::cout << " dump_object(x) - dumps information about the given symbol" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "usage : chai [option]+" << std::endl;
|
||||
std::cout << "option:" << std::endl;
|
||||
std::cout << " -h | --help" << std::endl;
|
||||
std::cout << " -i | --interactive" << std::endl;
|
||||
std::cout << " -c | --command cmd" << std::endl;
|
||||
std::cout << " -v | --version" << std::endl;
|
||||
std::cout << " - --stdin" << std::endl;
|
||||
std::cout << " filepath" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::string helloWorld(const std::string &t_name)
|
||||
{
|
||||
return "Hello " + t_name + "!";
|
||||
}
|
||||
|
||||
bool throws_exception(const std::function<void()> &f)
|
||||
{
|
||||
try {
|
||||
f();
|
||||
}
|
||||
catch (...) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
chaiscript::exception::eval_error get_eval_error(const std::function<void()> &f)
|
||||
{
|
||||
try {
|
||||
f();
|
||||
}
|
||||
catch (const chaiscript::exception::eval_error &e) {
|
||||
return e;
|
||||
}
|
||||
|
||||
throw std::runtime_error("no exception throw");
|
||||
}
|
||||
|
||||
std::string get_next_command() {
|
||||
std::string retval("quit");
|
||||
if (!std::cin.eof()) {
|
||||
char *input_raw = readline("eval> ");
|
||||
if (input_raw) {
|
||||
add_history(input_raw);
|
||||
|
||||
std::string val(input_raw);
|
||||
size_t pos = val.find_first_not_of("\t \n");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
val.erase(0, pos);
|
||||
}
|
||||
pos = val.find_last_not_of("\t \n");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
val.erase(pos + 1, std::string::npos);
|
||||
}
|
||||
|
||||
retval = val;
|
||||
|
||||
::free(input_raw);
|
||||
}
|
||||
}
|
||||
if (retval == "quit"
|
||||
|| retval == "exit"
|
||||
|| retval == "help"
|
||||
|| retval == "version")
|
||||
{
|
||||
retval += "(0)";
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
// We have to wrap exit with our own because Clang has a hard time with
|
||||
// function pointers to functions with special attributes (system exit being marked NORETURN)
|
||||
void myexit(int return_val) {
|
||||
exit(return_val);
|
||||
}
|
||||
|
||||
void interactive(chaiscript::ChaiScript& chai)
|
||||
{
|
||||
using_history();
|
||||
|
||||
for (;;) {
|
||||
std::string input = get_next_command();
|
||||
try {
|
||||
// evaluate input
|
||||
chaiscript::Boxed_Value val = chai.eval(input);
|
||||
|
||||
//Then, we try to print the result of the evaluation to the user
|
||||
if (!val.get_type_info().bare_equal(chaiscript::user_type<void>())) {
|
||||
try {
|
||||
std::cout << chai.eval<std::function<std::string(const chaiscript::Boxed_Value &bv)> >("to_string")(val) << std::endl;
|
||||
}
|
||||
catch (...) {} //If we can't, do nothing
|
||||
}
|
||||
}
|
||||
catch (const chaiscript::exception::eval_error &ee) {
|
||||
std::cout << ee.what();
|
||||
if (ee.call_stack.size() > 0) {
|
||||
std::cout << "during evaluation at (" << ee.call_stack[0]->start.line << ", " << ee.call_stack[0]->start.column << ")";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
std::cout << e.what();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
// Disable deprecation warning for getenv call.
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4996)
|
||||
#endif
|
||||
|
||||
const char *usepath = getenv("CHAI_USE_PATH");
|
||||
const char *modulepath = getenv("CHAI_MODULE_PATH");
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
std::vector<std::string> usepaths;
|
||||
usepaths.push_back("");
|
||||
if (usepath)
|
||||
{
|
||||
usepaths.push_back(usepath);
|
||||
}
|
||||
|
||||
std::vector<std::string> modulepaths;
|
||||
std::vector<std::string> searchpaths = default_search_paths();
|
||||
modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
|
||||
modulepaths.push_back("");
|
||||
if (modulepath)
|
||||
{
|
||||
modulepaths.push_back(modulepath);
|
||||
}
|
||||
|
||||
//chaiscript::ChaiScript chai(modulepaths, usepaths);
|
||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library(), usepaths);
|
||||
|
||||
chai.add(chaiscript::fun(&myexit), "exit");
|
||||
chai.add(chaiscript::fun(&myexit), "quit");
|
||||
chai.add(chaiscript::fun(&help), "help");
|
||||
chai.add(chaiscript::fun(&throws_exception), "throws_exception");
|
||||
chai.add(chaiscript::fun(&get_eval_error), "get_eval_error");
|
||||
|
||||
chai.add(chaiscript::fun(&helloWorld), "helloWorld");
|
||||
|
||||
clock_t begin = clock();
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
std::string str = helloWorld("Bob12345");
|
||||
fwrite(str.c_str(), 1, str.size(), stdout);
|
||||
}
|
||||
|
||||
clock_t end = clock();
|
||||
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
|
||||
|
||||
//begin = clock();
|
||||
|
||||
////for (int i = 0; i < 1000; i++)
|
||||
////{
|
||||
//// chai.eval("puts(helloWorld(\"Bob12345\"));");
|
||||
////}
|
||||
//chai.eval_file("E:\\C++\\ChaiScript - 5.4.0\\samples\forx.chai");
|
||||
|
||||
//end = clock();
|
||||
//elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
|
||||
//printf("**MyProgram::time= %lf\n", elapsed_secs);
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
if (i == 0 && argc > 1) {
|
||||
++i;
|
||||
}
|
||||
|
||||
std::string arg(i ? argv[i] : "--interactive");
|
||||
|
||||
enum {
|
||||
eInteractive
|
||||
, eCommand
|
||||
, eFile
|
||||
} mode = eCommand;
|
||||
|
||||
if (arg == "-c" || arg == "--command") {
|
||||
if ((i + 1) >= argc) {
|
||||
std::cout << "insufficient input following " << arg << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
else {
|
||||
arg = argv[++i];
|
||||
}
|
||||
}
|
||||
else if (arg == "-" || arg == "--stdin") {
|
||||
arg = "";
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
arg += line + '\n';
|
||||
}
|
||||
}
|
||||
else if (arg == "-v" || arg == "--version") {
|
||||
arg = "version()";
|
||||
}
|
||||
else if (arg == "-h" || arg == "--help") {
|
||||
arg = "help(-1)";
|
||||
}
|
||||
else if (arg == "-i" || arg == "--interactive") {
|
||||
mode = eInteractive;
|
||||
}
|
||||
else if (arg.find('-') == 0) {
|
||||
std::cout << "unrecognised argument " << arg << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
else {
|
||||
mode = eFile;
|
||||
}
|
||||
|
||||
chaiscript::Boxed_Value val;
|
||||
try {
|
||||
switch (mode) {
|
||||
case eInteractive: interactive(chai); break;
|
||||
case eCommand: val = chai.eval(arg); break;
|
||||
case eFile: {
|
||||
begin = clock();
|
||||
|
||||
val = chai.eval_file(arg);
|
||||
|
||||
end = clock();
|
||||
double elapsed_secs1 = double(end - begin) / CLOCKS_PER_SEC;
|
||||
printf("**C++::time= %.10f\n", elapsed_secs);
|
||||
printf("**ChaiScript::time= %.10f\n", elapsed_secs1);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (const chaiscript::exception::eval_error &ee) {
|
||||
std::cout << ee.pretty_print();
|
||||
std::cout << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@@ -8,6 +8,8 @@ class BaseClass
|
||||
{
|
||||
}
|
||||
|
||||
BaseClass(const BaseClass &) = default;
|
||||
|
||||
virtual ~BaseClass() {}
|
||||
|
||||
virtual std::string doSomething(float, double) const = 0;
|
||||
|
@@ -54,7 +54,7 @@ class test
|
||||
chaiscript::Boxed_Value val = chai.eval_file(sFile);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
std::cout << e.what() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
|
64
src/main.cpp
64
src/main.cpp
@@ -1,14 +1,17 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <regex>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
#ifdef READLINE_AVAILABLE
|
||||
@@ -18,7 +21,7 @@
|
||||
|
||||
char *mystrdup (const char *s) {
|
||||
size_t len = strlen(s); // Space for length plus nul
|
||||
char *d = static_cast<char*>(malloc (len+1));
|
||||
char *d = static_cast<char*>(malloc (len+1));
|
||||
if (d == nullptr) return nullptr; // No memory
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
strcpy_s(d, len, s); // Copy the characters
|
||||
@@ -136,26 +139,22 @@ std::vector<std::string> default_search_paths()
|
||||
|
||||
void help(int n) {
|
||||
if ( n >= 0 ) {
|
||||
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl;
|
||||
std::cout << "Additionally, you can inspect the runtime system using:" << std::endl;
|
||||
std::cout << " dump_system() - outputs all functions registered to the system" << std::endl;
|
||||
std::cout << " dump_object(x) - dumps information about the given symbol" << std::endl;
|
||||
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>.\n";
|
||||
std::cout << "Additionally, you can inspect the runtime system using:\n";
|
||||
std::cout << " dump_system() - outputs all functions registered to the system\n";
|
||||
std::cout << " dump_object(x) - dumps information about the given symbol\n";
|
||||
} else {
|
||||
std::cout << "usage : chai [option]+" << std::endl;
|
||||
std::cout << "option:" << std::endl;
|
||||
std::cout << " -h | --help" << std::endl;
|
||||
std::cout << " -i | --interactive" << std::endl;
|
||||
std::cout << " -c | --command cmd" << std::endl;
|
||||
std::cout << " -v | --version" << std::endl;
|
||||
std::cout << " - --stdin" << std::endl;
|
||||
std::cout << " filepath" << std::endl;
|
||||
std::cout << "usage : chai [option]+\n";
|
||||
std::cout << "option:" << '\n';
|
||||
std::cout << " -h | --help" << '\n';
|
||||
std::cout << " -i | --interactive" << '\n';
|
||||
std::cout << " -c | --command cmd" << '\n';
|
||||
std::cout << " -v | --version" << '\n';
|
||||
std::cout << " - --stdin" << '\n';
|
||||
std::cout << " filepath" << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
void version(int){
|
||||
std::cout << "chai: compiled " << __TIME__ << " " << __DATE__ << std::endl;
|
||||
}
|
||||
|
||||
bool throws_exception(const std::function<void ()> &f)
|
||||
{
|
||||
try {
|
||||
@@ -231,7 +230,7 @@ void interactive(chaiscript::ChaiScript& chai)
|
||||
//Then, we try to print the result of the evaluation to the user
|
||||
if (!val.get_type_info().bare_equal(chaiscript::user_type<void>())) {
|
||||
try {
|
||||
std::cout << chai.eval<std::function<std::string (const chaiscript::Boxed_Value &bv)> >("to_string")(val) << std::endl;
|
||||
std::cout << chai.eval<std::function<std::string (const chaiscript::Boxed_Value &bv)> >("to_string")(val) << '\n';
|
||||
}
|
||||
catch (...) {} //If we can't, do nothing
|
||||
}
|
||||
@@ -241,11 +240,11 @@ void interactive(chaiscript::ChaiScript& chai)
|
||||
if (ee.call_stack.size() > 0) {
|
||||
std::cout << "during evaluation at (" << ee.call_stack[0]->start.line << ", " << ee.call_stack[0]->start.column << ")";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
catch (const std::exception &e) {
|
||||
std::cout << e.what();
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,7 +286,6 @@ int main(int argc, char *argv[])
|
||||
chai.add(chaiscript::fun(&myexit), "exit");
|
||||
chai.add(chaiscript::fun(&myexit), "quit");
|
||||
chai.add(chaiscript::fun(&help), "help");
|
||||
chai.add(chaiscript::fun(&version), "version");
|
||||
chai.add(chaiscript::fun(&throws_exception), "throws_exception");
|
||||
chai.add(chaiscript::fun(&get_eval_error), "get_eval_error");
|
||||
|
||||
@@ -305,7 +303,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
if ( arg == "-c" || arg == "--command" ) {
|
||||
if ( (i+1) >= argc ) {
|
||||
std::cout << "insufficient input following " << arg << std::endl;
|
||||
std::cout << "insufficient input following " << arg << '\n';
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
arg = argv[++i];
|
||||
@@ -317,13 +315,13 @@ int main(int argc, char *argv[])
|
||||
arg += line + '\n' ;
|
||||
}
|
||||
} else if ( arg == "-v" || arg == "--version" ) {
|
||||
arg = "version(0)" ;
|
||||
arg = "version()" ;
|
||||
} else if ( arg == "-h" || arg == "--help" ) {
|
||||
arg = "help(-1)";
|
||||
} else if ( arg == "-i" || arg == "--interactive" ) {
|
||||
mode = eInteractive ;
|
||||
} else if ( arg.find('-') == 0 ) {
|
||||
std::cout << "unrecognised argument " << arg << std::endl;
|
||||
std::cout << "unrecognised argument " << arg << '\n';
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
mode = eFile;
|
||||
@@ -332,19 +330,23 @@ int main(int argc, char *argv[])
|
||||
chaiscript::Boxed_Value val ;
|
||||
try {
|
||||
switch ( mode ) {
|
||||
case eInteractive : interactive(chai); break;
|
||||
case eCommand : val = chai.eval(arg); break;
|
||||
case eFile : val = chai.eval_file(arg); break;
|
||||
default : std::cout << "Unrecognized execution mode" << std::endl; return EXIT_FAILURE;
|
||||
case eInteractive:
|
||||
interactive(chai);
|
||||
break;
|
||||
case eCommand:
|
||||
val = chai.eval(arg);
|
||||
break;
|
||||
case eFile:
|
||||
val = chai.eval_file(arg);
|
||||
}
|
||||
}
|
||||
catch (const chaiscript::exception::eval_error &ee) {
|
||||
std::cout << ee.pretty_print();
|
||||
std::cout << std::endl;
|
||||
std::cout << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
std::cout << e.what() << '\n';
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
// 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)
|
||||
// Copyright 2009-2015, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
@@ -9,7 +9,8 @@ class TestBaseType
|
||||
public:
|
||||
TestBaseType() : val(10), const_val(15) { }
|
||||
TestBaseType(int) : val(10), const_val(15) {}
|
||||
TestBaseType(int *) : val(10), const_val(15) {}
|
||||
TestBaseType(int *) : val(10), const_val(15) { }
|
||||
TestBaseType(const TestBaseType &) = default;
|
||||
virtual ~TestBaseType() {}
|
||||
virtual int func() { return 0; }
|
||||
|
||||
@@ -62,6 +63,8 @@ class TestDerivedType : public TestBaseType
|
||||
{
|
||||
public:
|
||||
virtual ~TestDerivedType() {}
|
||||
TestDerivedType(const TestDerivedType &) = default;
|
||||
TestDerivedType() = default;
|
||||
virtual int func() CHAISCRIPT_OVERRIDE { return 1; }
|
||||
int derived_only_func() { return 19; }
|
||||
|
||||
@@ -72,6 +75,8 @@ class TestDerivedType : public TestBaseType
|
||||
class TestMoreDerivedType : public TestDerivedType
|
||||
{
|
||||
public:
|
||||
TestMoreDerivedType(const TestMoreDerivedType &) = default;
|
||||
TestMoreDerivedType() = default;
|
||||
virtual ~TestMoreDerivedType() {}
|
||||
};
|
||||
|
||||
@@ -95,9 +100,11 @@ std::string hello_world()
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
static int global_i = 1;
|
||||
|
||||
int *get_new_int()
|
||||
{
|
||||
return new int(1);
|
||||
return &global_i;
|
||||
}
|
||||
|
||||
// MSVC doesn't like that we are using C++ return types from our C declared module
|
||||
|
@@ -15,16 +15,16 @@ bool run_test_type_conversion(const Boxed_Value &bv, bool expectedpass)
|
||||
use(ret);
|
||||
} catch (const chaiscript::exception::bad_boxed_cast &/*e*/) {
|
||||
if (expectedpass) {
|
||||
// std::cerr << "Failure in run_test_type_conversion: " << e.what() << std::endl;
|
||||
// std::cerr << "Failure in run_test_type_conversion: " << e.what() << '\n';
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "Unexpected standard exception when attempting cast_conversion: " << e.what() << std::endl;
|
||||
std::cerr << "Unexpected standard exception when attempting cast_conversion: " << e.what() << '\n';
|
||||
return false;
|
||||
} catch (...) {
|
||||
std::cerr << "Unexpected unknown exception when attempting cast_conversion." << std::endl;
|
||||
std::cerr << "Unexpected unknown exception when attempting cast_conversion.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ bool test_type_conversion(const Boxed_Value &bv, bool expectedpass)
|
||||
<< (bv.is_const()?(std::string("const ")):(std::string())) << bv.get_type_info().name()
|
||||
<< " To: "
|
||||
<< (std::is_const<To>::value?(std::string("const ")):(std::string())) << typeid(To).name()
|
||||
<< " test was expected to " << ((expectedpass)?(std::string("succeed")):(std::string("fail"))) << " but did not" << std::endl;
|
||||
<< " test was expected to " << ((expectedpass)?(std::string("succeed")):(std::string("fail"))) << " but did not\n";
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -264,21 +264,26 @@ bool pointer_test(const T& default_value, const T& new_value)
|
||||
|
||||
|
||||
if (p != (*result) ) {
|
||||
std::cerr << "Pointer passed in different than one returned" << std::endl;
|
||||
std::cerr << "Pointer passed in different than one returned\n";
|
||||
delete p;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (*p != *(*result) ) {
|
||||
std::cerr << "Somehow dereferenced pointer values are not the same?" << std::endl;
|
||||
std::cerr << "Somehow dereferenced pointer values are not the same?\n";
|
||||
delete p;
|
||||
return false;
|
||||
}
|
||||
|
||||
delete p;
|
||||
return true;
|
||||
} catch (const exception::bad_boxed_cast &) {
|
||||
std::cerr << "Bad boxed cast performing ** to ** test" << std::endl;
|
||||
std::cerr << "Bad boxed cast performing ** to ** test\n";
|
||||
delete p;
|
||||
return false;
|
||||
} catch (...) {
|
||||
std::cerr << "Unknown exception performing ** to ** test" << std::endl;
|
||||
std::cerr << "Unknown exception performing ** to ** test\n";
|
||||
delete p;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
9
unittests/divide_by_zero_protection.chai
Normal file
9
unittests/divide_by_zero_protection.chai
Normal file
@@ -0,0 +1,9 @@
|
||||
try {
|
||||
3/0
|
||||
assert_true(false); // should never get here
|
||||
} catch (e) {
|
||||
assert_equal("Arithmetic error: divide by zero", e.what())
|
||||
}
|
||||
|
||||
assert_equal(3/0.0, Infinity)
|
||||
|
@@ -8,7 +8,7 @@ void assert_equal(const LHS &lhs, const RHS &rhs)
|
||||
{
|
||||
return;
|
||||
} else {
|
||||
std::cout << "Got: " << lhs << " expected " << rhs << std::endl;
|
||||
std::cout << "Got: " << lhs << " expected " << rhs << '\n';
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@ int test_generic()
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "test_generic failed" << std::endl;
|
||||
std::cout << "test_generic failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ int test_1()
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "test_1 failed" << std::endl;
|
||||
std::cout << "test_1 failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ int test_2()
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "test_2 failed" << std::endl;
|
||||
std::cout << "test_2 failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -61,22 +61,22 @@ int test_5()
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
|
||||
} catch (const double) {
|
||||
std::cout << "test_5 failed with double" << std::endl;
|
||||
std::cout << "test_5 failed with double\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (int) {
|
||||
std::cout << "test_5 failed with int" << std::endl;
|
||||
std::cout << "test_5 failed with int\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (float) {
|
||||
std::cout << "test_5 failed with float" << std::endl;
|
||||
std::cout << "test_5 failed with float\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const std::string &) {
|
||||
std::cout << "test_5 failed with string" << std::endl;
|
||||
std::cout << "test_5 failed with string\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const std::exception &) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
std::cout << "test_5 failed" << std::endl;
|
||||
std::cout << "test_5 failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
@@ -87,22 +87,22 @@ int test_unhandled()
|
||||
try {
|
||||
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
|
||||
} catch (double) {
|
||||
std::cout << "test_unhandled failed with double" << std::endl;
|
||||
std::cout << "test_unhandled failed with double\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (int) {
|
||||
std::cout << "test_unhandled failed with int" << std::endl;
|
||||
std::cout << "test_unhandled failed with int\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (float) {
|
||||
std::cout << "test_unhandled failed with float" << std::endl;
|
||||
std::cout << "test_unhandled failed with float\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const std::exception &) {
|
||||
std::cout << "test_unhandled failed with std::exception" << std::endl;
|
||||
std::cout << "test_unhandled failed with std::exception\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const chaiscript::Boxed_Value &) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
std::cout << "test_unhandled failed" << std::endl;
|
||||
std::cout << "test_unhandled failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
21
unittests/exception_typed.chai
Normal file
21
unittests/exception_typed.chai
Normal file
@@ -0,0 +1,21 @@
|
||||
auto x = 1
|
||||
try {
|
||||
throw(x)
|
||||
x = 2
|
||||
}
|
||||
catch(int e) {
|
||||
x = e + 3
|
||||
}
|
||||
assert_equal(4, x);
|
||||
|
||||
x = 1
|
||||
try {
|
||||
throw(x)
|
||||
x = 2
|
||||
}
|
||||
catch(string e) {
|
||||
}
|
||||
catch(e) {
|
||||
x = e + 4
|
||||
}
|
||||
assert_equal(5, x);
|
34
unittests/exception_typed_2.chai
Normal file
34
unittests/exception_typed_2.chai
Normal file
@@ -0,0 +1,34 @@
|
||||
auto results = [];
|
||||
|
||||
for (auto i = 2; i < 6; ++i) {
|
||||
try {
|
||||
throw(i)
|
||||
}
|
||||
catch(int e) : e < 2 {
|
||||
results.push_back("c1: " + e.to_string());
|
||||
}
|
||||
catch(int e) : e < 4 {
|
||||
results.push_back("c2: " + e.to_string());
|
||||
}
|
||||
catch(e) {
|
||||
results.push_back("c3: " + e.to_string());
|
||||
}
|
||||
catch {
|
||||
// Should never get called
|
||||
assert_equal(false, true)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
throw(3)
|
||||
}
|
||||
catch(int e) : e < 3
|
||||
{
|
||||
// Should never get called
|
||||
assert_equal(false, true);
|
||||
}
|
||||
catch {
|
||||
results.push_back("defaultcatch");
|
||||
}
|
||||
|
||||
assert_equal(["c2: 2", "c2: 3", "c3: 4", "c3: 5", "defaultcatch"], results);
|
@@ -1,6 +1,6 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
double test_call(const std::function<double (int)> &f, int val)
|
||||
int test_call(const std::function<int (int)> &f, int val)
|
||||
{
|
||||
return f(val);
|
||||
}
|
||||
@@ -9,15 +9,15 @@ int main()
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
|
||||
chai.add(chaiscript::fun(&test_call), "test_call");
|
||||
|
||||
chai.eval("def func(i) { return i * 3.5; };");
|
||||
double d = chai.eval<double>("test_call(func, 3)");
|
||||
|
||||
if (d == 3 * 3.5)
|
||||
chai.eval("def func(i) { return i * 6; };");
|
||||
int d = chai.eval<int>("test_call(func, 3)");
|
||||
|
||||
if (d == 3 * 6)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
15
unittests/future.chai
Normal file
15
unittests/future.chai
Normal file
@@ -0,0 +1,15 @@
|
||||
var func = fun(){
|
||||
var ret = 0;
|
||||
for (var i = 0; i < 50000; ++i) {
|
||||
ret += i;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
var fut1 := async(func);
|
||||
var fut2 := async(func);
|
||||
|
||||
// simply executing without crashing is good enough for this test
|
||||
|
||||
print(" ${fut1.get()} ${fut2.get()} ")
|
@@ -7,7 +7,7 @@ bool test_literal(T val, const std::string &str)
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
T val2 = chai.eval<T>(str);
|
||||
std::cout << "Comparing : " << val << " " << val2 << std::endl;
|
||||
std::cout << "Comparing : " << val << " " << val2 << '\n';
|
||||
return val == val2;
|
||||
}
|
||||
|
||||
|
70
unittests/object_lifetime_test2.cpp
Normal file
70
unittests/object_lifetime_test2.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
template<typename T>
|
||||
struct Vector2
|
||||
{
|
||||
Vector2() : x(0), y(0) {}
|
||||
Vector2(T px, T py) : x(px), y(py) {}
|
||||
Vector2(const Vector2& cp) : x(cp.x), y(cp.y) {}
|
||||
|
||||
Vector2& operator+=(const Vector2& vec_r)
|
||||
{
|
||||
x += vec_r.x;
|
||||
y += vec_r.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2 operator+(const Vector2& vec_r)
|
||||
{
|
||||
return Vector2(*this += vec_r);
|
||||
}
|
||||
|
||||
Vector2 &operator=(const Vector2& ver_r)
|
||||
{
|
||||
x = ver_r.x;
|
||||
y = ver_r.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
T x;
|
||||
T y;
|
||||
};
|
||||
|
||||
Vector2<float> GetValue()
|
||||
{
|
||||
return Vector2<float>(10,15);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript _script(chaiscript::Std_Lib::library());
|
||||
|
||||
//Registering stuff
|
||||
_script.add(chaiscript::user_type<Vector2<float>>(), "Vector2f");
|
||||
_script.add(chaiscript::constructor<Vector2<float> ()>(), "Vector2f");
|
||||
_script.add(chaiscript::constructor<Vector2<float> (float, float)>(), "Vector2f");
|
||||
_script.add(chaiscript::constructor<Vector2<float> (const Vector2<float>&)>(), "Vector2f");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::x), "x");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::y), "y");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::operator +), "+");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::operator +=), "+=");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::operator =), "=");
|
||||
_script.add(chaiscript::fun(&GetValue), "getValue");
|
||||
|
||||
_script.eval(R"(
|
||||
var test = 0.0
|
||||
var test2 = Vector2f(10,10)
|
||||
|
||||
test = getValue().x
|
||||
print(test)
|
||||
print(test2.x)
|
||||
)");
|
||||
|
||||
if (_script.eval<std::string>("to_string(test)") != "10") { return EXIT_FAILURE; }
|
||||
if (_script.eval<std::string>("to_string(test2.x)") != "10") { return EXIT_FAILURE; }
|
||||
|
||||
|
||||
//_script.eval_file("object_lifetime_test2.inc");
|
||||
}
|
@@ -31,7 +31,7 @@ int main()
|
||||
test_type(chaiscript::user_type<const int *>(), true, true, false, false, false);
|
||||
test_type(chaiscript::Type_Info(), false, false, false, false, true);
|
||||
|
||||
std::cout << "Size of Type_Info " << sizeof(chaiscript::Type_Info) << std::endl;
|
||||
std::cout << "Size of Type_Info " << sizeof(chaiscript::Type_Info) << '\n';
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user