Compare commits
241 Commits
attempt_re
...
develop
Author | SHA1 | Date | |
---|---|---|---|
![]() |
0f9d9cae4a | ||
![]() |
468d65a661 | ||
![]() |
9847618cf3 | ||
![]() |
a281d9571e | ||
![]() |
204faa82c1 | ||
![]() |
be2fec02d9 | ||
![]() |
491b95099d | ||
![]() |
561c5bc981 | ||
![]() |
12829ee5d2 | ||
![]() |
f53a1ed951 | ||
![]() |
12100cce99 | ||
![]() |
d22c27b627 | ||
![]() |
60c43233c6 | ||
![]() |
c2f7ca3aa2 | ||
![]() |
72cb9bd940 | ||
![]() |
84f9c44ab6 | ||
![]() |
698dfb06db | ||
![]() |
244b5b224b | ||
![]() |
534897d835 | ||
![]() |
fac5a39066 | ||
![]() |
064a385a64 | ||
![]() |
e342243193 | ||
![]() |
283785faaf | ||
![]() |
c0c0bd3172 | ||
![]() |
40fb8d257e | ||
![]() |
f5f6ddf219 | ||
![]() |
87f1242ed4 | ||
![]() |
faba0f1317 | ||
![]() |
077c93ab27 | ||
![]() |
914bca6295 | ||
![]() |
2549b4e983 | ||
![]() |
1cb15d8b22 | ||
![]() |
2ce155237d | ||
![]() |
dca3ce4ea6 | ||
![]() |
ca7d4ab734 | ||
![]() |
f5ced799cf | ||
![]() |
1499061f86 | ||
![]() |
24352c62e8 | ||
![]() |
6b4c47c5ba | ||
![]() |
396d43a13f | ||
![]() |
18cf09b512 | ||
![]() |
2782cdd33b | ||
![]() |
d8d7bc79b7 | ||
![]() |
3e04210027 | ||
![]() |
c82c9ccb6e | ||
![]() |
efd37a7071 | ||
![]() |
83b7973cb8 | ||
![]() |
e7a6b2306c | ||
![]() |
0a18f0a809 | ||
![]() |
8efba903c3 | ||
![]() |
ca87c05cd4 | ||
![]() |
94fb7c2453 | ||
![]() |
c54d84fae6 | ||
![]() |
574f4a9664 | ||
![]() |
b7e8897a43 | ||
![]() |
7a588ed5cf | ||
![]() |
89f373d21c | ||
![]() |
037335a0ea | ||
![]() |
2431362e54 | ||
![]() |
9c59600b9f | ||
![]() |
51663df1ba | ||
![]() |
624c7c435b | ||
![]() |
cf89bdd804 | ||
![]() |
a8e70a4cfe | ||
![]() |
f79de06e0b | ||
![]() |
cee57f998a | ||
![]() |
ce62706fea | ||
![]() |
92c2ade1cd | ||
![]() |
6b7481e6a1 | ||
![]() |
d096f926d3 | ||
![]() |
fb7f8f194c | ||
![]() |
78f885ec61 | ||
![]() |
d6d50478de | ||
![]() |
defdb53a55 | ||
![]() |
0dea62dd54 | ||
![]() |
590905f4b3 | ||
![]() |
9218dda001 | ||
![]() |
9e17514b57 | ||
![]() |
5f402e71dd | ||
![]() |
95e119fffe | ||
![]() |
f17439a9d3 | ||
![]() |
940e0c2d86 | ||
![]() |
e8c03b33c6 | ||
![]() |
b68f917677 | ||
![]() |
7f4af72244 | ||
![]() |
6757b66f95 | ||
![]() |
c9034a0485 | ||
![]() |
50dcbc8c7e | ||
![]() |
1ea91faf52 | ||
![]() |
745e0c0f0b | ||
![]() |
c42477f2eb | ||
![]() |
335a02f165 | ||
![]() |
012f1ffff5 | ||
![]() |
9925b20fad | ||
![]() |
28122f7cb0 | ||
![]() |
b1f1803759 | ||
![]() |
359897a442 | ||
![]() |
ffcd7e3a76 | ||
![]() |
2c99e6cd32 | ||
![]() |
332a62769b | ||
![]() |
a38b254a98 | ||
![]() |
77231461ca | ||
![]() |
eefd50a6bc | ||
![]() |
0d4a99af82 | ||
![]() |
9f30d84f39 | ||
![]() |
8b18e301d2 | ||
![]() |
508729ec77 | ||
![]() |
0fe78f7ba5 | ||
![]() |
6202149b4f | ||
![]() |
4ad661475b | ||
![]() |
6d309b7516 | ||
![]() |
7d9e1b3af7 | ||
![]() |
b99ccafa07 | ||
![]() |
c97a69537d | ||
![]() |
fac8f3ec90 | ||
![]() |
ab07872857 | ||
![]() |
23c13e6570 | ||
![]() |
aa9267726f | ||
![]() |
396e78d295 | ||
![]() |
7339ff2c2f | ||
![]() |
1efcddd335 | ||
![]() |
21ccb1d1d0 | ||
![]() |
c37c901a0c | ||
![]() |
690b96d9ee | ||
![]() |
d638d87a0f | ||
![]() |
b091439567 | ||
![]() |
665125665a | ||
![]() |
d1c7645a4e | ||
![]() |
58faea1cf2 | ||
![]() |
8b7fe33bf1 | ||
![]() |
7cc100e3d7 | ||
![]() |
21495ebb40 | ||
![]() |
b2907fc608 | ||
![]() |
bec1b91b7b | ||
![]() |
4b81a24a0a | ||
![]() |
3b2c82ba2c | ||
![]() |
8cb3bd4af8 | ||
![]() |
26e6f51fa8 | ||
![]() |
87294fc89d | ||
![]() |
9cc0ce01b9 | ||
![]() |
c9ee707972 | ||
![]() |
cefb4d3c78 | ||
![]() |
4e6e63ab5d | ||
![]() |
7561aa8828 | ||
![]() |
e44724c780 | ||
![]() |
b0f07cbe5d | ||
![]() |
62639a4359 | ||
![]() |
176d608bb4 | ||
![]() |
a1d90c95f0 | ||
![]() |
a14f1983e8 | ||
![]() |
5642e062e6 | ||
![]() |
9e16cc2a79 | ||
![]() |
201fef49c6 | ||
![]() |
58ebb22c55 | ||
![]() |
830b7c93ca | ||
![]() |
368a3b78a2 | ||
![]() |
e3e90de02a | ||
![]() |
8478ddc470 | ||
![]() |
2adefaf46d | ||
![]() |
bd26355516 | ||
![]() |
19a730b78b | ||
![]() |
e3d1741c63 | ||
![]() |
09bdec4882 | ||
![]() |
c31ebb5665 | ||
![]() |
52a191df9e | ||
![]() |
228c942b6c | ||
![]() |
09ed0ca573 | ||
![]() |
ee68ff20ed | ||
![]() |
b72eed3921 | ||
![]() |
039d0edce3 | ||
![]() |
7b3f06b269 | ||
![]() |
5373bbd52e | ||
![]() |
17821be1e2 | ||
![]() |
9a526bc1ec | ||
![]() |
03803ee4c4 | ||
![]() |
dcedd64032 | ||
![]() |
d34d74205c | ||
![]() |
41c1c490c8 | ||
![]() |
70cdbef693 | ||
![]() |
f6c69f2826 | ||
![]() |
184ca7f7b2 | ||
![]() |
71caf5006f | ||
![]() |
4dbf1ee2bd | ||
![]() |
4324a700ad | ||
![]() |
5b78d5a898 | ||
![]() |
ff2ab6bb8d | ||
![]() |
25575564c0 | ||
![]() |
683164650a | ||
![]() |
647f8842fd | ||
![]() |
6d6f79b1a4 | ||
![]() |
06b2893bfb | ||
![]() |
7ab6bce7fa | ||
![]() |
f9294c8cbe | ||
![]() |
80cc18bf2f | ||
![]() |
c68488388e | ||
![]() |
7d5a97aa2f | ||
![]() |
83c6df11f0 | ||
![]() |
10b984556d | ||
![]() |
cf2fa09d6c | ||
![]() |
f3f84594ee | ||
![]() |
57aa874c6e | ||
![]() |
32bd936a18 | ||
![]() |
498339c202 | ||
![]() |
56b4f465a1 | ||
![]() |
1a42614441 | ||
![]() |
6fa83bca85 | ||
![]() |
fd57bec676 | ||
![]() |
14307194e9 | ||
![]() |
62e34c097c | ||
![]() |
cdb9dcc154 | ||
![]() |
14d429853b | ||
![]() |
e8ff1f9d7e | ||
![]() |
49ef5306a9 | ||
![]() |
7d9dbc3d86 | ||
![]() |
49dfdfd15a | ||
![]() |
720395e47a | ||
![]() |
5e0a882b18 | ||
![]() |
9603d3910a | ||
![]() |
6f0d02f158 | ||
![]() |
8d808f75c0 | ||
![]() |
2a1632f213 | ||
![]() |
e57f11fcf4 | ||
![]() |
2fe794fcae | ||
![]() |
b594043eef | ||
![]() |
fe8f8a89a7 | ||
![]() |
40694c798c | ||
![]() |
443828fa23 | ||
![]() |
866db4ee8b | ||
![]() |
5e97f459d8 | ||
![]() |
e02ac78195 | ||
![]() |
62cd8031ac | ||
![]() |
61dfcb00c0 | ||
![]() |
4bf619c80f | ||
![]() |
08a68f310a | ||
![]() |
641ac1a1ae | ||
![]() |
2400c64c82 | ||
![]() |
1e584048ce | ||
![]() |
7865f8e7f2 | ||
![]() |
5ff97979fd | ||
![]() |
5567e767a3 | ||
![]() |
5a947b5035 |
79
.travis.yml
79
.travis.yml
@ -1,33 +1,62 @@
|
|||||||
language: cpp
|
language: cpp
|
||||||
compiler:
|
|
||||||
- gcc
|
|
||||||
env:
|
|
||||||
matrix:
|
|
||||||
- GCC_VER="4.9"
|
|
||||||
- GCC_VER="5"
|
|
||||||
|
|
||||||
|
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
sources:
|
||||||
|
- ubuntu-toolchain-r-test
|
||||||
|
packages:
|
||||||
|
- g++-4.9
|
||||||
|
- g++-5
|
||||||
|
coverity_scan:
|
||||||
|
project:
|
||||||
|
name: "ChaiScript/ChaiScript"
|
||||||
|
description: "Build submitted via Travis CI"
|
||||||
|
notification_email: jason@emptycrate.com
|
||||||
|
build_command_prepend: "cmake -D ENABLE_COVERAGE:BOOL=TRUE -D CMAKE_BUILD_TYPE:STRING=Debug . "
|
||||||
|
build_command: "cmake --build . -- -j2"
|
||||||
|
branch_pattern: coverity_scan
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- os: linux
|
||||||
|
sudo: false
|
||||||
|
env: GCC_VER="4.9"
|
||||||
|
compiler: gcc
|
||||||
|
- os: linux
|
||||||
|
sudo: false
|
||||||
|
env: GCC_VER="4.9" CMAKE_OPTIONS="-D DYNLOAD_ENABLED:BOOL=FALSE -D MULTITHREAD_SUPPORT_ENABLED:BOOL=FALSE -D USE_STD_MAKE_SHARED:BOOL=TRUE" BUILD_ONLY=1
|
||||||
|
compiler: gcc
|
||||||
|
- os: linux
|
||||||
|
sudo: false
|
||||||
|
env: GCC_VER="5" CPPCHECK=1 COVERAGE=1 CMAKE_OPTIONS="-D RUN_FUZZY_TESTS:BOOL=TRUE"
|
||||||
|
compiler: gcc
|
||||||
|
- os: osx
|
||||||
|
compiler: clang
|
||||||
|
osx_image: xcode8
|
||||||
|
- os: osx
|
||||||
|
compiler: clang
|
||||||
|
osx_image: xcode8
|
||||||
|
env: CMAKE_OPTIONS="-D DYNLOAD_ENABLED:BOOL=FALSE -D MULTITHREAD_SUPPORT_ENABLED:BOOL=FALSE -D USE_STD_MAKE_SHARED:BOOL=TRUE" BUILD_ONLY=1
|
||||||
|
|
||||||
|
env:
|
||||||
global:
|
global:
|
||||||
- secure: eiaR6pXiiEpyB8+LLQ1NvZdl0Yylru1BLy9lMoHl+IpUNGGQGywmW/2WAn77rFfmR1OPA2qWQLfgPwgK0HxUA9HHlot9tre5QhiN2Lw8NOT8tCZ6tTm2+QntDBjBGJyal/knRvQkn/6qs6GxlXRerz4ArnnuPL1vESt3zwB0YtU=
|
- secure: eiaR6pXiiEpyB8+LLQ1NvZdl0Yylru1BLy9lMoHl+IpUNGGQGywmW/2WAn77rFfmR1OPA2qWQLfgPwgK0HxUA9HHlot9tre5QhiN2Lw8NOT8tCZ6tTm2+QntDBjBGJyal/knRvQkn/6qs6GxlXRerz4ArnnuPL1vESt3zwB0YtU=
|
||||||
# The next declaration is the encrypted COVERITY_SCAN_TOKEN, created
|
# The next declaration is the encrypted COVERITY_SCAN_TOKEN, created
|
||||||
- secure: "LfolGjP8tWm3yAwthfu3yp8Zn40aueFae580UUR34gusG87cyglq2tQDtxdy+63gWEeNfArbv9n5rZv+bDW3ggHyPjuCKKc1PlZAy07lfXUXf1uz+SFhNvNoYTn3mQG3VZ08o116p4Le2p8yqu4bylJ8wckEq7PrTwvSGVQWTWM="
|
- secure: "LfolGjP8tWm3yAwthfu3yp8Zn40aueFae580UUR34gusG87cyglq2tQDtxdy+63gWEeNfArbv9n5rZv+bDW3ggHyPjuCKKc1PlZAy07lfXUXf1uz+SFhNvNoYTn3mQG3VZ08o116p4Le2p8yqu4bylJ8wckEq7PrTwvSGVQWTWM="
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- export CXX="g++-$GCC_VER" CC="gcc-$GCC_VER" GCOV="gcov-$GCC_VER"
|
- if [ "${GCC_VER}" != "" ]; then export CXX="g++-$GCC_VER" CC="gcc-$GCC_VER" GCOV="gcov-$GCC_VER" ; fi
|
||||||
- if [ "$GCC_VER" = "5" ]; then export COVERAGE=1 CPPCHECK=1; fi
|
- pip install --user cpp-coveralls
|
||||||
- if [ ${COVERAGE} = 1 ]; then export FUZZY_CMD="-D RUN_FUZZY_TESTS:BOOL=TRUE"; fi
|
|
||||||
- sudo pip install cpp-coveralls
|
|
||||||
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
|
|
||||||
- sudo apt-get update
|
|
||||||
- sudo apt-get install -qq g++-$GCC_VER
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- if [ ${COVERITY_SCAN_BRANCH} != 1 ]; then cmake -D ENABLE_COVERAGE:BOOL=TRUE -D CMAKE_BUILD_TYPE:STRING=Debug $FUZZY_CMD . ; fi
|
- cmake -D ENABLE_COVERAGE:BOOL=TRUE -D CMAKE_BUILD_TYPE:STRING=Debug $CMAKE_OPTIONS .
|
||||||
- if [ ${COVERITY_SCAN_BRANCH} != 1 ]; then make -j2 ; fi
|
- cmake --build . -- -j2
|
||||||
- make test
|
- if [ "${BUILD_ONLY}" != "1" ]; then ctest; fi
|
||||||
- if [ ${COVERAGE} = 1 ]; then bash <(curl -s https://raw.githubusercontent.com/codecov/codecov-bash/master/codecov) -x $GCOV -a "-s `pwd`" ; fi
|
- if [ "${COVERAGE}" = "1" ]; then bash <(curl -s https://raw.githubusercontent.com/codecov/codecov-bash/master/codecov) -x $GCOV -a "-s `pwd`" ; fi
|
||||||
|
|
||||||
after_script:
|
#after_script:
|
||||||
- if [ ${CPPCHECK} = 1 ]; then contrib/codeanalysis/runcppcheck.sh ; fi
|
# - if [ ${CPPCHECK} = 1 ]; then contrib/codeanalysis/runcppcheck.sh ; fi
|
||||||
|
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
@ -42,15 +71,3 @@ notifications:
|
|||||||
on_success: change # options: [always|never|change] default: always
|
on_success: change # options: [always|never|change] default: always
|
||||||
on_failure: always # options: [always|never|change] default: always
|
on_failure: always # options: [always|never|change] default: always
|
||||||
on_start: false # default: false
|
on_start: false # default: false
|
||||||
|
|
||||||
addons:
|
|
||||||
coverity_scan:
|
|
||||||
project:
|
|
||||||
name: "ChaiScript/ChaiScript"
|
|
||||||
description: "Build submitted via Travis CI"
|
|
||||||
notification_email: jason@emptycrate.com
|
|
||||||
build_command_prepend: "cmake -D ENABLE_COVERAGE:BOOL=TRUE -D CMAKE_BUILD_TYPE:STRING=Debug . "
|
|
||||||
build_command: "cmake --build . -- -j2"
|
|
||||||
branch_pattern: coverity_scan
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,6 +14,7 @@ ELSE()
|
|||||||
project(chaiscript)
|
project(chaiscript)
|
||||||
|
|
||||||
option(MULTITHREAD_SUPPORT_ENABLED "Multithreaded Support Enabled" TRUE)
|
option(MULTITHREAD_SUPPORT_ENABLED "Multithreaded Support Enabled" TRUE)
|
||||||
|
option(DYNLOAD_ENABLED "Dynamic Loading Support Enabled" TRUE)
|
||||||
|
|
||||||
|
|
||||||
option(BUILD_MODULES "Build Extra Modules (stl)" TRUE)
|
option(BUILD_MODULES "Build Extra Modules (stl)" TRUE)
|
||||||
@ -213,7 +214,7 @@ endif()
|
|||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
|
|
||||||
set(Chai_INCLUDES include/chaiscript/chaiscript.hpp include/chaiscript/chaiscript_threading.hpp include/chaiscript/dispatchkit/bad_boxed_cast.hpp include/chaiscript/dispatchkit/bind_first.hpp include/chaiscript/dispatchkit/bootstrap.hpp include/chaiscript/dispatchkit/bootstrap_stl.hpp include/chaiscript/dispatchkit/boxed_cast.hpp include/chaiscript/dispatchkit/boxed_cast_helper.hpp include/chaiscript/dispatchkit/boxed_number.hpp include/chaiscript/dispatchkit/boxed_value.hpp include/chaiscript/dispatchkit/dispatchkit.hpp include/chaiscript/dispatchkit/type_conversions.hpp include/chaiscript/dispatchkit/dynamic_object.hpp include/chaiscript/dispatchkit/exception_specification.hpp include/chaiscript/dispatchkit/function_call.hpp include/chaiscript/dispatchkit/function_call_detail.hpp include/chaiscript/dispatchkit/handle_return.hpp include/chaiscript/dispatchkit/operators.hpp include/chaiscript/dispatchkit/proxy_constructors.hpp include/chaiscript/dispatchkit/proxy_functions.hpp include/chaiscript/dispatchkit/register_function.hpp include/chaiscript/dispatchkit/type_info.hpp include/chaiscript/language/chaiscript_algebraic.hpp include/chaiscript/language/chaiscript_common.hpp include/chaiscript/language/chaiscript_engine.hpp include/chaiscript/language/chaiscript_eval.hpp include/chaiscript/language/chaiscript_parser.hpp include/chaiscript/language/chaiscript_prelude.hpp include/chaiscript/language/chaiscript_prelude_docs.hpp include/chaiscript/utility/utility.hpp include/chaiscript/utility/json.hpp include/chaiscript/utility/json_wrap.hpp)
|
set(Chai_INCLUDES include/chaiscript/chaiscript.hpp include/chaiscript/chaiscript_threading.hpp include/chaiscript/dispatchkit/bad_boxed_cast.hpp include/chaiscript/dispatchkit/bind_first.hpp include/chaiscript/dispatchkit/bootstrap.hpp include/chaiscript/dispatchkit/bootstrap_stl.hpp include/chaiscript/dispatchkit/boxed_cast.hpp include/chaiscript/dispatchkit/boxed_cast_helper.hpp include/chaiscript/dispatchkit/boxed_number.hpp include/chaiscript/dispatchkit/boxed_value.hpp include/chaiscript/dispatchkit/dispatchkit.hpp include/chaiscript/dispatchkit/type_conversions.hpp include/chaiscript/dispatchkit/dynamic_object.hpp include/chaiscript/dispatchkit/exception_specification.hpp include/chaiscript/dispatchkit/function_call.hpp include/chaiscript/dispatchkit/function_call_detail.hpp include/chaiscript/dispatchkit/handle_return.hpp include/chaiscript/dispatchkit/operators.hpp include/chaiscript/dispatchkit/proxy_constructors.hpp include/chaiscript/dispatchkit/proxy_functions.hpp include/chaiscript/dispatchkit/proxy_functions_detail.hpp include/chaiscript/dispatchkit/register_function.hpp include/chaiscript/dispatchkit/type_info.hpp include/chaiscript/language/chaiscript_algebraic.hpp include/chaiscript/language/chaiscript_common.hpp include/chaiscript/language/chaiscript_engine.hpp include/chaiscript/language/chaiscript_eval.hpp include/chaiscript/language/chaiscript_parser.hpp include/chaiscript/language/chaiscript_prelude.hpp include/chaiscript/language/chaiscript_prelude_docs.hpp include/chaiscript/utility/utility.hpp include/chaiscript/utility/json.hpp include/chaiscript/utility/json_wrap.hpp)
|
||||||
|
|
||||||
set_source_files_properties(${Chai_INCLUDES} PROPERTIES HEADER_FILE_ONLY TRUE)
|
set_source_files_properties(${Chai_INCLUDES} PROPERTIES HEADER_FILE_ONLY TRUE)
|
||||||
|
|
||||||
@ -221,9 +222,15 @@ if(NOT MULTITHREAD_SUPPORT_ENABLED)
|
|||||||
add_definitions(-DCHAISCRIPT_NO_THREADS)
|
add_definitions(-DCHAISCRIPT_NO_THREADS)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(NOT DYNLOAD_ENABLED)
|
||||||
|
add_definitions(-DCHAISCRIPT_NO_DYNLOAD)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(CMAKE_HOST_UNIX)
|
if(CMAKE_HOST_UNIX)
|
||||||
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Haiku")
|
if(DYNLOAD_ENABLED)
|
||||||
list(APPEND LIBS "dl")
|
if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Haiku")
|
||||||
|
list(APPEND LIBS "dl")
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(MULTITHREAD_SUPPORT_ENABLED)
|
if(MULTITHREAD_SUPPORT_ENABLED)
|
||||||
@ -249,27 +256,30 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
|
|||||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
|
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
|
||||||
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
|
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
|
||||||
|
|
||||||
|
add_library(stdlib STATIC static_libs/chaiscript_stdlib.cpp)
|
||||||
|
add_library(parser STATIC static_libs/chaiscript_parser.cpp)
|
||||||
|
|
||||||
add_library(chaiscript_stdlib-${CHAI_VERSION} MODULE src/chaiscript_stdlib.cpp)
|
add_library(chaiscript_stdlib-${CHAI_VERSION} MODULE src/chaiscript_stdlib_module.cpp)
|
||||||
target_link_libraries(chaiscript_stdlib-${CHAI_VERSION} ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries(chaiscript_stdlib-${CHAI_VERSION} ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
|
||||||
|
set(CHAISCRIPT_LIBS stdlib parser)
|
||||||
|
|
||||||
add_executable(chai src/main.cpp ${Chai_INCLUDES})
|
add_executable(chai src/main.cpp ${Chai_INCLUDES})
|
||||||
target_link_libraries(chai ${LIBS})
|
target_link_libraries(chai ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_dependencies(chai chaiscript_stdlib-${CHAI_VERSION})
|
|
||||||
|
|
||||||
if(BUILD_SAMPLES)
|
if(BUILD_SAMPLES)
|
||||||
add_executable(example samples/example.cpp)
|
add_executable(example samples/example.cpp)
|
||||||
target_link_libraries(example ${LIBS})
|
target_link_libraries(example ${LIBS})
|
||||||
add_executable(test_num_exceptions samples/test_num_exceptions.cpp)
|
add_executable(test_num_exceptions samples/test_num_exceptions.cpp)
|
||||||
target_link_libraries(test_num_exceptions ${LIBS})
|
target_link_libraries(test_num_exceptions ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_executable(memory_leak_test samples/memory_leak_test.cpp)
|
add_executable(memory_leak_test samples/memory_leak_test.cpp)
|
||||||
target_link_libraries(memory_leak_test ${LIBS})
|
target_link_libraries(memory_leak_test ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_executable(inheritance samples/inheritance.cpp)
|
add_executable(inheritance samples/inheritance.cpp)
|
||||||
target_link_libraries(inheritance ${LIBS})
|
target_link_libraries(inheritance ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_executable(factory samples/factory.cpp)
|
add_executable(factory samples/factory.cpp)
|
||||||
target_link_libraries(factory ${LIBS})
|
target_link_libraries(factory ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_executable(fun_call_performance samples/fun_call_performance.cpp)
|
add_executable(fun_call_performance samples/fun_call_performance.cpp)
|
||||||
target_link_libraries(fun_call_performance ${LIBS})
|
target_link_libraries(fun_call_performance ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
@ -295,7 +305,7 @@ if (RUN_FUZZY_TESTS)
|
|||||||
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/unittests")
|
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/unittests")
|
||||||
|
|
||||||
execute_process(
|
execute_process(
|
||||||
COMMAND ${CMAKE_COMMAND} -E tar xjf ${CMAKE_CURRENT_SOURCE_DIR}/unittests/fuzzy_tests-2015-07-16.tar.bz2
|
COMMAND ${CMAKE_COMMAND} -E tar xjf ${CMAKE_CURRENT_SOURCE_DIR}/unittests/fuzzy_tests-2016-06-29.tar.bz2
|
||||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/unittests
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/unittests
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -421,9 +431,12 @@ if(BUILD_TESTING)
|
|||||||
|
|
||||||
if(NOT UNIT_TEST_LIGHT)
|
if(NOT UNIT_TEST_LIGHT)
|
||||||
add_executable(compiled_tests unittests/compiled_tests.cpp)
|
add_executable(compiled_tests unittests/compiled_tests.cpp)
|
||||||
target_link_libraries(compiled_tests ${LIBS})
|
target_link_libraries(compiled_tests ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
ADD_CATCH_TESTS(compiled_tests)
|
ADD_CATCH_TESTS(compiled_tests)
|
||||||
|
|
||||||
|
add_executable(static_chaiscript_test unittests/static_chaiscript.cpp)
|
||||||
|
target_link_libraries(static_chaiscript_test ${LIBS})
|
||||||
|
add_test(NAME Static_ChaiScript_Test COMMAND static_chaiscript_test)
|
||||||
|
|
||||||
add_executable(boxed_cast_test unittests/boxed_cast_test.cpp)
|
add_executable(boxed_cast_test unittests/boxed_cast_test.cpp)
|
||||||
target_link_libraries(boxed_cast_test ${LIBS})
|
target_link_libraries(boxed_cast_test ${LIBS})
|
||||||
@ -434,11 +447,11 @@ if(BUILD_TESTING)
|
|||||||
add_test(NAME Type_Info_Test COMMAND type_info_test)
|
add_test(NAME Type_Info_Test COMMAND type_info_test)
|
||||||
|
|
||||||
add_executable(c_linkage_test unittests/c_linkage_test.cpp)
|
add_executable(c_linkage_test unittests/c_linkage_test.cpp)
|
||||||
target_link_libraries(c_linkage_test ${LIBS})
|
target_link_libraries(c_linkage_test ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_test(NAME C_Linkage_Test COMMAND c_linkage_test)
|
add_test(NAME C_Linkage_Test COMMAND c_linkage_test)
|
||||||
|
|
||||||
add_executable(integer_literal_test unittests/integer_literal_test.cpp)
|
add_executable(integer_literal_test unittests/integer_literal_test.cpp)
|
||||||
target_link_libraries(integer_literal_test ${LIBS})
|
target_link_libraries(integer_literal_test ${LIBS} ${CHAISCRIPT_LIBS})
|
||||||
add_test(NAME Integer_Literal_Test COMMAND integer_literal_test)
|
add_test(NAME Integer_Literal_Test COMMAND integer_literal_test)
|
||||||
|
|
||||||
if(MULTITHREAD_SUPPORT_ENABLED)
|
if(MULTITHREAD_SUPPORT_ENABLED)
|
||||||
|
28
DesignGoals.md
Normal file
28
DesignGoals.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Introduction
|
||||||
|
|
||||||
|
This document outlines the principles that drive the development of ChaiScript. ChaiScript does not intent to be the perfect tool for *every* situation, but it does intend to be a good general purpose tool for *most* situations.
|
||||||
|
|
||||||
|
# Goals
|
||||||
|
|
||||||
|
1. Trivially easy to integrate with C++ projects
|
||||||
|
2. 0 external depenencies
|
||||||
|
3. "Perfect" integration with C++
|
||||||
|
* Direct mapping between ChaiScript objects and C++ objects
|
||||||
|
* Direct mapping between ChaiScript functions and C++ functions
|
||||||
|
* Direct mapping between ChaiScript exceptions and C++ exceptions
|
||||||
|
3. Never surprise the C++ developer
|
||||||
|
* Object lifetimes managed by the stack
|
||||||
|
* Familiar syntax to C++ developers
|
||||||
|
4. Perform "well enough" to not get in the way
|
||||||
|
|
||||||
|
|
||||||
|
# Alternatives
|
||||||
|
|
||||||
|
## Sol2
|
||||||
|
|
||||||
|
If you are looking for the fastest performing scripting language and don't mind Lua, you might want to consider [sol2](https://github.com/ThePhD/sol2).
|
||||||
|
|
||||||
|
## SWIG
|
||||||
|
|
||||||
|
If you are looking for the most flexible solution to be able to support multiple target languages, consider [SWIG](http://swig.org)
|
||||||
|
|
119
cheatsheet.md
119
cheatsheet.md
@ -27,6 +27,13 @@ chai.add(chaiscript::fun(&Class::method_name), "method_name");
|
|||||||
chai.add(chaiscript::fun(&Class::member_name), "member_name");
|
chai.add(chaiscript::fun(&Class::member_name), "member_name");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Bound Member Functions
|
||||||
|
|
||||||
|
```
|
||||||
|
chai.add(chaiscript::fun(&Class::method_name, Class_instance_ptr), "method_name");
|
||||||
|
chai.add(chaiscript::fun(&Class::member_name, Class_instance_ptr), "member_name");
|
||||||
|
```
|
||||||
|
|
||||||
### With Overloads
|
### With Overloads
|
||||||
|
|
||||||
#### Preferred
|
#### Preferred
|
||||||
@ -84,6 +91,49 @@ chai.add(chaiscript::user_type<MyClass>(), "MyClass");
|
|||||||
|
|
||||||
User defined type conversions are possible, defined in either script or in C++.
|
User defined type conversions are possible, defined in either script or in C++.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### ChaiScript Defined Conversions
|
||||||
|
|
||||||
|
Function objects (including lambdas) can be used to add type conversions
|
||||||
|
from inside of ChaiScript:
|
||||||
|
|
||||||
|
```
|
||||||
|
add_type_conversion(type("string"), type("Type_Info"), fun(s) { return type(s); });
|
||||||
|
```
|
||||||
|
|
||||||
|
### C++ Defined Conversions
|
||||||
|
|
||||||
|
Invoking a C++ type conversion possible with `static_cast`
|
||||||
|
|
||||||
|
```
|
||||||
|
chai.add(chaiscript::type_conversion<T, bool>());
|
||||||
|
```
|
||||||
|
|
||||||
|
Calling a user defined type conversion that takes a lambda
|
||||||
|
|
||||||
|
```
|
||||||
|
chai.add(chaiscript::type_conversion<TestBaseType, Type2>([](const TestBaseType &t_bt) { /* return converted thing */ }));
|
||||||
|
```
|
||||||
|
|
||||||
|
### Class Hierarchies
|
||||||
|
|
||||||
|
If you want objects to be convertable between base and derived classes, you must tell ChaiScritp about the relationship.
|
||||||
|
|
||||||
|
```
|
||||||
|
chai.add(chaiscript::base_class<Base, Derived>());
|
||||||
|
```
|
||||||
|
|
||||||
|
If you have multiple classes in your inheritance graph, you will probably want to tell ChaiScript about all relationships.
|
||||||
|
|
||||||
|
```
|
||||||
|
chai.add(chaiscript::base_class<Base, Derived>());
|
||||||
|
chai.add(chaiscript::base_class<Derived, MoreDerived>());
|
||||||
|
chai.add(chaiscript::base_class<Base, MoreDerived>());
|
||||||
|
```
|
||||||
|
|
||||||
|
### Helpers
|
||||||
|
|
||||||
A helper function exists for strongly typed and ChaiScript `Vector` function conversion definition:
|
A helper function exists for strongly typed and ChaiScript `Vector` function conversion definition:
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -97,6 +147,7 @@ chai.add(chaiscript::map_conversion<std::map<std::string, int>>());
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
This allows you to pass a ChaiScript function to a function requiring `std::vector<int>`
|
This allows you to pass a ChaiScript function to a function requiring `std::vector<int>`
|
||||||
|
|
||||||
## Adding Objects
|
## Adding Objects
|
||||||
@ -111,6 +162,7 @@ chai.add_global_const(chaiscript::const_var(somevar), "somevar"); // global cons
|
|||||||
chai.add_global(chaiscript::var(somevar), "somevar"); // global non-const, throws if object exists
|
chai.add_global(chaiscript::var(somevar), "somevar"); // global non-const, throws if object exists
|
||||||
chai.set_global(chaiscript::var(somevar), "somevar"); // global non-const, overwrites existing object
|
chai.set_global(chaiscript::var(somevar), "somevar"); // global non-const, overwrites existing object
|
||||||
```
|
```
|
||||||
|
|
||||||
# Using STL
|
# Using STL
|
||||||
ChaiScript recognize many types from STL, but you have to add specific instantiation yourself.
|
ChaiScript recognize many types from STL, but you have to add specific instantiation yourself.
|
||||||
|
|
||||||
@ -170,7 +222,7 @@ Conversion to `std::shared_ptr<T> &` is supported for function calls, but if you
|
|||||||
```cpp
|
```cpp
|
||||||
// ok this is supported, you can register it with chaiscript engine
|
// ok this is supported, you can register it with chaiscript engine
|
||||||
void nullify_shared_ptr(std::shared_ptr<int> &t) {
|
void nullify_shared_ptr(std::shared_ptr<int> &t) {
|
||||||
t == nullptr
|
t = nullptr
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -260,6 +312,44 @@ if (g2.is_var_undef()) { g2 = 4; } // only initialize g2 once, if global decl hi
|
|||||||
GLOBAL g3; // all upper case version also accepted
|
GLOBAL g3; // all upper case version also accepted
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Looping
|
||||||
|
|
||||||
|
```
|
||||||
|
// c-style for loops
|
||||||
|
for (var i = 0; i < 100; ++i) { print(i); }
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
// while
|
||||||
|
while (some_condition()) { /* do something */ }
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
// ranged for
|
||||||
|
for (x : [1,2,3]) { print(i); }
|
||||||
|
```
|
||||||
|
|
||||||
|
Each of the loop styles can be broken using the `break` statement. For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
while (some_condition()) {
|
||||||
|
/* do something */
|
||||||
|
if (another_condition()) { break; }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Conditionals
|
||||||
|
|
||||||
|
```
|
||||||
|
if (expression) { }
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
// C++17-style init-if blocks
|
||||||
|
// Value of 'statement' is scoped for entire `if` block
|
||||||
|
if (statement; expression) { }
|
||||||
|
```
|
||||||
|
|
||||||
## Built in Types
|
## Built in Types
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -418,9 +508,33 @@ the contained function.
|
|||||||
|
|
||||||
If both a 2 parameter and a 3 parameter signature match, the 3 parameter function always wins.
|
If both a 2 parameter and a 3 parameter signature match, the 3 parameter function always wins.
|
||||||
|
|
||||||
|
## Context
|
||||||
|
|
||||||
|
* `__LINE__` Current file line number
|
||||||
|
* `__FILE__` Full path of current file
|
||||||
|
* `__CLASS__` Name of current class
|
||||||
|
* `__FUNC__` Mame of current function
|
||||||
|
|
||||||
|
|
||||||
# Built In Functions
|
# Built In Functions
|
||||||
|
|
||||||
|
## Disabling Built-Ins
|
||||||
|
|
||||||
|
When constructing a ChaiScript object, a vector of parameters can be passed in to disable or enable various built-in methods.
|
||||||
|
|
||||||
|
Current options:
|
||||||
|
|
||||||
|
```
|
||||||
|
enum class Options
|
||||||
|
{
|
||||||
|
Load_Modules,
|
||||||
|
No_Load_Modules,
|
||||||
|
External_Scripts,
|
||||||
|
No_External_Scripts
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Evaluation
|
## Evaluation
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -432,4 +546,7 @@ use("filename") // evals file exactly once and returns value of last statement
|
|||||||
|
|
||||||
Both `use` and `eval_file` search the 'usepaths' passed to the ChaiScript constructor
|
Both `use` and `eval_file` search the 'usepaths' passed to the ChaiScript constructor
|
||||||
|
|
||||||
|
## JSON
|
||||||
|
|
||||||
|
* `from_json` converts a JSON string into its strongly typed (map, vector, int, double, string) representations
|
||||||
|
* `to_json` converts a ChaiScript object (either a `Object` or one of map, vector, int, double, string) tree into its JSON string representation
|
||||||
|
@ -4,7 +4,7 @@ pushd ..
|
|||||||
wget http://sourceforge.net/projects/cppcheck/files/cppcheck/1.66/cppcheck-1.66.tar.bz2
|
wget http://sourceforge.net/projects/cppcheck/files/cppcheck/1.66/cppcheck-1.66.tar.bz2
|
||||||
tar -xvf cppcheck-1.66.tar.bz2
|
tar -xvf cppcheck-1.66.tar.bz2
|
||||||
cd cppcheck-1.66
|
cd cppcheck-1.66
|
||||||
CXX=g++-4.8 make -j2
|
make -j2
|
||||||
popd
|
popd
|
||||||
../cppcheck-1.66/cppcheck --enable=all -I include --inline-suppr --suppress=missingIncludeSystem --std=c++11 --platform=unix64 src/main.cpp src/chai*.cpp --template ' - __{severity}__: [{file}:{line}](../blob/TRAVIS_COMMIT/{file}#L{line}) {message} ({id})' 2>output
|
../cppcheck-1.66/cppcheck --enable=all -I include --inline-suppr --suppress=missingIncludeSystem --std=c++11 --platform=unix64 src/main.cpp src/chai*.cpp --template ' - __{severity}__: [{file}:{line}](../blob/TRAVIS_COMMIT/{file}#L{line}) {message} ({id})' 2>output
|
||||||
sed -i "s/TRAVIS_COMMIT/${TRAVIS_COMMIT}/g" output
|
sed -i "s/TRAVIS_COMMIT/${TRAVIS_COMMIT}/g" output
|
||||||
|
@ -1,93 +0,0 @@
|
|||||||
# [PackageDev] target_format: plist, ext: tmLanguage
|
|
||||||
---
|
|
||||||
comment: 'ChaiScript Syntax: version 2.0'
|
|
||||||
fileTypes: [chai]
|
|
||||||
firstLineMatch: ^#!/usr/bin/env node
|
|
||||||
foldingStartMarker: ^.*\bdef\s*(\w+\s*)?\([^\)]*\)(\s*\{[^\}]*)?\s*$
|
|
||||||
foldingStopMarker: ^\s*\}
|
|
||||||
keyEquivalent: ^~J
|
|
||||||
name: ChaiScript
|
|
||||||
patterns:
|
|
||||||
- {comment: chaiscript shebang, match: ^#, name: comment.line.chai}
|
|
||||||
- {match: '\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?))\b', name: constant.numeric.chai}
|
|
||||||
- begin: ''''
|
|
||||||
beginCaptures:
|
|
||||||
'0': {name: punctuation.definition.string.begin.chai}
|
|
||||||
end: ''''
|
|
||||||
endCaptures:
|
|
||||||
'0': {name: punctuation.definition.string.end.chai}
|
|
||||||
name: string.quoted.single.chai
|
|
||||||
patterns:
|
|
||||||
- {match: '\\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.)', name: constant.character.escape.chai}
|
|
||||||
- begin: '"'
|
|
||||||
beginCaptures:
|
|
||||||
'0': {name: punctuation.definition.string.begin.chai}
|
|
||||||
end: '"'
|
|
||||||
endCaptures:
|
|
||||||
'0': {name: punctuation.definition.string.end.chai}
|
|
||||||
name: string.quoted.double.chai
|
|
||||||
patterns:
|
|
||||||
- {match: '\\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)', name: constant.character.escape.chai}
|
|
||||||
- begin: /\*\*(?!/)
|
|
||||||
captures:
|
|
||||||
'0': {name: punctuation.definition.comment.chai}
|
|
||||||
end: \*/
|
|
||||||
name: comment.block.documentation.chai
|
|
||||||
- begin: /\*
|
|
||||||
captures:
|
|
||||||
'0': {name: punctuation.definition.comment.chai}
|
|
||||||
end: \*/
|
|
||||||
name: comment.block.chai
|
|
||||||
- captures:
|
|
||||||
'1': {name: punctuation.definition.comment.chai}
|
|
||||||
match: (//).*$\n?
|
|
||||||
name: comment.line.double-slash.chai
|
|
||||||
- captures:
|
|
||||||
'0': {name: punctuation.definition.comment.html.chai}
|
|
||||||
'2': {name: punctuation.definition.comment.html.chai}
|
|
||||||
match: (<!--|-->)
|
|
||||||
name: comment.block.html.chai
|
|
||||||
- {match: \b(boolean|byte|char|class|double|enum|float|fun|def|int|interface|long|short|var|auto|attr)\b,
|
|
||||||
name: storage.type.chai}
|
|
||||||
- {match: \b(break|case|catch|continue|default|do|else|finally|else if|for|goto|if|return|switch|throw|try|while)\b,
|
|
||||||
name: keyword.control.chai}
|
|
||||||
- {match: \b(delete|in|instanceof|new|typeof|with)\b, name: keyword.operator.chai}
|
|
||||||
- {match: \btrue\b, name: constant.language.boolean.true.chai}
|
|
||||||
- {match: \bfalse\b, name: constant.language.boolean.false.chai}
|
|
||||||
- {match: \bnull\b, name: constant.language.null.chai}
|
|
||||||
- {match: \b(Anchor|Applet|Area|Array|Boolean|Button|Checkbox|Date|document|event|FileUpload|Form|Frame|Function|Hidden|History|Image|JavaArray|JavaClass|JavaObject|JavaPackage|java|Layer|Link|Location|Math|MimeType|Number|navigator|netscape|Object|Option|Packages|Password|Plugin|Radio|RegExp|Reset|Select|String|Style|Submit|screen|sun|Text|Textarea|window|XMLHttpRequest)\b,
|
|
||||||
name: support.class.chai}
|
|
||||||
- {match: '\b(s(h(ift|ow(Mod(elessDialog|alDialog)|Help))|croll(X|By(Pages|Lines)?|Y|To)?|t(op|rike)|i(n|zeToContent|debar|gnText)|ort|u(p|b(str(ing)?)?)|pli(ce|t)|e(nd|t(Re(sizable|questHeader)|M(i(nutes|lliseconds)|onth)|Seconds|Ho(tKeys|urs)|Year|Cursor|Time(out)?|Interval|ZOptions|Date|UTC(M(i(nutes|lliseconds)|onth)|Seconds|Hours|Date|FullYear)|FullYear|Active)|arch)|qrt|lice|avePreferences|mall)|h(ome|andleEvent)|navigate|c(har(CodeAt|At)|o(s|n(cat|textual|firm)|mpile)|eil|lear(Timeout|Interval)?|a(ptureEvents|ll)|reate(StyleSheet|Popup|EventObject))|t(o(GMTString|S(tring|ource)|U(TCString|pperCase)|Lo(caleString|werCase))|est|a(n|int(Enabled)?))|i(s(NaN|Finite)|ndexOf|talics)|d(isableExternalCapture|ump|etachEvent)|u(n(shift|taint|escape|watch)|pdateCommands)|j(oin|avaEnabled)|p(o(p|w)|ush|lugins.refresh|a(ddings|rse(Int|Float)?)|r(int|ompt|eference))|e(scape|nableExternalCapture|val|lementFromPoint|x(p|ec(Script|Command)?))|valueOf|UTC|queryCommand(State|Indeterm|Enabled|Value)|f(i(nd|le(ModifiedDate|Size|CreatedDate|UpdatedDate)|xed)|o(nt(size|color)|rward)|loor|romCharCode)|watch|l(ink|o(ad|g)|astIndexOf)|a(sin|nchor|cos|t(tachEvent|ob|an(2)?)|pply|lert|b(s|ort))|r(ou(nd|teEvents)|e(size(By|To)|calc|turnValue|place|verse|l(oad|ease(Capture|Events)))|andom)|g(o|et(ResponseHeader|M(i(nutes|lliseconds)|onth)|Se(conds|lection)|Hours|Year|Time(zoneOffset)?|Da(y|te)|UTC(M(i(nutes|lliseconds)|onth)|Seconds|Hours|Da(y|te)|FullYear)|FullYear|A(ttention|llResponseHeaders)))|m(in|ove(B(y|elow)|To(Absolute)?|Above)|ergeAttributes|a(tch|rgins|x))|b(toa|ig|o(ld|rderWidths)|link|ack))\b(?=\()',
|
|
||||||
name: support.function.chai}
|
|
||||||
- {match: '\b(s(ub(stringData|mit)|plitText|e(t(NamedItem|Attribute(Node)?)|lect))|has(ChildNodes|Feature)|namedItem|c(l(ick|o(se|neNode))|reate(C(omment|DATASection|aption)|T(Head|extNode|Foot)|DocumentFragment|ProcessingInstruction|E(ntityReference|lement)|Attribute))|tabIndex|i(nsert(Row|Before|Cell|Data)|tem)|open|delete(Row|C(ell|aption)|T(Head|Foot)|Data)|focus|write(ln)?|a(dd|ppend(Child|Data))|re(set|place(Child|Data)|move(NamedItem|Child|Attribute(Node)?)?)|get(NamedItem|Element(sBy(Name|TagName)|ById)|Attribute(Node)?)|blur)\b(?=\()',
|
|
||||||
name: support.function.dom.chai}
|
|
||||||
- {match: '(?<=\.)(s(ystemLanguage|cr(ipts|ollbars|een(X|Y|Top|Left))|t(yle(Sheets)?|atus(Text|bar)?)|ibling(Below|Above)|ource|uffixes|e(curity(Policy)?|l(ection|f)))|h(istory|ost(name)?|as(h|Focus))|y|X(MLDocument|SLDocument)|n(ext|ame(space(s|URI)|Prop))|M(IN_VALUE|AX_VALUE)|c(haracterSet|o(n(structor|trollers)|okieEnabled|lorDepth|mp(onents|lete))|urrent|puClass|l(i(p(boardData)?|entInformation)|osed|asses)|alle(e|r)|rypto)|t(o(olbar|p)|ext(Transform|Indent|Decoration|Align)|ags)|SQRT(1_2|2)|i(n(ner(Height|Width)|put)|ds|gnoreCase)|zIndex|o(scpu|n(readystatechange|Line)|uter(Height|Width)|p(sProfile|ener)|ffscreenBuffering)|NEGATIVE_INFINITY|d(i(splay|alog(Height|Top|Width|Left|Arguments)|rectories)|e(scription|fault(Status|Ch(ecked|arset)|View)))|u(ser(Profile|Language|Agent)|n(iqueID|defined)|pdateInterval)|_content|p(ixelDepth|ort|ersonalbar|kcs11|l(ugins|atform)|a(thname|dding(Right|Bottom|Top|Left)|rent(Window|Layer)?|ge(X(Offset)?|Y(Offset)?))|r(o(to(col|type)|duct(Sub)?|mpter)|e(vious|fix)))|e(n(coding|abledPlugin)|x(ternal|pando)|mbeds)|v(isibility|endor(Sub)?|Linkcolor)|URLUnencoded|P(I|OSITIVE_INFINITY)|f(ilename|o(nt(Size|Family|Weight)|rmName)|rame(s|Element)|gColor)|E|whiteSpace|l(i(stStyleType|n(eHeight|kColor))|o(ca(tion(bar)?|lName)|wsrc)|e(ngth|ft(Context)?)|a(st(M(odified|atch)|Index|Paren)|yer(s|X)|nguage))|a(pp(MinorVersion|Name|Co(deName|re)|Version)|vail(Height|Top|Width|Left)|ll|r(ity|guments)|Linkcolor|bove)|r(ight(Context)?|e(sponse(XML|Text)|adyState))|global|x|m(imeTypes|ultiline|enubar|argin(Right|Bottom|Top|Left))|L(N(10|2)|OG(10E|2E))|b(o(ttom|rder(Width|RightWidth|BottomWidth|Style|Color|TopWidth|LeftWidth))|ufferDepth|elow|ackground(Color|Image)))\b',
|
|
||||||
name: support.constant.chai}
|
|
||||||
- {match: '(?<=\.)(s(hape|ystemId|c(heme|ope|rolling)|ta(ndby|rt)|ize|ummary|pecified|e(ctionRowIndex|lected(Index)?)|rc)|h(space|t(tpEquiv|mlFor)|e(ight|aders)|ref(lang)?)|n(o(Resize|tation(s|Name)|Shade|Href|de(Name|Type|Value)|Wrap)|extSibling|ame)|c(h(ildNodes|Off|ecked|arset)?|ite|o(ntent|o(kie|rds)|de(Base|Type)?|l(s|Span|or)|mpact)|ell(s|Spacing|Padding)|l(ear|assName)|aption)|t(ype|Bodies|itle|Head|ext|a(rget|gName)|Foot)|i(sMap|ndex|d|m(plementation|ages))|o(ptions|wnerDocument|bject)|d(i(sabled|r)|o(c(type|umentElement)|main)|e(clare|f(er|ault(Selected|Checked|Value)))|at(eTime|a))|useMap|p(ublicId|arentNode|r(o(file|mpt)|eviousSibling))|e(n(ctype|tities)|vent|lements)|v(space|ersion|alue(Type)?|Link|Align)|URL|f(irstChild|orm(s)?|ace|rame(Border)?)|width|l(ink(s)?|o(ngDesc|wSrc)|a(stChild|ng|bel))|a(nchors|c(ce(ssKey|pt(Charset)?)|tion)|ttributes|pplets|l(t|ign)|r(chive|eas)|xis|Link|bbr)|r(ow(s|Span|Index)|ules|e(v|ferrer|l|adOnly))|m(ultiple|e(thod|dia)|a(rgin(Height|Width)|xLength))|b(o(dy|rder)|ackground|gColor))\b',
|
|
||||||
name: support.constant.dom.chai}
|
|
||||||
- {match: \b(ELEMENT_NODE|ATTRIBUTE_NODE|TEXT_NODE|CDATA_SECTION_NODE|ENTITY_REFERENCE_NODE|ENTITY_NODE|PROCESSING_INSTRUCTION_NODE|COMMENT_NODE|DOCUMENT_NODE|DOCUMENT_TYPE_NODE|DOCUMENT_FRAGMENT_NODE|NOTATION_NODE|INDEX_SIZE_ERR|DOMSTRING_SIZE_ERR|HIERARCHY_REQUEST_ERR|WRONG_DOCUMENT_ERR|INVALID_CHARACTER_ERR|NO_DATA_ALLOWED_ERR|NO_MODIFICATION_ALLOWED_ERR|NOT_FOUND_ERR|NOT_SUPPORTED_ERR|INUSE_ATTRIBUTE_ERR)\b,
|
|
||||||
name: support.constant.dom.chai}
|
|
||||||
- {match: '\bon(R(ow(s(inserted|delete)|e(nter|xit))|e(s(ize(start|end)?|et)|adystatechange))|Mouse(o(ut|ver)|down|up|move)|B(efore(cut|deactivate|u(nload|pdate)|p(aste|rint)|editfocus|activate)|lur)|S(croll|top|ubmit|elect(start|ionchange)?)|H(over|elp)|C(hange|ont(extmenu|rolselect)|ut|ellchange|l(ick|ose))|D(eactivate|ata(setc(hanged|omplete)|available)|r(op|ag(start|over|drop|en(ter|d)|leave)?)|blclick)|Unload|P(aste|ropertychange)|Error(update)?|Key(down|up|press)|Focus|Load|A(ctivate|fter(update|print)|bort))\b',
|
|
||||||
name: support.function.event-handler.chai}
|
|
||||||
- {match: '!|\$|%|&|\*|\-\-|\-|\+\+|\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\|\||\?\:|\*=|(?<!\()/=|%=|\+=|\-=|&=|\^=|\b(in|instanceof|new|delete|typeof|void)\b',
|
|
||||||
name: keyword.operator.chai}
|
|
||||||
- {match: \b(Infinity|NaN|undefined)\b, name: constant.language.chai}
|
|
||||||
- begin: (?<=[=(:]|^|return|&&|\|\||!)\s*(/)(?![/*+{}?])
|
|
||||||
beginCaptures:
|
|
||||||
'1': {name: punctuation.definition.string.begin.chai}
|
|
||||||
end: (/)[igm]*
|
|
||||||
endCaptures:
|
|
||||||
'1': {name: punctuation.definition.string.end.chai}
|
|
||||||
name: string.regexp.chai
|
|
||||||
patterns:
|
|
||||||
- {match: \\., name: constant.character.escape.chai}
|
|
||||||
- {match: \;, name: punctuation.terminator.statement.chai}
|
|
||||||
- {match: ',[ |\t]*', name: meta.delimiter.object.comma.chai}
|
|
||||||
- {match: \., name: meta.delimiter.method.period.chai}
|
|
||||||
- {match: '\{|\}', name: meta.brace.curly.chai}
|
|
||||||
- {match: \(|\), name: meta.brace.round.chai}
|
|
||||||
- {match: '\[|\]', name: meta.brace.square.chai}
|
|
||||||
scopeName: source.chai
|
|
||||||
uuid: 93E017CC-6F27-11D9-90EB-000D93589AF6
|
|
||||||
|
|
||||||
...
|
|
@ -1,333 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
||||||
<plist version="1.0">
|
|
||||||
<dict>
|
|
||||||
<key>comment</key>
|
|
||||||
<string>ChaiScript Syntax: version 2.0</string>
|
|
||||||
<key>fileTypes</key>
|
|
||||||
<array>
|
|
||||||
<string>chai</string>
|
|
||||||
</array>
|
|
||||||
<key>firstLineMatch</key>
|
|
||||||
<string>^#!/usr/bin/env node</string>
|
|
||||||
<key>foldingStartMarker</key>
|
|
||||||
<string>^.*\bdef\s*(\w+\s*)?\([^\)]*\)(\s*\{[^\}]*)?\s*$</string>
|
|
||||||
<key>foldingStopMarker</key>
|
|
||||||
<string>^\s*\}</string>
|
|
||||||
<key>keyEquivalent</key>
|
|
||||||
<string>^~J</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>ChaiScript</string>
|
|
||||||
<key>patterns</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>comment</key>
|
|
||||||
<string>chaiscript shebang</string>
|
|
||||||
<key>match</key>
|
|
||||||
<string>^#</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>comment.line.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?))\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.numeric.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>begin</key>
|
|
||||||
<string>'</string>
|
|
||||||
<key>beginCaptures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.string.begin.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>end</key>
|
|
||||||
<string>'</string>
|
|
||||||
<key>endCaptures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.string.end.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>string.quoted.single.chai</string>
|
|
||||||
<key>patterns</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]?|37[0-7]?|[4-7][0-7]?|.)</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.character.escape.chai</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>begin</key>
|
|
||||||
<string>"</string>
|
|
||||||
<key>beginCaptures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.string.begin.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>end</key>
|
|
||||||
<string>"</string>
|
|
||||||
<key>endCaptures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.string.end.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>string.quoted.double.chai</string>
|
|
||||||
<key>patterns</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\\(x\h{2}|[0-2][0-7]{,2}|3[0-6][0-7]|37[0-7]?|[4-7][0-7]?|.)</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.character.escape.chai</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>begin</key>
|
|
||||||
<string>/\*\*(?!/)</string>
|
|
||||||
<key>captures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.comment.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>end</key>
|
|
||||||
<string>\*/</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>comment.block.documentation.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>begin</key>
|
|
||||||
<string>/\*</string>
|
|
||||||
<key>captures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.comment.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>end</key>
|
|
||||||
<string>\*/</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>comment.block.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>captures</key>
|
|
||||||
<dict>
|
|
||||||
<key>1</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.comment.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>(//).*$\n?</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>comment.line.double-slash.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>captures</key>
|
|
||||||
<dict>
|
|
||||||
<key>0</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.comment.html.chai</string>
|
|
||||||
</dict>
|
|
||||||
<key>2</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.comment.html.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>(<!--|-->)</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>comment.block.html.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(boolean|byte|char|class|double|enum|float|fun|def|int|interface|long|short|var|auto|attr)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>storage.type.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(break|case|catch|continue|default|do|else|finally|else if|for|goto|if|return|switch|throw|try|while)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>keyword.control.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(delete|in|instanceof|new|typeof|with)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>keyword.operator.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\btrue\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.language.boolean.true.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\bfalse\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.language.boolean.false.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\bnull\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.language.null.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(Anchor|Applet|Area|Array|Boolean|Button|Checkbox|Date|document|event|FileUpload|Form|Frame|Function|Hidden|History|Image|JavaArray|JavaClass|JavaObject|JavaPackage|java|Layer|Link|Location|Math|MimeType|Number|navigator|netscape|Object|Option|Packages|Password|Plugin|Radio|RegExp|Reset|Select|String|Style|Submit|screen|sun|Text|Textarea|window|XMLHttpRequest)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.class.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(s(h(ift|ow(Mod(elessDialog|alDialog)|Help))|croll(X|By(Pages|Lines)?|Y|To)?|t(op|rike)|i(n|zeToContent|debar|gnText)|ort|u(p|b(str(ing)?)?)|pli(ce|t)|e(nd|t(Re(sizable|questHeader)|M(i(nutes|lliseconds)|onth)|Seconds|Ho(tKeys|urs)|Year|Cursor|Time(out)?|Interval|ZOptions|Date|UTC(M(i(nutes|lliseconds)|onth)|Seconds|Hours|Date|FullYear)|FullYear|Active)|arch)|qrt|lice|avePreferences|mall)|h(ome|andleEvent)|navigate|c(har(CodeAt|At)|o(s|n(cat|textual|firm)|mpile)|eil|lear(Timeout|Interval)?|a(ptureEvents|ll)|reate(StyleSheet|Popup|EventObject))|t(o(GMTString|S(tring|ource)|U(TCString|pperCase)|Lo(caleString|werCase))|est|a(n|int(Enabled)?))|i(s(NaN|Finite)|ndexOf|talics)|d(isableExternalCapture|ump|etachEvent)|u(n(shift|taint|escape|watch)|pdateCommands)|j(oin|avaEnabled)|p(o(p|w)|ush|lugins.refresh|a(ddings|rse(Int|Float)?)|r(int|ompt|eference))|e(scape|nableExternalCapture|val|lementFromPoint|x(p|ec(Script|Command)?))|valueOf|UTC|queryCommand(State|Indeterm|Enabled|Value)|f(i(nd|le(ModifiedDate|Size|CreatedDate|UpdatedDate)|xed)|o(nt(size|color)|rward)|loor|romCharCode)|watch|l(ink|o(ad|g)|astIndexOf)|a(sin|nchor|cos|t(tachEvent|ob|an(2)?)|pply|lert|b(s|ort))|r(ou(nd|teEvents)|e(size(By|To)|calc|turnValue|place|verse|l(oad|ease(Capture|Events)))|andom)|g(o|et(ResponseHeader|M(i(nutes|lliseconds)|onth)|Se(conds|lection)|Hours|Year|Time(zoneOffset)?|Da(y|te)|UTC(M(i(nutes|lliseconds)|onth)|Seconds|Hours|Da(y|te)|FullYear)|FullYear|A(ttention|llResponseHeaders)))|m(in|ove(B(y|elow)|To(Absolute)?|Above)|ergeAttributes|a(tch|rgins|x))|b(toa|ig|o(ld|rderWidths)|link|ack))\b(?=\()</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.function.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(s(ub(stringData|mit)|plitText|e(t(NamedItem|Attribute(Node)?)|lect))|has(ChildNodes|Feature)|namedItem|c(l(ick|o(se|neNode))|reate(C(omment|DATASection|aption)|T(Head|extNode|Foot)|DocumentFragment|ProcessingInstruction|E(ntityReference|lement)|Attribute))|tabIndex|i(nsert(Row|Before|Cell|Data)|tem)|open|delete(Row|C(ell|aption)|T(Head|Foot)|Data)|focus|write(ln)?|a(dd|ppend(Child|Data))|re(set|place(Child|Data)|move(NamedItem|Child|Attribute(Node)?)?)|get(NamedItem|Element(sBy(Name|TagName)|ById)|Attribute(Node)?)|blur)\b(?=\()</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.function.dom.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>(?<=\.)(s(ystemLanguage|cr(ipts|ollbars|een(X|Y|Top|Left))|t(yle(Sheets)?|atus(Text|bar)?)|ibling(Below|Above)|ource|uffixes|e(curity(Policy)?|l(ection|f)))|h(istory|ost(name)?|as(h|Focus))|y|X(MLDocument|SLDocument)|n(ext|ame(space(s|URI)|Prop))|M(IN_VALUE|AX_VALUE)|c(haracterSet|o(n(structor|trollers)|okieEnabled|lorDepth|mp(onents|lete))|urrent|puClass|l(i(p(boardData)?|entInformation)|osed|asses)|alle(e|r)|rypto)|t(o(olbar|p)|ext(Transform|Indent|Decoration|Align)|ags)|SQRT(1_2|2)|i(n(ner(Height|Width)|put)|ds|gnoreCase)|zIndex|o(scpu|n(readystatechange|Line)|uter(Height|Width)|p(sProfile|ener)|ffscreenBuffering)|NEGATIVE_INFINITY|d(i(splay|alog(Height|Top|Width|Left|Arguments)|rectories)|e(scription|fault(Status|Ch(ecked|arset)|View)))|u(ser(Profile|Language|Agent)|n(iqueID|defined)|pdateInterval)|_content|p(ixelDepth|ort|ersonalbar|kcs11|l(ugins|atform)|a(thname|dding(Right|Bottom|Top|Left)|rent(Window|Layer)?|ge(X(Offset)?|Y(Offset)?))|r(o(to(col|type)|duct(Sub)?|mpter)|e(vious|fix)))|e(n(coding|abledPlugin)|x(ternal|pando)|mbeds)|v(isibility|endor(Sub)?|Linkcolor)|URLUnencoded|P(I|OSITIVE_INFINITY)|f(ilename|o(nt(Size|Family|Weight)|rmName)|rame(s|Element)|gColor)|E|whiteSpace|l(i(stStyleType|n(eHeight|kColor))|o(ca(tion(bar)?|lName)|wsrc)|e(ngth|ft(Context)?)|a(st(M(odified|atch)|Index|Paren)|yer(s|X)|nguage))|a(pp(MinorVersion|Name|Co(deName|re)|Version)|vail(Height|Top|Width|Left)|ll|r(ity|guments)|Linkcolor|bove)|r(ight(Context)?|e(sponse(XML|Text)|adyState))|global|x|m(imeTypes|ultiline|enubar|argin(Right|Bottom|Top|Left))|L(N(10|2)|OG(10E|2E))|b(o(ttom|rder(Width|RightWidth|BottomWidth|Style|Color|TopWidth|LeftWidth))|ufferDepth|elow|ackground(Color|Image)))\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.constant.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>(?<=\.)(s(hape|ystemId|c(heme|ope|rolling)|ta(ndby|rt)|ize|ummary|pecified|e(ctionRowIndex|lected(Index)?)|rc)|h(space|t(tpEquiv|mlFor)|e(ight|aders)|ref(lang)?)|n(o(Resize|tation(s|Name)|Shade|Href|de(Name|Type|Value)|Wrap)|extSibling|ame)|c(h(ildNodes|Off|ecked|arset)?|ite|o(ntent|o(kie|rds)|de(Base|Type)?|l(s|Span|or)|mpact)|ell(s|Spacing|Padding)|l(ear|assName)|aption)|t(ype|Bodies|itle|Head|ext|a(rget|gName)|Foot)|i(sMap|ndex|d|m(plementation|ages))|o(ptions|wnerDocument|bject)|d(i(sabled|r)|o(c(type|umentElement)|main)|e(clare|f(er|ault(Selected|Checked|Value)))|at(eTime|a))|useMap|p(ublicId|arentNode|r(o(file|mpt)|eviousSibling))|e(n(ctype|tities)|vent|lements)|v(space|ersion|alue(Type)?|Link|Align)|URL|f(irstChild|orm(s)?|ace|rame(Border)?)|width|l(ink(s)?|o(ngDesc|wSrc)|a(stChild|ng|bel))|a(nchors|c(ce(ssKey|pt(Charset)?)|tion)|ttributes|pplets|l(t|ign)|r(chive|eas)|xis|Link|bbr)|r(ow(s|Span|Index)|ules|e(v|ferrer|l|adOnly))|m(ultiple|e(thod|dia)|a(rgin(Height|Width)|xLength))|b(o(dy|rder)|ackground|gColor))\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.constant.dom.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(ELEMENT_NODE|ATTRIBUTE_NODE|TEXT_NODE|CDATA_SECTION_NODE|ENTITY_REFERENCE_NODE|ENTITY_NODE|PROCESSING_INSTRUCTION_NODE|COMMENT_NODE|DOCUMENT_NODE|DOCUMENT_TYPE_NODE|DOCUMENT_FRAGMENT_NODE|NOTATION_NODE|INDEX_SIZE_ERR|DOMSTRING_SIZE_ERR|HIERARCHY_REQUEST_ERR|WRONG_DOCUMENT_ERR|INVALID_CHARACTER_ERR|NO_DATA_ALLOWED_ERR|NO_MODIFICATION_ALLOWED_ERR|NOT_FOUND_ERR|NOT_SUPPORTED_ERR|INUSE_ATTRIBUTE_ERR)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.constant.dom.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\bon(R(ow(s(inserted|delete)|e(nter|xit))|e(s(ize(start|end)?|et)|adystatechange))|Mouse(o(ut|ver)|down|up|move)|B(efore(cut|deactivate|u(nload|pdate)|p(aste|rint)|editfocus|activate)|lur)|S(croll|top|ubmit|elect(start|ionchange)?)|H(over|elp)|C(hange|ont(extmenu|rolselect)|ut|ellchange|l(ick|ose))|D(eactivate|ata(setc(hanged|omplete)|available)|r(op|ag(start|over|drop|en(ter|d)|leave)?)|blclick)|Unload|P(aste|ropertychange)|Error(update)?|Key(down|up|press)|Focus|Load|A(ctivate|fter(update|print)|bort))\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>support.function.event-handler.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>!|\$|%|&|\*|\-\-|\-|\+\+|\+|~|===|==|=|!=|!==|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\|\||\?\:|\*=|(?<!\()/=|%=|\+=|\-=|&=|\^=|\b(in|instanceof|new|delete|typeof|void)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>keyword.operator.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\b(Infinity|NaN|undefined)\b</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.language.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>begin</key>
|
|
||||||
<string>(?<=[=(:]|^|return|&&|\|\||!)\s*(/)(?![/*+{}?])</string>
|
|
||||||
<key>beginCaptures</key>
|
|
||||||
<dict>
|
|
||||||
<key>1</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.string.begin.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>end</key>
|
|
||||||
<string>(/)[igm]*</string>
|
|
||||||
<key>endCaptures</key>
|
|
||||||
<dict>
|
|
||||||
<key>1</key>
|
|
||||||
<dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.definition.string.end.chai</string>
|
|
||||||
</dict>
|
|
||||||
</dict>
|
|
||||||
<key>name</key>
|
|
||||||
<string>string.regexp.chai</string>
|
|
||||||
<key>patterns</key>
|
|
||||||
<array>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\\.</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>constant.character.escape.chai</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\;</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>punctuation.terminator.statement.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>,[ |\t]*</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>meta.delimiter.object.comma.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\.</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>meta.delimiter.method.period.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\{|\}</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>meta.brace.curly.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\(|\)</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>meta.brace.round.chai</string>
|
|
||||||
</dict>
|
|
||||||
<dict>
|
|
||||||
<key>match</key>
|
|
||||||
<string>\[|\]</string>
|
|
||||||
<key>name</key>
|
|
||||||
<string>meta.brace.square.chai</string>
|
|
||||||
</dict>
|
|
||||||
</array>
|
|
||||||
<key>scopeName</key>
|
|
||||||
<string>source.chai</string>
|
|
||||||
<key>uuid</key>
|
|
||||||
<string>93E017CC-6F27-11D9-90EB-000D93589AF6</string>
|
|
||||||
</dict>
|
|
||||||
</plist>
|
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_HPP_
|
#ifndef CHAISCRIPT_HPP_
|
||||||
#define CHAISCRIPT_HPP_
|
#define CHAISCRIPT_HPP_
|
||||||
|
|
||||||
@ -65,7 +69,7 @@
|
|||||||
/// int main()
|
/// int main()
|
||||||
/// {
|
/// {
|
||||||
/// chaiscript::ChaiScript chai;
|
/// chaiscript::ChaiScript chai;
|
||||||
/// chai.add(&function, "function");
|
/// chai.add(chaiscript::fun(&function), "function");
|
||||||
///
|
///
|
||||||
/// double d = chai.eval<double>("function(3, 4.75);");
|
/// double d = chai.eval<double>("function(3, 4.75);");
|
||||||
/// }
|
/// }
|
||||||
@ -695,11 +699,10 @@
|
|||||||
/// Begins a function or method definition
|
/// Begins a function or method definition
|
||||||
///
|
///
|
||||||
/// ~~~~~~~~
|
/// ~~~~~~~~
|
||||||
/// Function Definition ::= [annotation + CR/LF] "def" identifier "(" [[type] arg ("," [type] arg)*] ")" [":" guard] block
|
/// Function Definition ::= "def" identifier "(" [[type] arg ("," [type] arg)*] ")" [":" guard] block
|
||||||
/// Method Definition ::= [annotation + CR/LF] "def" class_name "::" method_name "(" [[type] arg ("," [type] arg)*] ")" [":" guard] block
|
/// Method Definition ::= "def" class_name "::" method_name "(" [[type] arg ("," [type] arg)*] ")" [":" guard] block
|
||||||
/// ~~~~~~~~
|
/// ~~~~~~~~
|
||||||
///
|
///
|
||||||
/// annotation: meta-annotation on function, currently used as documentation. Optional.
|
|
||||||
/// identifier: name of function. Required.
|
/// identifier: name of function. Required.
|
||||||
/// args: comma-delimited list of parameter names with optional type specifiers. Optional.
|
/// args: comma-delimited list of parameter names with optional type specifiers. Optional.
|
||||||
/// guards: guarding statement that act as a prerequisite for the function. Optional.
|
/// guards: guarding statement that act as a prerequisite for the function. Optional.
|
||||||
@ -817,16 +820,26 @@
|
|||||||
/// @namespace chaiscript::detail
|
/// @namespace chaiscript::detail
|
||||||
/// @brief Classes and functions reserved for internal use. Items in this namespace are not supported.
|
/// @brief Classes and functions reserved for internal use. Items in this namespace are not supported.
|
||||||
|
|
||||||
#include "chaiscript_defines.hpp"
|
#include "chaiscript_basic.hpp"
|
||||||
|
#include "language/chaiscript_parser.hpp"
|
||||||
#include "dispatchkit/dispatchkit.hpp"
|
#include "chaiscript_stdlib.hpp"
|
||||||
#include "dispatchkit/function_call.hpp"
|
|
||||||
#include "dispatchkit/dynamic_object.hpp"
|
|
||||||
#include "dispatchkit/boxed_number.hpp"
|
|
||||||
|
|
||||||
#include "language/chaiscript_eval.hpp"
|
|
||||||
#include "language/chaiscript_engine.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
class ChaiScript : public ChaiScript_Basic
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ChaiScript(std::vector<std::string> t_modulepaths = {},
|
||||||
|
std::vector<std::string> t_usepaths = {},
|
||||||
|
const std::vector<Options> &t_opts = {})
|
||||||
|
: ChaiScript_Basic(
|
||||||
|
chaiscript::Std_Lib::library(),
|
||||||
|
std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer_Default>>(),
|
||||||
|
t_modulepaths, t_usepaths, t_opts)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* CHAISCRIPT_HPP_ */
|
#endif /* CHAISCRIPT_HPP_ */
|
||||||
|
39
include/chaiscript/chaiscript_basic.hpp
Normal file
39
include/chaiscript/chaiscript_basic.hpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_BASIC_HPP_
|
||||||
|
#define CHAISCRIPT_BASIC_HPP_
|
||||||
|
|
||||||
|
#include "chaiscript_defines.hpp"
|
||||||
|
|
||||||
|
#include "dispatchkit/dispatchkit.hpp"
|
||||||
|
#include "dispatchkit/function_call.hpp"
|
||||||
|
#include "dispatchkit/dynamic_object.hpp"
|
||||||
|
#include "dispatchkit/boxed_number.hpp"
|
||||||
|
|
||||||
|
#include "language/chaiscript_eval.hpp"
|
||||||
|
#include "language/chaiscript_engine.hpp"
|
||||||
|
|
||||||
|
// This file includes all of the basic requirements for ChaiScript,
|
||||||
|
// to use, you might do something like:
|
||||||
|
//
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
#include "chaiscript_stdlib.hpp"
|
||||||
|
#include "language/chaiscript_parser.hpp"
|
||||||
|
|
||||||
|
ChaiScript_Basic chai(
|
||||||
|
chaiscript::Std_Lib::library(),
|
||||||
|
std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer_Default>>());
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// If you want a fully packaged ready to go ChaiScript, use chaiscript.hpp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* CHAISCRIPT_BASIC_HPP_ */
|
@ -1,7 +1,7 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_DEFINES_HPP_
|
#ifndef CHAISCRIPT_DEFINES_HPP_
|
||||||
@ -9,9 +9,13 @@
|
|||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#define CHAISCRIPT_STRINGIZE(x) "" #x
|
#define CHAISCRIPT_STRINGIZE(x) "" #x
|
||||||
#define CHAISCRIPT_COMPILER_VERSION CHAISCRIPT_STRINGIZE(_MSC_FULL_VER)
|
#define CHAISCRIPT_STRINGIZE_EXPANDED(x) CHAISCRIPT_STRINGIZE(x)
|
||||||
|
#define CHAISCRIPT_COMPILER_VERSION CHAISCRIPT_STRINGIZE_EXPANDED(_MSC_FULL_VER)
|
||||||
#define CHAISCRIPT_MSVC _MSC_VER
|
#define CHAISCRIPT_MSVC _MSC_VER
|
||||||
#define CHAISCRIPT_HAS_DECLSPEC
|
#define CHAISCRIPT_HAS_DECLSPEC
|
||||||
|
|
||||||
|
static_assert(_MSC_FULL_VER >= 190024210, "Visual C++ 2015 Update 3 or later required");
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define CHAISCRIPT_COMPILER_VERSION __VERSION__
|
#define CHAISCRIPT_COMPILER_VERSION __VERSION__
|
||||||
#endif
|
#endif
|
||||||
@ -44,10 +48,6 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CHAISCRIPT_MSVC) || (defined(__GNUC__) && __GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || (defined(__llvm__) && !defined(CHAISCRIPT_LIBCPP))
|
|
||||||
/// \todo Make this support other compilers when possible
|
|
||||||
#define CHAISCRIPT_HAS_THREAD_LOCAL
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__llvm__)
|
#if defined(__llvm__)
|
||||||
#define CHAISCRIPT_CLANG
|
#define CHAISCRIPT_CLANG
|
||||||
@ -60,6 +60,10 @@
|
|||||||
#define CHAISCRIPT_MODULE_EXPORT extern "C"
|
#define CHAISCRIPT_MODULE_EXPORT extern "C"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(CHAISCRIPT_MSVC) || (defined(__GNUC__) && __GNUC__ >= 5) || defined(CHAISCRIPT_CLANG)
|
||||||
|
#define CHAISCRIPT_UTF16_UTF32
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
#define CHAISCRIPT_DEBUG true
|
#define CHAISCRIPT_DEBUG true
|
||||||
#else
|
#else
|
||||||
@ -89,18 +93,59 @@ namespace chaiscript {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Iter, typename Distance>
|
struct Build_Info {
|
||||||
Iter advance_copy(Iter iter, Distance distance) {
|
static int version_major()
|
||||||
std::advance(iter, static_cast<typename std::iterator_traits<Iter>::difference_type>(distance));
|
{
|
||||||
return iter;
|
return chaiscript::version_major;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int version_minor()
|
||||||
|
{
|
||||||
|
return chaiscript::version_minor;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int version_patch()
|
||||||
|
{
|
||||||
|
return chaiscript::version_patch;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string version()
|
||||||
|
{
|
||||||
|
return std::to_string(version_major()) + '.' + std::to_string(version_minor()) + '.' + std::to_string(version_patch());
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string compiler_id()
|
||||||
|
{
|
||||||
|
return compiler_name() + '-' + compiler_version();
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string build_id()
|
||||||
|
{
|
||||||
|
return compiler_id() + (debug_build()?"-Debug":"-Release");
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string compiler_version()
|
||||||
|
{
|
||||||
|
return chaiscript::compiler_version;
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string compiler_name()
|
||||||
|
{
|
||||||
|
return chaiscript::compiler_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool debug_build()
|
||||||
|
{
|
||||||
|
return chaiscript::debug_build;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto parse_num(const char *t_str) -> typename std::enable_if<std::is_integral<T>::value, T>::type
|
auto parse_num(const char *t_str) -> typename std::enable_if<std::is_integral<T>::value, T>::type
|
||||||
{
|
{
|
||||||
T t = 0;
|
T t = 0;
|
||||||
for (char c = *t_str; (c = *t_str); ++t_str) {
|
for (char c = *t_str; (c = *t_str) != 0; ++t_str) {
|
||||||
if (c < '0' || c > '9') {
|
if (c < '0' || c > '9') {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
@ -161,6 +206,22 @@ namespace chaiscript {
|
|||||||
return parse_num<T>(t_str.c_str());
|
return parse_num<T>(t_str.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum class Options
|
||||||
|
{
|
||||||
|
No_Load_Modules,
|
||||||
|
Load_Modules,
|
||||||
|
No_External_Scripts,
|
||||||
|
External_Scripts
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline std::vector<Options> default_options()
|
||||||
|
{
|
||||||
|
#ifdef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
return {Options::No_Load_Modules, Options::External_Scripts};
|
||||||
|
#else
|
||||||
|
return {Options::Load_Modules, Options::External_Scripts};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -14,11 +14,17 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "chaiscript_defines.hpp"
|
#include "chaiscript_defines.hpp"
|
||||||
#include "dispatchkit/dispatchkit.hpp"
|
#include "language/chaiscript_common.hpp"
|
||||||
|
|
||||||
|
#include "dispatchkit/function_call.hpp"
|
||||||
|
|
||||||
|
//#include "dispatchkit/dispatchkit.hpp"
|
||||||
|
#include "dispatchkit/operators.hpp"
|
||||||
#include "dispatchkit/bootstrap.hpp"
|
#include "dispatchkit/bootstrap.hpp"
|
||||||
#include "dispatchkit/bootstrap_stl.hpp"
|
#include "dispatchkit/bootstrap_stl.hpp"
|
||||||
#include "dispatchkit/boxed_value.hpp"
|
//#include "dispatchkit/boxed_value.hpp"
|
||||||
#include "language/chaiscript_prelude.hpp"
|
#include "language/chaiscript_prelude.hpp"
|
||||||
|
#include "dispatchkit/register_function.hpp"
|
||||||
#include "utility/json_wrap.hpp"
|
#include "utility/json_wrap.hpp"
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_NO_THREADS
|
#ifndef CHAISCRIPT_NO_THREADS
|
||||||
@ -38,21 +44,20 @@ namespace chaiscript
|
|||||||
|
|
||||||
static ModulePtr library()
|
static ModulePtr library()
|
||||||
{
|
{
|
||||||
using namespace bootstrap;
|
auto lib = std::make_shared<Module>();
|
||||||
|
bootstrap::Bootstrap::bootstrap(*lib);
|
||||||
|
|
||||||
ModulePtr lib = Bootstrap::bootstrap();
|
bootstrap::standard_library::vector_type<std::vector<Boxed_Value> >("Vector", *lib);
|
||||||
|
bootstrap::standard_library::string_type<std::string>("string", *lib);
|
||||||
lib->add(standard_library::vector_type<std::vector<Boxed_Value> >("Vector"));
|
bootstrap::standard_library::map_type<std::map<std::string, Boxed_Value> >("Map", *lib);
|
||||||
lib->add(standard_library::string_type<std::string>("string"));
|
bootstrap::standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair", *lib);
|
||||||
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
|
#ifndef CHAISCRIPT_NO_THREADS
|
||||||
lib->add(standard_library::future_type<std::future<chaiscript::Boxed_Value>>("future"));
|
bootstrap::standard_library::future_type<std::future<chaiscript::Boxed_Value>>("future", *lib);
|
||||||
lib->add(chaiscript::fun([](const std::function<chaiscript::Boxed_Value ()> &t_func){ return std::async(std::launch::async, t_func);}), "async");
|
lib->add(chaiscript::fun([](const std::function<chaiscript::Boxed_Value ()> &t_func){ return std::async(std::launch::async, t_func);}), "async");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
lib->add(json_wrap::library());
|
json_wrap::library(*lib);
|
||||||
|
|
||||||
lib->eval(ChaiScript_Prelude::chaiscript_prelude() /*, "standard prelude"*/ );
|
lib->eval(ChaiScript_Prelude::chaiscript_prelude() /*, "standard prelude"*/ );
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_THREADING_HPP_
|
#ifndef CHAISCRIPT_THREADING_HPP_
|
||||||
#define CHAISCRIPT_THREADING_HPP_
|
#define CHAISCRIPT_THREADING_HPP_
|
||||||
|
|
||||||
@ -57,115 +61,71 @@ namespace chaiscript
|
|||||||
|
|
||||||
using std::recursive_mutex;
|
using std::recursive_mutex;
|
||||||
|
|
||||||
#ifdef CHAISCRIPT_HAS_THREAD_LOCAL
|
|
||||||
/// Typesafe thread specific storage. If threading is enabled, this class uses a mutex protected map. If
|
/// Typesafe thread specific storage. If threading is enabled, this class uses a mutex protected map. If
|
||||||
/// threading is not enabled, the class always returns the same data, regardless of which thread it is called from.
|
/// threading is not enabled, the class always returns the same data, regardless of which thread it is called from.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Thread_Storage
|
class Thread_Storage
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Thread_Storage() = default;
|
||||||
explicit Thread_Storage(void *t_key)
|
Thread_Storage(const Thread_Storage &) = delete;
|
||||||
: m_key(t_key)
|
Thread_Storage(Thread_Storage &&) = delete;
|
||||||
{
|
Thread_Storage &operator=(const Thread_Storage &) = delete;
|
||||||
}
|
Thread_Storage &operator=(Thread_Storage &&) = delete;
|
||||||
|
|
||||||
~Thread_Storage()
|
~Thread_Storage()
|
||||||
{
|
{
|
||||||
t().erase(m_key);
|
if (!destroyed) {
|
||||||
|
t().erase(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const T *operator->() const
|
inline const T *operator->() const
|
||||||
{
|
{
|
||||||
return &(t()[m_key]);
|
return &(t()[const_cast<Thread_Storage *>(this)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const T &operator*() const
|
inline const T &operator*() const
|
||||||
{
|
{
|
||||||
return t()[m_key];
|
return t()[const_cast<Thread_Storage *>(this)];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T *operator->()
|
inline T *operator->()
|
||||||
{
|
{
|
||||||
return &(t()[m_key]);
|
return &(t()[this]);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline T &operator*()
|
inline T &operator*()
|
||||||
{
|
{
|
||||||
return t()[m_key];
|
return t()[this];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *m_key;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::unordered_map<void*, T> &t()
|
struct Map_Holder {
|
||||||
|
std::unordered_map<Thread_Storage<T> *, T> map;
|
||||||
|
|
||||||
|
Map_Holder() = default;
|
||||||
|
Map_Holder(const Map_Holder &) = delete;
|
||||||
|
Map_Holder(Map_Holder &&) = delete;
|
||||||
|
Map_Holder& operator=(Map_Holder &&) = delete;
|
||||||
|
Map_Holder& operator=(const Map_Holder &&) = delete;
|
||||||
|
~Map_Holder() {
|
||||||
|
// here is the theory:
|
||||||
|
// * If the Map_Holder is destroyed before the Thread_Storage, a flag will get set
|
||||||
|
// * If destroyed after the Thread_Storage, the * will have been removed from `map` and nothing will happen
|
||||||
|
for(auto &elem : map) { elem.first->destroyed = true; }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::unordered_map<Thread_Storage<T> *, T> &t()
|
||||||
{
|
{
|
||||||
thread_local static std::unordered_map<void *, T> my_t;
|
thread_local Map_Holder my_map;
|
||||||
return my_t;
|
return my_map.map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool destroyed{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
/// Typesafe thread specific storage. If threading is enabled, this class uses a mutex protected map. If
|
|
||||||
/// threading is not enabled, the class always returns the same data, regardless of which thread it is called from.
|
|
||||||
///
|
|
||||||
/// This version is used if the compiler does not support thread_local
|
|
||||||
template<typename T>
|
|
||||||
class Thread_Storage
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
explicit Thread_Storage(void *)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const T *operator->() const
|
|
||||||
{
|
|
||||||
return get_tls().get();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const T &operator*() const
|
|
||||||
{
|
|
||||||
return *get_tls();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline T *operator->()
|
|
||||||
{
|
|
||||||
return get_tls().get();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline T &operator*()
|
|
||||||
{
|
|
||||||
return *get_tls();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
/// \todo this leaks thread instances. It needs to be culled from time to time
|
|
||||||
std::shared_ptr<T> get_tls() const
|
|
||||||
{
|
|
||||||
unique_lock<mutex> lock(m_mutex);
|
|
||||||
|
|
||||||
const auto id = std::this_thread::get_id();
|
|
||||||
auto itr = m_instances.find(id);
|
|
||||||
|
|
||||||
if (itr != m_instances.end()) { return itr->second; }
|
|
||||||
|
|
||||||
std::shared_ptr<T> new_instance(std::make_shared<T>());
|
|
||||||
|
|
||||||
m_instances.insert(std::make_pair(id, new_instance));
|
|
||||||
|
|
||||||
return new_instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mutable mutex m_mutex;
|
|
||||||
mutable std::unordered_map<std::thread::id, std::shared_ptr<T> > m_instances;
|
|
||||||
};
|
|
||||||
#endif // threading enabled but no tls
|
|
||||||
|
|
||||||
#else // threading disabled
|
#else // threading disabled
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class unique_lock
|
class unique_lock
|
||||||
|
@ -21,10 +21,7 @@ namespace chaiscript {
|
|||||||
class bad_any_cast : public std::bad_cast
|
class bad_any_cast : public std::bad_cast
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
bad_any_cast() noexcept
|
bad_any_cast() = default;
|
||||||
: m_what("bad any cast")
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bad_any_cast(const bad_any_cast &) = default;
|
bad_any_cast(const bad_any_cast &) = default;
|
||||||
|
|
||||||
@ -37,7 +34,7 @@ namespace chaiscript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_what;
|
std::string m_what = "bad any cast";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +72,7 @@ namespace chaiscript {
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void *data() override
|
void *data() override
|
||||||
{
|
{
|
||||||
return &m_data;
|
return &m_data;
|
||||||
}
|
}
|
||||||
@ -151,8 +148,7 @@ namespace chaiscript {
|
|||||||
|
|
||||||
const std::type_info & type() const
|
const std::type_info & type() const
|
||||||
{
|
{
|
||||||
if (m_data)
|
if (m_data) {
|
||||||
{
|
|
||||||
return m_data->type();
|
return m_data->type();
|
||||||
} else {
|
} else {
|
||||||
return typeid(void);
|
return typeid(void);
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BAD_BOXED_CAST_HPP_
|
#ifndef CHAISCRIPT_BAD_BOXED_CAST_HPP_
|
||||||
#define CHAISCRIPT_BAD_BOXED_CAST_HPP_
|
#define CHAISCRIPT_BAD_BOXED_CAST_HPP_
|
||||||
|
|
||||||
@ -31,31 +35,31 @@ namespace chaiscript
|
|||||||
public:
|
public:
|
||||||
bad_boxed_cast(Type_Info t_from, const std::type_info &t_to,
|
bad_boxed_cast(Type_Info t_from, const std::type_info &t_to,
|
||||||
std::string t_what) noexcept
|
std::string t_what) noexcept
|
||||||
: from(std::move(t_from)), to(&t_to), m_what(std::move(t_what))
|
: from(t_from), to(&t_to), m_what(std::move(t_what))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_boxed_cast(Type_Info t_from, const std::type_info &t_to)
|
bad_boxed_cast(Type_Info t_from, const std::type_info &t_to)
|
||||||
: from(std::move(t_from)), to(&t_to), m_what("Cannot perform boxed_cast: " + t_from.name() + " to: " + t_to.name())
|
: from(t_from), to(&t_to), m_what("Cannot perform boxed_cast: " + t_from.name() + " to: " + t_to.name())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit bad_boxed_cast(std::string t_what) noexcept
|
explicit bad_boxed_cast(std::string t_what) noexcept
|
||||||
: to(nullptr), m_what(std::move(t_what))
|
: m_what(std::move(t_what))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_boxed_cast(const bad_boxed_cast &) = default;
|
bad_boxed_cast(const bad_boxed_cast &) = default;
|
||||||
virtual ~bad_boxed_cast() noexcept = default;
|
~bad_boxed_cast() noexcept override = default;
|
||||||
|
|
||||||
/// \brief Description of what error occurred
|
/// \brief Description of what error occurred
|
||||||
virtual const char * what() const noexcept override
|
const char * what() const noexcept override
|
||||||
{
|
{
|
||||||
return m_what.c_str();
|
return m_what.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
Type_Info from; ///< Type_Info contained in the Boxed_Value
|
Type_Info from; ///< Type_Info contained in the Boxed_Value
|
||||||
const std::type_info *to; ///< std::type_info of the desired (but failed) result type
|
const std::type_info *to = nullptr; ///< std::type_info of the desired (but failed) result type
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_what;
|
std::string m_what;
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BIND_FIRST_HPP_
|
#ifndef CHAISCRIPT_BIND_FIRST_HPP_
|
||||||
#define CHAISCRIPT_BIND_FIRST_HPP_
|
#define CHAISCRIPT_BIND_FIRST_HPP_
|
||||||
|
|
||||||
@ -27,45 +31,52 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename O, typename Ret, typename P1, typename ... Param>
|
template<typename O, typename Ret, typename P1, typename ... Param>
|
||||||
std::function<Ret (Param...)> bind_first(Ret (*f)(P1, Param...), O&& o)
|
auto bind_first(Ret (*f)(P1, Param...), O&& o)
|
||||||
{
|
{
|
||||||
return std::function<Ret (Param...)>(
|
return [f, o](Param...param) -> Ret {
|
||||||
[f, o](Param...param) -> Ret {
|
return f(std::forward<O>(o), std::forward<Param>(param)...);
|
||||||
return f(std::forward<O>(o), std::forward<Param>(param)...);
|
};
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename O, typename Ret, typename Class, typename ... Param>
|
template<typename O, typename Ret, typename Class, typename ... Param>
|
||||||
std::function<Ret (Param...)> bind_first(Ret (Class::*f)(Param...), O&& o)
|
auto bind_first(Ret (Class::*f)(Param...), O&& o)
|
||||||
{
|
{
|
||||||
return std::function<Ret (Param...)>(
|
return [f, o](Param...param) -> Ret {
|
||||||
[f, o](Param...param) -> Ret {
|
return (get_pointer(o)->*f)(std::forward<Param>(param)...);
|
||||||
return (get_pointer(o)->*f)(std::forward<Param>(param)...);
|
};
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename O, typename Ret, typename Class, typename ... Param>
|
template<typename O, typename Ret, typename Class, typename ... Param>
|
||||||
std::function<Ret (Param...)> bind_first(Ret (Class::*f)(Param...) const, O&& o)
|
auto bind_first(Ret (Class::*f)(Param...) const, O&& o)
|
||||||
{
|
{
|
||||||
return std::function<Ret (Param...)>(
|
return [f, o](Param...param) -> Ret {
|
||||||
[f, o](Param...param) -> Ret {
|
return (get_pointer(o)->*f)(std::forward<Param>(param)...);
|
||||||
return (get_pointer(o)->*f)(std::forward<Param>(param)...);
|
};
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename O, typename Ret, typename P1, typename ... Param>
|
template<typename O, typename Ret, typename P1, typename ... Param>
|
||||||
std::function<Ret (Param...)> bind_first(const std::function<Ret (P1, Param...)> &f, O&& o)
|
auto bind_first(const std::function<Ret (P1, Param...)> &f, O&& o)
|
||||||
{
|
{
|
||||||
return std::function<Ret (Param...)>(
|
return [f, o](Param...param) -> Ret {
|
||||||
[f, o](Param...param) -> Ret {
|
return f(o, std::forward<Param>(param)...);
|
||||||
return f(o, std::forward<Param>(param)...);
|
};
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename F, typename O, typename Ret, typename Class, typename P1, typename ... Param>
|
||||||
|
auto bind_first(const F &fo, O&& o, Ret (Class::*f)(P1, Param...) const)
|
||||||
|
{
|
||||||
|
return [fo, o, f](Param ...param) -> Ret {
|
||||||
|
return (fo.*f)(o, std::forward<Param>(param)...);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename F, typename O>
|
||||||
|
auto bind_first(const F &f, O&& o)
|
||||||
|
{
|
||||||
|
return bind_first(f, std::forward<O>(o), &F::operator());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,31 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BOOTSTRAP_HPP_
|
#ifndef CHAISCRIPT_BOOTSTRAP_HPP_
|
||||||
#define CHAISCRIPT_BOOTSTRAP_HPP_
|
#define CHAISCRIPT_BOOTSTRAP_HPP_
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <exception>
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <vector>
|
|
||||||
#include <iterator>
|
|
||||||
|
|
||||||
#include "bad_boxed_cast.hpp"
|
|
||||||
#include "boxed_cast.hpp"
|
|
||||||
#include "boxed_number.hpp"
|
|
||||||
#include "boxed_value.hpp"
|
|
||||||
#include "dispatchkit.hpp"
|
|
||||||
#include "type_conversions.hpp"
|
|
||||||
#include "dynamic_object.hpp"
|
|
||||||
#include "operators.hpp"
|
|
||||||
#include "proxy_constructors.hpp"
|
|
||||||
#include "proxy_functions.hpp"
|
|
||||||
#include "register_function.hpp"
|
|
||||||
#include "type_info.hpp"
|
|
||||||
#include "../utility/utility.hpp"
|
#include "../utility/utility.hpp"
|
||||||
#include "../language/chaiscript_common.hpp"
|
#include "register_function.hpp"
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
/// \brief Classes and functions useful for bootstrapping of ChaiScript and adding of new types
|
/// \brief Classes and functions useful for bootstrapping of ChaiScript and adding of new types
|
||||||
namespace bootstrap
|
namespace bootstrap
|
||||||
{
|
{
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
/// \brief Constructs a new POD value object from a Boxed_Number
|
|
||||||
/// \param[in] v Boxed_Number to copy into the new object
|
|
||||||
/// \returns The newly created object.
|
|
||||||
template<typename P1>
|
|
||||||
std::shared_ptr<P1> construct_pod(const Boxed_Number &v)
|
|
||||||
{
|
|
||||||
return std::make_shared<P1>(v.get_as<P1>());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
|
template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
|
||||||
ModulePtr array(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void array(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
typedef typename std::remove_extent<T>::type ReturnType;
|
typedef typename std::remove_extent<T>::type ReturnType;
|
||||||
const auto extent = std::extent<T>::value;
|
const auto extent = std::extent<T>::value;
|
||||||
m->add(user_type<T>(), type);
|
m.add(user_type<T>(), type);
|
||||||
m->add(fun(
|
m.add(fun(
|
||||||
[extent](T& t, size_t index)->ReturnType &{
|
[extent](T& t, size_t index)->ReturnType &{
|
||||||
if (extent > 0 && index >= extent) {
|
if (extent > 0 && index >= extent) {
|
||||||
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
|
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
|
||||||
@ -67,7 +36,7 @@ namespace chaiscript
|
|||||||
), "[]"
|
), "[]"
|
||||||
);
|
);
|
||||||
|
|
||||||
m->add(fun(
|
m.add(fun(
|
||||||
[extent](const T &t, size_t index)->const ReturnType &{
|
[extent](const T &t, size_t index)->const ReturnType &{
|
||||||
if (extent > 0 && index >= extent) {
|
if (extent > 0 && index >= extent) {
|
||||||
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
|
throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent));
|
||||||
@ -78,33 +47,29 @@ namespace chaiscript
|
|||||||
), "[]"
|
), "[]"
|
||||||
);
|
);
|
||||||
|
|
||||||
m->add(fun(
|
m.add(fun(
|
||||||
[extent](const T &) {
|
[extent](const T &) {
|
||||||
return extent;
|
return extent;
|
||||||
}), "size");
|
}), "size");
|
||||||
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Adds a copy constructor for the given type to the given Model
|
/// \brief Adds a copy constructor for the given type to the given Model
|
||||||
/// \param[in] type The name of the type. The copy constructor will be named "type".
|
/// \param[in] type The name of the type. The copy constructor will be named "type".
|
||||||
/// \param[in,out] m The Module to add the copy constructor to
|
/// \param[in,out] m The Module to add the copy constructor to
|
||||||
/// \tparam T The type to add a copy constructor for
|
/// \tparam T The type to add a copy constructor for
|
||||||
/// \returns The passed in ModulePtr, or the newly constructed one if the default param is used
|
/// \returns The passed in Module
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr copy_constructor(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void copy_constructor(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(constructor<T (const T &)>(), type);
|
m.add(constructor<T (const T &)>(), type);
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users.
|
/// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users.
|
||||||
/// \tparam T Type to create comparison operators for
|
/// \tparam T Type to create comparison operators for
|
||||||
/// \param[in,out] m module to add comparison operators to
|
/// \param[in,out] m module to add comparison operators to
|
||||||
/// \returns the passed in ModulePtr or the newly constructed one if the default params are used.
|
/// \returns the passed in Module.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr opers_comparison(ModulePtr m = std::make_shared<Module>())
|
void opers_comparison(Module& m)
|
||||||
{
|
{
|
||||||
operators::equal<T>(m);
|
operators::equal<T>(m);
|
||||||
operators::greater_than<T>(m);
|
operators::greater_than<T>(m);
|
||||||
@ -112,7 +77,6 @@ namespace chaiscript
|
|||||||
operators::less_than<T>(m);
|
operators::less_than<T>(m);
|
||||||
operators::less_than_equal<T>(m);
|
operators::less_than_equal<T>(m);
|
||||||
operators::not_equal<T>(m);
|
operators::not_equal<T>(m);
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -121,15 +85,14 @@ namespace chaiscript
|
|||||||
/// \param[in] type The name of the type to add the constructors for.
|
/// \param[in] type The name of the type to add the constructors for.
|
||||||
/// \param[in,out] m The Module to add the basic constructors to
|
/// \param[in,out] m The Module to add the basic constructors to
|
||||||
/// \tparam T Type to generate basic constructors for
|
/// \tparam T Type to generate basic constructors for
|
||||||
/// \returns The passed in ModulePtr, or the newly constructed one if the default param is used
|
/// \returns The passed in Module
|
||||||
/// \sa copy_constructor
|
/// \sa copy_constructor
|
||||||
/// \sa constructor
|
/// \sa constructor
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr basic_constructors(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void basic_constructors(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(constructor<T ()>(), type);
|
m.add(constructor<T ()>(), type);
|
||||||
copy_constructor<T>(type, m);
|
copy_constructor<T>(type, m);
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Adds a constructor for a POD type
|
/// \brief Adds a constructor for a POD type
|
||||||
@ -137,22 +100,12 @@ namespace chaiscript
|
|||||||
/// \param[in] type The name of the type
|
/// \param[in] type The name of the type
|
||||||
/// \param[in,out] m The Module to add the constructor to
|
/// \param[in,out] m The Module to add the constructor to
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr construct_pod(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void construct_pod(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(fun(&detail::construct_pod<T>), type);
|
m.add(fun([](const Boxed_Number &bn){ return bn.get_as<T>(); }), type);
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// to_string function for internal use. Uses ostream operator<<
|
|
||||||
template<typename Input>
|
|
||||||
std::string to_string(Input i)
|
|
||||||
{
|
|
||||||
std::stringstream ss;
|
|
||||||
ss << i;
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Internal function for converting from a string to a value
|
/// Internal function for converting from a string to a value
|
||||||
/// uses ostream operator >> to perform the conversion
|
/// uses ostream operator >> to perform the conversion
|
||||||
template<typename Input>
|
template<typename Input>
|
||||||
@ -184,14 +137,13 @@ namespace chaiscript
|
|||||||
/// Add all common functions for a POD type. All operators, and
|
/// Add all common functions for a POD type. All operators, and
|
||||||
/// common conversions
|
/// common conversions
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr bootstrap_pod_type(const std::string &name, ModulePtr m = std::make_shared<Module>())
|
void bootstrap_pod_type(const std::string &name, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<T>(), name);
|
m.add(user_type<T>(), name);
|
||||||
m->add(constructor<T()>(), name);
|
m.add(constructor<T()>(), name);
|
||||||
construct_pod<T>(name, m);
|
construct_pod<T>(name, m);
|
||||||
|
|
||||||
m->add(fun(&parse_string<T>), "to_" + name);
|
m.add(fun(&parse_string<T>), "to_" + name);
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -201,15 +153,14 @@ namespace chaiscript
|
|||||||
/// for handling of Proxy_Function object (that is,
|
/// for handling of Proxy_Function object (that is,
|
||||||
/// function variables.
|
/// function variables.
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
std::shared_ptr<Type> shared_ptr_clone(const std::shared_ptr<Type> &p)
|
auto shared_ptr_clone(const std::shared_ptr<Type> &p)
|
||||||
{
|
{
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Specific version of shared_ptr_clone just for Proxy_Functions
|
/// Specific version of shared_ptr_clone just for Proxy_Functions
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
std::shared_ptr<typename std::remove_const<Type>::type>
|
std::shared_ptr<typename std::remove_const<Type>::type> shared_ptr_unconst_clone(const std::shared_ptr<typename std::add_const<Type>::type> &p)
|
||||||
shared_ptr_unconst_clone(const std::shared_ptr<typename std::add_const<Type>::type> &p)
|
|
||||||
{
|
{
|
||||||
return std::const_pointer_cast<typename std::remove_const<Type>::type>(p);
|
return std::const_pointer_cast<typename std::remove_const<Type>::type>(p);
|
||||||
}
|
}
|
||||||
@ -260,43 +211,41 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
/// Add all arithmetic operators for PODs
|
/// Add all arithmetic operators for PODs
|
||||||
static void opers_arithmetic_pod(ModulePtr m = std::make_shared<Module>())
|
static void opers_arithmetic_pod(Module& m)
|
||||||
{
|
{
|
||||||
m->add(fun(&Boxed_Number::equals), "==");
|
m.add(fun(&Boxed_Number::equals), "==");
|
||||||
m->add(fun(&Boxed_Number::less_than), "<");
|
m.add(fun(&Boxed_Number::less_than), "<");
|
||||||
m->add(fun(&Boxed_Number::greater_than), ">");
|
m.add(fun(&Boxed_Number::greater_than), ">");
|
||||||
m->add(fun(&Boxed_Number::greater_than_equal), ">=");
|
m.add(fun(&Boxed_Number::greater_than_equal), ">=");
|
||||||
m->add(fun(&Boxed_Number::less_than_equal), "<=");
|
m.add(fun(&Boxed_Number::less_than_equal), "<=");
|
||||||
m->add(fun(&Boxed_Number::not_equal), "!=");
|
m.add(fun(&Boxed_Number::not_equal), "!=");
|
||||||
|
|
||||||
m->add(fun(&Boxed_Number::pre_decrement), "--");
|
|
||||||
m->add(fun(&Boxed_Number::pre_increment), "++");
|
|
||||||
m->add(fun(&Boxed_Number::sum), "+");
|
|
||||||
m->add(fun(&Boxed_Number::unary_plus), "+");
|
|
||||||
m->add(fun(&Boxed_Number::unary_minus), "-");
|
|
||||||
m->add(fun(&Boxed_Number::difference), "-");
|
|
||||||
m->add(fun(&Boxed_Number::assign_bitwise_and), "&=");
|
|
||||||
m->add(fun(&Boxed_Number::assign), "=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_bitwise_or), "|=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_bitwise_xor), "^=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_remainder), "%=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_shift_left), "<<=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_shift_right), ">>=");
|
|
||||||
m->add(fun(&Boxed_Number::bitwise_and), "&");
|
|
||||||
m->add(fun(&Boxed_Number::bitwise_complement), "~");
|
|
||||||
m->add(fun(&Boxed_Number::bitwise_xor), "^");
|
|
||||||
m->add(fun(&Boxed_Number::bitwise_or), "|");
|
|
||||||
m->add(fun(&Boxed_Number::assign_product), "*=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_quotient), "/=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_sum), "+=");
|
|
||||||
m->add(fun(&Boxed_Number::assign_difference), "-=");
|
|
||||||
m->add(fun(&Boxed_Number::quotient), "/");
|
|
||||||
m->add(fun(&Boxed_Number::shift_left), "<<");
|
|
||||||
m->add(fun(&Boxed_Number::product), "*");
|
|
||||||
m->add(fun(&Boxed_Number::remainder), "%");
|
|
||||||
m->add(fun(&Boxed_Number::shift_right), ">>");
|
|
||||||
|
|
||||||
|
|
||||||
|
m.add(fun(&Boxed_Number::pre_decrement), "--");
|
||||||
|
m.add(fun(&Boxed_Number::pre_increment), "++");
|
||||||
|
m.add(fun(&Boxed_Number::sum), "+");
|
||||||
|
m.add(fun(&Boxed_Number::unary_plus), "+");
|
||||||
|
m.add(fun(&Boxed_Number::unary_minus), "-");
|
||||||
|
m.add(fun(&Boxed_Number::difference), "-");
|
||||||
|
m.add(fun(&Boxed_Number::assign_bitwise_and), "&=");
|
||||||
|
m.add(fun(&Boxed_Number::assign), "=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_bitwise_or), "|=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_bitwise_xor), "^=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_remainder), "%=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_shift_left), "<<=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_shift_right), ">>=");
|
||||||
|
m.add(fun(&Boxed_Number::bitwise_and), "&");
|
||||||
|
m.add(fun(&Boxed_Number::bitwise_complement), "~");
|
||||||
|
m.add(fun(&Boxed_Number::bitwise_xor), "^");
|
||||||
|
m.add(fun(&Boxed_Number::bitwise_or), "|");
|
||||||
|
m.add(fun(&Boxed_Number::assign_product), "*=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_quotient), "/=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_sum), "+=");
|
||||||
|
m.add(fun(&Boxed_Number::assign_difference), "-=");
|
||||||
|
m.add(fun(&Boxed_Number::quotient), "/");
|
||||||
|
m.add(fun(&Boxed_Number::shift_left), "<<");
|
||||||
|
m.add(fun(&Boxed_Number::product), "*");
|
||||||
|
m.add(fun(&Boxed_Number::remainder), "%");
|
||||||
|
m.add(fun(&Boxed_Number::shift_right), ">>");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a bound function object. The first param is the function to bind
|
/// Create a bound function object. The first param is the function to bind
|
||||||
@ -336,26 +285,6 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void throw_exception(const Boxed_Value &bv) {
|
|
||||||
throw bv;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string what(const std::exception &e)
|
|
||||||
{
|
|
||||||
return e.what();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Boolean specialization of internal to_string function
|
|
||||||
static std::string bool_to_string(bool b)
|
|
||||||
{
|
|
||||||
if (b)
|
|
||||||
{
|
|
||||||
return "true";
|
|
||||||
} else {
|
|
||||||
return "false";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename FunctionType>
|
template<typename FunctionType>
|
||||||
static std::vector<Boxed_Value> do_return_boxed_value_vector(FunctionType f,
|
static std::vector<Boxed_Value> do_return_boxed_value_vector(FunctionType f,
|
||||||
const dispatch::Proxy_Function_Base *b)
|
const dispatch::Proxy_Function_Base *b)
|
||||||
@ -391,7 +320,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Function>
|
template<typename Function>
|
||||||
static std::function<std::vector<Boxed_Value> (const dispatch::Proxy_Function_Base*)> return_boxed_value_vector(const Function &f)
|
static auto return_boxed_value_vector(const Function &f)
|
||||||
{
|
{
|
||||||
return [f](const dispatch::Proxy_Function_Base *b) {
|
return [f](const dispatch::Proxy_Function_Base *b) {
|
||||||
return do_return_boxed_value_vector(f, b);
|
return do_return_boxed_value_vector(f, b);
|
||||||
@ -402,57 +331,56 @@ namespace chaiscript
|
|||||||
public:
|
public:
|
||||||
/// \brief perform all common bootstrap functions for std::string, void and POD types
|
/// \brief perform all common bootstrap functions for std::string, void and POD types
|
||||||
/// \param[in,out] m Module to add bootstrapped functions to
|
/// \param[in,out] m Module to add bootstrapped functions to
|
||||||
/// \returns passed in ModulePtr, or newly created one if default argument is used
|
/// \returns passed in Module
|
||||||
static ModulePtr bootstrap(ModulePtr m = std::make_shared<Module>())
|
static void bootstrap(Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<void>(), "void");
|
m.add(user_type<void>(), "void");
|
||||||
m->add(user_type<bool>(), "bool");
|
m.add(user_type<bool>(), "bool");
|
||||||
m->add(user_type<Boxed_Value>(), "Object");
|
m.add(user_type<Boxed_Value>(), "Object");
|
||||||
m->add(user_type<Boxed_Number>(), "Number");
|
m.add(user_type<Boxed_Number>(), "Number");
|
||||||
m->add(user_type<Proxy_Function>(), "Function");
|
m.add(user_type<Proxy_Function>(), "Function");
|
||||||
m->add(user_type<dispatch::Assignable_Proxy_Function>(), "Assignable_Function");
|
m.add(user_type<dispatch::Assignable_Proxy_Function>(), "Assignable_Function");
|
||||||
m->add(user_type<std::exception>(), "exception");
|
m.add(user_type<std::exception>(), "exception");
|
||||||
|
|
||||||
m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
|
m.add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity");
|
||||||
m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation");
|
m.add(fun(&dispatch::Proxy_Function_Base::operator==), "==");
|
||||||
m->add(fun(&dispatch::Proxy_Function_Base::operator==), "==");
|
|
||||||
|
|
||||||
|
|
||||||
m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types");
|
m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types");
|
||||||
m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions");
|
m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions");
|
||||||
|
|
||||||
|
m.add(fun([](const std::exception &e){ return std::string(e.what()); }), "what");
|
||||||
|
|
||||||
m->add(user_type<std::out_of_range>(), "out_of_range");
|
m.add(user_type<std::out_of_range>(), "out_of_range");
|
||||||
m->add(user_type<std::logic_error>(), "logic_error");
|
m.add(user_type<std::logic_error>(), "logic_error");
|
||||||
m->add(chaiscript::base_class<std::exception, std::logic_error>());
|
m.add(chaiscript::base_class<std::exception, std::logic_error>());
|
||||||
m->add(chaiscript::base_class<std::logic_error, std::out_of_range>());
|
m.add(chaiscript::base_class<std::logic_error, std::out_of_range>());
|
||||||
m->add(chaiscript::base_class<std::exception, std::out_of_range>());
|
m.add(chaiscript::base_class<std::exception, std::out_of_range>());
|
||||||
|
|
||||||
m->add(user_type<std::runtime_error>(), "runtime_error");
|
m.add(user_type<std::runtime_error>(), "runtime_error");
|
||||||
m->add(chaiscript::base_class<std::exception, std::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(constructor<std::runtime_error (const std::string &)>(), "runtime_error");
|
||||||
m->add(fun(std::function<std::string (const std::runtime_error &)>(&what)), "what");
|
|
||||||
|
|
||||||
m->add(user_type<dispatch::Dynamic_Object>(), "Dynamic_Object");
|
m.add(user_type<dispatch::Dynamic_Object>(), "Dynamic_Object");
|
||||||
m->add(constructor<dispatch::Dynamic_Object (const std::string &)>(), "Dynamic_Object");
|
m.add(constructor<dispatch::Dynamic_Object (const std::string &)>(), "Dynamic_Object");
|
||||||
m->add(constructor<dispatch::Dynamic_Object ()>(), "Dynamic_Object");
|
m.add(constructor<dispatch::Dynamic_Object ()>(), "Dynamic_Object");
|
||||||
m->add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name");
|
m.add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name");
|
||||||
m->add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs");
|
m.add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs");
|
||||||
m->add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit");
|
m.add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit");
|
||||||
m->add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit");
|
m.add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit");
|
||||||
m->add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr");
|
m.add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr");
|
||||||
|
|
||||||
m->add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
|
m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
|
||||||
m->add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
|
m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "get_attr");
|
||||||
|
|
||||||
m->add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
|
m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
|
||||||
m->add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
|
m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::method_missing)), "method_missing");
|
||||||
|
|
||||||
m->add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "[]");
|
m.add(fun(static_cast<Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &)>(&dispatch::Dynamic_Object::get_attr)), "[]");
|
||||||
m->add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "[]");
|
m.add(fun(static_cast<const Boxed_Value & (dispatch::Dynamic_Object::*)(const std::string &) const>(&dispatch::Dynamic_Object::get_attr)), "[]");
|
||||||
|
|
||||||
m->eval(R"chaiscript(
|
m.eval(R"chaiscript(
|
||||||
def Dynamic_Object::clone() {
|
def Dynamic_Object::clone() {
|
||||||
auto &new_o = Dynamic_Object(this.get_type_name());
|
auto &new_o = Dynamic_Object(this.get_type_name());
|
||||||
for_each(this.get_attrs(), fun[new_o](x) { new_o.get_attr(x.first) = x.second; } );
|
for_each(this.get_attrs(), fun[new_o](x) { new_o.get_attr(x.first) = x.second; } );
|
||||||
@ -489,37 +417,37 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
)chaiscript");
|
)chaiscript");
|
||||||
|
|
||||||
m->add(fun(&has_guard), "has_guard");
|
m.add(fun(&has_guard), "has_guard");
|
||||||
m->add(fun(&get_guard), "get_guard");
|
m.add(fun(&get_guard), "get_guard");
|
||||||
|
|
||||||
m->add(fun(&Boxed_Value::is_undef), "is_var_undef");
|
m.add(fun(&Boxed_Value::is_undef), "is_var_undef");
|
||||||
m->add(fun(&Boxed_Value::is_null), "is_var_null");
|
m.add(fun(&Boxed_Value::is_null), "is_var_null");
|
||||||
m->add(fun(&Boxed_Value::is_const), "is_var_const");
|
m.add(fun(&Boxed_Value::is_const), "is_var_const");
|
||||||
m->add(fun(&Boxed_Value::is_ref), "is_var_reference");
|
m.add(fun(&Boxed_Value::is_ref), "is_var_reference");
|
||||||
m->add(fun(&Boxed_Value::is_pointer), "is_var_pointer");
|
m.add(fun(&Boxed_Value::is_pointer), "is_var_pointer");
|
||||||
m->add(fun(&Boxed_Value::is_return_value), "is_var_return_value");
|
m.add(fun(&Boxed_Value::is_return_value), "is_var_return_value");
|
||||||
m->add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value");
|
m.add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value");
|
||||||
m->add(fun(&Boxed_Value::is_type), "is_type");
|
m.add(fun(&Boxed_Value::is_type), "is_type");
|
||||||
m->add(fun(&Boxed_Value::get_attr), "get_var_attr");
|
m.add(fun(&Boxed_Value::get_attr), "get_var_attr");
|
||||||
m->add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs");
|
m.add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs");
|
||||||
m->add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs");
|
m.add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs");
|
||||||
|
|
||||||
m->add(fun(&Boxed_Value::get_type_info), "get_type_info");
|
m.add(fun(&Boxed_Value::get_type_info), "get_type_info");
|
||||||
m->add(user_type<Type_Info>(), "Type_Info");
|
m.add(user_type<Type_Info>(), "Type_Info");
|
||||||
m->add(constructor<Type_Info (const Type_Info &)>(), "Type_Info");
|
m.add(constructor<Type_Info (const Type_Info &)>(), "Type_Info");
|
||||||
|
|
||||||
|
|
||||||
operators::equal<Type_Info>(m);
|
operators::equal<Type_Info>(m);
|
||||||
|
|
||||||
m->add(fun(&Type_Info::is_const), "is_type_const");
|
m.add(fun(&Type_Info::is_const), "is_type_const");
|
||||||
m->add(fun(&Type_Info::is_reference), "is_type_reference");
|
m.add(fun(&Type_Info::is_reference), "is_type_reference");
|
||||||
m->add(fun(&Type_Info::is_void), "is_type_void");
|
m.add(fun(&Type_Info::is_void), "is_type_void");
|
||||||
m->add(fun(&Type_Info::is_undef), "is_type_undef");
|
m.add(fun(&Type_Info::is_undef), "is_type_undef");
|
||||||
m->add(fun(&Type_Info::is_pointer), "is_type_pointer");
|
m.add(fun(&Type_Info::is_pointer), "is_type_pointer");
|
||||||
m->add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic");
|
m.add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic");
|
||||||
m->add(fun(&Type_Info::name), "cpp_name");
|
m.add(fun(&Type_Info::name), "cpp_name");
|
||||||
m->add(fun(&Type_Info::bare_name), "cpp_bare_name");
|
m.add(fun(&Type_Info::bare_name), "cpp_bare_name");
|
||||||
m->add(fun(&Type_Info::bare_equal), "bare_equal");
|
m.add(fun(&Type_Info::bare_equal), "bare_equal");
|
||||||
|
|
||||||
|
|
||||||
basic_constructors<bool>("bool", m);
|
basic_constructors<bool>("bool", m);
|
||||||
@ -527,14 +455,14 @@ namespace chaiscript
|
|||||||
operators::equal<bool>(m);
|
operators::equal<bool>(m);
|
||||||
operators::not_equal<bool>(m);
|
operators::not_equal<bool>(m);
|
||||||
|
|
||||||
m->add(fun([](const std::string &s) -> std::string { return s; }), "to_string");
|
m.add(fun([](const std::string &s) { return s; }), "to_string");
|
||||||
m->add(fun(&Bootstrap::bool_to_string), "to_string");
|
m.add(fun([](const bool b) { return std::string(b?"true":"false"); }), "to_string");
|
||||||
m->add(fun(&unknown_assign), "=");
|
m.add(fun(&unknown_assign), "=");
|
||||||
m->add(fun(&throw_exception), "throw");
|
m.add(fun([](const Boxed_Value &bv) { throw bv; }), "throw");
|
||||||
m->add(fun(&what), "what");
|
|
||||||
|
m.add(fun([](const char c) { return std::string(1, c); }), "to_string");
|
||||||
|
m.add(fun(&Boxed_Number::to_string), "to_string");
|
||||||
|
|
||||||
m->add(fun(&to_string<char>), "to_string");
|
|
||||||
m->add(fun(&Boxed_Number::to_string), "to_string");
|
|
||||||
|
|
||||||
bootstrap_pod_type<double>("double", m);
|
bootstrap_pod_type<double>("double", m);
|
||||||
bootstrap_pod_type<long double>("long_double", m);
|
bootstrap_pod_type<long double>("long_double", m);
|
||||||
@ -559,58 +487,71 @@ namespace chaiscript
|
|||||||
bootstrap_pod_type<std::uint32_t>("uint32_t", m);
|
bootstrap_pod_type<std::uint32_t>("uint32_t", m);
|
||||||
bootstrap_pod_type<std::uint64_t>("uint64_t", m);
|
bootstrap_pod_type<std::uint64_t>("uint64_t", m);
|
||||||
|
|
||||||
|
|
||||||
operators::logical_compliment<bool>(m);
|
operators::logical_compliment<bool>(m);
|
||||||
|
|
||||||
opers_arithmetic_pod(m);
|
opers_arithmetic_pod(m);
|
||||||
|
|
||||||
|
|
||||||
m->add(fun(&print), "print_string");
|
m.add(fun(&Build_Info::version_major), "version_major");
|
||||||
m->add(fun(&println), "println_string");
|
m.add(fun(&Build_Info::version_minor), "version_minor");
|
||||||
|
m.add(fun(&Build_Info::version_patch), "version_patch");
|
||||||
|
m.add(fun(&Build_Info::version), "version");
|
||||||
|
m.add(fun(&Build_Info::compiler_version), "compiler_version");
|
||||||
|
m.add(fun(&Build_Info::compiler_name), "compiler_name");
|
||||||
|
m.add(fun(&Build_Info::compiler_id), "compiler_id");
|
||||||
|
m.add(fun(&Build_Info::debug_build), "debug_build");
|
||||||
|
|
||||||
m->add(dispatch::make_dynamic_proxy_function(&bind_function), "bind");
|
|
||||||
|
|
||||||
m->add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone");
|
m.add(fun(&print), "print_string");
|
||||||
m->add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "=");
|
m.add(fun(&println), "println_string");
|
||||||
m->add(fun(&ptr_assign<std::add_const<dispatch::Proxy_Function_Base>::type>), "=");
|
|
||||||
m->add(chaiscript::base_class<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function>());
|
m.add(dispatch::make_dynamic_proxy_function(&bind_function), "bind");
|
||||||
m->add(fun(
|
|
||||||
|
m.add(fun(&shared_ptr_unconst_clone<dispatch::Proxy_Function_Base>), "clone");
|
||||||
|
m.add(fun(&ptr_assign<std::remove_const<dispatch::Proxy_Function_Base>::type>), "=");
|
||||||
|
m.add(fun(&ptr_assign<std::add_const<dispatch::Proxy_Function_Base>::type>), "=");
|
||||||
|
m.add(chaiscript::base_class<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function>());
|
||||||
|
m.add(fun(
|
||||||
[](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr<const dispatch::Proxy_Function_Base> &t_rhs) {
|
[](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr<const dispatch::Proxy_Function_Base> &t_rhs) {
|
||||||
t_lhs.assign(t_rhs);
|
t_lhs.assign(t_rhs);
|
||||||
}
|
}
|
||||||
), "="
|
), "="
|
||||||
);
|
);
|
||||||
|
|
||||||
m->add(fun(&Boxed_Value::type_match), "type_match");
|
m.add(fun(&Boxed_Value::type_match), "type_match");
|
||||||
|
|
||||||
|
|
||||||
m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree");
|
m.add(chaiscript::fun(&has_parse_tree), "has_parse_tree");
|
||||||
m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree");
|
m.add(chaiscript::fun(&get_parse_tree), "get_parse_tree");
|
||||||
|
|
||||||
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>());
|
m.add(chaiscript::base_class<std::runtime_error, chaiscript::exception::eval_error>());
|
||||||
|
m.add(chaiscript::base_class<std::exception, chaiscript::exception::eval_error>());
|
||||||
|
|
||||||
m->add(chaiscript::user_type<chaiscript::exception::arithmetic_error>(), "arithmetic_error");
|
m.add(chaiscript::user_type<chaiscript::exception::arithmetic_error>(), "arithmetic_error");
|
||||||
m->add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>());
|
m.add(chaiscript::base_class<std::runtime_error, chaiscript::exception::arithmetic_error>());
|
||||||
|
m.add(chaiscript::base_class<std::exception, chaiscript::exception::arithmetic_error>());
|
||||||
|
|
||||||
|
|
||||||
// chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m);
|
// chaiscript::bootstrap::standard_library::vector_type<std::vector<std::shared_ptr<chaiscript::AST_Node> > >("AST_NodeVector", m);
|
||||||
|
|
||||||
|
|
||||||
chaiscript::utility::add_class<chaiscript::exception::eval_error>(*m,
|
chaiscript::utility::add_class<chaiscript::exception::eval_error>(m,
|
||||||
"eval_error",
|
"eval_error",
|
||||||
{ },
|
{ },
|
||||||
{ {fun(&chaiscript::exception::eval_error::reason), "reason"},
|
{ {fun(&chaiscript::exception::eval_error::reason), "reason"},
|
||||||
{fun(&chaiscript::exception::eval_error::pretty_print), "pretty_print"},
|
{fun(&chaiscript::exception::eval_error::pretty_print), "pretty_print"},
|
||||||
{fun(std::function<std::vector<Boxed_Value> (const chaiscript::exception::eval_error &t_eval_error)>([](const chaiscript::exception::eval_error &t_eval_error) -> std::vector<Boxed_Value> {
|
{fun([](const chaiscript::exception::eval_error &t_eval_error) {
|
||||||
std::vector<Boxed_Value> retval;
|
std::vector<Boxed_Value> retval;
|
||||||
std::transform(t_eval_error.call_stack.begin(), t_eval_error.call_stack.end(),
|
std::transform(t_eval_error.call_stack.begin(), t_eval_error.call_stack.end(),
|
||||||
std::back_inserter(retval),
|
std::back_inserter(retval),
|
||||||
&chaiscript::var<const std::shared_ptr<const chaiscript::AST_Node> &>);
|
&chaiscript::var<const std::shared_ptr<const chaiscript::AST_Node> &>);
|
||||||
return retval;
|
return retval;
|
||||||
})), "call_stack"} }
|
}), "call_stack"} }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
chaiscript::utility::add_class<chaiscript::File_Position>(*m,
|
chaiscript::utility::add_class<chaiscript::File_Position>(m,
|
||||||
"File_Position",
|
"File_Position",
|
||||||
{ constructor<File_Position()>(),
|
{ constructor<File_Position()>(),
|
||||||
constructor<File_Position(int, int)>() },
|
constructor<File_Position(int, int)>() },
|
||||||
@ -619,7 +560,7 @@ namespace chaiscript
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
chaiscript::utility::add_class<AST_Node>(*m,
|
chaiscript::utility::add_class<AST_Node>(m,
|
||||||
"AST_Node",
|
"AST_Node",
|
||||||
{ },
|
{ },
|
||||||
{ {fun(&AST_Node::text), "text"},
|
{ {fun(&AST_Node::text), "text"},
|
||||||
@ -628,28 +569,17 @@ namespace chaiscript
|
|||||||
{fun(&AST_Node::start), "start"},
|
{fun(&AST_Node::start), "start"},
|
||||||
{fun(&AST_Node::end), "end"},
|
{fun(&AST_Node::end), "end"},
|
||||||
{fun(&AST_Node::to_string), "to_string"},
|
{fun(&AST_Node::to_string), "to_string"},
|
||||||
{fun(std::function<std::vector<Boxed_Value> (const chaiscript::AST_Node &t_node)>([](const chaiscript::AST_Node &t_node) -> std::vector<Boxed_Value> {
|
{fun([](const chaiscript::AST_Node &t_node) -> std::vector<Boxed_Value> {
|
||||||
std::vector<Boxed_Value> retval;
|
std::vector<Boxed_Value> retval;
|
||||||
std::transform(t_node.children.begin(), t_node.children.end(),
|
const auto children = t_node.get_children();
|
||||||
|
std::transform(children.begin(), children.end(),
|
||||||
std::back_inserter(retval),
|
std::back_inserter(retval),
|
||||||
&chaiscript::var<const std::shared_ptr<chaiscript::AST_Node> &>);
|
&chaiscript::var<const std::shared_ptr<chaiscript::AST_Node> &>);
|
||||||
return retval;
|
return retval;
|
||||||
})), "children"},
|
}), "children"}
|
||||||
{fun(&AST_Node::replace_child), "replace_child"}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
chaiscript::utility::add_class<parser::ChaiScript_Parser>(*m,
|
|
||||||
"ChaiScript_Parser",
|
|
||||||
{ constructor<parser::ChaiScript_Parser ()>() },
|
|
||||||
{ {fun(&parser::ChaiScript_Parser::parse), "parse"},
|
|
||||||
{fun(&parser::ChaiScript_Parser::ast), "ast"} }
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
/// \file
|
/// \file
|
||||||
/// This file contains utility functions for registration of STL container
|
/// This file contains utility functions for registration of STL container
|
||||||
/// classes. The methodology used is based on the SGI STL concepts.
|
/// classes. The methodology used is based on the SGI STL concepts.
|
||||||
@ -37,11 +41,10 @@ namespace chaiscript
|
|||||||
/// Bidir_Range, based on the D concept of ranges.
|
/// Bidir_Range, based on the D concept of ranges.
|
||||||
/// \todo Update the Range code to base its capabilities on
|
/// \todo Update the Range code to base its capabilities on
|
||||||
/// the user_typetraits of the iterator passed in
|
/// the user_typetraits of the iterator passed in
|
||||||
template<typename Container>
|
template<typename Container, typename IterType>
|
||||||
struct Bidir_Range
|
struct Bidir_Range
|
||||||
{
|
{
|
||||||
typedef Container container_type;
|
typedef Container container_type;
|
||||||
typedef typename std::iterator_traits<typename Container::iterator>::reference reference_type;
|
|
||||||
|
|
||||||
Bidir_Range(Container &c)
|
Bidir_Range(Container &c)
|
||||||
: m_begin(c.begin()), m_end(c.end())
|
: m_begin(c.begin()), m_end(c.end())
|
||||||
@ -71,86 +74,28 @@ namespace chaiscript
|
|||||||
--m_end;
|
--m_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
reference_type front() const
|
decltype(auto) front() const
|
||||||
{
|
{
|
||||||
if (empty())
|
if (empty())
|
||||||
{
|
{
|
||||||
throw std::range_error("Range empty");
|
throw std::range_error("Range empty");
|
||||||
}
|
}
|
||||||
return *m_begin;
|
return (*m_begin);
|
||||||
}
|
}
|
||||||
|
|
||||||
reference_type back() const
|
decltype(auto) back() const
|
||||||
{
|
{
|
||||||
if (empty())
|
if (empty())
|
||||||
{
|
{
|
||||||
throw std::range_error("Range empty");
|
throw std::range_error("Range empty");
|
||||||
}
|
}
|
||||||
typename Container::iterator pos = m_end;
|
auto pos = m_end;
|
||||||
--pos;
|
--pos;
|
||||||
return *(pos);
|
return (*(pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
typename Container::iterator m_begin;
|
IterType m_begin;
|
||||||
typename Container::iterator m_end;
|
IterType m_end;
|
||||||
};
|
|
||||||
|
|
||||||
template<typename Container>
|
|
||||||
struct Const_Bidir_Range
|
|
||||||
{
|
|
||||||
typedef const Container container_type;
|
|
||||||
typedef typename std::iterator_traits<typename Container::const_iterator>::reference const_reference_type;
|
|
||||||
|
|
||||||
Const_Bidir_Range(const Container &c)
|
|
||||||
: m_begin(c.begin()), m_end(c.end())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const
|
|
||||||
{
|
|
||||||
return m_begin == m_end;
|
|
||||||
}
|
|
||||||
|
|
||||||
void pop_front()
|
|
||||||
{
|
|
||||||
if (empty())
|
|
||||||
{
|
|
||||||
throw std::range_error("Range empty");
|
|
||||||
}
|
|
||||||
++m_begin;
|
|
||||||
}
|
|
||||||
|
|
||||||
void pop_back()
|
|
||||||
{
|
|
||||||
if (empty())
|
|
||||||
{
|
|
||||||
throw std::range_error("Range empty");
|
|
||||||
}
|
|
||||||
--m_end;
|
|
||||||
}
|
|
||||||
|
|
||||||
const_reference_type front() const
|
|
||||||
{
|
|
||||||
if (empty())
|
|
||||||
{
|
|
||||||
throw std::range_error("Range empty");
|
|
||||||
}
|
|
||||||
return *m_begin;
|
|
||||||
}
|
|
||||||
|
|
||||||
const_reference_type back() const
|
|
||||||
{
|
|
||||||
if (empty())
|
|
||||||
{
|
|
||||||
throw std::range_error("Range empty");
|
|
||||||
}
|
|
||||||
typename Container::const_iterator pos = m_end;
|
|
||||||
--pos;
|
|
||||||
return *(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
typename Container::const_iterator m_begin;
|
|
||||||
typename Container::const_iterator m_end;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@ -177,21 +122,19 @@ namespace chaiscript
|
|||||||
|
|
||||||
/// Add Bidir_Range support for the given ContainerType
|
/// Add Bidir_Range support for the given ContainerType
|
||||||
template<typename Bidir_Type>
|
template<typename Bidir_Type>
|
||||||
ModulePtr input_range_type_impl(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void input_range_type_impl(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<Bidir_Type>(), type + "_Range");
|
m.add(user_type<Bidir_Type>(), type + "_Range");
|
||||||
|
|
||||||
copy_constructor<Bidir_Type>(type + "_Range", m);
|
copy_constructor<Bidir_Type>(type + "_Range", m);
|
||||||
|
|
||||||
m->add(constructor<Bidir_Type (typename Bidir_Type::container_type &)>(), "range_internal");
|
m.add(constructor<Bidir_Type (typename Bidir_Type::container_type &)>(), "range_internal");
|
||||||
|
|
||||||
m->add(fun(&Bidir_Type::empty), "empty");
|
m.add(fun(&Bidir_Type::empty), "empty");
|
||||||
m->add(fun(&Bidir_Type::pop_front), "pop_front");
|
m.add(fun(&Bidir_Type::pop_front), "pop_front");
|
||||||
m->add(fun(&Bidir_Type::front), "front");
|
m.add(fun(&Bidir_Type::front), "front");
|
||||||
m->add(fun(&Bidir_Type::pop_back), "pop_back");
|
m.add(fun(&Bidir_Type::pop_back), "pop_back");
|
||||||
m->add(fun(&Bidir_Type::back), "back");
|
m.add(fun(&Bidir_Type::back), "back");
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -230,10 +173,16 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr input_range_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void input_range_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
detail::input_range_type_impl<Bidir_Range<ContainerType> >(type,m);
|
detail::input_range_type_impl<Bidir_Range<ContainerType, typename ContainerType::iterator> >(type,m);
|
||||||
detail::input_range_type_impl<Const_Bidir_Range<ContainerType> >("Const_" + type, m);
|
detail::input_range_type_impl<Bidir_Range<const ContainerType, typename ContainerType::const_iterator> >("Const_" + type,m);
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr input_range_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
input_range_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,11 +190,11 @@ namespace chaiscript
|
|||||||
/// Add random_access_container concept to the given ContainerType
|
/// Add random_access_container concept to the given ContainerType
|
||||||
/// http://www.sgi.com/tech/stl/RandomAccessContainer.html
|
/// http://www.sgi.com/tech/stl/RandomAccessContainer.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>())
|
void random_access_container_type(const std::string &/*type*/, Module& m)
|
||||||
{
|
{
|
||||||
//In the interest of runtime safety for the m, we prefer the at() method for [] access,
|
//In the interest of runtime safety for the m, we prefer the at() method for [] access,
|
||||||
//to throw an exception in an out of bounds condition.
|
//to throw an exception in an out of bounds condition.
|
||||||
m->add(
|
m.add(
|
||||||
fun(
|
fun(
|
||||||
[](ContainerType &c, int index) -> typename ContainerType::reference {
|
[](ContainerType &c, int index) -> typename ContainerType::reference {
|
||||||
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
|
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
|
||||||
@ -253,25 +202,71 @@ namespace chaiscript
|
|||||||
return c.at(static_cast<typename ContainerType::size_type>(index));
|
return c.at(static_cast<typename ContainerType::size_type>(index));
|
||||||
}), "[]");
|
}), "[]");
|
||||||
|
|
||||||
m->add(
|
m.add(
|
||||||
fun(
|
fun(
|
||||||
[](const ContainerType &c, int index) -> typename ContainerType::const_reference {
|
[](const ContainerType &c, int index) -> typename ContainerType::const_reference {
|
||||||
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
|
/// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
|
||||||
/// during dispatch. reevaluate
|
/// during dispatch. reevaluate
|
||||||
return c.at(static_cast<typename ContainerType::size_type>(index));
|
return c.at(static_cast<typename ContainerType::size_type>(index));
|
||||||
}), "[]");
|
}), "[]");
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr random_access_container_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
random_access_container_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Add assignable concept to the given ContainerType
|
/// Add assignable concept to the given ContainerType
|
||||||
/// http://www.sgi.com/tech/stl/Assignable.html
|
/// http://www.sgi.com/tech/stl/Assignable.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr assignable_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void assignable_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
copy_constructor<ContainerType>(type, m);
|
copy_constructor<ContainerType>(type, m);
|
||||||
operators::assign<ContainerType>(m);
|
operators::assign<ContainerType>(m);
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr assignable_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
assignable_type<ContainerType>(type, *m);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Add container resize concept to the given ContainerType
|
||||||
|
/// http://www.cplusplus.com/reference/stl/
|
||||||
|
template<typename ContainerType>
|
||||||
|
void resizable_type(const std::string &/*type*/, Module& m)
|
||||||
|
{
|
||||||
|
m.add(fun([](ContainerType *a, typename ContainerType::size_type n, const typename ContainerType::value_type& val) { return a->resize(n, val); } ), "resize");
|
||||||
|
m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->resize(n); } ), "resize");
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr resizable_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
resizable_type<ContainerType>(type, *m);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Add container reserve concept to the given ContainerType
|
||||||
|
/// http://www.cplusplus.com/reference/stl/
|
||||||
|
template<typename ContainerType>
|
||||||
|
void reservable_type(const std::string &/*type*/, Module& m)
|
||||||
|
{
|
||||||
|
m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->reserve(n); } ), "reserve");
|
||||||
|
m.add(fun([](const ContainerType *a) { return a->capacity(); } ), "capacity");
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr reservable_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
reservable_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,33 +274,44 @@ namespace chaiscript
|
|||||||
/// Add container concept to the given ContainerType
|
/// Add container concept to the given ContainerType
|
||||||
/// http://www.sgi.com/tech/stl/Container.html
|
/// http://www.sgi.com/tech/stl/Container.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr container_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>())
|
void container_type(const std::string &/*type*/, Module& m)
|
||||||
{
|
{
|
||||||
m->add(fun([](const ContainerType *a) { return a->size(); } ), "size");
|
m.add(fun([](const ContainerType *a) { return a->size(); } ), "size");
|
||||||
m->add(fun([](const ContainerType *a) { return a->empty(); } ), "empty");
|
m.add(fun([](const ContainerType *a) { return a->empty(); } ), "empty");
|
||||||
m->add(fun([](ContainerType *a) { a->clear(); } ), "clear");
|
m.add(fun([](ContainerType *a) { a->clear(); } ), "clear");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
template <typename ContainerType>
|
||||||
|
ModulePtr container_type(const std::string& type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
container_type<ContainerType>(type, *m);
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Add default constructable concept to the given Type
|
/// Add default constructable concept to the given Type
|
||||||
/// http://www.sgi.com/tech/stl/DefaultConstructible.html
|
/// http://www.sgi.com/tech/stl/DefaultConstructible.html
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
ModulePtr default_constructible_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void default_constructible_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(constructor<Type ()>(), type);
|
m.add(constructor<Type ()>(), type);
|
||||||
|
}
|
||||||
|
template <typename Type>
|
||||||
|
ModulePtr default_constructible_type(const std::string& type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
default_constructible_type<Type>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Add sequence concept to the given ContainerType
|
/// Add sequence concept to the given ContainerType
|
||||||
/// http://www.sgi.com/tech/stl/Sequence.html
|
/// http://www.sgi.com/tech/stl/Sequence.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>())
|
void sequence_type(const std::string &/*type*/, Module& m)
|
||||||
{
|
{
|
||||||
m->add(fun(&detail::insert_at<ContainerType>),
|
m.add(fun(&detail::insert_at<ContainerType>),
|
||||||
[]()->std::string{
|
[]()->std::string{
|
||||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
|
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
|
||||||
return "insert_ref_at";
|
return "insert_ref_at";
|
||||||
@ -314,27 +320,31 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
m->add(fun(&detail::erase_at<ContainerType>), "erase_at");
|
m.add(fun(&detail::erase_at<ContainerType>), "erase_at");
|
||||||
|
}
|
||||||
|
template <typename ContainerType>
|
||||||
|
ModulePtr sequence_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
sequence_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Add back insertion sequence concept to the given ContainerType
|
/// Add back insertion sequence concept to the given ContainerType
|
||||||
/// http://www.sgi.com/tech/stl/BackInsertionSequence.html
|
/// http://www.sgi.com/tech/stl/BackInsertionSequence.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr back_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void back_insertion_sequence_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
typedef typename ContainerType::reference (ContainerType::*backptr)();
|
typedef typename ContainerType::reference (ContainerType::*backptr)();
|
||||||
|
|
||||||
m->add(fun(static_cast<backptr>(&ContainerType::back)), "back");
|
m.add(fun(static_cast<backptr>(&ContainerType::back)), "back");
|
||||||
|
|
||||||
|
|
||||||
typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &);
|
typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &);
|
||||||
m->add(fun(static_cast<push_back>(&ContainerType::push_back)),
|
m.add(fun(static_cast<push_back>(&ContainerType::push_back)),
|
||||||
[&]()->std::string{
|
[&]()->std::string{
|
||||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
|
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
|
||||||
m->eval(
|
m.eval(
|
||||||
"# Pushes the second value onto the container while making a clone of the value\n"
|
"# Pushes the second value onto the container while making a clone of the value\n"
|
||||||
"def push_back(" + type + " container, x)\n"
|
"def push_back(" + type + " container, x)\n"
|
||||||
"{ \n"
|
"{ \n"
|
||||||
@ -353,7 +363,13 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
m->add(fun(&ContainerType::pop_back), "pop_back");
|
m.add(fun(&ContainerType::pop_back), "pop_back");
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr back_insertion_sequence_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
back_insertion_sequence_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,20 +378,20 @@ namespace chaiscript
|
|||||||
/// Front insertion sequence
|
/// Front insertion sequence
|
||||||
/// http://www.sgi.com/tech/stl/FrontInsertionSequence.html
|
/// http://www.sgi.com/tech/stl/FrontInsertionSequence.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void front_insertion_sequence_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
typedef typename ContainerType::reference (ContainerType::*front_ptr)();
|
typedef typename ContainerType::reference (ContainerType::*front_ptr)();
|
||||||
typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const;
|
typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const;
|
||||||
typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference);
|
typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference);
|
||||||
typedef void (ContainerType::*pop_ptr)();
|
typedef void (ContainerType::*pop_ptr)();
|
||||||
|
|
||||||
m->add(fun(static_cast<front_ptr>(&ContainerType::front)), "front");
|
m.add(fun(static_cast<front_ptr>(&ContainerType::front)), "front");
|
||||||
m->add(fun(static_cast<const_front_ptr>(&ContainerType::front)), "front");
|
m.add(fun(static_cast<const_front_ptr>(&ContainerType::front)), "front");
|
||||||
|
|
||||||
m->add(fun(static_cast<push_ptr>(&ContainerType::push_front)),
|
m.add(fun(static_cast<push_ptr>(&ContainerType::push_front)),
|
||||||
[&]()->std::string{
|
[&]()->std::string{
|
||||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
|
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
|
||||||
m->eval(
|
m.eval(
|
||||||
"# Pushes the second value onto the front of container while making a clone of the value\n"
|
"# Pushes the second value onto the front of container while making a clone of the value\n"
|
||||||
"def push_front(" + type + " container, x)\n"
|
"def push_front(" + type + " container, x)\n"
|
||||||
"{ \n"
|
"{ \n"
|
||||||
@ -393,7 +409,13 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
m->add(fun(static_cast<pop_ptr>(&ContainerType::pop_front)), "pop_front");
|
m.add(fun(static_cast<pop_ptr>(&ContainerType::pop_front)), "pop_front");
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr front_insertion_sequence_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
front_insertion_sequence_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,20 +423,21 @@ namespace chaiscript
|
|||||||
/// bootstrap a given PairType
|
/// bootstrap a given PairType
|
||||||
/// http://www.sgi.com/tech/stl/pair.html
|
/// http://www.sgi.com/tech/stl/pair.html
|
||||||
template<typename PairType>
|
template<typename PairType>
|
||||||
ModulePtr pair_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void pair_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<PairType>(), type);
|
m.add(user_type<PairType>(), type);
|
||||||
|
|
||||||
|
m.add(fun(&PairType::first), "first");
|
||||||
typename PairType::first_type PairType::* f = &PairType::first;
|
m.add(fun(&PairType::second), "second");
|
||||||
typename PairType::second_type PairType::* s = &PairType::second;
|
|
||||||
|
|
||||||
m->add(fun(f), "first");
|
|
||||||
m->add(fun(s), "second");
|
|
||||||
|
|
||||||
basic_constructors<PairType>(type, m);
|
basic_constructors<PairType>(type, m);
|
||||||
m->add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
|
m.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
|
||||||
|
}
|
||||||
|
template<typename PairType>
|
||||||
|
ModulePtr pair_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
pair_type<PairType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,10 +447,15 @@ namespace chaiscript
|
|||||||
/// http://www.sgi.com/tech/stl/PairAssociativeContainer.html
|
/// http://www.sgi.com/tech/stl/PairAssociativeContainer.html
|
||||||
|
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void pair_associative_container_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
pair_type<typename ContainerType::value_type>(type + "_Pair", m);
|
pair_type<typename ContainerType::value_type>(type + "_Pair", m);
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr pair_associative_container_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
pair_associative_container_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,17 +463,17 @@ namespace chaiscript
|
|||||||
/// Add unique associative container concept to the given ContainerType
|
/// Add unique associative container concept to the given ContainerType
|
||||||
/// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
|
/// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
|
||||||
template<typename ContainerType>
|
template<typename ContainerType>
|
||||||
ModulePtr unique_associative_container_type(const std::string &/*type*/, ModulePtr m = std::make_shared<Module>())
|
void unique_associative_container_type(const std::string &/*type*/, Module& m)
|
||||||
{
|
{
|
||||||
m->add(fun(detail::count<ContainerType>), "count");
|
m.add(fun(detail::count<ContainerType>), "count");
|
||||||
|
|
||||||
typedef size_t (ContainerType::*erase_ptr)(const typename ContainerType::key_type &);
|
typedef size_t (ContainerType::*erase_ptr)(const typename ContainerType::key_type &);
|
||||||
|
|
||||||
m->add(fun(static_cast<erase_ptr>(&ContainerType::erase)), "erase");
|
m.add(fun(static_cast<erase_ptr>(&ContainerType::erase)), "erase");
|
||||||
|
|
||||||
m->add(fun(&detail::insert<ContainerType>), "insert");
|
m.add(fun(&detail::insert<ContainerType>), "insert");
|
||||||
|
|
||||||
m->add(fun(&detail::insert_ref<ContainerType>),
|
m.add(fun(&detail::insert_ref<ContainerType>),
|
||||||
[]()->std::string{
|
[]()->std::string{
|
||||||
if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) {
|
if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) {
|
||||||
return "insert_ref";
|
return "insert_ref";
|
||||||
@ -453,8 +481,12 @@ namespace chaiscript
|
|||||||
return "insert";
|
return "insert";
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
}
|
||||||
|
template<typename ContainerType>
|
||||||
|
ModulePtr unique_associative_container_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
unique_associative_container_type<ContainerType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -462,21 +494,21 @@ namespace chaiscript
|
|||||||
/// Add a MapType container
|
/// Add a MapType container
|
||||||
/// http://www.sgi.com/tech/stl/Map.html
|
/// http://www.sgi.com/tech/stl/Map.html
|
||||||
template<typename MapType>
|
template<typename MapType>
|
||||||
ModulePtr map_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void map_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<MapType>(), type);
|
m.add(user_type<MapType>(), type);
|
||||||
|
|
||||||
typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &);
|
typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &);
|
||||||
typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const;
|
typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const;
|
||||||
|
|
||||||
m->add(fun(static_cast<elem_access>(&MapType::operator[])), "[]");
|
m.add(fun(static_cast<elem_access>(&MapType::operator[])), "[]");
|
||||||
|
|
||||||
m->add(fun(static_cast<elem_access>(&MapType::at)), "at");
|
m.add(fun(static_cast<elem_access>(&MapType::at)), "at");
|
||||||
m->add(fun(static_cast<const_elem_access>(&MapType::at)), "at");
|
m.add(fun(static_cast<const_elem_access>(&MapType::at)), "at");
|
||||||
|
|
||||||
if (typeid(MapType) == typeid(std::map<std::string, Boxed_Value>))
|
if (typeid(MapType) == typeid(std::map<std::string, Boxed_Value>))
|
||||||
{
|
{
|
||||||
m->eval(R"(
|
m.eval(R"(
|
||||||
def Map::`==`(Map rhs) {
|
def Map::`==`(Map rhs) {
|
||||||
if ( rhs.size() != this.size() ) {
|
if ( rhs.size() != this.size() ) {
|
||||||
return false;
|
return false;
|
||||||
@ -504,26 +536,36 @@ namespace chaiscript
|
|||||||
unique_associative_container_type<MapType>(type, m);
|
unique_associative_container_type<MapType>(type, m);
|
||||||
pair_associative_container_type<MapType>(type, m);
|
pair_associative_container_type<MapType>(type, m);
|
||||||
input_range_type<MapType>(type, m);
|
input_range_type<MapType>(type, m);
|
||||||
|
}
|
||||||
|
template<typename MapType>
|
||||||
|
ModulePtr map_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
map_type<MapType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// hopefully working List type
|
|
||||||
/// http://www.sgi.com/tech/stl/List.html
|
/// http://www.sgi.com/tech/stl/List.html
|
||||||
template<typename ListType>
|
template<typename ListType>
|
||||||
ModulePtr list_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void list_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<ListType>(), type);
|
m.add(user_type<ListType>(), type);
|
||||||
|
|
||||||
front_insertion_sequence_type<ListType>(type, m);
|
front_insertion_sequence_type<ListType>(type, m);
|
||||||
back_insertion_sequence_type<ListType>(type, m);
|
back_insertion_sequence_type<ListType>(type, m);
|
||||||
sequence_type<ListType>(type, m);
|
sequence_type<ListType>(type, m);
|
||||||
|
resizable_type<ListType>(type, m);
|
||||||
container_type<ListType>(type, m);
|
container_type<ListType>(type, m);
|
||||||
default_constructible_type<ListType>(type, m);
|
default_constructible_type<ListType>(type, m);
|
||||||
assignable_type<ListType>(type, m);
|
assignable_type<ListType>(type, m);
|
||||||
input_range_type<ListType>(type, m);
|
input_range_type<ListType>(type, m);
|
||||||
|
}
|
||||||
|
template<typename ListType>
|
||||||
|
ModulePtr list_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
list_type<ListType>(type, m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -531,20 +573,22 @@ namespace chaiscript
|
|||||||
/// Create a vector type with associated concepts
|
/// Create a vector type with associated concepts
|
||||||
/// http://www.sgi.com/tech/stl/Vector.html
|
/// http://www.sgi.com/tech/stl/Vector.html
|
||||||
template<typename VectorType>
|
template<typename VectorType>
|
||||||
ModulePtr vector_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void vector_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<VectorType>(), type);
|
m.add(user_type<VectorType>(), type);
|
||||||
|
|
||||||
typedef typename VectorType::reference (VectorType::*frontptr)();
|
typedef typename VectorType::reference (VectorType::*frontptr)();
|
||||||
typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const;
|
typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const;
|
||||||
|
|
||||||
m->add(fun(static_cast<frontptr>(&VectorType::front)), "front");
|
m.add(fun(static_cast<frontptr>(&VectorType::front)), "front");
|
||||||
m->add(fun(static_cast<constfrontptr>(&VectorType::front)), "front");
|
m.add(fun(static_cast<constfrontptr>(&VectorType::front)), "front");
|
||||||
|
|
||||||
|
|
||||||
back_insertion_sequence_type<VectorType>(type, m);
|
back_insertion_sequence_type<VectorType>(type, m);
|
||||||
sequence_type<VectorType>(type, m);
|
sequence_type<VectorType>(type, m);
|
||||||
random_access_container_type<VectorType>(type, m);
|
random_access_container_type<VectorType>(type, m);
|
||||||
|
resizable_type<VectorType>(type, m);
|
||||||
|
reservable_type<VectorType>(type, m);
|
||||||
container_type<VectorType>(type, m);
|
container_type<VectorType>(type, m);
|
||||||
default_constructible_type<VectorType>(type, m);
|
default_constructible_type<VectorType>(type, m);
|
||||||
assignable_type<VectorType>(type, m);
|
assignable_type<VectorType>(type, m);
|
||||||
@ -552,7 +596,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
if (typeid(VectorType) == typeid(std::vector<Boxed_Value>))
|
if (typeid(VectorType) == typeid(std::vector<Boxed_Value>))
|
||||||
{
|
{
|
||||||
m->eval(R"(
|
m.eval(R"(
|
||||||
def Vector::`==`(Vector rhs) {
|
def Vector::`==`(Vector rhs) {
|
||||||
if ( rhs.size() != this.size() ) {
|
if ( rhs.size() != this.size() ) {
|
||||||
return false;
|
return false;
|
||||||
@ -573,16 +617,21 @@ namespace chaiscript
|
|||||||
} )"
|
} )"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
template<typename VectorType>
|
||||||
|
ModulePtr vector_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
vector_type<VectorType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a String container
|
/// Add a String container
|
||||||
/// http://www.sgi.com/tech/stl/basic_string.html
|
/// http://www.sgi.com/tech/stl/basic_string.html
|
||||||
template<typename String>
|
template<typename String>
|
||||||
ModulePtr string_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void string_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<String>(), type);
|
m.add(user_type<String>(), type);
|
||||||
operators::addition<String>(m);
|
operators::addition<String>(m);
|
||||||
operators::assign_sum<String>(m);
|
operators::assign_sum<String>(m);
|
||||||
opers_comparison<String>(m);
|
opers_comparison<String>(m);
|
||||||
@ -594,7 +643,7 @@ namespace chaiscript
|
|||||||
input_range_type<String>(type, m);
|
input_range_type<String>(type, m);
|
||||||
|
|
||||||
//Special case: add push_back to string (which doesn't support other back_insertion operations
|
//Special case: add push_back to string (which doesn't support other back_insertion operations
|
||||||
m->add(fun(&String::push_back),
|
m.add(fun(&String::push_back),
|
||||||
[]()->std::string{
|
[]()->std::string{
|
||||||
if (typeid(typename String::value_type) == typeid(Boxed_Value)) {
|
if (typeid(typename String::value_type) == typeid(Boxed_Value)) {
|
||||||
return "push_back_ref";
|
return "push_back_ref";
|
||||||
@ -604,21 +653,26 @@ namespace chaiscript
|
|||||||
}());
|
}());
|
||||||
|
|
||||||
|
|
||||||
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find");
|
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find");
|
||||||
m->add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind");
|
m.add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind");
|
||||||
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of");
|
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of");
|
||||||
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of");
|
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of");
|
||||||
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of");
|
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of");
|
||||||
m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of");
|
m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of");
|
||||||
|
|
||||||
m->add(fun([](String *s) { s->clear(); } ), "clear");
|
m.add(fun([](String *s) { s->clear(); } ), "clear");
|
||||||
m->add(fun([](const String *s) { return s->empty(); } ), "empty");
|
m.add(fun([](const String *s) { return s->empty(); } ), "empty");
|
||||||
m->add(fun([](const String *s) { return s->size(); } ), "size");
|
m.add(fun([](const String *s) { return s->size(); } ), "size");
|
||||||
|
|
||||||
m->add(fun([](const String *s) { return s->c_str(); } ), "c_str");
|
|
||||||
m->add(fun([](const String *s) { return s->data(); } ), "data");
|
|
||||||
m->add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr");
|
|
||||||
|
|
||||||
|
m.add(fun([](const String *s) { return s->c_str(); } ), "c_str");
|
||||||
|
m.add(fun([](const String *s) { return s->data(); } ), "data");
|
||||||
|
m.add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr");
|
||||||
|
}
|
||||||
|
template<typename String>
|
||||||
|
ModulePtr string_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
string_type<String>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -627,14 +681,19 @@ namespace chaiscript
|
|||||||
/// Add a MapType container
|
/// Add a MapType container
|
||||||
/// http://www.sgi.com/tech/stl/Map.html
|
/// http://www.sgi.com/tech/stl/Map.html
|
||||||
template<typename FutureType>
|
template<typename FutureType>
|
||||||
ModulePtr future_type(const std::string &type, ModulePtr m = std::make_shared<Module>())
|
void future_type(const std::string &type, Module& m)
|
||||||
{
|
{
|
||||||
m->add(user_type<FutureType>(), type);
|
m.add(user_type<FutureType>(), type);
|
||||||
|
|
||||||
m->add(fun([](const FutureType &t) { return t.valid(); }), "valid");
|
|
||||||
m->add(fun(&FutureType::get), "get");
|
|
||||||
m->add(fun(&FutureType::wait), "wait");
|
|
||||||
|
|
||||||
|
m.add(fun([](const FutureType &t) { return t.valid(); }), "valid");
|
||||||
|
m.add(fun(&FutureType::get), "get");
|
||||||
|
m.add(fun(&FutureType::wait), "wait");
|
||||||
|
}
|
||||||
|
template<typename FutureType>
|
||||||
|
ModulePtr future_type(const std::string &type)
|
||||||
|
{
|
||||||
|
auto m = std::make_shared<Module>();
|
||||||
|
future_type<FutureType>(type, *m);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BOXED_CAST_HPP_
|
#ifndef CHAISCRIPT_BOXED_CAST_HPP_
|
||||||
#define CHAISCRIPT_BOXED_CAST_HPP_
|
#define CHAISCRIPT_BOXED_CAST_HPP_
|
||||||
|
|
||||||
@ -69,11 +73,11 @@ namespace chaiscript
|
|||||||
/// assert(i == 5);
|
/// assert(i == 5);
|
||||||
/// \endcode
|
/// \endcode
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr)
|
decltype(auto) boxed_cast(const Boxed_Value &bv, const Type_Conversions_State *t_conversions = nullptr)
|
||||||
{
|
{
|
||||||
if (!t_conversions || bv.get_type_info().bare_equal(user_type<Type>()) || (t_conversions && !(*t_conversions)->convertable_type<Type>())) {
|
if (!t_conversions || bv.get_type_info().bare_equal(user_type<Type>()) || (t_conversions && !(*t_conversions)->convertable_type<Type>())) {
|
||||||
try {
|
try {
|
||||||
return detail::Cast_Helper<Type>::cast(bv, t_conversions);
|
return(detail::Cast_Helper<Type>::cast(bv, t_conversions));
|
||||||
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,11 +88,11 @@ namespace chaiscript
|
|||||||
try {
|
try {
|
||||||
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
|
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
|
||||||
// either way, we are not responsible if it doesn't work
|
// either way, we are not responsible if it doesn't work
|
||||||
return detail::Cast_Helper<Type>::cast((*t_conversions)->boxed_type_conversion<Type>(t_conversions->saves(), bv), t_conversions);
|
return(detail::Cast_Helper<Type>::cast((*t_conversions)->boxed_type_conversion<Type>(t_conversions->saves(), bv), t_conversions));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
try {
|
try {
|
||||||
// try going the other way
|
// try going the other way
|
||||||
return detail::Cast_Helper<Type>::cast((*t_conversions)->boxed_type_down_conversion<Type>(t_conversions->saves(), bv), t_conversions);
|
return(detail::Cast_Helper<Type>::cast((*t_conversions)->boxed_type_down_conversion<Type>(t_conversions->saves(), bv), t_conversions));
|
||||||
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
||||||
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
|
throw exception::bad_boxed_cast(bv.get_type_info(), typeid(Type));
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BOXED_CAST_HELPER_HPP_
|
#ifndef CHAISCRIPT_BOXED_CAST_HELPER_HPP_
|
||||||
#define CHAISCRIPT_BOXED_CAST_HELPER_HPP_
|
#define CHAISCRIPT_BOXED_CAST_HELPER_HPP_
|
||||||
|
|
||||||
@ -25,25 +29,54 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
T* throw_if_null(T *t)
|
T* throw_if_null(T *t)
|
||||||
{
|
{
|
||||||
if (t) return t;
|
if (t) { return t; }
|
||||||
throw std::runtime_error("Attempted to dereference null Boxed_Value");
|
throw std::runtime_error("Attempted to dereference null Boxed_Value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static const T *verify_type_no_throw(const Boxed_Value &ob, const std::type_info &ti, const T *ptr) {
|
||||||
|
if (ob.get_type_info() == ti) {
|
||||||
|
return ptr;
|
||||||
|
} else {
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T *verify_type_no_throw(const Boxed_Value &ob, const std::type_info &ti, T *ptr) {
|
||||||
|
if (!ob.is_const() && ob.get_type_info() == ti) {
|
||||||
|
return ptr;
|
||||||
|
} else {
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static const T *verify_type(const Boxed_Value &ob, const std::type_info &ti, const T *ptr) {
|
||||||
|
if (ob.get_type_info().bare_equal_type_info(ti)) {
|
||||||
|
return throw_if_null(ptr);
|
||||||
|
} else {
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static T *verify_type(const Boxed_Value &ob, const std::type_info &ti, T *ptr) {
|
||||||
|
if (!ob.is_const() && ob.get_type_info().bare_equal_type_info(ti)) {
|
||||||
|
return throw_if_null(ptr);
|
||||||
|
} else {
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Generic Cast_Helper_Inner, for casting to any type
|
/// Generic Cast_Helper_Inner, for casting to any type
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner
|
struct Cast_Helper_Inner
|
||||||
{
|
{
|
||||||
typedef typename std::add_const<Result>::type Result_Type;
|
static Result cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return *static_cast<const Result *>(verify_type(ob, typeid(Result), ob.get_const_ptr()));
|
||||||
{
|
|
||||||
auto p = throw_if_null(ob.get_const_ptr());
|
|
||||||
return *static_cast<const Result *>(p);
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -57,15 +90,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<const Result *>
|
struct Cast_Helper_Inner<const Result *>
|
||||||
{
|
{
|
||||||
typedef const Result * Result_Type;
|
static const Result * cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return static_cast<const Result *>(verify_type_no_throw(ob, typeid(Result), ob.get_const_ptr()));
|
||||||
{
|
|
||||||
return static_cast<const Result *>(ob.get_const_ptr());
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -73,15 +100,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<Result *>
|
struct Cast_Helper_Inner<Result *>
|
||||||
{
|
{
|
||||||
typedef Result * Result_Type;
|
static Result * cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().is_const() && ob.get_type_info() == typeid(Result))
|
return static_cast<Result *>(verify_type_no_throw(ob, typeid(Result), ob.get_ptr()));
|
||||||
{
|
|
||||||
return static_cast<Result *>(ob.get_ptr());
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -100,17 +121,9 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<const Result &>
|
struct Cast_Helper_Inner<const Result &>
|
||||||
{
|
{
|
||||||
typedef const Result& Result_Type;
|
static const Result & cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return *static_cast<const Result *>(verify_type(ob, typeid(Result), ob.get_const_ptr()));
|
||||||
{
|
|
||||||
auto p = throw_if_null(ob.get_const_ptr());
|
|
||||||
return *static_cast<const Result *>(p);
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -120,26 +133,61 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<Result &>
|
struct Cast_Helper_Inner<Result &>
|
||||||
{
|
{
|
||||||
typedef Result& Result_Type;
|
static Result& cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().is_const() && ob.get_type_info().bare_equal_type_info(typeid(Result)))
|
return *static_cast<Result *>(verify_type(ob, typeid(Result), ob.get_ptr()));
|
||||||
{
|
|
||||||
return *(static_cast<Result *>(throw_if_null(ob.get_ptr())));
|
|
||||||
} else {
|
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Cast_Helper_Inner for casting to a && type
|
||||||
|
template<typename Result>
|
||||||
|
struct Cast_Helper_Inner<Result &&>
|
||||||
|
{
|
||||||
|
static Result&& cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
{
|
||||||
|
return std::move(*static_cast<Result *>(verify_type(ob, typeid(Result), ob.get_ptr())));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Cast_Helper_Inner for casting to a std::unique_ptr<> && type
|
||||||
|
/// \todo Fix the fact that this has to be in a shared_ptr for now
|
||||||
|
template<typename Result>
|
||||||
|
struct Cast_Helper_Inner<std::unique_ptr<Result> &&>
|
||||||
|
{
|
||||||
|
static std::unique_ptr<Result> &&cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
{
|
||||||
|
return std::move(*(ob.get().cast<std::shared_ptr<std::unique_ptr<Result>>>()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Cast_Helper_Inner for casting to a std::unique_ptr<> & type
|
||||||
|
/// \todo Fix the fact that this has to be in a shared_ptr for now
|
||||||
|
template<typename Result>
|
||||||
|
struct Cast_Helper_Inner<std::unique_ptr<Result> &>
|
||||||
|
{
|
||||||
|
static std::unique_ptr<Result> &cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
{
|
||||||
|
return *(ob.get().cast<std::shared_ptr<std::unique_ptr<Result>>>());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Cast_Helper_Inner for casting to a std::unique_ptr<> & type
|
||||||
|
/// \todo Fix the fact that this has to be in a shared_ptr for now
|
||||||
|
template<typename Result>
|
||||||
|
struct Cast_Helper_Inner<const std::unique_ptr<Result> &>
|
||||||
|
{
|
||||||
|
static std::unique_ptr<Result> &cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
{
|
||||||
|
return *(ob.get().cast<std::shared_ptr<std::unique_ptr<Result>>>());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Cast_Helper_Inner for casting to a std::shared_ptr<> type
|
/// Cast_Helper_Inner for casting to a std::shared_ptr<> type
|
||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<std::shared_ptr<Result> >
|
struct Cast_Helper_Inner<std::shared_ptr<Result> >
|
||||||
{
|
{
|
||||||
typedef std::shared_ptr<Result> Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return ob.get().cast<std::shared_ptr<Result> >();
|
return ob.get().cast<std::shared_ptr<Result> >();
|
||||||
}
|
}
|
||||||
@ -149,9 +197,7 @@ namespace chaiscript
|
|||||||
template<typename Result>
|
template<typename Result>
|
||||||
struct Cast_Helper_Inner<std::shared_ptr<const Result> >
|
struct Cast_Helper_Inner<std::shared_ptr<const Result> >
|
||||||
{
|
{
|
||||||
typedef std::shared_ptr<const Result> Result_Type;
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
if (!ob.get_type_info().is_const())
|
if (!ob.get_type_info().is_const())
|
||||||
{
|
{
|
||||||
@ -177,10 +223,7 @@ namespace chaiscript
|
|||||||
struct Cast_Helper_Inner<std::shared_ptr<Result> &>
|
struct Cast_Helper_Inner<std::shared_ptr<Result> &>
|
||||||
{
|
{
|
||||||
static_assert(!std::is_const<Result>::value, "Non-const reference to std::shared_ptr<const T> is not supported");
|
static_assert(!std::is_const<Result>::value, "Non-const reference to std::shared_ptr<const T> is not supported");
|
||||||
|
static auto cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
typedef Boxed_Value::Sentinel<Result> Result_Type;
|
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
std::shared_ptr<Result> &res = ob.get().cast<std::shared_ptr<Result> >();
|
std::shared_ptr<Result> &res = ob.get().cast<std::shared_ptr<Result> >();
|
||||||
return ob.pointer_sentinel(res);
|
return ob.pointer_sentinel(res);
|
||||||
@ -204,9 +247,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Cast_Helper_Inner<Boxed_Value>
|
struct Cast_Helper_Inner<Boxed_Value>
|
||||||
{
|
{
|
||||||
typedef Boxed_Value Result_Type;
|
static Boxed_Value cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return ob;
|
return ob;
|
||||||
}
|
}
|
||||||
@ -216,9 +257,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Cast_Helper_Inner<Boxed_Value &>
|
struct Cast_Helper_Inner<Boxed_Value &>
|
||||||
{
|
{
|
||||||
typedef std::reference_wrapper<Boxed_Value> Result_Type;
|
static std::reference_wrapper<Boxed_Value> cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return std::ref(const_cast<Boxed_Value &>(ob));
|
return std::ref(const_cast<Boxed_Value &>(ob));
|
||||||
}
|
}
|
||||||
@ -272,11 +311,9 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Cast_Helper
|
struct Cast_Helper
|
||||||
{
|
{
|
||||||
typedef typename Cast_Helper_Inner<T>::Result_Type Result_Type;
|
static decltype(auto) cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
return Cast_Helper_Inner<T>::cast(ob, t_conversions);
|
return(Cast_Helper_Inner<T>::cast(ob, t_conversions));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BOXED_NUMERIC_HPP_
|
#ifndef CHAISCRIPT_BOXED_NUMERIC_HPP_
|
||||||
#define CHAISCRIPT_BOXED_NUMERIC_HPP_
|
#define CHAISCRIPT_BOXED_NUMERIC_HPP_
|
||||||
|
|
||||||
@ -28,9 +31,9 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
struct arithmetic_error : std::runtime_error
|
struct arithmetic_error : std::runtime_error
|
||||||
{
|
{
|
||||||
arithmetic_error(const std::string& reason) : std::runtime_error("Arithmetic error: " + reason) {}
|
explicit arithmetic_error(const std::string& reason) : std::runtime_error("Arithmetic error: " + reason) {}
|
||||||
arithmetic_error(const arithmetic_error &) = default;
|
arithmetic_error(const arithmetic_error &) = default;
|
||||||
virtual ~arithmetic_error() noexcept = default;
|
~arithmetic_error() noexcept override = default;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -162,17 +165,17 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::equals:
|
case Operators::Opers::equals:
|
||||||
return const_var(t == u);
|
return const_var(t == u);
|
||||||
case Operators::less_than:
|
case Operators::Opers::less_than:
|
||||||
return const_var(t < u);
|
return const_var(t < u);
|
||||||
case Operators::greater_than:
|
case Operators::Opers::greater_than:
|
||||||
return const_var(t > u);
|
return const_var(t > u);
|
||||||
case Operators::less_than_equal:
|
case Operators::Opers::less_than_equal:
|
||||||
return const_var(t <= u);
|
return const_var(t <= u);
|
||||||
case Operators::greater_than_equal:
|
case Operators::Opers::greater_than_equal:
|
||||||
return const_var(t >= u);
|
return const_var(t >= u);
|
||||||
case Operators::not_equal:
|
case Operators::Opers::not_equal:
|
||||||
return const_var(t != u);
|
return const_var(t != u);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -184,10 +187,10 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::pre_increment:
|
case Operators::Opers::pre_increment:
|
||||||
++t;
|
++t;
|
||||||
break;
|
break;
|
||||||
case Operators::pre_decrement:
|
case Operators::Opers::pre_decrement:
|
||||||
--t;
|
--t;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -202,20 +205,20 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::assign:
|
case Operators::Opers::assign:
|
||||||
t = u;
|
t = u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_product:
|
case Operators::Opers::assign_product:
|
||||||
t *= u;
|
t *= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_sum:
|
case Operators::Opers::assign_sum:
|
||||||
t += u;
|
t += u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_quotient:
|
case Operators::Opers::assign_quotient:
|
||||||
check_divide_by_zero(u);
|
check_divide_by_zero(u);
|
||||||
t /= u;
|
t /= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_difference:
|
case Operators::Opers::assign_difference:
|
||||||
t -= u;
|
t -= u;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -230,23 +233,23 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::assign_bitwise_and:
|
case Operators::Opers::assign_bitwise_and:
|
||||||
t &= u;
|
t &= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_bitwise_or:
|
case Operators::Opers::assign_bitwise_or:
|
||||||
t |= u;
|
t |= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_shift_left:
|
case Operators::Opers::assign_shift_left:
|
||||||
t <<= u;
|
t <<= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_shift_right:
|
case Operators::Opers::assign_shift_right:
|
||||||
t >>= u;
|
t >>= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_remainder:
|
case Operators::Opers::assign_remainder:
|
||||||
check_divide_by_zero(u);
|
check_divide_by_zero(u);
|
||||||
t %= u;
|
t %= u;
|
||||||
break;
|
break;
|
||||||
case Operators::assign_bitwise_xor:
|
case Operators::Opers::assign_bitwise_xor:
|
||||||
t ^= u;
|
t ^= u;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -260,7 +263,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::bitwise_complement:
|
case Operators::Opers::bitwise_complement:
|
||||||
return const_var(~t);
|
return const_var(~t);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -272,18 +275,18 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::shift_left:
|
case Operators::Opers::shift_left:
|
||||||
return const_var(t << u);
|
return const_var(t << u);
|
||||||
case Operators::shift_right:
|
case Operators::Opers::shift_right:
|
||||||
return const_var(t >> u);
|
return const_var(t >> u);
|
||||||
case Operators::remainder:
|
case Operators::Opers::remainder:
|
||||||
check_divide_by_zero(u);
|
check_divide_by_zero(u);
|
||||||
return const_var(t % u);
|
return const_var(t % u);
|
||||||
case Operators::bitwise_and:
|
case Operators::Opers::bitwise_and:
|
||||||
return const_var(t & u);
|
return const_var(t & u);
|
||||||
case Operators::bitwise_or:
|
case Operators::Opers::bitwise_or:
|
||||||
return const_var(t | u);
|
return const_var(t | u);
|
||||||
case Operators::bitwise_xor:
|
case Operators::Opers::bitwise_xor:
|
||||||
return const_var(t ^ u);
|
return const_var(t ^ u);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -295,9 +298,9 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::unary_minus:
|
case Operators::Opers::unary_minus:
|
||||||
return const_var(-t);
|
return const_var(-t);
|
||||||
case Operators::unary_plus:
|
case Operators::Opers::unary_plus:
|
||||||
return const_var(+t);
|
return const_var(+t);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -309,14 +312,14 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
switch (t_oper)
|
switch (t_oper)
|
||||||
{
|
{
|
||||||
case Operators::sum:
|
case Operators::Opers::sum:
|
||||||
return const_var(t + u);
|
return const_var(t + u);
|
||||||
case Operators::quotient:
|
case Operators::Opers::quotient:
|
||||||
check_divide_by_zero(u);
|
check_divide_by_zero(u);
|
||||||
return const_var(t / u);
|
return const_var(t / u);
|
||||||
case Operators::product:
|
case Operators::Opers::product:
|
||||||
return const_var(t * u);
|
return const_var(t * u);
|
||||||
case Operators::difference:
|
case Operators::Opers::difference:
|
||||||
return const_var(t - u);
|
return const_var(t - u);
|
||||||
default:
|
default:
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -328,16 +331,16 @@ namespace chaiscript
|
|||||||
-> typename std::enable_if<!std::is_floating_point<LHS>::value && !std::is_floating_point<RHS>::value, Boxed_Value>::type
|
-> typename std::enable_if<!std::is_floating_point<LHS>::value && !std::is_floating_point<RHS>::value, Boxed_Value>::type
|
||||||
{
|
{
|
||||||
typedef typename std::common_type<LHS, RHS>::type common_type;
|
typedef typename std::common_type<LHS, RHS>::type common_type;
|
||||||
if (t_oper > Operators::boolean_flag && t_oper < Operators::non_const_flag)
|
if (t_oper > Operators::Opers::boolean_flag && t_oper < Operators::Opers::non_const_flag)
|
||||||
{
|
{
|
||||||
return boolean_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
return boolean_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
||||||
} else if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
} else if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
||||||
return binary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), get_as_aux<common_type, RHS>(t_rhs), t_lhs);
|
return binary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), get_as_aux<common_type, RHS>(t_rhs), t_lhs);
|
||||||
} else if (t_oper > Operators::non_const_int_flag && t_oper < Operators::const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
} else if (t_oper > Operators::Opers::non_const_int_flag && t_oper < Operators::Opers::const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
||||||
return binary_int_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), get_as_aux<common_type, RHS>(t_rhs), t_lhs);
|
return binary_int_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), get_as_aux<common_type, RHS>(t_rhs), t_lhs);
|
||||||
} else if (t_oper > Operators::const_int_flag && t_oper < Operators::const_flag) {
|
} else if (t_oper > Operators::Opers::const_int_flag && t_oper < Operators::Opers::const_flag) {
|
||||||
return const_binary_int_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
return const_binary_int_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
||||||
} else if (t_oper > Operators::const_flag) {
|
} else if (t_oper > Operators::Opers::const_flag) {
|
||||||
return const_binary_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
return const_binary_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
||||||
} else {
|
} else {
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -349,12 +352,12 @@ namespace chaiscript
|
|||||||
-> typename std::enable_if<std::is_floating_point<LHS>::value || std::is_floating_point<RHS>::value, Boxed_Value>::type
|
-> typename std::enable_if<std::is_floating_point<LHS>::value || std::is_floating_point<RHS>::value, Boxed_Value>::type
|
||||||
{
|
{
|
||||||
typedef typename std::common_type<LHS, RHS>::type common_type;
|
typedef typename std::common_type<LHS, RHS>::type common_type;
|
||||||
if (t_oper > Operators::boolean_flag && t_oper < Operators::non_const_flag)
|
if (t_oper > Operators::Opers::boolean_flag && t_oper < Operators::Opers::non_const_flag)
|
||||||
{
|
{
|
||||||
return boolean_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
return boolean_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
||||||
} else if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
} else if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
||||||
return binary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), get_as_aux<common_type, RHS>(t_rhs), t_lhs);
|
return binary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), get_as_aux<common_type, RHS>(t_rhs), t_lhs);
|
||||||
} else if (t_oper > Operators::const_flag) {
|
} else if (t_oper > Operators::Opers::const_flag) {
|
||||||
return const_binary_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
return const_binary_go(t_oper, get_as_aux<common_type, LHS>(t_lhs), get_as_aux<common_type, RHS>(t_rhs));
|
||||||
} else {
|
} else {
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -366,11 +369,11 @@ namespace chaiscript
|
|||||||
static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs)
|
static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs)
|
||||||
-> typename std::enable_if<!std::is_floating_point<LHS>::value, Boxed_Value>::type
|
-> typename std::enable_if<!std::is_floating_point<LHS>::value, Boxed_Value>::type
|
||||||
{
|
{
|
||||||
if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
||||||
return unary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), t_lhs);
|
return unary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), t_lhs);
|
||||||
} else if (t_oper > Operators::const_int_flag && t_oper < Operators::const_flag) {
|
} else if (t_oper > Operators::Opers::const_int_flag && t_oper < Operators::Opers::const_flag) {
|
||||||
return const_unary_int_go(t_oper, *static_cast<const LHS *>(t_lhs.get_const_ptr()));
|
return const_unary_int_go(t_oper, *static_cast<const LHS *>(t_lhs.get_const_ptr()));
|
||||||
} else if (t_oper > Operators::const_flag) {
|
} else if (t_oper > Operators::Opers::const_flag) {
|
||||||
return const_unary_go(t_oper, *static_cast<const LHS *>(t_lhs.get_const_ptr()));
|
return const_unary_go(t_oper, *static_cast<const LHS *>(t_lhs.get_const_ptr()));
|
||||||
} else {
|
} else {
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -381,9 +384,9 @@ namespace chaiscript
|
|||||||
static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs)
|
static auto go(Operators::Opers t_oper, const Boxed_Value &t_lhs)
|
||||||
-> typename std::enable_if<std::is_floating_point<LHS>::value, Boxed_Value>::type
|
-> typename std::enable_if<std::is_floating_point<LHS>::value, Boxed_Value>::type
|
||||||
{
|
{
|
||||||
if (t_oper > Operators::non_const_flag && t_oper < Operators::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
if (t_oper > Operators::Opers::non_const_flag && t_oper < Operators::Opers::non_const_int_flag && !t_lhs.is_const() && !t_lhs.is_return_value()) {
|
||||||
return unary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), t_lhs);
|
return unary_go(t_oper, *static_cast<LHS *>(t_lhs.get_ptr()), t_lhs);
|
||||||
} else if (t_oper > Operators::const_flag) {
|
} else if (t_oper > Operators::Opers::const_flag) {
|
||||||
return const_unary_go(t_oper, *static_cast<const LHS *>(t_lhs.get_const_ptr()));
|
return const_unary_go(t_oper, *static_cast<const LHS *>(t_lhs.get_const_ptr()));
|
||||||
} else {
|
} else {
|
||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
@ -502,7 +505,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Boxed_Number(Boxed_Value v)
|
explicit Boxed_Number(Boxed_Value v)
|
||||||
: bv(std::move(v))
|
: bv(std::move(v))
|
||||||
{
|
{
|
||||||
validate_boxed_number(bv);
|
validate_boxed_number(bv);
|
||||||
@ -585,6 +588,67 @@ namespace chaiscript
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Source, typename Target>
|
||||||
|
static void check_type()
|
||||||
|
{
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
// MSVC complains about this being redundant / tautologica l
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4127 6287)
|
||||||
|
#endif
|
||||||
|
if (sizeof(Source) != sizeof(Target)
|
||||||
|
|| std::is_signed<Source>() != std::is_signed<Target>()
|
||||||
|
|| std::is_floating_point<Source>() != std::is_floating_point<Target>())
|
||||||
|
{
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
}
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Target> Target get_as_checked() const
|
||||||
|
{
|
||||||
|
switch (get_common_type(bv)) {
|
||||||
|
case Common_Types::t_int32:
|
||||||
|
check_type<int32_t, Target>();
|
||||||
|
return get_as_aux<Target, int32_t>(bv);
|
||||||
|
case Common_Types::t_uint8:
|
||||||
|
check_type<uint8_t, Target>();
|
||||||
|
return get_as_aux<Target, uint8_t>(bv);
|
||||||
|
case Common_Types::t_int8:
|
||||||
|
check_type<int8_t, Target>();
|
||||||
|
return get_as_aux<Target, int8_t>(bv);
|
||||||
|
case Common_Types::t_uint16:
|
||||||
|
check_type<uint16_t, Target>();
|
||||||
|
return get_as_aux<Target, uint16_t>(bv);
|
||||||
|
case Common_Types::t_int16:
|
||||||
|
check_type<int16_t, Target>();
|
||||||
|
return get_as_aux<Target, int16_t>(bv);
|
||||||
|
case Common_Types::t_uint32:
|
||||||
|
check_type<uint32_t, Target>();
|
||||||
|
return get_as_aux<Target, uint32_t>(bv);
|
||||||
|
case Common_Types::t_uint64:
|
||||||
|
check_type<uint64_t, Target>();
|
||||||
|
return get_as_aux<Target, uint64_t>(bv);
|
||||||
|
case Common_Types::t_int64:
|
||||||
|
check_type<int64_t, Target>();
|
||||||
|
return get_as_aux<Target, int64_t>(bv);
|
||||||
|
case Common_Types::t_double:
|
||||||
|
check_type<double, Target>();
|
||||||
|
return get_as_aux<Target, double>(bv);
|
||||||
|
case Common_Types::t_float:
|
||||||
|
check_type<float, Target>();
|
||||||
|
return get_as_aux<Target, float>(bv);
|
||||||
|
case Common_Types::t_long_double:
|
||||||
|
check_type<long double, Target>();
|
||||||
|
return get_as_aux<Target, long double>(bv);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Target> Target get_as() const
|
template<typename Target> Target get_as() const
|
||||||
{
|
{
|
||||||
switch (get_common_type(bv)) {
|
switch (get_common_type(bv)) {
|
||||||
@ -645,71 +709,6 @@ namespace chaiscript
|
|||||||
throw chaiscript::detail::exception::bad_any_cast();
|
throw chaiscript::detail::exception::bad_any_cast();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return boxed_cast<bool>(oper(Operators::equals, this->bv, t_rhs.bv));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator<(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return boxed_cast<bool>(oper(Operators::less_than, this->bv, t_rhs.bv));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator>(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return boxed_cast<bool>(oper(Operators::greater_than, this->bv, t_rhs.bv));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator>=(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return boxed_cast<bool>(oper(Operators::greater_than_equal, this->bv, t_rhs.bv));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator<=(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return boxed_cast<bool>(oper(Operators::less_than_equal, this->bv, t_rhs.bv));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return boxed_cast<bool>(oper(Operators::not_equal, this->bv, t_rhs.bv));
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator--()
|
|
||||||
{
|
|
||||||
return oper(Operators::pre_decrement, this->bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator++()
|
|
||||||
{
|
|
||||||
return oper(Operators::pre_increment, this->bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator+(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::sum, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator+() const
|
|
||||||
{
|
|
||||||
return oper(Operators::unary_plus, this->bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator-() const
|
|
||||||
{
|
|
||||||
return oper(Operators::unary_minus, this->bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator-(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::difference, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator&=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_bitwise_and, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void validate_boxed_number(const Boxed_Value &v)
|
static void validate_boxed_number(const Boxed_Value &v)
|
||||||
{
|
{
|
||||||
const Type_Info &inp_ = v.get_type_info();
|
const Type_Info &inp_ = v.get_type_info();
|
||||||
@ -724,266 +723,165 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cppcheck-suppress operatorEq
|
|
||||||
Boxed_Number operator=(const Boxed_Value &v)
|
|
||||||
{
|
|
||||||
validate_boxed_number(v);
|
|
||||||
bv = v;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// cppcheck-suppress operatorEq
|
|
||||||
Boxed_Number operator=(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::assign, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator|=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_bitwise_or, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator^=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_bitwise_xor, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator%=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_remainder, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator<<=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_shift_left, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator>>=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_shift_right, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator&(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::bitwise_and, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator~() const
|
|
||||||
{
|
|
||||||
return oper(Operators::bitwise_complement, this->bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator^(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::bitwise_xor, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator|(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::bitwise_or, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator*=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_product, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
Boxed_Number operator/=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_quotient, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
Boxed_Number operator+=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_sum, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
Boxed_Number operator-=(const Boxed_Number &t_rhs)
|
|
||||||
{
|
|
||||||
return oper(Operators::assign_difference, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator/(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::quotient, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator<<(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::shift_left, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator*(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::product, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator%(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::remainder, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Number operator>>(const Boxed_Number &t_rhs) const
|
|
||||||
{
|
|
||||||
return oper(Operators::shift_right, this->bv, t_rhs.bv);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static bool equals(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static bool equals(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return boxed_cast<bool>(oper(Operators::equals, t_lhs.bv, t_rhs.bv));
|
return boxed_cast<bool>(oper(Operators::Opers::equals, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool less_than(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static bool less_than(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return boxed_cast<bool>(oper(Operators::less_than, t_lhs.bv, t_rhs.bv));
|
return boxed_cast<bool>(oper(Operators::Opers::less_than, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool greater_than(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static bool greater_than(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return boxed_cast<bool>(oper(Operators::greater_than, t_lhs.bv, t_rhs.bv));
|
return boxed_cast<bool>(oper(Operators::Opers::greater_than, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool greater_than_equal(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static bool greater_than_equal(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return boxed_cast<bool>(oper(Operators::greater_than_equal, t_lhs.bv, t_rhs.bv));
|
return boxed_cast<bool>(oper(Operators::Opers::greater_than_equal, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool less_than_equal(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static bool less_than_equal(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return boxed_cast<bool>(oper(Operators::less_than_equal, t_lhs.bv, t_rhs.bv));
|
return boxed_cast<bool>(oper(Operators::Opers::less_than_equal, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool not_equal(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static bool not_equal(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return boxed_cast<bool>(oper(Operators::not_equal, t_lhs.bv, t_rhs.bv));
|
return boxed_cast<bool>(oper(Operators::Opers::not_equal, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number pre_decrement(Boxed_Number t_lhs)
|
static Boxed_Number pre_decrement(Boxed_Number t_lhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::pre_decrement, t_lhs.bv);
|
return Boxed_Number(oper(Operators::Opers::pre_decrement, t_lhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number pre_increment(Boxed_Number t_lhs)
|
static Boxed_Number pre_increment(Boxed_Number t_lhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::pre_increment, t_lhs.bv);
|
return Boxed_Number(oper(Operators::Opers::pre_increment, t_lhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number sum(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number sum(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::sum, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::sum, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number unary_plus(const Boxed_Number &t_lhs)
|
static const Boxed_Number unary_plus(const Boxed_Number &t_lhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::unary_plus, t_lhs.bv);
|
return Boxed_Number(oper(Operators::Opers::unary_plus, t_lhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number unary_minus(const Boxed_Number &t_lhs)
|
static const Boxed_Number unary_minus(const Boxed_Number &t_lhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::unary_minus, t_lhs.bv);
|
return Boxed_Number(oper(Operators::Opers::unary_minus, t_lhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number difference(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number difference(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::difference, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::difference, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_bitwise_and(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_bitwise_and(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_bitwise_and, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_bitwise_and, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_bitwise_or(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_bitwise_or(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_bitwise_or, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_bitwise_or, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_bitwise_xor(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_bitwise_xor(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_bitwise_xor, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_bitwise_xor, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_remainder(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_remainder(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_remainder, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_remainder, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_shift_left(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_shift_left(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_shift_left, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_shift_left, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_shift_right(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_shift_right(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_shift_right, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_shift_right, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number bitwise_and(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number bitwise_and(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::bitwise_and, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::bitwise_and, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number bitwise_complement(const Boxed_Number &t_lhs)
|
static const Boxed_Number bitwise_complement(const Boxed_Number &t_lhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::bitwise_complement, t_lhs.bv, Boxed_Value(0));
|
return Boxed_Number(oper(Operators::Opers::bitwise_complement, t_lhs.bv, Boxed_Value(0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number bitwise_xor(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number bitwise_xor(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::bitwise_xor, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::bitwise_xor, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number bitwise_or(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number bitwise_or(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::bitwise_or, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::bitwise_or, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_product(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_product(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_product, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_product, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_quotient(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_quotient(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_quotient, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_quotient, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Number assign_sum(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_sum(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_sum, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_sum, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
static Boxed_Number assign_difference(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
static Boxed_Number assign_difference(Boxed_Number t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::assign_difference, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::assign_difference, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number quotient(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number quotient(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::quotient, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::quotient, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number shift_left(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number shift_left(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::shift_left, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::shift_left, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number product(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number product(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::product, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::product, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number remainder(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number remainder(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::remainder, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::remainder, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const Boxed_Number shift_right(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
static const Boxed_Number shift_right(const Boxed_Number &t_lhs, const Boxed_Number &t_rhs)
|
||||||
{
|
{
|
||||||
return oper(Operators::shift_right, t_lhs.bv, t_rhs.bv);
|
return Boxed_Number(oper(Operators::Opers::shift_right, t_lhs.bv, t_rhs.bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1009,9 +907,7 @@ namespace chaiscript
|
|||||||
template<>
|
template<>
|
||||||
struct Cast_Helper<Boxed_Number>
|
struct Cast_Helper<Boxed_Number>
|
||||||
{
|
{
|
||||||
typedef Boxed_Number Result_Type;
|
static Boxed_Number cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *)
|
|
||||||
{
|
{
|
||||||
return Boxed_Number(ob);
|
return Boxed_Number(ob);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_BOXED_VALUE_HPP_
|
#ifndef CHAISCRIPT_BOXED_VALUE_HPP_
|
||||||
#define CHAISCRIPT_BOXED_VALUE_HPP_
|
#define CHAISCRIPT_BOXED_VALUE_HPP_
|
||||||
|
|
||||||
@ -35,11 +39,11 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
Data(const Type_Info &ti,
|
Data(const Type_Info &ti,
|
||||||
chaiscript::detail::Any to,
|
chaiscript::detail::Any to,
|
||||||
bool tr,
|
bool is_ref,
|
||||||
const void *t_void_ptr,
|
const void *t_void_ptr,
|
||||||
bool t_return_value)
|
bool t_return_value)
|
||||||
: m_type_info(ti), m_obj(std::move(to)), m_data_ptr(ti.is_const()?nullptr:const_cast<void *>(t_void_ptr)), m_const_data_ptr(t_void_ptr),
|
: m_type_info(ti), m_obj(std::move(to)), m_data_ptr(ti.is_const()?nullptr:const_cast<void *>(t_void_ptr)), m_const_data_ptr(t_void_ptr),
|
||||||
m_is_ref(tr), m_return_value(t_return_value)
|
m_is_ref(is_ref), m_return_value(t_return_value)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +58,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
if (rhs.m_attrs)
|
if (rhs.m_attrs)
|
||||||
{
|
{
|
||||||
m_attrs = std::unique_ptr<std::map<std::string, std::shared_ptr<Data>>>(new std::map<std::string, std::shared_ptr<Data>>(*rhs.m_attrs));
|
m_attrs = std::make_unique<std::map<std::string, std::shared_ptr<Data>>>(*rhs.m_attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
@ -77,7 +81,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
struct Object_Data
|
struct Object_Data
|
||||||
{
|
{
|
||||||
static std::shared_ptr<Data> get(Boxed_Value::Void_Type, bool t_return_value)
|
static auto get(Boxed_Value::Void_Type, bool t_return_value)
|
||||||
{
|
{
|
||||||
return std::make_shared<Data>(
|
return std::make_shared<Data>(
|
||||||
detail::Get_Type_Info<void>::get(),
|
detail::Get_Type_Info<void>::get(),
|
||||||
@ -89,13 +93,13 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(const std::shared_ptr<T> *obj, bool t_return_value)
|
static auto get(const std::shared_ptr<T> *obj, bool t_return_value)
|
||||||
{
|
{
|
||||||
return get(*obj, t_return_value);
|
return get(*obj, t_return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(const std::shared_ptr<T> &obj, bool t_return_value)
|
static auto get(const std::shared_ptr<T> &obj, bool t_return_value)
|
||||||
{
|
{
|
||||||
return std::make_shared<Data>(
|
return std::make_shared<Data>(
|
||||||
detail::Get_Type_Info<T>::get(),
|
detail::Get_Type_Info<T>::get(),
|
||||||
@ -107,7 +111,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(std::shared_ptr<T> &&obj, bool t_return_value)
|
static auto get(std::shared_ptr<T> &&obj, bool t_return_value)
|
||||||
{
|
{
|
||||||
auto ptr = obj.get();
|
auto ptr = obj.get();
|
||||||
return std::make_shared<Data>(
|
return std::make_shared<Data>(
|
||||||
@ -119,21 +123,23 @@ namespace chaiscript
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(T *t, bool t_return_value)
|
static auto get(T *t, bool t_return_value)
|
||||||
{
|
{
|
||||||
return get(std::ref(*t), t_return_value);
|
return get(std::ref(*t), t_return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(const T *t, bool t_return_value)
|
static auto get(const T *t, bool t_return_value)
|
||||||
{
|
{
|
||||||
return get(std::cref(*t), t_return_value);
|
return get(std::cref(*t), t_return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(std::reference_wrapper<T> obj, bool t_return_value)
|
static auto get(std::reference_wrapper<T> obj, bool t_return_value)
|
||||||
{
|
{
|
||||||
auto p = &obj.get();
|
auto p = &obj.get();
|
||||||
return std::make_shared<Data>(
|
return std::make_shared<Data>(
|
||||||
@ -146,7 +152,20 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static std::shared_ptr<Data> get(T t, bool t_return_value)
|
static auto get(std::unique_ptr<T> &&obj, bool t_return_value)
|
||||||
|
{
|
||||||
|
auto ptr = obj.get();
|
||||||
|
return std::make_shared<Data>(
|
||||||
|
detail::Get_Type_Info<T>::get(),
|
||||||
|
chaiscript::detail::Any(std::make_shared<std::unique_ptr<T>>(std::move(obj))),
|
||||||
|
true,
|
||||||
|
ptr,
|
||||||
|
t_return_value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static auto get(T t, bool t_return_value)
|
||||||
{
|
{
|
||||||
auto p = std::make_shared<T>(std::move(t));
|
auto p = std::make_shared<T>(std::move(t));
|
||||||
auto ptr = p.get();
|
auto ptr = p.get();
|
||||||
@ -182,10 +201,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Unknown-type constructor
|
/// Unknown-type constructor
|
||||||
Boxed_Value()
|
Boxed_Value() = default;
|
||||||
: m_data(Object_Data::get())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Boxed_Value(Boxed_Value&&) = default;
|
Boxed_Value(Boxed_Value&&) = default;
|
||||||
Boxed_Value& operator=(Boxed_Value&&) = default;
|
Boxed_Value& operator=(Boxed_Value&&) = default;
|
||||||
@ -226,48 +242,40 @@ namespace chaiscript
|
|||||||
return m_data->m_type_info.bare_equal(ti);
|
return m_data->m_type_info.bare_equal(ti);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct Sentinel {
|
|
||||||
Sentinel(std::shared_ptr<T> &ptr, Data &data)
|
|
||||||
: m_ptr(ptr), m_data(data)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~Sentinel()
|
|
||||||
{
|
|
||||||
// save new pointer data
|
|
||||||
m_data.get().m_data_ptr = m_ptr.get().get();
|
|
||||||
m_data.get().m_const_data_ptr = m_ptr.get().get();
|
|
||||||
}
|
|
||||||
|
|
||||||
Sentinel& operator=(Sentinel&&s) {
|
|
||||||
m_ptr = std::move(s.m_ptr);
|
|
||||||
m_data = std::move(s.m_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
Sentinel(Sentinel &&s)
|
|
||||||
: m_ptr(std::move(s.m_ptr)),
|
|
||||||
m_data(std::move(s.m_data))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
operator std::shared_ptr<T>&() const
|
|
||||||
{
|
|
||||||
return m_ptr.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
Sentinel &operator=(const Sentinel &) = delete;
|
|
||||||
Sentinel(Sentinel&) = delete;
|
|
||||||
|
|
||||||
std::reference_wrapper<std::shared_ptr<T>> m_ptr;
|
|
||||||
std::reference_wrapper<Data> m_data;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Sentinel<T> pointer_sentinel(std::shared_ptr<T> &ptr) const
|
auto pointer_sentinel(std::shared_ptr<T> &ptr) const
|
||||||
{
|
{
|
||||||
return Sentinel<T>(ptr, *(m_data.get()));
|
struct Sentinel {
|
||||||
|
Sentinel(std::shared_ptr<T> &t_ptr, Data &data)
|
||||||
|
: m_ptr(t_ptr), m_data(data)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~Sentinel()
|
||||||
|
{
|
||||||
|
// save new pointer data
|
||||||
|
const auto ptr_ = m_ptr.get().get();
|
||||||
|
m_data.get().m_data_ptr = ptr_;
|
||||||
|
m_data.get().m_const_data_ptr = ptr_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Sentinel& operator=(Sentinel&&s) = default;
|
||||||
|
Sentinel(Sentinel &&s) = default;
|
||||||
|
|
||||||
|
operator std::shared_ptr<T>&() const
|
||||||
|
{
|
||||||
|
return m_ptr.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Sentinel &operator=(const Sentinel &) = delete;
|
||||||
|
Sentinel(Sentinel&) = delete;
|
||||||
|
|
||||||
|
std::reference_wrapper<std::shared_ptr<T>> m_ptr;
|
||||||
|
std::reference_wrapper<Data> m_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
return Sentinel(ptr, *(m_data.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_null() const noexcept
|
bool is_null() const noexcept
|
||||||
@ -314,7 +322,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
if (!m_data->m_attrs)
|
if (!m_data->m_attrs)
|
||||||
{
|
{
|
||||||
m_data->m_attrs = std::unique_ptr<std::map<std::string, std::shared_ptr<Data>>>(new std::map<std::string, std::shared_ptr<Data>>());
|
m_data->m_attrs = std::make_unique<std::map<std::string, std::shared_ptr<Data>>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto &attr = (*m_data->m_attrs)[t_name];
|
auto &attr = (*m_data->m_attrs)[t_name];
|
||||||
@ -331,7 +339,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
if (t_obj.m_data->m_attrs)
|
if (t_obj.m_data->m_attrs)
|
||||||
{
|
{
|
||||||
m_data->m_attrs = std::unique_ptr<std::map<std::string, std::shared_ptr<Data>>>(new std::map<std::string, std::shared_ptr<Data>>(*t_obj.m_data->m_attrs));
|
m_data->m_attrs = std::make_unique<std::map<std::string, std::shared_ptr<Data>>>(*t_obj.m_data->m_attrs);
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -354,11 +362,11 @@ namespace chaiscript
|
|||||||
// necessary to avoid hitting the templated && constructor of Boxed_Value
|
// necessary to avoid hitting the templated && constructor of Boxed_Value
|
||||||
struct Internal_Construction{};
|
struct Internal_Construction{};
|
||||||
|
|
||||||
Boxed_Value(const std::shared_ptr<Data> &t_data, Internal_Construction)
|
Boxed_Value(std::shared_ptr<Data> t_data, Internal_Construction)
|
||||||
: m_data(t_data) {
|
: m_data(std::move(t_data)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Data> m_data;
|
std::shared_ptr<Data> m_data = Object_Data::get();
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @brief Creates a Boxed_Value. If the object passed in is a value type, it is copied. If it is a pointer, std::shared_ptr, or std::reference_type
|
/// @brief Creates a Boxed_Value. If the object passed in is a value type, it is copied. If it is a pointer, std::shared_ptr, or std::reference_type
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_CALLABLE_TRAITS_HPP_
|
#ifndef CHAISCRIPT_CALLABLE_TRAITS_HPP_
|
||||||
@ -13,6 +13,53 @@ namespace chaiscript {
|
|||||||
namespace dispatch {
|
namespace dispatch {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
template<typename Class, typename ... Param>
|
||||||
|
struct Constructor
|
||||||
|
{
|
||||||
|
template<typename ... Inner>
|
||||||
|
std::shared_ptr<Class> operator()(Inner&& ... inner) const {
|
||||||
|
return std::make_shared<Class>(std::forward<Inner>(inner)...);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Ret, typename Class, typename ... Param>
|
||||||
|
struct Const_Caller
|
||||||
|
{
|
||||||
|
explicit Const_Caller(Ret (Class::*t_func)(Param...) const) : m_func(t_func) {}
|
||||||
|
|
||||||
|
template<typename ... Inner>
|
||||||
|
Ret operator()(const Class &o, Inner&& ... inner) const {
|
||||||
|
return (o.*m_func)(std::forward<Inner>(inner)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ret (Class::*m_func)(Param...) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Ret, typename ... Param>
|
||||||
|
struct Fun_Caller
|
||||||
|
{
|
||||||
|
explicit Fun_Caller(Ret( * t_func)(Param...) ) : m_func(t_func) {}
|
||||||
|
|
||||||
|
template<typename ... Inner>
|
||||||
|
Ret operator()(Inner&& ... inner) const {
|
||||||
|
return (m_func)(std::forward<Inner>(inner)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ret(*m_func)(Param...);
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Ret, typename Class, typename ... Param>
|
||||||
|
struct Caller
|
||||||
|
{
|
||||||
|
explicit Caller(Ret (Class::*t_func)(Param...)) : m_func(t_func) {}
|
||||||
|
|
||||||
|
template<typename ... Inner>
|
||||||
|
Ret operator()(Class &o, Inner&& ... inner) const {
|
||||||
|
return (o.*m_func)(std::forward<Inner>(inner)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ret (Class::*m_func)(Param...);
|
||||||
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Arity
|
struct Arity
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_DISPATCHKIT_HPP_
|
#ifndef CHAISCRIPT_DISPATCHKIT_HPP_
|
||||||
#define CHAISCRIPT_DISPATCHKIT_HPP_
|
#define CHAISCRIPT_DISPATCHKIT_HPP_
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <deque>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -31,12 +34,16 @@
|
|||||||
#include "proxy_constructors.hpp"
|
#include "proxy_constructors.hpp"
|
||||||
#include "proxy_functions.hpp"
|
#include "proxy_functions.hpp"
|
||||||
#include "type_info.hpp"
|
#include "type_info.hpp"
|
||||||
|
#include "short_alloc.hpp"
|
||||||
|
|
||||||
namespace chaiscript {
|
namespace chaiscript {
|
||||||
class Boxed_Number;
|
class Boxed_Number;
|
||||||
} // namespace chaiscript
|
} // namespace chaiscript
|
||||||
|
|
||||||
namespace chaiscript {
|
namespace chaiscript {
|
||||||
|
namespace parser {
|
||||||
|
class ChaiScript_Parser_Base;
|
||||||
|
}
|
||||||
namespace dispatch {
|
namespace dispatch {
|
||||||
class Dynamic_Proxy_Function;
|
class Dynamic_Proxy_Function;
|
||||||
class Proxy_Function_Base;
|
class Proxy_Function_Base;
|
||||||
@ -45,6 +52,7 @@ struct Placeholder_Object;
|
|||||||
} // namespace chaiscript
|
} // namespace chaiscript
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// \namespace chaiscript::dispatch
|
/// \namespace chaiscript::dispatch
|
||||||
/// \brief Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use to the end user.
|
/// \brief Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use to the end user.
|
||||||
|
|
||||||
@ -63,7 +71,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
reserved_word_error(const reserved_word_error &) = default;
|
reserved_word_error(const reserved_word_error &) = default;
|
||||||
|
|
||||||
virtual ~reserved_word_error() noexcept = default;
|
~reserved_word_error() noexcept override = default;
|
||||||
|
|
||||||
std::string word() const
|
std::string word() const
|
||||||
{
|
{
|
||||||
@ -85,7 +93,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
illegal_name_error(const illegal_name_error &) = default;
|
illegal_name_error(const illegal_name_error &) = default;
|
||||||
|
|
||||||
virtual ~illegal_name_error() noexcept = default;
|
~illegal_name_error() noexcept override = default;
|
||||||
|
|
||||||
std::string name() const
|
std::string name() const
|
||||||
{
|
{
|
||||||
@ -108,7 +116,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
name_conflict_error(const name_conflict_error &) = default;
|
name_conflict_error(const name_conflict_error &) = default;
|
||||||
|
|
||||||
virtual ~name_conflict_error() noexcept = default;
|
~name_conflict_error() noexcept override = default;
|
||||||
|
|
||||||
std::string name() const
|
std::string name() const
|
||||||
{
|
{
|
||||||
@ -131,7 +139,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
global_non_const(const global_non_const &) = default;
|
global_non_const(const global_non_const &) = default;
|
||||||
virtual ~global_non_const() noexcept = default;
|
~global_non_const() noexcept override = default;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +151,7 @@ namespace chaiscript
|
|||||||
public:
|
public:
|
||||||
Module &add(Type_Info ti, std::string name)
|
Module &add(Type_Info ti, std::string name)
|
||||||
{
|
{
|
||||||
m_typeinfos.emplace_back(std::move(ti), std::move(name));
|
m_typeinfos.emplace_back(ti, std::move(name));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,12 +186,6 @@ namespace chaiscript
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Module &add(const std::shared_ptr<Module> &m)
|
|
||||||
{
|
|
||||||
m->apply(*this, *this);
|
|
||||||
return *m;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Eval, typename Engine>
|
template<typename Eval, typename Engine>
|
||||||
void apply(Eval &t_eval, Engine &t_engine) const
|
void apply(Eval &t_eval, Engine &t_engine) const
|
||||||
{
|
{
|
||||||
@ -268,7 +270,7 @@ namespace chaiscript
|
|||||||
class Dispatch_Function final : public dispatch::Proxy_Function_Base
|
class Dispatch_Function final : public dispatch::Proxy_Function_Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Dispatch_Function(std::vector<Proxy_Function> t_funcs)
|
explicit Dispatch_Function(std::vector<Proxy_Function> t_funcs)
|
||||||
: Proxy_Function_Base(build_type_infos(t_funcs), calculate_arity(t_funcs)),
|
: Proxy_Function_Base(build_type_infos(t_funcs), calculate_arity(t_funcs)),
|
||||||
m_funcs(std::move(t_funcs))
|
m_funcs(std::move(t_funcs))
|
||||||
{
|
{
|
||||||
@ -316,11 +318,6 @@ namespace chaiscript
|
|||||||
[&vals, &t_conversions](const Proxy_Function &f){ return f->call_match(vals, t_conversions); });
|
[&vals, &t_conversions](const Proxy_Function &f){ return f->call_match(vals, t_conversions); });
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string annotation() const override
|
|
||||||
{
|
|
||||||
return "Multiple method dispatch function wrapper.";
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
@ -363,7 +360,7 @@ namespace chaiscript
|
|||||||
++begin;
|
++begin;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(type_infos.size() > 0 && " type_info vector size is < 0, this is only possible if something else is broken");
|
assert(!type_infos.empty() && " type_info vector size is < 0, this is only possible if something else is broken");
|
||||||
|
|
||||||
if (size_mismatch)
|
if (size_mismatch)
|
||||||
{
|
{
|
||||||
@ -383,22 +380,56 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
struct Stack_Holder
|
struct Stack_Holder
|
||||||
{
|
{
|
||||||
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
//template <class T, std::size_t BufSize = sizeof(T)*20000>
|
||||||
typedef std::vector<Scope> StackData;
|
// using SmallVector = std::vector<T, short_alloc<T, BufSize>>;
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
using SmallVector = std::vector<T>;
|
||||||
|
|
||||||
|
|
||||||
|
typedef SmallVector<std::pair<std::string, Boxed_Value>> Scope;
|
||||||
|
typedef SmallVector<Scope> StackData;
|
||||||
|
typedef SmallVector<StackData> Stacks;
|
||||||
|
typedef SmallVector<Boxed_Value> Call_Param_List;
|
||||||
|
typedef SmallVector<Call_Param_List> Call_Params;
|
||||||
|
|
||||||
Stack_Holder()
|
Stack_Holder()
|
||||||
: call_depth(0)
|
|
||||||
{
|
{
|
||||||
stacks.reserve(2);
|
push_stack();
|
||||||
stacks.emplace_back(1);
|
push_call_params();
|
||||||
call_params.emplace_back();
|
|
||||||
call_params.back().reserve(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<StackData> stacks;
|
void push_stack_data()
|
||||||
|
{
|
||||||
|
stacks.back().emplace_back();
|
||||||
|
// stacks.back().emplace_back(Scope(scope_allocator));
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::vector<Boxed_Value>> call_params;
|
void push_stack()
|
||||||
int call_depth;
|
{
|
||||||
|
stacks.emplace_back(1);
|
||||||
|
// stacks.emplace_back(StackData(1, Scope(scope_allocator), stack_data_allocator));
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_call_params()
|
||||||
|
{
|
||||||
|
call_params.emplace_back();
|
||||||
|
// call_params.emplace_back(Call_Param_List(call_param_list_allocator));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Scope::allocator_type::arena_type scope_allocator;
|
||||||
|
//StackData::allocator_type::arena_type stack_data_allocator;
|
||||||
|
//Stacks::allocator_type::arena_type stacks_allocator;
|
||||||
|
//Call_Param_List::allocator_type::arena_type call_param_list_allocator;
|
||||||
|
//Call_Params::allocator_type::arena_type call_params_allocator;
|
||||||
|
|
||||||
|
// Stacks stacks = Stacks(stacks_allocator);
|
||||||
|
// Call_Params call_params = Call_Params(call_params_allocator);
|
||||||
|
|
||||||
|
Stacks stacks;
|
||||||
|
Call_Params call_params;
|
||||||
|
|
||||||
|
int call_depth = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Main class for the dispatchkit. Handles management
|
/// Main class for the dispatchkit. Handles management
|
||||||
@ -409,7 +440,7 @@ namespace chaiscript
|
|||||||
public:
|
public:
|
||||||
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
|
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
|
||||||
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
||||||
typedef std::vector<Scope> StackData;
|
typedef Stack_Holder::StackData StackData;
|
||||||
|
|
||||||
struct State
|
struct State
|
||||||
{
|
{
|
||||||
@ -418,28 +449,20 @@ namespace chaiscript
|
|||||||
std::vector<std::pair<std::string, Boxed_Value>> m_boxed_functions;
|
std::vector<std::pair<std::string, Boxed_Value>> m_boxed_functions;
|
||||||
std::map<std::string, Boxed_Value> m_global_objects;
|
std::map<std::string, Boxed_Value> m_global_objects;
|
||||||
Type_Name_Map m_types;
|
Type_Name_Map m_types;
|
||||||
std::set<std::string> m_reserved_words;
|
|
||||||
|
|
||||||
State &operator=(const State &) = default;
|
|
||||||
State() = default;
|
|
||||||
State(const State &) = default;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Dispatch_Engine()
|
explicit Dispatch_Engine(chaiscript::parser::ChaiScript_Parser_Base &parser)
|
||||||
: m_stack_holder(this)
|
: m_stack_holder(),
|
||||||
{
|
m_parser(parser)
|
||||||
}
|
|
||||||
|
|
||||||
~Dispatch_Engine()
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief casts an object while applying any Dynamic_Conversion available
|
/// \brief casts an object while applying any Dynamic_Conversion available
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
|
decltype(auto) boxed_cast(const Boxed_Value &bv) const
|
||||||
{
|
{
|
||||||
Type_Conversions_State state(m_conversions, m_conversions.conversion_saves());
|
Type_Conversions_State state(m_conversions, m_conversions.conversion_saves());
|
||||||
return chaiscript::boxed_cast<Type>(bv, &state);
|
return(chaiscript::boxed_cast<Type>(bv, &state));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new conversion for upcasting to a base class
|
/// Add a new conversion for upcasting to a base class
|
||||||
@ -451,7 +474,6 @@ namespace chaiscript
|
|||||||
/// Add a new named Proxy_Function to the system
|
/// Add a new named Proxy_Function to the system
|
||||||
void add(const Proxy_Function &f, const std::string &name)
|
void add(const Proxy_Function &f, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_object_name(name);
|
|
||||||
add_function(f, name);
|
add_function(f, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -459,7 +481,6 @@ namespace chaiscript
|
|||||||
/// is not available in the current scope it is created
|
/// is not available in the current scope it is created
|
||||||
void add(Boxed_Value obj, const std::string &name)
|
void add(Boxed_Value obj, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_object_name(name);
|
|
||||||
auto &stack = get_stack_data();
|
auto &stack = get_stack_data();
|
||||||
|
|
||||||
for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem)
|
for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem)
|
||||||
@ -479,6 +500,26 @@ namespace chaiscript
|
|||||||
add_object(name, std::move(obj));
|
add_object(name, std::move(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a named object to the current scope
|
||||||
|
/// \warning This version does not check the validity of the name
|
||||||
|
/// it is meant for internal use only
|
||||||
|
Boxed_Value &add_get_object(const std::string &t_name, Boxed_Value obj, Stack_Holder &t_holder)
|
||||||
|
{
|
||||||
|
auto &stack_elem = get_stack_data(t_holder).back();
|
||||||
|
|
||||||
|
if (std::any_of(stack_elem.begin(), stack_elem.end(),
|
||||||
|
[&](const std::pair<std::string, Boxed_Value> &o) {
|
||||||
|
return o.first == t_name;
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
throw chaiscript::exception::name_conflict_error(t_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
stack_elem.emplace_back(t_name, std::move(obj));
|
||||||
|
return stack_elem.back().second;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Adds a named object to the current scope
|
/// Adds a named object to the current scope
|
||||||
/// \warning This version does not check the validity of the name
|
/// \warning This version does not check the validity of the name
|
||||||
/// it is meant for internal use only
|
/// it is meant for internal use only
|
||||||
@ -494,7 +535,7 @@ namespace chaiscript
|
|||||||
throw chaiscript::exception::name_conflict_error(t_name);
|
throw chaiscript::exception::name_conflict_error(t_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
get_stack_data(t_holder).back().emplace_back(t_name, std::move(obj));
|
stack_elem.emplace_back(t_name, std::move(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -509,7 +550,6 @@ namespace chaiscript
|
|||||||
/// Adds a new global shared object, between all the threads
|
/// Adds a new global shared object, between all the threads
|
||||||
void add_global_const(const Boxed_Value &obj, const std::string &name)
|
void add_global_const(const Boxed_Value &obj, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_object_name(name);
|
|
||||||
if (!obj.is_const())
|
if (!obj.is_const())
|
||||||
{
|
{
|
||||||
throw chaiscript::exception::global_non_const();
|
throw chaiscript::exception::global_non_const();
|
||||||
@ -528,8 +568,6 @@ namespace chaiscript
|
|||||||
/// Adds a new global (non-const) shared object, between all the threads
|
/// Adds a new global (non-const) shared object, between all the threads
|
||||||
Boxed_Value add_global_no_throw(const Boxed_Value &obj, const std::string &name)
|
Boxed_Value add_global_no_throw(const Boxed_Value &obj, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_object_name(name);
|
|
||||||
|
|
||||||
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||||
|
|
||||||
const auto itr = m_state.m_global_objects.find(name);
|
const auto itr = m_state.m_global_objects.find(name);
|
||||||
@ -546,8 +584,6 @@ namespace chaiscript
|
|||||||
/// Adds a new global (non-const) shared object, between all the threads
|
/// Adds a new global (non-const) shared object, between all the threads
|
||||||
void add_global(const Boxed_Value &obj, const std::string &name)
|
void add_global(const Boxed_Value &obj, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_object_name(name);
|
|
||||||
|
|
||||||
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||||
|
|
||||||
if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end())
|
if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end())
|
||||||
@ -561,8 +597,6 @@ namespace chaiscript
|
|||||||
/// Updates an existing global shared object or adds a new global shared object if not found
|
/// Updates an existing global shared object or adds a new global shared object if not found
|
||||||
void set_global(const Boxed_Value &obj, const std::string &name)
|
void set_global(const Boxed_Value &obj, const std::string &name)
|
||||||
{
|
{
|
||||||
validate_object_name(name);
|
|
||||||
|
|
||||||
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||||
|
|
||||||
const auto itr = m_state.m_global_objects.find(name);
|
const auto itr = m_state.m_global_objects.find(name);
|
||||||
@ -589,8 +623,8 @@ namespace chaiscript
|
|||||||
/// Adds a new scope to the stack
|
/// Adds a new scope to the stack
|
||||||
static void new_scope(Stack_Holder &t_holder)
|
static void new_scope(Stack_Holder &t_holder)
|
||||||
{
|
{
|
||||||
get_stack_data(t_holder).emplace_back();
|
t_holder.push_stack_data();
|
||||||
t_holder.call_params.emplace_back();
|
t_holder.push_call_params();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pops the current scope from the stack
|
/// Pops the current scope from the stack
|
||||||
@ -609,7 +643,7 @@ namespace chaiscript
|
|||||||
static void new_stack(Stack_Holder &t_holder)
|
static void new_stack(Stack_Holder &t_holder)
|
||||||
{
|
{
|
||||||
// add a new Stack with 1 element
|
// add a new Stack with 1 element
|
||||||
t_holder.stacks.emplace_back(1);
|
t_holder.push_stack();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pop_stack(Stack_Holder &t_holder)
|
static void pop_stack(Stack_Holder &t_holder)
|
||||||
@ -651,7 +685,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
t_loc = static_cast<uint_fast32_t>(Loc::located);
|
t_loc = static_cast<uint_fast32_t>(Loc::located);
|
||||||
} else if (loc & static_cast<uint_fast32_t>(Loc::is_local)) {
|
} else if ((loc & static_cast<uint_fast32_t>(Loc::is_local)) != 0u) {
|
||||||
auto &stack = get_stack_data(t_holder);
|
auto &stack = get_stack_data(t_holder);
|
||||||
|
|
||||||
return stack[stack.size() - 1 - ((loc & static_cast<uint_fast32_t>(Loc::stack_mask)) >> 16)][loc & static_cast<uint_fast32_t>(Loc::loc_mask)].second;
|
return stack[stack.size() - 1 - ((loc & static_cast<uint_fast32_t>(Loc::stack_mask)) >> 16)][loc & static_cast<uint_fast32_t>(Loc::loc_mask)].second;
|
||||||
@ -668,9 +702,9 @@ namespace chaiscript
|
|||||||
|
|
||||||
// no? is it a function object?
|
// no? is it a function object?
|
||||||
auto obj = get_function_object_int(name, loc);
|
auto obj = get_function_object_int(name, loc);
|
||||||
if (obj.first != loc) t_loc = uint_fast32_t(obj.first);
|
if (obj.first != loc) { t_loc = uint_fast32_t(obj.first); }
|
||||||
return obj.second;
|
|
||||||
|
|
||||||
|
return obj.second;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -733,7 +767,10 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
uint_fast32_t method_missing_loc = m_method_missing_loc;
|
uint_fast32_t method_missing_loc = m_method_missing_loc;
|
||||||
auto method_missing_funs = get_function("method_missing", method_missing_loc);
|
auto method_missing_funs = get_function("method_missing", method_missing_loc);
|
||||||
if (method_missing_funs.first != method_missing_loc) m_method_missing_loc = uint_fast32_t(method_missing_funs.first);
|
if (method_missing_funs.first != method_missing_loc) {
|
||||||
|
m_method_missing_loc = uint_fast32_t(method_missing_funs.first);
|
||||||
|
}
|
||||||
|
|
||||||
return std::move(method_missing_funs.second);
|
return std::move(method_missing_funs.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -821,7 +858,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
auto &stack = get_stack_data();
|
auto &stack = get_stack_data();
|
||||||
auto &scope = stack.front();
|
auto &scope = stack.front();
|
||||||
scope = std::vector<std::pair<std::string, Boxed_Value>>(t_locals.begin(), t_locals.end());
|
scope.assign(t_locals.begin(), t_locals.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -892,12 +929,6 @@ namespace chaiscript
|
|||||||
return rets;
|
return rets;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_reserved_word(const std::string &name)
|
|
||||||
{
|
|
||||||
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
|
||||||
|
|
||||||
m_state.m_reserved_words.insert(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
const Type_Conversions &conversions() const
|
const Type_Conversions &conversions() const
|
||||||
{
|
{
|
||||||
@ -930,7 +961,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
uint_fast32_t loc = t_loc;
|
uint_fast32_t loc = t_loc;
|
||||||
const auto funs = get_function(t_name, loc);
|
const auto funs = get_function(t_name, loc);
|
||||||
if (funs.first != loc) t_loc = uint_fast32_t(funs.first);
|
if (funs.first != loc) { t_loc = uint_fast32_t(funs.first); }
|
||||||
|
|
||||||
const auto do_attribute_call =
|
const auto do_attribute_call =
|
||||||
[this](int l_num_params, const std::vector<Boxed_Value> &l_params, const std::vector<Proxy_Function> &l_funs, const Type_Conversions_State &l_conversions)->Boxed_Value
|
[this](int l_num_params, const std::vector<Boxed_Value> &l_params, const std::vector<Proxy_Function> &l_funs, const Type_Conversions_State &l_conversions)->Boxed_Value
|
||||||
@ -953,14 +984,21 @@ namespace chaiscript
|
|||||||
|
|
||||||
This_Foist fi(*this, l_params.front());
|
This_Foist fi(*this, l_params.front());
|
||||||
|
|
||||||
auto func = boxed_cast<std::shared_ptr<const dispatch::Proxy_Function_Base>>(bv);
|
|
||||||
try {
|
try {
|
||||||
return (*func)({l_params.begin() + l_num_params, l_params.end()}, l_conversions);
|
auto func = boxed_cast<const dispatch::Proxy_Function_Base *>(bv);
|
||||||
|
try {
|
||||||
|
return (*func)({l_params.begin() + l_num_params, l_params.end()}, l_conversions);
|
||||||
|
} catch (const chaiscript::exception::bad_boxed_cast &) {
|
||||||
|
} catch (const chaiscript::exception::arity_error &) {
|
||||||
|
} catch (const chaiscript::exception::guard_error &) {
|
||||||
|
}
|
||||||
|
throw chaiscript::exception::dispatch_error({l_params.begin() + l_num_params, l_params.end()},
|
||||||
|
std::vector<Const_Proxy_Function>{boxed_cast<Const_Proxy_Function>(bv)});
|
||||||
} catch (const chaiscript::exception::bad_boxed_cast &) {
|
} catch (const chaiscript::exception::bad_boxed_cast &) {
|
||||||
} catch (const chaiscript::exception::arity_error &) {
|
// unable to convert bv into a Proxy_Function_Base
|
||||||
} catch (const chaiscript::exception::guard_error &) {
|
throw chaiscript::exception::dispatch_error({l_params.begin() + l_num_params, l_params.end()},
|
||||||
|
std::vector<Const_Proxy_Function>(l_funs.begin(), l_funs.end()));
|
||||||
}
|
}
|
||||||
throw chaiscript::exception::dispatch_error({l_params.begin() + l_num_params, l_params.end()}, std::vector<Const_Proxy_Function>{func});
|
|
||||||
} else {
|
} else {
|
||||||
return bv;
|
return bv;
|
||||||
}
|
}
|
||||||
@ -1043,7 +1081,8 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
uint_fast32_t loc = t_loc;
|
uint_fast32_t loc = t_loc;
|
||||||
const auto funs = get_function(t_name, loc);
|
const auto funs = get_function(t_name, loc);
|
||||||
if (funs.first != loc) t_loc = uint_fast32_t(funs.first);
|
if (funs.first != loc) { t_loc = uint_fast32_t(funs.first);
|
||||||
|
}
|
||||||
return dispatch::dispatch(*funs.second, params, t_conversions);
|
return dispatch::dispatch(*funs.second, params, t_conversions);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1064,11 +1103,7 @@ namespace chaiscript
|
|||||||
void dump_function(const std::pair<const std::string, Proxy_Function > &f) const
|
void dump_function(const std::pair<const std::string, Proxy_Function > &f) const
|
||||||
{
|
{
|
||||||
std::vector<Type_Info> params = f.second->get_param_types();
|
std::vector<Type_Info> params = f.second->get_param_types();
|
||||||
std::string annotation = f.second->annotation();
|
|
||||||
|
|
||||||
if (annotation.size() > 0) {
|
|
||||||
std::cout << annotation;
|
|
||||||
}
|
|
||||||
dump_type(params.front());
|
dump_type(params.front());
|
||||||
std::cout << " " << f.first << "(";
|
std::cout << " " << f.first << "(";
|
||||||
|
|
||||||
@ -1171,7 +1206,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
static void save_function_params(Stack_Holder &t_s, std::initializer_list<Boxed_Value> t_params)
|
static void save_function_params(Stack_Holder &t_s, std::initializer_list<Boxed_Value> t_params)
|
||||||
{
|
{
|
||||||
t_s.call_params.back().insert(t_s.call_params.back().begin(), std::move(t_params));
|
t_s.call_params.back().insert(t_s.call_params.back().begin(), t_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void save_function_params(Stack_Holder &t_s, std::vector<Boxed_Value> &&t_params)
|
static void save_function_params(Stack_Holder &t_s, std::vector<Boxed_Value> &&t_params)
|
||||||
@ -1189,7 +1224,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
void save_function_params(std::initializer_list<Boxed_Value> t_params)
|
void save_function_params(std::initializer_list<Boxed_Value> t_params)
|
||||||
{
|
{
|
||||||
save_function_params(*m_stack_holder, std::move(t_params));
|
save_function_params(*m_stack_holder, t_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_function_params(std::vector<Boxed_Value> &&t_params)
|
void save_function_params(std::vector<Boxed_Value> &&t_params)
|
||||||
@ -1259,6 +1294,11 @@ namespace chaiscript
|
|||||||
return m_stack_holder->stacks.back();
|
return m_stack_holder->stacks.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parser::ChaiScript_Parser_Base &get_parser()
|
||||||
|
{
|
||||||
|
return m_parser.get();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
const std::vector<std::pair<std::string, Boxed_Value>> &get_boxed_functions_int() const
|
const std::vector<std::pair<std::string, Boxed_Value>> &get_boxed_functions_int() const
|
||||||
@ -1355,10 +1395,6 @@ namespace chaiscript
|
|||||||
|
|
||||||
if (rt.bare_equal(boxed_type))
|
if (rt.bare_equal(boxed_type))
|
||||||
{
|
{
|
||||||
if (lt.bare_equal(boxed_pod_type))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1380,20 +1416,6 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Throw a reserved_word exception if the name is not allowed
|
|
||||||
void validate_object_name(const std::string &name) const
|
|
||||||
{
|
|
||||||
if (name.find("::") != std::string::npos) {
|
|
||||||
throw chaiscript::exception::illegal_name_error(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
|
||||||
|
|
||||||
if (m_state.m_reserved_words.find(name) != m_state.m_reserved_words.end())
|
|
||||||
{
|
|
||||||
throw chaiscript::exception::reserved_word_error(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Container, typename Key, typename Value>
|
template<typename Container, typename Key, typename Value>
|
||||||
static void add_keyed_value(Container &t_c, const Key &t_key, Value &&t_value)
|
static void add_keyed_value(Container &t_c, const Key &t_key, Value &&t_value)
|
||||||
@ -1431,7 +1453,7 @@ namespace chaiscript
|
|||||||
static typename Container::const_iterator find_keyed_value(const Container &t_c, const Key &t_key, const size_t t_hint)
|
static typename Container::const_iterator find_keyed_value(const Container &t_c, const Key &t_key, const size_t t_hint)
|
||||||
{
|
{
|
||||||
if (t_c.size() > t_hint && t_c[t_hint].first == t_key) {
|
if (t_c.size() > t_hint && t_c[t_hint].first == t_key) {
|
||||||
return advance_copy(t_c.begin(), t_hint);
|
return std::next(t_c.begin(), static_cast<typename std::iterator_traits<typename Container::const_iterator>::difference_type>(t_hint));
|
||||||
} else {
|
} else {
|
||||||
return find_keyed_value(t_c, t_key);
|
return find_keyed_value(t_c, t_key);
|
||||||
}
|
}
|
||||||
@ -1488,8 +1510,9 @@ namespace chaiscript
|
|||||||
|
|
||||||
Type_Conversions m_conversions;
|
Type_Conversions m_conversions;
|
||||||
chaiscript::detail::threading::Thread_Storage<Stack_Holder> m_stack_holder;
|
chaiscript::detail::threading::Thread_Storage<Stack_Holder> m_stack_holder;
|
||||||
|
std::reference_wrapper<parser::ChaiScript_Parser_Base> m_parser;
|
||||||
|
|
||||||
mutable std::atomic_uint_fast32_t m_method_missing_loc;
|
mutable std::atomic_uint_fast32_t m_method_missing_loc = {0};
|
||||||
|
|
||||||
State m_state;
|
State m_state;
|
||||||
};
|
};
|
||||||
@ -1497,7 +1520,7 @@ namespace chaiscript
|
|||||||
class Dispatch_State
|
class Dispatch_State
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Dispatch_State(Dispatch_Engine &t_engine)
|
explicit Dispatch_State(Dispatch_Engine &t_engine)
|
||||||
: m_engine(t_engine),
|
: m_engine(t_engine),
|
||||||
m_stack_holder(t_engine.get_stack_holder()),
|
m_stack_holder(t_engine.get_stack_holder()),
|
||||||
m_conversions(t_engine.conversions(), t_engine.conversions().conversion_saves())
|
m_conversions(t_engine.conversions(), t_engine.conversions().conversion_saves())
|
||||||
@ -1524,8 +1547,12 @@ namespace chaiscript
|
|||||||
return m_conversions.saves();
|
return m_conversions.saves();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Boxed_Value &add_get_object(const std::string &t_name, Boxed_Value obj) const {
|
||||||
|
return m_engine.get().add_get_object(t_name, std::move(obj), m_stack_holder.get());
|
||||||
|
}
|
||||||
|
|
||||||
void add_object(const std::string &t_name, Boxed_Value obj) const {
|
void add_object(const std::string &t_name, Boxed_Value obj) const {
|
||||||
m_engine.get().add_object(t_name, std::move(obj), m_stack_holder.get());
|
return m_engine.get().add_object(t_name, std::move(obj), m_stack_holder.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
Boxed_Value get_object(const std::string &t_name, std::atomic_uint_fast32_t &t_loc) const {
|
Boxed_Value get_object(const std::string &t_name, std::atomic_uint_fast32_t &t_loc) const {
|
||||||
@ -1542,3 +1569,4 @@ namespace chaiscript
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_
|
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_HPP_
|
||||||
#define CHAISCRIPT_DYNAMIC_OBJECT_HPP_
|
#define CHAISCRIPT_DYNAMIC_OBJECT_HPP_
|
||||||
|
|
||||||
@ -25,7 +29,7 @@ namespace chaiscript
|
|||||||
namespace dispatch
|
namespace dispatch
|
||||||
{
|
{
|
||||||
struct option_explicit_set : std::runtime_error {
|
struct option_explicit_set : std::runtime_error {
|
||||||
option_explicit_set(const std::string &t_param_name)
|
explicit option_explicit_set(const std::string &t_param_name)
|
||||||
: std::runtime_error("option explicit set and parameter '" + t_param_name + "' does not exist")
|
: std::runtime_error("option explicit set and parameter '" + t_param_name + "' does not exist")
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -33,13 +37,13 @@ namespace chaiscript
|
|||||||
|
|
||||||
option_explicit_set(const option_explicit_set &) = default;
|
option_explicit_set(const option_explicit_set &) = default;
|
||||||
|
|
||||||
virtual ~option_explicit_set() noexcept = default;
|
~option_explicit_set() noexcept override = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Dynamic_Object
|
class Dynamic_Object
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Dynamic_Object(std::string t_type_name)
|
explicit Dynamic_Object(std::string t_type_name)
|
||||||
: m_type_name(std::move(t_type_name)), m_option_explicit(false)
|
: m_type_name(std::move(t_type_name)), m_option_explicit(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_DETAIL_HPP_
|
#ifndef CHAISCRIPT_DYNAMIC_OBJECT_DETAIL_HPP_
|
||||||
@ -98,14 +98,8 @@ namespace chaiscript
|
|||||||
return {m_func};
|
return {m_func};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string annotation() const override
|
|
||||||
{
|
|
||||||
return m_func->annotation();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
if (dynamic_object_typename_match(params, m_type_name, m_ti, t_conversions))
|
if (dynamic_object_typename_match(params, m_type_name, m_ti, t_conversions))
|
||||||
{
|
{
|
||||||
@ -115,7 +109,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool compare_first_type(const Boxed_Value &bv, const Type_Conversions_State &t_conversions) const override
|
bool compare_first_type(const Boxed_Value &bv, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
return dynamic_object_typename_match(bv, m_type_name, m_ti, t_conversions);
|
return dynamic_object_typename_match(bv, m_type_name, m_ti, t_conversions);
|
||||||
}
|
}
|
||||||
@ -157,7 +151,7 @@ namespace chaiscript
|
|||||||
bool dynamic_object_typename_match(const std::vector<Boxed_Value> &bvs, const std::string &name,
|
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_State &t_conversions) const
|
const std::unique_ptr<Type_Info> &ti, const Type_Conversions_State &t_conversions) const
|
||||||
{
|
{
|
||||||
if (bvs.size() > 0)
|
if (!bvs.empty())
|
||||||
{
|
{
|
||||||
return dynamic_object_typename_match(bvs[0], name, ti, t_conversions);
|
return dynamic_object_typename_match(bvs[0], name, ti, t_conversions);
|
||||||
} else {
|
} else {
|
||||||
@ -208,7 +202,7 @@ namespace chaiscript
|
|||||||
bool operator==(const Proxy_Function_Base &f) const override
|
bool operator==(const Proxy_Function_Base &f) const override
|
||||||
{
|
{
|
||||||
const Dynamic_Object_Constructor *dc = dynamic_cast<const Dynamic_Object_Constructor*>(&f);
|
const Dynamic_Object_Constructor *dc = dynamic_cast<const Dynamic_Object_Constructor*>(&f);
|
||||||
return dc && dc->m_type_name == m_type_name && (*dc->m_func) == (*m_func);
|
return (dc != nullptr) && dc->m_type_name == m_type_name && (*dc->m_func) == (*m_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
||||||
@ -219,11 +213,6 @@ namespace chaiscript
|
|||||||
return m_func->call_match(new_vals, t_conversions);
|
return m_func->call_match(new_vals, t_conversions);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string annotation() const override
|
|
||||||
{
|
|
||||||
return m_func->annotation();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_
|
#ifndef CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_
|
||||||
#define CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_
|
#define CHAISCRIPT_EXCEPTION_SPECIFICATION_HPP_
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_FUNCTION_CALL_HPP_
|
#ifndef CHAISCRIPT_FUNCTION_CALL_HPP_
|
||||||
#define CHAISCRIPT_FUNCTION_CALL_HPP_
|
#define CHAISCRIPT_FUNCTION_CALL_HPP_
|
||||||
|
|
||||||
@ -81,9 +85,7 @@ namespace chaiscript
|
|||||||
template<typename Signature>
|
template<typename Signature>
|
||||||
struct Cast_Helper<const std::function<Signature> &>
|
struct Cast_Helper<const std::function<Signature> &>
|
||||||
{
|
{
|
||||||
typedef std::function<Signature> Result_Type;
|
static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
||||||
{
|
{
|
||||||
@ -98,9 +100,7 @@ namespace chaiscript
|
|||||||
template<typename Signature>
|
template<typename Signature>
|
||||||
struct Cast_Helper<std::function<Signature> >
|
struct Cast_Helper<std::function<Signature> >
|
||||||
{
|
{
|
||||||
typedef std::function<Signature> Result_Type;
|
static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
||||||
{
|
{
|
||||||
@ -115,9 +115,7 @@ namespace chaiscript
|
|||||||
template<typename Signature>
|
template<typename Signature>
|
||||||
struct Cast_Helper<const std::function<Signature> >
|
struct Cast_Helper<const std::function<Signature> >
|
||||||
{
|
{
|
||||||
typedef std::function<Signature> Result_Type;
|
static std::function<Signature> cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
||||||
|
|
||||||
static Result_Type cast(const Boxed_Value &ob, const Type_Conversions_State *t_conversions)
|
|
||||||
{
|
{
|
||||||
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
if (ob.get_type_info().bare_equal(user_type<Const_Proxy_Function>()))
|
||||||
{
|
{
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_FUNCTION_CALL_DETAIL_HPP_
|
#ifndef CHAISCRIPT_FUNCTION_CALL_DETAIL_HPP_
|
||||||
#define CHAISCRIPT_FUNCTION_CALL_DETAIL_HPP_
|
#define CHAISCRIPT_FUNCTION_CALL_DETAIL_HPP_
|
||||||
|
|
||||||
@ -33,7 +37,7 @@ namespace chaiscript
|
|||||||
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
|
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
|
||||||
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State *t_conversions)
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State *t_conversions)
|
||||||
{
|
{
|
||||||
if (t_conversions) {
|
if (t_conversions != nullptr) {
|
||||||
return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, *t_conversions), t_conversions);
|
return boxed_cast<Ret>(dispatch::dispatch(t_funcs, params, *t_conversions), t_conversions);
|
||||||
} else {
|
} else {
|
||||||
Type_Conversions conv;
|
Type_Conversions conv;
|
||||||
@ -52,7 +56,7 @@ namespace chaiscript
|
|||||||
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
|
static Ret call(const std::vector<Const_Proxy_Function> &t_funcs,
|
||||||
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State *t_conversions)
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State *t_conversions)
|
||||||
{
|
{
|
||||||
if (t_conversions) {
|
if (t_conversions != nullptr) {
|
||||||
return Boxed_Number(dispatch::dispatch(t_funcs, params, *t_conversions)).get_as<Ret>();
|
return Boxed_Number(dispatch::dispatch(t_funcs, params, *t_conversions)).get_as<Ret>();
|
||||||
} else {
|
} else {
|
||||||
Type_Conversions conv;
|
Type_Conversions conv;
|
||||||
@ -72,7 +76,7 @@ namespace chaiscript
|
|||||||
static void call(const std::vector<Const_Proxy_Function> &t_funcs,
|
static void call(const std::vector<Const_Proxy_Function> &t_funcs,
|
||||||
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State *t_conversions)
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State *t_conversions)
|
||||||
{
|
{
|
||||||
if (t_conversions) {
|
if (t_conversions != nullptr) {
|
||||||
dispatch::dispatch(t_funcs, params, *t_conversions);
|
dispatch::dispatch(t_funcs, params, *t_conversions);
|
||||||
} else {
|
} else {
|
||||||
Type_Conversions conv;
|
Type_Conversions conv;
|
||||||
@ -141,6 +145,22 @@ namespace chaiscript
|
|||||||
template<typename Ret, typename ... Params>
|
template<typename Ret, typename ... Params>
|
||||||
std::function<Ret (Params...)> build_function_caller_helper(Ret (Params...), const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions_State *t_conversions)
|
std::function<Ret (Params...)> build_function_caller_helper(Ret (Params...), const std::vector<Const_Proxy_Function> &funcs, const Type_Conversions_State *t_conversions)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
if (funcs.size() == 1)
|
||||||
|
{
|
||||||
|
std::shared_ptr<const Proxy_Function_Impl<Ret (Params...)>> pfi =
|
||||||
|
std::dynamic_pointer_cast<const Proxy_Function_Impl<Ret (Params...)> >
|
||||||
|
(funcs[0]);
|
||||||
|
|
||||||
|
if (pfi)
|
||||||
|
{
|
||||||
|
return pfi->internal_function();
|
||||||
|
}
|
||||||
|
// looks like this either wasn't a Proxy_Function_Impl or the types didn't match
|
||||||
|
// we cannot make any other guesses or assumptions really, so continuing
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
return std::function<Ret (Params...)>(Build_Function_Caller_Helper<Ret, Params...>(funcs, t_conversions?t_conversions->get():nullptr));
|
return std::function<Ret (Params...)>(Build_Function_Caller_Helper<Ret, Params...>(funcs, t_conversions?t_conversions->get():nullptr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_HANDLE_RETURN_HPP_
|
#ifndef CHAISCRIPT_HANDLE_RETURN_HPP_
|
||||||
#define CHAISCRIPT_HANDLE_RETURN_HPP_
|
#define CHAISCRIPT_HANDLE_RETURN_HPP_
|
||||||
|
|
||||||
@ -20,24 +24,14 @@ class Boxed_Number;
|
|||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
template<typename T> std::shared_ptr<dispatch::Proxy_Function_Base> fun(const T &t);
|
|
||||||
|
|
||||||
template<typename Ret, typename ... Param>
|
|
||||||
std::shared_ptr<dispatch::Proxy_Function_Base> assignable_fun(
|
|
||||||
std::reference_wrapper<std::function<Ret (Param...)>> t_func,
|
|
||||||
std::shared_ptr<std::function<Ret (Param...)>> t_ptr
|
|
||||||
);
|
|
||||||
|
|
||||||
namespace dispatch
|
namespace dispatch
|
||||||
{
|
{
|
||||||
|
template<class T, class U> class Proxy_Function_Callable_Impl;
|
||||||
template<class T> class Assignable_Proxy_Function_Impl;
|
template<class T> class Assignable_Proxy_Function_Impl;
|
||||||
|
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
/**
|
/// Used internally for handling a return value from a Proxy_Function call
|
||||||
* Used internally for handling a return value from a Proxy_Function call
|
|
||||||
*/
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
struct Handle_Return
|
struct Handle_Return
|
||||||
{
|
{
|
||||||
@ -61,7 +55,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
static Boxed_Value handle(const std::function<Ret> &f) {
|
static Boxed_Value handle(const std::function<Ret> &f) {
|
||||||
return Boxed_Value(
|
return Boxed_Value(
|
||||||
chaiscript::fun(f)
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -76,7 +70,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
static Boxed_Value handle(const std::shared_ptr<std::function<Ret>> &f) {
|
static Boxed_Value handle(const std::shared_ptr<std::function<Ret>> &f) {
|
||||||
return Boxed_Value(
|
return Boxed_Value(
|
||||||
assignable_fun(std::ref(*f), f)
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function_Impl<Ret>>(std::ref(*f),f)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -96,17 +90,36 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
static Boxed_Value handle(std::function<Ret> &f) {
|
static Boxed_Value handle(std::function<Ret> &f) {
|
||||||
return Boxed_Value(
|
return Boxed_Value(
|
||||||
assignable_fun(std::ref(f), std::shared_ptr<std::function<Ret>>())
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Assignable_Proxy_Function_Impl<Ret>>(std::ref(f),
|
||||||
|
std::shared_ptr<std::function<Ret>>())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Value handle(const std::function<Ret> &f) {
|
static Boxed_Value handle(const std::function<Ret> &f) {
|
||||||
return Boxed_Value(
|
return Boxed_Value(
|
||||||
chaiscript::fun(f)
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret, std::function<Ret>>>(f)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Ret>
|
||||||
|
struct Handle_Return<Ret *&>
|
||||||
|
{
|
||||||
|
static Boxed_Value handle(Ret *p)
|
||||||
|
{
|
||||||
|
return Boxed_Value(p, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Ret>
|
||||||
|
struct Handle_Return<const Ret *&>
|
||||||
|
{
|
||||||
|
static Boxed_Value handle(const Ret *p)
|
||||||
|
{
|
||||||
|
return Boxed_Value(p, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
struct Handle_Return<Ret *>
|
struct Handle_Return<Ret *>
|
||||||
{
|
{
|
||||||
@ -144,6 +157,18 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Ret>
|
||||||
|
struct Handle_Return<std::unique_ptr<Ret>> : Handle_Return<std::unique_ptr<Ret> &>
|
||||||
|
{
|
||||||
|
static Boxed_Value handle(std::unique_ptr<Ret> &&r)
|
||||||
|
{
|
||||||
|
return Boxed_Value(std::move(r), true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
struct Handle_Return<const Ret &>
|
struct Handle_Return<const Ret &>
|
||||||
{
|
{
|
||||||
@ -153,6 +178,14 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Ret>
|
||||||
|
struct Handle_Return<const Ret>
|
||||||
|
{
|
||||||
|
static Boxed_Value handle(const Ret &r)
|
||||||
|
{
|
||||||
|
return Boxed_Value(std::cref(r));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
struct Handle_Return<Ret &>
|
struct Handle_Return<Ret &>
|
||||||
@ -161,11 +194,6 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
return Boxed_Value(std::ref(r));
|
return Boxed_Value(std::ref(r));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Boxed_Value handle(const Ret &r)
|
|
||||||
{
|
|
||||||
return Boxed_Value(std::cref(r));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_OPERATORS_HPP_
|
#ifndef CHAISCRIPT_OPERATORS_HPP_
|
||||||
#define CHAISCRIPT_OPERATORS_HPP_
|
#define CHAISCRIPT_OPERATORS_HPP_
|
||||||
|
|
||||||
@ -16,447 +20,202 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
namespace operators
|
namespace operators
|
||||||
{
|
{
|
||||||
namespace detail
|
|
||||||
{
|
|
||||||
/// \todo make this return a decltype once we drop gcc 4.6
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign(L l, R r) -> L&
|
|
||||||
{
|
|
||||||
return (l = r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_bitwise_and(L l, R r) -> decltype((l &= r))
|
|
||||||
{
|
|
||||||
return (l &= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_xor(L l, R r) -> decltype((l^=r))
|
|
||||||
{
|
|
||||||
return (l ^= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_bitwise_or(L l, R r) -> decltype((l |= r))
|
|
||||||
{
|
|
||||||
return (l |= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_difference(L l, R r) -> decltype(( l -= r))
|
|
||||||
{
|
|
||||||
return (l -= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_left_shift(L l, R r) -> decltype(( l <<= r))
|
|
||||||
{
|
|
||||||
return (l <<= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_product(L l, R r) -> decltype(( l *= r ))
|
|
||||||
{
|
|
||||||
return (l *= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_quotient(L l, R r) -> decltype(( l /= r ))
|
|
||||||
{
|
|
||||||
return (l /= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_remainder(L l, R r) -> decltype(( l %= r ))
|
|
||||||
{
|
|
||||||
return (l %= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_right_shift(L l, R r) -> decltype(( l >>= r))
|
|
||||||
{
|
|
||||||
return (l >>= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \todo make this return a decltype once we drop gcc 4.6
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto assign_sum(L l, R r) -> L&
|
|
||||||
{
|
|
||||||
return (l += r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L>
|
|
||||||
auto prefix_decrement(L l) -> decltype(( --l ))
|
|
||||||
{
|
|
||||||
return (--l);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L>
|
|
||||||
auto prefix_increment(L l) -> decltype(( ++l ))
|
|
||||||
{
|
|
||||||
return (++l);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto equal(L l, R r) -> decltype(( l == r ))
|
|
||||||
{
|
|
||||||
return (l == r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto greater_than(L l, R r) -> decltype(( l > r ))
|
|
||||||
{
|
|
||||||
return (l > r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto greater_than_equal(L l, R r) -> decltype(( l >= r ))
|
|
||||||
{
|
|
||||||
return (l >= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto less_than(L l, R r) -> decltype(( l < r ))
|
|
||||||
{
|
|
||||||
return (l < r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto less_than_equal(L l, R r) -> decltype(( l <= r ))
|
|
||||||
{
|
|
||||||
return (l <= r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L>
|
|
||||||
auto logical_compliment(L l) -> decltype(( !l ))
|
|
||||||
{
|
|
||||||
return (!l);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto not_equal(L l, R r) -> decltype(( l != r ))
|
|
||||||
{
|
|
||||||
return (l != r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto addition(L l, R r) -> decltype(( l + r ))
|
|
||||||
{
|
|
||||||
return (l + r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L>
|
|
||||||
auto unary_plus(L l) -> decltype(( +l ))
|
|
||||||
{
|
|
||||||
return (+l);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto subtraction(L l, R r) -> decltype(( l - r ))
|
|
||||||
{
|
|
||||||
return (l - r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L>
|
|
||||||
auto unary_minus(L l) -> decltype(( -l ))
|
|
||||||
{
|
|
||||||
#ifdef CHAISCRIPT_MSVC
|
|
||||||
#pragma warning(push)
|
|
||||||
#pragma warning(disable : 4146)
|
|
||||||
return (-l);
|
|
||||||
#pragma warning(pop)
|
|
||||||
#else
|
|
||||||
return (-l);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto bitwise_and(L l, R r) -> decltype(( l & r ))
|
|
||||||
{
|
|
||||||
return (l & r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L>
|
|
||||||
auto bitwise_compliment(L l) -> decltype(( ~l ))
|
|
||||||
{
|
|
||||||
return (~l);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto bitwise_xor(L l, R r) -> decltype(( l ^ r ))
|
|
||||||
{
|
|
||||||
return (l ^ r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto bitwise_or(L l, R r) -> decltype(( l | r ))
|
|
||||||
{
|
|
||||||
return (l | r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto division(L l, R r) -> decltype(( l / r ))
|
|
||||||
{
|
|
||||||
return (l / r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto left_shift(L l, R r) -> decltype(( l << r ))
|
|
||||||
{
|
|
||||||
return l << r;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto multiplication(L l, R r) -> decltype(( l * r ))
|
|
||||||
{
|
|
||||||
return l * r;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto remainder(L l, R r) -> decltype(( l % r ))
|
|
||||||
{
|
|
||||||
return (l % r);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename L, typename R>
|
|
||||||
auto right_shift(L l, R r) -> decltype(( l >> r ))
|
|
||||||
{
|
|
||||||
return (l >> r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign(ModulePtr m = std::make_shared<Module>())
|
void assign(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign<T &, const T&>), "=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs = rhs;}), "=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_bitwise_and(ModulePtr m = std::make_shared<Module>())
|
void assign_bitwise_and(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_bitwise_and<T &, const T&>), "&=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs &= rhs;}), "&=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_xor(ModulePtr m = std::make_shared<Module>())
|
void assign_xor(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_xor<T &, const T&>), "^=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs ^= rhs;}), "^=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_bitwise_or(ModulePtr m = std::make_shared<Module>())
|
void assign_bitwise_or(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_bitwise_or<T &, const T&>), "|=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs |= rhs;}), "|=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_difference(ModulePtr m = std::make_shared<Module>())
|
void assign_difference(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_difference<T &, const T&>), "-=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs -= rhs;}), "-=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_left_shift(ModulePtr m = std::make_shared<Module>())
|
void assign_left_shift(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_left_shift<T &, const T&>), "<<=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs <<= rhs;}), "<<=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_product(ModulePtr m = std::make_shared<Module>())
|
void assign_product(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_product<T &, const T&>), "*=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs <<= rhs;}), "*=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_quotient(ModulePtr m = std::make_shared<Module>())
|
void assign_quotient(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_quotient<T &, const T&>), "/=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs /= rhs;}), "/=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_remainder(ModulePtr m = std::make_shared<Module>())
|
void assign_remainder(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_remainder<T &, const T&>), "%=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs %= rhs;}), "%=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_right_shift(ModulePtr m = std::make_shared<Module>())
|
void assign_right_shift(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_right_shift<T &, const T&>), ">>=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs >>= rhs;}), ">>=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr assign_sum(ModulePtr m = std::make_shared<Module>())
|
void assign_sum(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::assign_sum<T &, const T&>), "+=");
|
m.add(chaiscript::fun([](T &lhs, const T&rhs)->T&{return lhs += rhs;}), "+=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr prefix_decrement(ModulePtr m = std::make_shared<Module>())
|
void prefix_decrement(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::prefix_decrement<T &>), "--");
|
m.add(chaiscript::fun([](T &lhs)->T&{return --lhs;}), "--");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr prefix_increment(ModulePtr m = std::make_shared<Module>())
|
void prefix_increment(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::prefix_increment<T &>), "++");
|
m.add(chaiscript::fun([](T &lhs)->T&{return ++lhs;}), "++");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr equal(ModulePtr m = std::make_shared<Module>())
|
void equal(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::equal<const T&, const T&>), "==");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs==rhs;}), "==");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr greater_than(ModulePtr m = std::make_shared<Module>())
|
void greater_than(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::greater_than<const T&, const T&>), ">");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs>rhs;}), ">");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr greater_than_equal(ModulePtr m = std::make_shared<Module>())
|
void greater_than_equal(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::greater_than_equal<const T&, const T&>), ">=");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs>=rhs;}), ">=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr less_than(ModulePtr m = std::make_shared<Module>())
|
void less_than(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::less_than<const T&, const T&>), "<");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs<rhs;}), "<");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr less_than_equal(ModulePtr m = std::make_shared<Module>())
|
void less_than_equal(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::less_than_equal<const T&, const T&>), "<=");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs<=rhs;}), "<=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr logical_compliment(ModulePtr m = std::make_shared<Module>())
|
void logical_compliment(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::logical_compliment<const T &>), "!");
|
m.add(chaiscript::fun([](const T &lhs){return !lhs;}), "!");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr not_equal(ModulePtr m = std::make_shared<Module>())
|
void not_equal(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::not_equal<const T &, const T &>), "!=");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs!=rhs;}), "!=");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr addition(ModulePtr m = std::make_shared<Module>())
|
void addition(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::addition<const T &, const T &>), "+");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs+rhs;}), "+");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr unary_plus(ModulePtr m = std::make_shared<Module>())
|
void unary_plus(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::unary_plus<const T &>), "+");
|
m.add(chaiscript::fun([](const T &lhs){return +lhs;}), "+");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr subtraction(ModulePtr m = std::make_shared<Module>())
|
void subtraction(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::subtraction<const T &, const T &>), "-");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs-rhs;}), "-");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr unary_minus(ModulePtr m = std::make_shared<Module>())
|
void unary_minus(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::unary_minus<const T &>), "-");
|
m.add(chaiscript::fun([](const T &lhs){return -lhs;}), "-");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr bitwise_and(ModulePtr m = std::make_shared<Module>())
|
void bitwise_and(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::bitwise_and<const T &, const T &>), "&");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs&rhs;}), "&");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr bitwise_compliment(ModulePtr m = std::make_shared<Module>())
|
void bitwise_compliment(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::bitwise_compliment<const T &>), "~");
|
m.add(chaiscript::fun([](const T &lhs){return ~lhs;}), "~");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr bitwise_xor(ModulePtr m = std::make_shared<Module>())
|
void bitwise_xor(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::bitwise_xor<const T &, const T &>), "^");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs^rhs;}), "^");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr bitwise_or(ModulePtr m = std::make_shared<Module>())
|
void bitwise_or(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::bitwise_or<const T &, const T &>), "|");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs|rhs;}), "|");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr division(ModulePtr m = std::make_shared<Module>())
|
void division(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::division<const T &, const T &>), "/");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs/rhs;}), "/");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr left_shift(ModulePtr m = std::make_shared<Module>())
|
void left_shift(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::left_shift<const T &, const T &>), "<<");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs<<rhs;}), "<<");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr multiplication(ModulePtr m = std::make_shared<Module>())
|
void multiplication(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::multiplication<const T &, const T &>), "*");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs*rhs;}), "*");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr remainder(ModulePtr m = std::make_shared<Module>())
|
void remainder(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::remainder<const T &, const T &>), "%");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs%rhs;}), "%");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ModulePtr right_shift(ModulePtr m = std::make_shared<Module>())
|
void right_shift(Module& m)
|
||||||
{
|
{
|
||||||
m->add(chaiscript::fun(&detail::right_shift<const T &, const T &>), ">>");
|
m.add(chaiscript::fun([](const T &lhs, const T &rhs){return lhs>>rhs;}), ">>");
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
|
#ifndef CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
|
||||||
#define CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
|
#define CHAISCRIPT_PROXY_CONSTRUCTORS_HPP_
|
||||||
@ -17,32 +20,13 @@ namespace chaiscript
|
|||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename Class, typename ... Params, size_t ... I >
|
template<typename Class, typename ... Params >
|
||||||
Proxy_Function build_constructor_(Class (*)(Params...), std::index_sequence<I...>)
|
Proxy_Function build_constructor_(Class (*)(Params...))
|
||||||
{
|
{
|
||||||
return [](){
|
auto call = dispatch::detail::Constructor<Class, Params...>();
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func()
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<std::shared_ptr<Class>>(), user_type<Params>()...})
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
return Proxy_Function(
|
||||||
{
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<std::shared_ptr<Class> (Params...), decltype(call)>>(call));
|
||||||
return compare_types_with_cast_impl<Params...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return Handle_Return<std::shared_ptr<Class>>::handle(std::make_shared<Class>(boxed_cast<Params>(params[I], &t_conversions)...));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>();
|
|
||||||
}();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,7 +47,7 @@ namespace chaiscript
|
|||||||
Proxy_Function constructor()
|
Proxy_Function constructor()
|
||||||
{
|
{
|
||||||
T *f = nullptr;
|
T *f = nullptr;
|
||||||
return dispatch::detail::build_constructor_(f, std::make_index_sequence<dispatch::detail::Arity<T>::arity>());
|
return (dispatch::detail::build_constructor_(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_PROXY_FUNCTIONS_HPP_
|
#ifndef CHAISCRIPT_PROXY_FUNCTIONS_HPP_
|
||||||
#define CHAISCRIPT_PROXY_FUNCTIONS_HPP_
|
#define CHAISCRIPT_PROXY_FUNCTIONS_HPP_
|
||||||
@ -21,34 +24,15 @@
|
|||||||
#include "../chaiscript_defines.hpp"
|
#include "../chaiscript_defines.hpp"
|
||||||
#include "boxed_cast.hpp"
|
#include "boxed_cast.hpp"
|
||||||
#include "boxed_value.hpp"
|
#include "boxed_value.hpp"
|
||||||
|
#include "proxy_functions_detail.hpp"
|
||||||
#include "type_info.hpp"
|
#include "type_info.hpp"
|
||||||
#include "dynamic_object.hpp"
|
#include "dynamic_object.hpp"
|
||||||
#include "callable_traits.hpp"
|
|
||||||
#include "handle_return.hpp"
|
|
||||||
|
|
||||||
namespace chaiscript {
|
namespace chaiscript {
|
||||||
class Type_Conversions;
|
class Type_Conversions;
|
||||||
namespace exception {
|
namespace exception {
|
||||||
class bad_boxed_cast;
|
class bad_boxed_cast;
|
||||||
/**
|
struct arity_error;
|
||||||
* Exception thrown when there is a mismatch in number of
|
|
||||||
* parameters during Proxy_Function execution
|
|
||||||
*/
|
|
||||||
struct arity_error : std::range_error
|
|
||||||
{
|
|
||||||
arity_error(int t_got, int t_expected)
|
|
||||||
: std::range_error("Function dispatch arity mismatch"),
|
|
||||||
got(t_got), expected(t_expected)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
arity_error(const arity_error &) = default;
|
|
||||||
|
|
||||||
virtual ~arity_error() noexcept {}
|
|
||||||
|
|
||||||
int got;
|
|
||||||
int expected;
|
|
||||||
};
|
|
||||||
} // namespace exception
|
} // namespace exception
|
||||||
} // namespace chaiscript
|
} // namespace chaiscript
|
||||||
|
|
||||||
@ -72,7 +56,7 @@ namespace chaiscript
|
|||||||
m_doti(user_type<Dynamic_Object>())
|
m_doti(user_type<Dynamic_Object>())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Param_Types(std::vector<std::pair<std::string, Type_Info>> t_types)
|
explicit Param_Types(std::vector<std::pair<std::string, Type_Info>> t_types)
|
||||||
: m_types(std::move(t_types)),
|
: m_types(std::move(t_types)),
|
||||||
m_has_types(false),
|
m_has_types(false),
|
||||||
m_doti(user_type<Dynamic_Object>())
|
m_doti(user_type<Dynamic_Object>())
|
||||||
@ -82,7 +66,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
void push_front(std::string t_name, Type_Info t_ti)
|
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));
|
m_types.emplace(m_types.begin(), std::move(t_name), t_ti);
|
||||||
update_has_types();
|
update_has_types();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,10 +75,51 @@ namespace chaiscript
|
|||||||
return m_types == t_rhs.m_types;
|
return m_types == t_rhs.m_types;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const
|
std::vector<Boxed_Value> convert(std::vector<Boxed_Value> vals, const Type_Conversions_State &t_conversions) const
|
||||||
{
|
{
|
||||||
if (!m_has_types) return true;
|
for (size_t i = 0; i < vals.size(); ++i)
|
||||||
if (vals.size() != m_types.size()) return false;
|
{
|
||||||
|
const auto &name = m_types[i].first;
|
||||||
|
if (!name.empty()) {
|
||||||
|
const auto &bv = vals[i];
|
||||||
|
|
||||||
|
if (!bv.get_type_info().bare_equal(m_doti))
|
||||||
|
{
|
||||||
|
const auto &ti = m_types[i].second;
|
||||||
|
if (!ti.is_undef())
|
||||||
|
{
|
||||||
|
if (!bv.get_type_info().bare_equal(ti)) {
|
||||||
|
if (t_conversions->converts(ti, bv.get_type_info())) {
|
||||||
|
try {
|
||||||
|
// 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
|
||||||
|
vals[i] = t_conversions->boxed_type_conversion(m_types[i].second, t_conversions.saves(), vals[i]);
|
||||||
|
} catch (...) {
|
||||||
|
try {
|
||||||
|
// try going the other way
|
||||||
|
vals[i] = t_conversions->boxed_type_down_conversion(m_types[i].second, t_conversions.saves(), vals[i]);
|
||||||
|
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
||||||
|
throw exception::bad_boxed_cast(bv.get_type_info(), *m_types[i].second.bare_type_info());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return vals;
|
||||||
|
}
|
||||||
|
|
||||||
|
// first result: is a match
|
||||||
|
// second result: needs conversions
|
||||||
|
std::pair<bool, bool> match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const
|
||||||
|
{
|
||||||
|
bool needs_conversion = false;
|
||||||
|
|
||||||
|
if (!m_has_types) { return std::make_pair(true, needs_conversion); }
|
||||||
|
if (vals.size() != m_types.size()) { return std::make_pair(false, needs_conversion); }
|
||||||
|
|
||||||
for (size_t i = 0; i < vals.size(); ++i)
|
for (size_t i = 0; i < vals.size(); ++i)
|
||||||
{
|
{
|
||||||
@ -106,25 +131,31 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
|
const Dynamic_Object &d = boxed_cast<const Dynamic_Object &>(bv, &t_conversions);
|
||||||
return name == "Dynamic_Object" || d.get_type_name() == name;
|
if (!(name == "Dynamic_Object" || d.get_type_name() == name)) {
|
||||||
|
return std::make_pair(false, false);
|
||||||
|
}
|
||||||
} catch (const std::bad_cast &) {
|
} catch (const std::bad_cast &) {
|
||||||
return false;
|
return std::make_pair(false, false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const auto &ti = m_types[i].second;
|
const auto &ti = m_types[i].second;
|
||||||
if (!ti.is_undef())
|
if (!ti.is_undef())
|
||||||
{
|
{
|
||||||
if (!bv.get_type_info().bare_equal(ti)) {
|
if (!bv.get_type_info().bare_equal(ti)) {
|
||||||
return false;
|
if (!t_conversions->converts(ti, bv.get_type_info())) {
|
||||||
|
return std::make_pair(false, false);
|
||||||
|
} else {
|
||||||
|
needs_conversion = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return std::make_pair(false, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return std::make_pair(true, needs_conversion);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<std::pair<std::string, Type_Info>> &types() const
|
const std::vector<std::pair<std::string, Type_Info>> &types() const
|
||||||
@ -200,20 +231,15 @@ namespace chaiscript
|
|||||||
//! to the passed in values
|
//! to the passed in values
|
||||||
bool filter(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const
|
bool filter(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const
|
||||||
{
|
{
|
||||||
|
assert(m_arity == -1 || (m_arity > 0 && static_cast<int>(vals.size()) == m_arity));
|
||||||
|
|
||||||
if (m_arity < 0)
|
if (m_arity < 0)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
} else if (static_cast<size_t>(m_arity) == vals.size()) {
|
} else if (m_arity > 1) {
|
||||||
if (m_arity == 0)
|
return compare_type_to_param(m_types[1], vals[0], t_conversions) && compare_type_to_param(m_types[2], vals[1], t_conversions);
|
||||||
{
|
|
||||||
return true;
|
|
||||||
} else if (m_arity > 1) {
|
|
||||||
return compare_type_to_param(m_types[1], vals[0], t_conversions) && compare_type_to_param(m_types[2], vals[1], t_conversions);
|
|
||||||
} else {
|
|
||||||
return compare_type_to_param(m_types[1], vals[0], t_conversions);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return compare_type_to_param(m_types[1], vals[0], t_conversions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,8 +249,6 @@ namespace chaiscript
|
|||||||
return m_arity;
|
return m_arity;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string annotation() const = 0;
|
|
||||||
|
|
||||||
static bool compare_type_to_param(const Type_Info &ti, const Boxed_Value &bv, const Type_Conversions_State &t_conversions)
|
static bool compare_type_to_param(const Type_Info &ti, const Boxed_Value &bv, const Type_Conversions_State &t_conversions)
|
||||||
{
|
{
|
||||||
if (ti.is_undef()
|
if (ti.is_undef()
|
||||||
@ -308,7 +332,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
guard_error(const guard_error &) = default;
|
guard_error(const guard_error &) = default;
|
||||||
|
|
||||||
virtual ~guard_error() noexcept = default;
|
~guard_error() noexcept override = default;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,30 +349,28 @@ namespace chaiscript
|
|||||||
int t_arity=-1,
|
int t_arity=-1,
|
||||||
AST_NodePtr t_parsenode = AST_NodePtr(),
|
AST_NodePtr t_parsenode = AST_NodePtr(),
|
||||||
Param_Types t_param_types = Param_Types(),
|
Param_Types t_param_types = Param_Types(),
|
||||||
std::string t_description = "",
|
|
||||||
Proxy_Function t_guard = Proxy_Function())
|
Proxy_Function t_guard = Proxy_Function())
|
||||||
: Proxy_Function_Base(build_param_type_list(t_param_types), t_arity),
|
: Proxy_Function_Base(build_param_type_list(t_param_types), t_arity),
|
||||||
m_param_types(std::move(t_param_types)),
|
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_guard(std::move(t_guard)), m_parsenode(std::move(t_parsenode))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
virtual bool operator==(const Proxy_Function_Base &rhs) const override
|
bool operator==(const Proxy_Function_Base &rhs) const override
|
||||||
{
|
{
|
||||||
const Dynamic_Proxy_Function *prhs = dynamic_cast<const Dynamic_Proxy_Function *>(&rhs);
|
const Dynamic_Proxy_Function *prhs = dynamic_cast<const Dynamic_Proxy_Function *>(&rhs);
|
||||||
|
|
||||||
return this == &rhs
|
return this == &rhs
|
||||||
|| (prhs
|
|| ((prhs != nullptr)
|
||||||
&& this->m_arity == prhs->m_arity
|
&& 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);
|
&& this->m_param_types == prhs->m_param_types);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
return (m_arity < 0 || (vals.size() == size_t(m_arity) && m_param_types.match(vals, t_conversions)))
|
return call_match_internal(vals, t_conversions).first;
|
||||||
&& test_guard(vals, t_conversions);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -362,11 +384,6 @@ namespace chaiscript
|
|||||||
return m_parsenode;
|
return m_parsenode;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string annotation() const override
|
|
||||||
{
|
|
||||||
return m_description;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool test_guard(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const
|
bool test_guard(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const
|
||||||
@ -385,6 +402,26 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// first result: is a match
|
||||||
|
// second result: needs conversions
|
||||||
|
std::pair<bool, bool> call_match_internal(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const
|
||||||
|
{
|
||||||
|
const auto comparison_result = [&](){
|
||||||
|
if (m_arity < 0) {
|
||||||
|
return std::make_pair(true, false);
|
||||||
|
} else if (vals.size() == size_t(m_arity)) {
|
||||||
|
return m_param_types.match(vals, t_conversions);
|
||||||
|
} else {
|
||||||
|
return std::make_pair(false, false);
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
|
||||||
|
return std::make_pair(
|
||||||
|
comparison_result.first && test_guard(vals, t_conversions),
|
||||||
|
comparison_result.second
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::vector<Type_Info> build_param_type_list(const Param_Types &t_types)
|
static std::vector<Type_Info> build_param_type_list(const Param_Types &t_types)
|
||||||
{
|
{
|
||||||
@ -403,10 +440,12 @@ namespace chaiscript
|
|||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
Param_Types m_param_types;
|
Param_Types m_param_types;
|
||||||
|
|
||||||
|
private:
|
||||||
Proxy_Function m_guard;
|
Proxy_Function m_guard;
|
||||||
AST_NodePtr m_parsenode;
|
AST_NodePtr m_parsenode;
|
||||||
std::string m_description;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -420,13 +459,11 @@ namespace chaiscript
|
|||||||
int t_arity=-1,
|
int t_arity=-1,
|
||||||
AST_NodePtr t_parsenode = AST_NodePtr(),
|
AST_NodePtr t_parsenode = AST_NodePtr(),
|
||||||
Param_Types t_param_types = Param_Types(),
|
Param_Types t_param_types = Param_Types(),
|
||||||
std::string t_description = "",
|
|
||||||
Proxy_Function t_guard = Proxy_Function())
|
Proxy_Function t_guard = Proxy_Function())
|
||||||
: Dynamic_Proxy_Function(
|
: Dynamic_Proxy_Function(
|
||||||
t_arity,
|
t_arity,
|
||||||
std::move(t_parsenode),
|
std::move(t_parsenode),
|
||||||
std::move(t_param_types),
|
std::move(t_param_types),
|
||||||
std::move(t_description),
|
|
||||||
std::move(t_guard)
|
std::move(t_guard)
|
||||||
),
|
),
|
||||||
m_f(std::move(t_f))
|
m_f(std::move(t_f))
|
||||||
@ -437,9 +474,14 @@ namespace chaiscript
|
|||||||
protected:
|
protected:
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
if (call_match(params, t_conversions) && test_guard(params, t_conversions))
|
const auto match_results = call_match_internal(params, t_conversions);
|
||||||
|
if (match_results.first)
|
||||||
{
|
{
|
||||||
return m_f(params);
|
if (match_results.second) {
|
||||||
|
return m_f(m_param_types.convert(params, t_conversions));
|
||||||
|
} else {
|
||||||
|
return m_f(params);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw exception::guard_error();
|
throw exception::guard_error();
|
||||||
}
|
}
|
||||||
@ -525,10 +567,6 @@ namespace chaiscript
|
|||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string annotation() const override
|
|
||||||
{
|
|
||||||
return "Bound: " + m_f->annotation();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static std::vector<Type_Info> build_param_type_info(const Const_Proxy_Function &t_f,
|
static std::vector<Type_Info> build_param_type_info(const Const_Proxy_Function &t_f,
|
||||||
@ -555,7 +593,7 @@ namespace chaiscript
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
return (*m_f)(build_param_list(params), t_conversions);
|
return (*m_f)(build_param_list(params), t_conversions);
|
||||||
}
|
}
|
||||||
@ -568,58 +606,11 @@ namespace chaiscript
|
|||||||
class Proxy_Function_Impl_Base : public Proxy_Function_Base
|
class Proxy_Function_Impl_Base : public Proxy_Function_Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Proxy_Function_Impl_Base(const std::vector<Type_Info> &t_types)
|
explicit Proxy_Function_Impl_Base(const std::vector<Type_Info> &t_types)
|
||||||
: Proxy_Function_Base(t_types, static_cast<int>(t_types.size()) - 1)
|
: Proxy_Function_Base(t_types, static_cast<int>(t_types.size()) - 1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string annotation() const override
|
|
||||||
{
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const Proxy_Function_Base &t_func) const override
|
|
||||||
{
|
|
||||||
const auto *t_other = dynamic_cast<const Proxy_Function_Impl_Base *>(&t_func);
|
|
||||||
return
|
|
||||||
t_other != nullptr &&
|
|
||||||
t_other->m_types.size() == m_types.size() &&
|
|
||||||
[this, &t_other](){
|
|
||||||
auto begin1 = std::begin(m_types);
|
|
||||||
const auto end1 = std::end(m_types);
|
|
||||||
auto begin2 = std::begin(t_other->m_types);
|
|
||||||
|
|
||||||
while (begin1 != end1) {
|
|
||||||
if (*begin1 != *begin2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (begin1->is_const() != begin2->is_const()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
++begin1;
|
|
||||||
++begin2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ... Param>
|
|
||||||
static bool compare_types_with_cast_impl(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
std::vector<Boxed_Value>::size_type i = 0;
|
|
||||||
(void)i; (void)params; (void)t_conversions;
|
|
||||||
// this is ok because the order of evaluation of initializer lists is well defined
|
|
||||||
(void)std::initializer_list<int>{(boxed_cast<Param>(params[i++], &t_conversions), 0)...};
|
|
||||||
return true;
|
|
||||||
} catch (const exception::bad_boxed_cast &) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
return static_cast<int>(vals.size()) == get_arity()
|
return static_cast<int>(vals.size()) == get_arity()
|
||||||
@ -630,10 +621,44 @@ namespace chaiscript
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// For any callable object
|
||||||
|
template<typename Func, typename Callable>
|
||||||
|
class Proxy_Function_Callable_Impl final : public Proxy_Function_Impl_Base
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit Proxy_Function_Callable_Impl(Callable f)
|
||||||
|
: Proxy_Function_Impl_Base(detail::build_param_type_list(static_cast<Func *>(nullptr))),
|
||||||
|
m_f(std::move(f))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool compare_types_with_cast(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
||||||
|
{
|
||||||
|
return detail::compare_types_cast(static_cast<Func *>(nullptr), vals, t_conversions);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const Proxy_Function_Base &t_func) const override
|
||||||
|
{
|
||||||
|
return dynamic_cast<const Proxy_Function_Callable_Impl<Func, Callable> *>(&t_func) != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
|
{
|
||||||
|
return detail::call_func(detail::Function_Signature<Func>(), m_f, params, t_conversions);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Callable m_f;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class Assignable_Proxy_Function : public Proxy_Function_Impl_Base
|
class Assignable_Proxy_Function : public Proxy_Function_Impl_Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Assignable_Proxy_Function(const std::vector<Type_Info> &t_types)
|
explicit Assignable_Proxy_Function(const std::vector<Type_Info> &t_types)
|
||||||
: Proxy_Function_Impl_Base(t_types)
|
: Proxy_Function_Impl_Base(t_types)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -641,6 +666,47 @@ namespace chaiscript
|
|||||||
virtual void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) = 0;
|
virtual void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Func>
|
||||||
|
class Assignable_Proxy_Function_Impl final : public Assignable_Proxy_Function
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Assignable_Proxy_Function_Impl(std::reference_wrapper<std::function<Func>> t_f, std::shared_ptr<std::function<Func>> t_ptr)
|
||||||
|
: Assignable_Proxy_Function(detail::build_param_type_list(static_cast<Func *>(nullptr))),
|
||||||
|
m_f(std::move(t_f)), m_shared_ptr_holder(std::move(t_ptr))
|
||||||
|
{
|
||||||
|
assert(!m_shared_ptr_holder || m_shared_ptr_holder.get() == &m_f.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool compare_types_with_cast(const std::vector<Boxed_Value> &vals, const Type_Conversions_State &t_conversions) const override
|
||||||
|
{
|
||||||
|
return detail::compare_types_cast(static_cast<Func *>(nullptr), vals, t_conversions);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const Proxy_Function_Base &t_func) const override
|
||||||
|
{
|
||||||
|
return dynamic_cast<const Assignable_Proxy_Function_Impl<Func> *>(&t_func) != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<Func> internal_function() const
|
||||||
|
{
|
||||||
|
return m_f.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) override {
|
||||||
|
m_f.get() = dispatch::functor<Func>(t_rhs, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
|
{
|
||||||
|
return detail::call_func(detail::Function_Signature<Func>(), m_f.get(), params, t_conversions);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::reference_wrapper<std::function<Func>> m_f;
|
||||||
|
std::shared_ptr<std::function<Func>> m_shared_ptr_holder;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Attribute getter Proxy_Function implementation
|
/// Attribute getter Proxy_Function implementation
|
||||||
@ -648,7 +714,7 @@ namespace chaiscript
|
|||||||
class Attribute_Access final : public Proxy_Function_Base
|
class Attribute_Access final : public Proxy_Function_Base
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Attribute_Access(T Class::* t_attr)
|
explicit Attribute_Access(T Class::* t_attr)
|
||||||
: Proxy_Function_Base(param_types(), 1),
|
: Proxy_Function_Base(param_types(), 1),
|
||||||
m_attr(t_attr)
|
m_attr(t_attr)
|
||||||
{
|
{
|
||||||
@ -678,11 +744,6 @@ namespace chaiscript
|
|||||||
return vals[0].get_type_info().bare_equal(user_type<Class>());
|
return vals[0].get_type_info().bare_equal(user_type<Class>());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string annotation() const override
|
|
||||||
{
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
||||||
{
|
{
|
||||||
@ -713,13 +774,13 @@ namespace chaiscript
|
|||||||
template<typename Type>
|
template<typename Type>
|
||||||
auto do_call_impl(Class *o) const -> std::enable_if_t<!std::is_pointer<Type>::value, Boxed_Value>
|
auto do_call_impl(Class *o) const -> std::enable_if_t<!std::is_pointer<Type>::value, Boxed_Value>
|
||||||
{
|
{
|
||||||
return detail::Handle_Return<const typename std::add_lvalue_reference<Type>::type>::handle(o->*m_attr);
|
return detail::Handle_Return<typename std::add_lvalue_reference<Type>::type>::handle(o->*m_attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
auto do_call_impl(const Class *o) const -> std::enable_if_t<!std::is_pointer<Type>::value, Boxed_Value>
|
auto do_call_impl(const Class *o) const -> std::enable_if_t<!std::is_pointer<Type>::value, Boxed_Value>
|
||||||
{
|
{
|
||||||
return detail::Handle_Return<const typename std::add_lvalue_reference<Type>::type>::handle(o->*m_attr);
|
return detail::Handle_Return<typename std::add_lvalue_reference<typename std::add_const<Type>::type>::type>::handle(o->*m_attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -758,7 +819,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
dispatch_error(const dispatch_error &) = default;
|
dispatch_error(const dispatch_error &) = default;
|
||||||
virtual ~dispatch_error() noexcept = default;
|
~dispatch_error() noexcept override = default;
|
||||||
|
|
||||||
std::vector<Boxed_Value> parameters;
|
std::vector<Boxed_Value> parameters;
|
||||||
std::vector<Const_Proxy_Function> functions;
|
std::vector<Const_Proxy_Function> functions;
|
||||||
@ -775,7 +836,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
const std::vector<Type_Info> &types = t_func->get_param_types();
|
const std::vector<Type_Info> &types = t_func->get_param_types();
|
||||||
|
|
||||||
if (t_func->get_arity() == -1) return false;
|
if (t_func->get_arity() == -1) { return false; }
|
||||||
|
|
||||||
assert(plist.size() == types.size() - 1);
|
assert(plist.size() == types.size() - 1);
|
||||||
|
|
||||||
@ -835,7 +896,8 @@ namespace chaiscript
|
|||||||
plist.begin(),
|
plist.begin(),
|
||||||
std::back_inserter(newplist),
|
std::back_inserter(newplist),
|
||||||
[](const Type_Info &ti, const Boxed_Value ¶m) -> Boxed_Value {
|
[](const Type_Info &ti, const Boxed_Value ¶m) -> Boxed_Value {
|
||||||
if (ti.is_arithmetic() && param.get_type_info().is_arithmetic()) {
|
if (ti.is_arithmetic() && param.get_type_info().is_arithmetic()
|
||||||
|
&& param.get_type_info() != ti) {
|
||||||
return Boxed_Number(param).get_as(ti).bv;
|
return Boxed_Number(param).get_as(ti).bv;
|
||||||
} else {
|
} else {
|
||||||
return param;
|
return param;
|
||||||
@ -843,8 +905,6 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return (*(matching_func->second))(newplist, t_conversions);
|
return (*(matching_func->second))(newplist, t_conversions);
|
||||||
} catch (const exception::bad_boxed_cast &) {
|
} catch (const exception::bad_boxed_cast &) {
|
||||||
@ -860,11 +920,9 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/// Take a vector of functions and a vector of parameters. Attempt to execute
|
||||||
* Take a vector of functions and a vector of parameters. Attempt to execute
|
/// each function against the set of parameters, in order, until a matching
|
||||||
* each function against the set of parameters, in order, until a matching
|
/// function is found or throw dispatch_error if no matching function is found
|
||||||
* function is found or throw dispatch_error if no matching function is found
|
|
||||||
*/
|
|
||||||
template<typename Funcs>
|
template<typename Funcs>
|
||||||
Boxed_Value dispatch(const Funcs &funcs,
|
Boxed_Value dispatch(const Funcs &funcs,
|
||||||
const std::vector<Boxed_Value> &plist, const Type_Conversions_State &t_conversions)
|
const std::vector<Boxed_Value> &plist, const Type_Conversions_State &t_conversions)
|
||||||
@ -898,7 +956,7 @@ namespace chaiscript
|
|||||||
for (const auto &func : ordered_funcs )
|
for (const auto &func : ordered_funcs )
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (func.first == i && func.second->filter(plist, t_conversions))
|
if (func.first == i && (i == 0 || func.second->filter(plist, t_conversions)))
|
||||||
{
|
{
|
||||||
return (*(func.second))(plist, t_conversions);
|
return (*(func.second))(plist, t_conversions);
|
||||||
}
|
}
|
||||||
|
139
include/chaiscript/dispatchkit/proxy_functions_detail.hpp
Normal file
139
include/chaiscript/dispatchkit/proxy_functions_detail.hpp
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_
|
||||||
|
#define CHAISCRIPT_PROXY_FUNCTIONS_DETAIL_HPP_
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <vector>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#include "../chaiscript_defines.hpp"
|
||||||
|
#include "boxed_cast.hpp"
|
||||||
|
#include "boxed_value.hpp"
|
||||||
|
#include "handle_return.hpp"
|
||||||
|
#include "type_info.hpp"
|
||||||
|
#include "callable_traits.hpp"
|
||||||
|
|
||||||
|
namespace chaiscript {
|
||||||
|
class Type_Conversions_State;
|
||||||
|
namespace exception {
|
||||||
|
class bad_boxed_cast;
|
||||||
|
} // namespace exception
|
||||||
|
} // namespace chaiscript
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
namespace exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Exception thrown when there is a mismatch in number of
|
||||||
|
* parameters during Proxy_Function execution
|
||||||
|
*/
|
||||||
|
struct arity_error : std::range_error
|
||||||
|
{
|
||||||
|
arity_error(int t_got, int t_expected)
|
||||||
|
: std::range_error("Function dispatch arity mismatch"),
|
||||||
|
got(t_got), expected(t_expected)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
arity_error(const arity_error &) = default;
|
||||||
|
|
||||||
|
~arity_error() noexcept override = default;
|
||||||
|
|
||||||
|
int got;
|
||||||
|
int expected;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace dispatch
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Used by Proxy_Function_Impl to return a list of all param types
|
||||||
|
* it contains.
|
||||||
|
*/
|
||||||
|
template<typename Ret, typename ... Params>
|
||||||
|
std::vector<Type_Info> build_param_type_list(Ret (*)(Params...))
|
||||||
|
{
|
||||||
|
/// \note somehow this is responsible for a large part of the code generation
|
||||||
|
return { user_type<Ret>(), user_type<Params>()... };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used by Proxy_Function_Impl to determine if it is equivalent to another
|
||||||
|
* Proxy_Function_Impl object. This function is primarily used to prevent
|
||||||
|
* registration of two functions with the exact same signatures
|
||||||
|
*/
|
||||||
|
template<typename Ret, typename ... Params>
|
||||||
|
bool compare_types_cast(Ret (*)(Params...),
|
||||||
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
std::vector<Boxed_Value>::size_type i = 0;
|
||||||
|
(void)i;
|
||||||
|
(void)params; (void)t_conversions;
|
||||||
|
// this is ok because the order of evaluation of initializer lists is well defined
|
||||||
|
(void)std::initializer_list<int>{(boxed_cast<Params>(params[i++], &t_conversions), 0)...};
|
||||||
|
return true;
|
||||||
|
} catch (const exception::bad_boxed_cast &) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Callable, typename Ret, typename ... Params, size_t ... I>
|
||||||
|
Ret call_func(const chaiscript::dispatch::detail::Function_Signature<Ret (Params...)> &,
|
||||||
|
std::index_sequence<I...>, const Callable &f,
|
||||||
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions)
|
||||||
|
{
|
||||||
|
(void)params; (void)t_conversions;
|
||||||
|
return f(boxed_cast<Params>(params[I], &t_conversions)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Used by Proxy_Function_Impl to perform typesafe execution of a function.
|
||||||
|
/// The function attempts to unbox each parameter to the expected type.
|
||||||
|
/// if any unboxing fails the execution of the function fails and
|
||||||
|
/// the bad_boxed_cast is passed up to the caller.
|
||||||
|
template<typename Callable, typename Ret, typename ... Params>
|
||||||
|
Boxed_Value call_func(const chaiscript::dispatch::detail::Function_Signature<Ret (Params...)> &sig, const Callable &f,
|
||||||
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions)
|
||||||
|
{
|
||||||
|
return Handle_Return<Ret>::handle(call_func(sig, std::index_sequence_for<Params...>{}, f, params, t_conversions));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callable, typename ... Params>
|
||||||
|
Boxed_Value call_func(const chaiscript::dispatch::detail::Function_Signature<void (Params...)> &sig, const Callable &f,
|
||||||
|
const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions)
|
||||||
|
{
|
||||||
|
call_func(sig, std::index_sequence_for<Params...>{}, f, params, t_conversions);
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4702)
|
||||||
|
#endif
|
||||||
|
// MSVC is reporting that this is unreachable code - and it's wrong.
|
||||||
|
return Handle_Return<void>::handle();
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_
|
#ifndef CHAISCRIPT_REGISTER_FUNCTION_HPP_
|
||||||
#define CHAISCRIPT_REGISTER_FUNCTION_HPP_
|
#define CHAISCRIPT_REGISTER_FUNCTION_HPP_
|
||||||
|
|
||||||
@ -11,8 +15,6 @@
|
|||||||
|
|
||||||
#include "bind_first.hpp"
|
#include "bind_first.hpp"
|
||||||
#include "proxy_functions.hpp"
|
#include "proxy_functions.hpp"
|
||||||
#include "handle_return.hpp"
|
|
||||||
#include "function_call.hpp"
|
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
@ -37,396 +39,45 @@ namespace chaiscript
|
|||||||
/// \endcode
|
/// \endcode
|
||||||
///
|
///
|
||||||
/// \sa \ref adding_functions
|
/// \sa \ref adding_functions
|
||||||
//
|
|
||||||
//
|
|
||||||
template<typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function assignable_fun(
|
|
||||||
std::reference_wrapper<std::function<void (Param...)>> t_func,
|
|
||||||
std::shared_ptr<std::function<void (Param...)>> t_ptr,
|
|
||||||
std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func, t_ptr](){
|
|
||||||
class Func final : public dispatch::Assignable_Proxy_Function
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(std::reference_wrapper<std::function<void (Param...)>> t_f, std::shared_ptr<std::function<void (Param...)>> t_p)
|
|
||||||
: Assignable_Proxy_Function({user_type<void>(), user_type<Param>()...}),
|
|
||||||
m_f(std::move(t_f)), m_shared_ptr_holder(std::move(t_p))
|
|
||||||
{
|
|
||||||
assert(!m_shared_ptr_holder || m_shared_ptr_holder.get() == &m_f.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) override {
|
|
||||||
m_f.get() = dispatch::functor<void (Param...)>(t_rhs, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
m_f(boxed_cast<Param>(params.at(I), &t_conversions)...);
|
|
||||||
return dispatch::detail::Handle_Return<void>::handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::reference_wrapper<std::function<void (Param...)>> m_f;
|
|
||||||
std::shared_ptr<std::function<void (Param...)>> m_shared_ptr_holder;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func, t_ptr);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function assignable_fun(
|
|
||||||
std::reference_wrapper<std::function<Ret (Param...)>> t_func,
|
|
||||||
std::shared_ptr<std::function<Ret (Param...)>> t_ptr,
|
|
||||||
std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func, t_ptr](){
|
|
||||||
class Func final : public dispatch::Assignable_Proxy_Function
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(std::reference_wrapper<std::function<Ret (Param...)>> t_f, std::shared_ptr<std::function<Ret (Param...)>> t_p)
|
|
||||||
: Assignable_Proxy_Function({user_type<Ret>(), user_type<Param>()...}),
|
|
||||||
m_f(std::move(t_f)), m_shared_ptr_holder(std::move(t_p))
|
|
||||||
{
|
|
||||||
assert(!m_shared_ptr_holder || m_shared_ptr_holder.get() == &m_f.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
void assign(const std::shared_ptr<const Proxy_Function_Base> &t_rhs) override {
|
|
||||||
m_f.get() = dispatch::functor<Ret (Param...)>(t_rhs, nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return dispatch::detail::Handle_Return<Ret>::handle(m_f(boxed_cast<Param>(params.at(I), &t_conversions)...));
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::reference_wrapper<std::function<Ret (Param...)>> m_f;
|
|
||||||
std::shared_ptr<std::function<Ret (Param...)>> m_shared_ptr_holder;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func, t_ptr);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename ... Param>
|
|
||||||
Proxy_Function assignable_fun(
|
|
||||||
std::reference_wrapper<std::function<Ret (Param...)>> t_func,
|
|
||||||
std::shared_ptr<std::function<Ret (Param...)>> t_ptr
|
|
||||||
)
|
|
||||||
{
|
|
||||||
return assignable_fun(std::move(t_func), std::move(t_ptr), std::make_index_sequence<sizeof...(Param)>());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(const T &t_func, void (*)(Param...), std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(const T &func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<void>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
m_f(boxed_cast<Param>(params[I], &t_conversions)...);
|
|
||||||
return dispatch::detail::Handle_Return<void>::handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
T m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename T, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(const T &t_func, Ret (*)(Param...), std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(const T &func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<Ret>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return dispatch::detail::Handle_Return<Ret>::handle(m_f(boxed_cast<Param>(params.at(I), &t_conversions)...));
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
T m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Proxy_Function fun(const T &t)
|
Proxy_Function fun(const T &t)
|
||||||
{
|
{
|
||||||
typedef typename dispatch::detail::Callable_Traits<T>::Signature Signature;
|
typedef typename dispatch::detail::Callable_Traits<T>::Signature Signature;
|
||||||
Signature *f = nullptr;
|
|
||||||
return fun(t, f, std::make_index_sequence<dispatch::detail::Arity<Signature>::arity>());
|
return Proxy_Function(
|
||||||
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Signature, T>>(t));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ... Param, size_t ... I>
|
template<typename Ret, typename ... Param>
|
||||||
Proxy_Function fun(void (*t_func)(Param...), std::index_sequence<I...>)
|
Proxy_Function fun(Ret (*func)(Param...))
|
||||||
{
|
{
|
||||||
return [t_func](){
|
auto fun_call = dispatch::detail::Fun_Caller<Ret, Param...>(func);
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(decltype(t_func) func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<void>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
return Proxy_Function(
|
||||||
{
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Param...), decltype(fun_call)>>(fun_call));
|
||||||
return compare_types_with_cast_impl<Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
(*m_f)(boxed_cast<Param>(params[I], &t_conversions)...);
|
|
||||||
return dispatch::detail::Handle_Return<void>::handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
decltype(t_func) m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(Ret (*t_func)(Param...), std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(decltype(t_func) func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<Ret>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return dispatch::detail::Handle_Return<Ret>::handle((*m_f)(boxed_cast<Param>(params[I], &t_conversions)...));
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
decltype(t_func) m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Class, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(void (Class::*t_func)(Param...) const, std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(decltype(t_func) func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<void>(), user_type<const Class &>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<const Class &, Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
const Class &o = static_cast<const Class &>(boxed_cast<const Class &>(params[0], &t_conversions));
|
|
||||||
(o.*m_f)(boxed_cast<Param>(params[I+1], &t_conversions)...);
|
|
||||||
return dispatch::detail::Handle_Return<void>::handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
decltype(t_func) m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename Class, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(Ret (Class::*t_func)(Param...) const, std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(decltype(t_func) func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<Ret>(), user_type<const Class &>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<const Class &, Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
const Class &o = static_cast<const Class &>(boxed_cast<const Class &>(params[0], &t_conversions));
|
|
||||||
return dispatch::detail::Handle_Return<Ret>::handle((o.*m_f)(boxed_cast<Param>(params[I+1], &t_conversions)...));
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
decltype(t_func) m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Class, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(void (Class::*t_func)(Param...), std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(decltype(t_func) func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<void>(), user_type<Class &>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Class &, Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
Class &o = static_cast<Class &>(boxed_cast<Class &>(params[0], &t_conversions));
|
|
||||||
(o.*m_f)(boxed_cast<Param>(params[I+1], &t_conversions)...);
|
|
||||||
return dispatch::detail::Handle_Return<void>::handle();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
decltype(t_func) m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename Class, typename ... Param, size_t ... I>
|
|
||||||
Proxy_Function fun(Ret (Class::*t_func)(Param...), std::index_sequence<I...>)
|
|
||||||
{
|
|
||||||
return [t_func](){
|
|
||||||
class Func final : public dispatch::Proxy_Function_Impl_Base
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Func(decltype(t_func) func)
|
|
||||||
: dispatch::Proxy_Function_Impl_Base({user_type<Ret>(), user_type<Class &>(), user_type<Param>()...}),
|
|
||||||
m_f(func)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool compare_types_with_cast(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
return compare_types_with_cast_impl<Class &, Param...>(params, t_conversions);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Boxed_Value do_call(const std::vector<Boxed_Value> ¶ms, const Type_Conversions_State &t_conversions) const override
|
|
||||||
{
|
|
||||||
Class &o = static_cast<Class &>(boxed_cast<Class &>(params[0], &t_conversions));
|
|
||||||
return dispatch::detail::Handle_Return<Ret>::handle((o.*m_f)(boxed_cast<Param>(params[I+1], &t_conversions)...));
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
decltype(t_func) m_f;
|
|
||||||
};
|
|
||||||
|
|
||||||
return chaiscript::make_shared<dispatch::Proxy_Function_Base, Func>(t_func);
|
|
||||||
}();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Ret, typename Class, typename ... Param>
|
template<typename Ret, typename Class, typename ... Param>
|
||||||
Proxy_Function fun(Ret (Class::*t_func)(Param...) const)
|
Proxy_Function fun(Ret (Class::*t_func)(Param...) const)
|
||||||
{
|
{
|
||||||
return fun(t_func, std::make_index_sequence<sizeof...(Param)>());
|
auto call = dispatch::detail::Const_Caller<Ret, Class, Param...>(t_func);
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Ret, typename ... Param>
|
return Proxy_Function(
|
||||||
Proxy_Function fun(Ret (*func)(Param...))
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (const Class &, Param...), decltype(call)>>(call));
|
||||||
{
|
|
||||||
return fun(func, std::make_index_sequence<sizeof...(Param)>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename Ret, typename Class, typename ... Param>
|
template<typename Ret, typename Class, typename ... Param>
|
||||||
Proxy_Function fun(Ret (Class::*t_func)(Param...))
|
Proxy_Function fun(Ret (Class::*t_func)(Param...))
|
||||||
{
|
{
|
||||||
return fun(t_func, std::make_index_sequence<sizeof...(Param)>());
|
auto call = dispatch::detail::Caller<Ret, Class, Param...>(t_func);
|
||||||
|
|
||||||
|
return Proxy_Function(
|
||||||
|
chaiscript::make_shared<dispatch::Proxy_Function_Base, dispatch::Proxy_Function_Callable_Impl<Ret (Class &, Param...), decltype(call)>>(call));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<typename T, typename Class /*, typename = typename std::enable_if<std::is_member_object_pointer<T>::value>::type*/>
|
template<typename T, typename Class /*, typename = typename std::enable_if<std::is_member_object_pointer<T>::value>::type*/>
|
||||||
Proxy_Function fun(T Class::* m /*, typename std::enable_if<std::is_member_object_pointer<T>::value>::type* = 0*/ )
|
Proxy_Function fun(T Class::* m /*, typename std::enable_if<std::is_member_object_pointer<T>::value>::type* = 0*/ )
|
||||||
{
|
{
|
||||||
@ -460,31 +111,6 @@ namespace chaiscript
|
|||||||
return fun(detail::bind_first(std::forward<T>(t), q));
|
return fun(detail::bind_first(std::forward<T>(t), q));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Creates a new Proxy_Function object from a free function or member function and binds the first and second parameters of it
|
|
||||||
/// \param[in] t Function / member to expose
|
|
||||||
/// \param[in] q Value to bind to first parameter
|
|
||||||
/// \param[in] r Value to bind to second parameter
|
|
||||||
///
|
|
||||||
/// \b Example:
|
|
||||||
/// \code
|
|
||||||
/// struct MyClass
|
|
||||||
/// {
|
|
||||||
/// void memberfunction(int);
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// MyClass obj;
|
|
||||||
/// chaiscript::ChaiScript chai;
|
|
||||||
/// // Add function taking only no arguments, and permanently bound to "obj" and "1"
|
|
||||||
/// // memberfunction() will be equivalent to obj.memberfunction(1)
|
|
||||||
/// chai.add(fun(&MyClass::memberfunction, std::ref(obj), 1), "memberfunction");
|
|
||||||
/// \endcode
|
|
||||||
///
|
|
||||||
/// \sa \ref adding_functions
|
|
||||||
template<typename T, typename Q, typename R>
|
|
||||||
Proxy_Function fun(T &&t, Q &&q, R &&r)
|
|
||||||
{
|
|
||||||
return fun(detail::bind_first(detail::bind_first(std::forward<T>(t), std::forward<Q>(q)), std::forward<R>(r)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
159
include/chaiscript/dispatchkit/short_alloc.hpp
Normal file
159
include/chaiscript/dispatchkit/short_alloc.hpp
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
#ifndef SHORT_ALLOC_H
|
||||||
|
#define SHORT_ALLOC_H
|
||||||
|
|
||||||
|
// The MIT License (MIT)
|
||||||
|
//
|
||||||
|
// Copyright (c) 2015 Howard Hinnant
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
template <std::size_t N, std::size_t alignment = alignof(std::max_align_t)>
|
||||||
|
class arena
|
||||||
|
{
|
||||||
|
alignas(alignment) char buf_[N];
|
||||||
|
char* ptr_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
~arena() {ptr_ = nullptr;}
|
||||||
|
arena() noexcept : ptr_(buf_) {}
|
||||||
|
arena(const arena&) = delete;
|
||||||
|
arena& operator=(const arena&) = delete;
|
||||||
|
|
||||||
|
template <std::size_t ReqAlign> char* allocate(std::size_t n);
|
||||||
|
void deallocate(char* p, std::size_t n) noexcept;
|
||||||
|
|
||||||
|
static constexpr std::size_t size() noexcept {return N;}
|
||||||
|
std::size_t used() const noexcept {return static_cast<std::size_t>(ptr_ - buf_);}
|
||||||
|
void reset() noexcept {ptr_ = buf_;}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static
|
||||||
|
std::size_t
|
||||||
|
align_up(std::size_t n) noexcept
|
||||||
|
{return (n + (alignment-1)) & ~(alignment-1);}
|
||||||
|
|
||||||
|
bool
|
||||||
|
pointer_in_buffer(char* p) noexcept
|
||||||
|
{return buf_ <= p && p <= buf_ + N;}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t N, std::size_t alignment>
|
||||||
|
template <std::size_t ReqAlign>
|
||||||
|
char*
|
||||||
|
arena<N, alignment>::allocate(std::size_t n)
|
||||||
|
{
|
||||||
|
static_assert(ReqAlign <= alignment, "alignment is too small for this arena");
|
||||||
|
assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
|
||||||
|
auto const aligned_n = align_up(n);
|
||||||
|
if (static_cast<decltype(aligned_n)>(buf_ + N - ptr_) >= aligned_n)
|
||||||
|
{
|
||||||
|
char* r = ptr_;
|
||||||
|
ptr_ += aligned_n;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
static_assert(alignment <= alignof(std::max_align_t), "you've chosen an "
|
||||||
|
"alignment that is larger than alignof(std::max_align_t), and "
|
||||||
|
"cannot be guaranteed by normal operator new");
|
||||||
|
return static_cast<char*>(::operator new(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t N, std::size_t alignment>
|
||||||
|
void
|
||||||
|
arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
|
||||||
|
if (pointer_in_buffer(p))
|
||||||
|
{
|
||||||
|
n = align_up(n);
|
||||||
|
if (p + n == ptr_) {
|
||||||
|
ptr_ = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
::operator delete(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, std::size_t N, std::size_t Align = alignof(std::max_align_t)>
|
||||||
|
class short_alloc
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
static auto constexpr alignment = Align;
|
||||||
|
static auto constexpr size = N;
|
||||||
|
using arena_type = arena<size, alignment>;
|
||||||
|
|
||||||
|
private:
|
||||||
|
arena_type& a_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
short_alloc(const short_alloc&) = default;
|
||||||
|
short_alloc& operator=(const short_alloc&) = delete;
|
||||||
|
|
||||||
|
explicit short_alloc(arena_type& a) noexcept : a_(a)
|
||||||
|
{
|
||||||
|
static_assert(size % alignment == 0,
|
||||||
|
"size N needs to be a multiple of alignment Align");
|
||||||
|
}
|
||||||
|
template <class U>
|
||||||
|
explicit short_alloc(const short_alloc<U, N, alignment>& a) noexcept
|
||||||
|
: a_(a.a_) {}
|
||||||
|
|
||||||
|
template <class _Up> struct rebind {using other = short_alloc<_Up, N, alignment>;};
|
||||||
|
|
||||||
|
T* allocate(std::size_t n)
|
||||||
|
{
|
||||||
|
return reinterpret_cast<T*>(a_.template allocate<alignof(T)>(n*sizeof(T)));
|
||||||
|
}
|
||||||
|
void deallocate(T* p, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T1, std::size_t N1, std::size_t A1,
|
||||||
|
class U, std::size_t M, std::size_t A2>
|
||||||
|
friend
|
||||||
|
bool
|
||||||
|
operator==(const short_alloc<T1, N1, A1>& x, const short_alloc<U, M, A2>& y) noexcept;
|
||||||
|
|
||||||
|
template <class U, std::size_t M, std::size_t A> friend class short_alloc;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
|
||||||
|
inline
|
||||||
|
bool
|
||||||
|
operator==(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
|
||||||
|
{
|
||||||
|
return N == M && A1 == A2 && &x.a_ == &y.a_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
|
||||||
|
inline
|
||||||
|
bool
|
||||||
|
operator!=(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
|
||||||
|
{
|
||||||
|
return !(x == y);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SHORT_ALLOC_HPP
|
||||||
|
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
|
#ifndef CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
|
||||||
#define CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
|
#define CHAISCRIPT_DYNAMIC_CAST_CONVERSION_HPP_
|
||||||
|
|
||||||
@ -39,14 +43,14 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_boxed_dynamic_cast(const std::string &w) noexcept
|
explicit bad_boxed_dynamic_cast(const std::string &w) noexcept
|
||||||
: bad_boxed_cast(w)
|
: bad_boxed_cast(w)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_boxed_dynamic_cast(const bad_boxed_dynamic_cast &) = default;
|
bad_boxed_dynamic_cast(const bad_boxed_dynamic_cast &) = default;
|
||||||
|
|
||||||
virtual ~bad_boxed_dynamic_cast() noexcept = default;
|
~bad_boxed_dynamic_cast() noexcept override = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
class bad_boxed_type_cast : public bad_boxed_cast
|
class bad_boxed_type_cast : public bad_boxed_cast
|
||||||
@ -63,14 +67,14 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_boxed_type_cast(const std::string &w) noexcept
|
explicit bad_boxed_type_cast(const std::string &w) noexcept
|
||||||
: bad_boxed_cast(w)
|
: bad_boxed_cast(w)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
bad_boxed_type_cast(const bad_boxed_type_cast &) = default;
|
bad_boxed_type_cast(const bad_boxed_type_cast &) = default;
|
||||||
|
|
||||||
virtual ~bad_boxed_type_cast() noexcept = default;
|
~bad_boxed_type_cast() noexcept override = default;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +104,8 @@ namespace chaiscript
|
|||||||
virtual ~Type_Conversion_Base() = default;
|
virtual ~Type_Conversion_Base() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Type_Conversion_Base(const Type_Info &t_to, const Type_Info &t_from)
|
Type_Conversion_Base(Type_Info t_to, Type_Info t_from)
|
||||||
: m_to(t_to), m_from(t_from)
|
: m_to(std::move(t_to)), m_from(std::move(t_from))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,7 +290,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Type_Conversion_Impl(Type_Info t_from, Type_Info t_to, Callable t_func)
|
Type_Conversion_Impl(Type_Info t_from, Type_Info t_to, Callable t_func)
|
||||||
: Type_Conversion_Base(std::move(t_to), std::move(t_from)),
|
: Type_Conversion_Base(t_to, t_from),
|
||||||
m_func(std::move(t_func))
|
m_func(std::move(t_func))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -302,7 +306,7 @@ namespace chaiscript
|
|||||||
return m_func(t_from);
|
return m_func(t_from);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool bidir() const override
|
bool bidir() const override
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -334,21 +338,15 @@ namespace chaiscript
|
|||||||
: m_mutex(),
|
: m_mutex(),
|
||||||
m_conversions(),
|
m_conversions(),
|
||||||
m_convertableTypes(),
|
m_convertableTypes(),
|
||||||
m_num_types(0),
|
m_num_types(0)
|
||||||
m_thread_cache(this),
|
|
||||||
m_conversion_saves(this)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
Type_Conversions(const Type_Conversions &t_other)
|
Type_Conversions(const Type_Conversions &t_other) = delete;
|
||||||
: m_mutex(),
|
Type_Conversions(Type_Conversions &&) = default;
|
||||||
m_conversions(t_other.get_conversions()),
|
|
||||||
m_convertableTypes(t_other.m_convertableTypes),
|
Type_Conversions &operator=(const Type_Conversions &) = delete;
|
||||||
m_num_types(m_conversions.size()),
|
Type_Conversions &operator=(Type_Conversions &&) = default;
|
||||||
m_thread_cache(this),
|
|
||||||
m_conversion_saves(this)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::set<const std::type_info *, Less_Than> &thread_cache() const
|
const std::set<const std::type_info *, Less_Than> &thread_cache() const
|
||||||
{
|
{
|
||||||
@ -397,28 +395,39 @@ namespace chaiscript
|
|||||||
template<typename To>
|
template<typename To>
|
||||||
Boxed_Value boxed_type_conversion(Conversion_Saves &t_saves, const Boxed_Value &from) const
|
Boxed_Value boxed_type_conversion(Conversion_Saves &t_saves, const Boxed_Value &from) const
|
||||||
{
|
{
|
||||||
try {
|
return boxed_type_conversion(user_type<To>(), t_saves, from);
|
||||||
Boxed_Value ret = get_conversion(user_type<To>(), from.get_type_info())->convert(from);
|
|
||||||
if (t_saves.enabled) t_saves.saves.push_back(ret);
|
|
||||||
return ret;
|
|
||||||
} catch (const std::out_of_range &) {
|
|
||||||
throw exception::bad_boxed_dynamic_cast(from.get_type_info(), typeid(To), "No known conversion");
|
|
||||||
} catch (const std::bad_cast &) {
|
|
||||||
throw exception::bad_boxed_dynamic_cast(from.get_type_info(), typeid(To), "Unable to perform dynamic_cast operation");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename From>
|
template<typename From>
|
||||||
Boxed_Value boxed_type_down_conversion(Conversion_Saves &t_saves, const Boxed_Value &to) const
|
Boxed_Value boxed_type_down_conversion(Conversion_Saves &t_saves, const Boxed_Value &to) const
|
||||||
|
{
|
||||||
|
return boxed_type_down_conversion(user_type<From>(), t_saves, to);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Boxed_Value boxed_type_conversion(const Type_Info &to, Conversion_Saves &t_saves, const Boxed_Value &from) const
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Boxed_Value ret = get_conversion(to.get_type_info(), user_type<From>())->convert_down(to);
|
Boxed_Value ret = get_conversion(to, from.get_type_info())->convert(from);
|
||||||
if (t_saves.enabled) t_saves.saves.push_back(ret);
|
if (t_saves.enabled) { t_saves.saves.push_back(ret); }
|
||||||
return ret;
|
return ret;
|
||||||
} catch (const std::out_of_range &) {
|
} catch (const std::out_of_range &) {
|
||||||
throw exception::bad_boxed_dynamic_cast(to.get_type_info(), typeid(From), "No known conversion");
|
throw exception::bad_boxed_dynamic_cast(from.get_type_info(), *to.bare_type_info(), "No known conversion");
|
||||||
} catch (const std::bad_cast &) {
|
} catch (const std::bad_cast &) {
|
||||||
throw exception::bad_boxed_dynamic_cast(to.get_type_info(), typeid(From), "Unable to perform dynamic_cast operation");
|
throw exception::bad_boxed_dynamic_cast(from.get_type_info(), *to.bare_type_info(), "Unable to perform dynamic_cast operation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Boxed_Value boxed_type_down_conversion(const Type_Info &from, Conversion_Saves &t_saves, const Boxed_Value &to) const
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Boxed_Value ret = get_conversion(to.get_type_info(), from)->convert_down(to);
|
||||||
|
if (t_saves.enabled) { t_saves.saves.push_back(ret); }
|
||||||
|
return ret;
|
||||||
|
} catch (const std::out_of_range &) {
|
||||||
|
throw exception::bad_boxed_dynamic_cast(to.get_type_info(), *from.bare_type_info(), "No known conversion");
|
||||||
|
} catch (const std::bad_cast &) {
|
||||||
|
throw exception::bad_boxed_dynamic_cast(to.get_type_info(), *from.bare_type_info(), "Unable to perform dynamic_cast operation");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_TYPE_INFO_HPP_
|
#ifndef CHAISCRIPT_TYPE_INFO_HPP_
|
||||||
#define CHAISCRIPT_TYPE_INFO_HPP_
|
#define CHAISCRIPT_TYPE_INFO_HPP_
|
||||||
|
|
||||||
@ -29,8 +33,8 @@ namespace chaiscript
|
|||||||
class Type_Info
|
class Type_Info
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
constexpr Type_Info(bool t_is_const, bool t_is_reference, bool t_is_pointer, bool t_is_void,
|
constexpr Type_Info(const bool t_is_const, const bool t_is_reference, const bool t_is_pointer, const bool t_is_void,
|
||||||
bool t_is_arithmetic, const std::type_info *t_ti, const std::type_info *t_bare_ti)
|
const bool t_is_arithmetic, const std::type_info *t_ti, const std::type_info *t_bare_ti)
|
||||||
: m_type_info(t_ti), m_bare_type_info(t_bare_ti),
|
: m_type_info(t_ti), m_bare_type_info(t_bare_ti),
|
||||||
m_flags((static_cast<unsigned int>(t_is_const) << is_const_flag)
|
m_flags((static_cast<unsigned int>(t_is_const) << is_const_flag)
|
||||||
+ (static_cast<unsigned int>(t_is_reference) << is_reference_flag)
|
+ (static_cast<unsigned int>(t_is_reference) << is_reference_flag)
|
||||||
@ -47,17 +51,22 @@ namespace chaiscript
|
|||||||
return m_type_info < ti.m_type_info;
|
return m_type_info < ti.m_type_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(const Type_Info &ti) const noexcept
|
||||||
|
{
|
||||||
|
return !(operator==(ti));
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(const std::type_info &ti) const noexcept
|
||||||
|
{
|
||||||
|
return !(operator==(ti));
|
||||||
|
}
|
||||||
|
|
||||||
constexpr bool operator==(const Type_Info &ti) const noexcept
|
constexpr bool operator==(const Type_Info &ti) const noexcept
|
||||||
{
|
{
|
||||||
return ti.m_type_info == m_type_info
|
return ti.m_type_info == m_type_info
|
||||||
|| *ti.m_type_info == *m_type_info;
|
|| *ti.m_type_info == *m_type_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool operator!=(const Type_Info &ti) const noexcept
|
|
||||||
{
|
|
||||||
return !(*this == ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr bool operator==(const std::type_info &ti) const noexcept
|
constexpr bool operator==(const std::type_info &ti) const noexcept
|
||||||
{
|
{
|
||||||
return !is_undef() && (*m_type_info) == ti;
|
return !is_undef() && (*m_type_info) == ti;
|
||||||
@ -126,9 +135,7 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Get_Type_Info
|
struct Get_Type_Info
|
||||||
{
|
{
|
||||||
typedef T type;
|
static constexpr Type_Info get()
|
||||||
|
|
||||||
static Type_Info get()
|
|
||||||
{
|
{
|
||||||
return Type_Info(std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value,
|
return Type_Info(std::is_const<typename std::remove_pointer<typename std::remove_reference<T>::type>::type>::value,
|
||||||
std::is_reference<T>::value, std::is_pointer<T>::value,
|
std::is_reference<T>::value, std::is_pointer<T>::value,
|
||||||
@ -143,9 +150,9 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Get_Type_Info<std::shared_ptr<T> >
|
struct Get_Type_Info<std::shared_ptr<T> >
|
||||||
{
|
{
|
||||||
typedef T type;
|
// typedef T type;
|
||||||
|
|
||||||
static Type_Info get()
|
static constexpr Type_Info get()
|
||||||
{
|
{
|
||||||
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
||||||
std::is_void<T>::value,
|
std::is_void<T>::value,
|
||||||
@ -163,9 +170,7 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Get_Type_Info<const std::shared_ptr<T> &>
|
struct Get_Type_Info<const std::shared_ptr<T> &>
|
||||||
{
|
{
|
||||||
typedef T type;
|
static constexpr Type_Info get()
|
||||||
|
|
||||||
static Type_Info get()
|
|
||||||
{
|
{
|
||||||
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
||||||
std::is_void<T>::value,
|
std::is_void<T>::value,
|
||||||
@ -178,9 +183,7 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Get_Type_Info<std::reference_wrapper<T> >
|
struct Get_Type_Info<std::reference_wrapper<T> >
|
||||||
{
|
{
|
||||||
typedef T type;
|
static constexpr Type_Info get()
|
||||||
|
|
||||||
static Type_Info get()
|
|
||||||
{
|
{
|
||||||
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
||||||
std::is_void<T>::value,
|
std::is_void<T>::value,
|
||||||
@ -193,9 +196,7 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct Get_Type_Info<const std::reference_wrapper<T> &>
|
struct Get_Type_Info<const std::reference_wrapper<T> &>
|
||||||
{
|
{
|
||||||
typedef T type;
|
static constexpr Type_Info get()
|
||||||
|
|
||||||
static Type_Info get()
|
|
||||||
{
|
{
|
||||||
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
return Type_Info(std::is_const<T>::value, std::is_reference<T>::value, std::is_pointer<T>::value,
|
||||||
std::is_void<T>::value,
|
std::is_void<T>::value,
|
||||||
@ -217,7 +218,7 @@ namespace chaiscript
|
|||||||
/// chaiscript::Type_Info ti = chaiscript::user_type(i);
|
/// chaiscript::Type_Info ti = chaiscript::user_type(i);
|
||||||
/// \endcode
|
/// \endcode
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Type_Info user_type(const T &/*t*/)
|
constexpr Type_Info user_type(const T &/*t*/)
|
||||||
{
|
{
|
||||||
return detail::Get_Type_Info<T>::get();
|
return detail::Get_Type_Info<T>::get();
|
||||||
}
|
}
|
||||||
@ -232,7 +233,7 @@ namespace chaiscript
|
|||||||
/// chaiscript::Type_Info ti = chaiscript::user_type<int>();
|
/// chaiscript::Type_Info ti = chaiscript::user_type<int>();
|
||||||
/// \endcode
|
/// \endcode
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Type_Info user_type()
|
constexpr Type_Info user_type()
|
||||||
{
|
{
|
||||||
return detail::Get_Type_Info<T>::get();
|
return detail::Get_Type_Info<T>::get();
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,25 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_ALGEBRAIC_HPP_
|
#ifndef CHAISCRIPT_ALGEBRAIC_HPP_
|
||||||
#define CHAISCRIPT_ALGEBRAIC_HPP_
|
#define CHAISCRIPT_ALGEBRAIC_HPP_
|
||||||
|
|
||||||
|
#include "../utility/fnv1a.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
|
|
||||||
struct Operators {
|
struct Operators {
|
||||||
enum Opers
|
enum class Opers
|
||||||
{
|
{
|
||||||
boolean_flag,
|
boolean_flag,
|
||||||
equals, less_than, greater_than, less_than_equal, greater_than_equal, not_equal,
|
equals, less_than, greater_than, less_than_equal, greater_than_equal, not_equal,
|
||||||
@ -31,7 +37,7 @@ namespace chaiscript
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const char *to_string(Opers t_oper) {
|
static const char *to_string(Opers t_oper) {
|
||||||
const char *opers[] = {
|
static const char *opers[] = {
|
||||||
"",
|
"",
|
||||||
"==", "<", ">", "<=", ">=", "!=",
|
"==", "<", ">", "<=", ">=", "!=",
|
||||||
"",
|
"",
|
||||||
@ -46,81 +52,53 @@ namespace chaiscript
|
|||||||
"+", "/", "*", "-", "+", "-",
|
"+", "/", "*", "-", "+", "-",
|
||||||
""
|
""
|
||||||
};
|
};
|
||||||
return opers[t_oper];
|
return opers[static_cast<int>(t_oper)];
|
||||||
}
|
}
|
||||||
|
|
||||||
static Opers to_operator(const std::string &t_str, bool t_is_unary = false)
|
static Opers to_operator(const std::string &t_str, bool t_is_unary = false)
|
||||||
{
|
{
|
||||||
if (t_str == "==")
|
#ifdef CHAISCRIPT_MSVC
|
||||||
{
|
#pragma warning(push)
|
||||||
return equals;
|
#pragma warning(disable : 4307)
|
||||||
} else if (t_str == "<") {
|
#endif
|
||||||
return less_than;
|
|
||||||
} else if (t_str == ">") {
|
const auto op_hash = utility::fnv1a_32(t_str.c_str());
|
||||||
return greater_than;
|
switch (op_hash) {
|
||||||
} else if (t_str == "<=") {
|
case utility::fnv1a_32("=="): { return Opers::equals; }
|
||||||
return less_than_equal;
|
case utility::fnv1a_32("<"): { return Opers::less_than; }
|
||||||
} else if (t_str == ">=") {
|
case utility::fnv1a_32(">"): { return Opers::greater_than; }
|
||||||
return greater_than_equal;
|
case utility::fnv1a_32("<="): { return Opers::less_than_equal; }
|
||||||
} else if (t_str == "!=") {
|
case utility::fnv1a_32(">="): { return Opers::greater_than_equal; }
|
||||||
return not_equal;
|
case utility::fnv1a_32("!="): { return Opers::not_equal; }
|
||||||
} else if (t_str == "=") {
|
case utility::fnv1a_32("="): { return Opers::assign; }
|
||||||
return assign;
|
case utility::fnv1a_32("++"): { return Opers::pre_increment; }
|
||||||
} else if (t_str == "++") {
|
case utility::fnv1a_32("--"): { return Opers::pre_decrement; }
|
||||||
return pre_increment;
|
case utility::fnv1a_32("*="): { return Opers::assign_product; }
|
||||||
} else if (t_str == "--") {
|
case utility::fnv1a_32("+="): { return Opers::assign_sum; }
|
||||||
return pre_decrement;
|
case utility::fnv1a_32("-="): { return Opers::assign_difference; }
|
||||||
} else if (t_str == "*=") {
|
case utility::fnv1a_32("&="): { return Opers::assign_bitwise_and; }
|
||||||
return assign_product;
|
case utility::fnv1a_32("|="): { return Opers::assign_bitwise_or; }
|
||||||
} else if (t_str == "+=") {
|
case utility::fnv1a_32("<<="): { return Opers::assign_shift_left; }
|
||||||
return assign_sum;
|
case utility::fnv1a_32(">>="): { return Opers::assign_shift_right; }
|
||||||
} else if (t_str == "-=") {
|
case utility::fnv1a_32("%="): { return Opers::assign_remainder; }
|
||||||
return assign_difference;
|
case utility::fnv1a_32("^="): { return Opers::assign_bitwise_xor; }
|
||||||
} else if (t_str == "&=") {
|
case utility::fnv1a_32("<<"): { return Opers::shift_left; }
|
||||||
return assign_bitwise_and;
|
case utility::fnv1a_32(">>"): { return Opers::shift_right; }
|
||||||
} else if (t_str == "|=") {
|
case utility::fnv1a_32("%"): { return Opers::remainder; }
|
||||||
return assign_bitwise_or;
|
case utility::fnv1a_32("&"): { return Opers::bitwise_and; }
|
||||||
} else if (t_str == "<<=") {
|
case utility::fnv1a_32("|"): { return Opers::bitwise_or; }
|
||||||
return assign_shift_left;
|
case utility::fnv1a_32("^"): { return Opers::bitwise_xor; }
|
||||||
} else if (t_str == ">>=") {
|
case utility::fnv1a_32("~"): { return Opers::bitwise_complement; }
|
||||||
return assign_shift_right;
|
case utility::fnv1a_32("+"): { return t_is_unary ? Opers::unary_plus : Opers::sum; }
|
||||||
} else if (t_str == "%=") {
|
case utility::fnv1a_32("-"): { return t_is_unary ? Opers::unary_minus : Opers::difference; }
|
||||||
return assign_remainder;
|
case utility::fnv1a_32("/"): { return Opers::quotient; }
|
||||||
} else if (t_str == "^=") {
|
case utility::fnv1a_32("*"): { return Opers::product; }
|
||||||
return assign_bitwise_xor;
|
default: { return Opers::invalid; }
|
||||||
} else if (t_str == "<<") {
|
|
||||||
return shift_left;
|
|
||||||
} else if (t_str == ">>") {
|
|
||||||
return shift_right;
|
|
||||||
} else if (t_str == "%") {
|
|
||||||
return remainder;
|
|
||||||
} else if (t_str == "&") {
|
|
||||||
return bitwise_and;
|
|
||||||
} else if (t_str == "|") {
|
|
||||||
return bitwise_or;
|
|
||||||
} else if (t_str == "^") {
|
|
||||||
return bitwise_xor;
|
|
||||||
} else if (t_str == "~") {
|
|
||||||
return bitwise_complement;
|
|
||||||
} else if (t_str == "+") {
|
|
||||||
if (t_is_unary) {
|
|
||||||
return unary_plus;
|
|
||||||
} else {
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
} else if (t_str == "-") {
|
|
||||||
if (t_is_unary) {
|
|
||||||
return unary_minus;
|
|
||||||
} else {
|
|
||||||
return difference;
|
|
||||||
}
|
|
||||||
} else if (t_str == "/") {
|
|
||||||
return quotient;
|
|
||||||
} else if (t_str == "*") {
|
|
||||||
return product;
|
|
||||||
} else {
|
|
||||||
return invalid;
|
|
||||||
}
|
}
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_COMMON_HPP_
|
#ifndef CHAISCRIPT_COMMON_HPP_
|
||||||
#define CHAISCRIPT_COMMON_HPP_
|
#define CHAISCRIPT_COMMON_HPP_
|
||||||
|
|
||||||
@ -26,34 +30,60 @@ struct AST_Node;
|
|||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
|
struct Name_Validator {
|
||||||
|
static bool is_reserved_word(const std::string &name)
|
||||||
|
{
|
||||||
|
static const std::set<std::string> m_reserved_words
|
||||||
|
= {"def", "fun", "while", "for", "if", "else", "&&", "||", ",", "auto",
|
||||||
|
"return", "break", "true", "false", "class", "attr", "var", "global", "GLOBAL", "_",
|
||||||
|
"__LINE__", "__FILE__", "__FUNC__", "__CLASS__"};
|
||||||
|
return m_reserved_words.count(name) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool valid_object_name(const std::string &name)
|
||||||
|
{
|
||||||
|
return name.find("::") == std::string::npos && !is_reserved_word(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void validate_object_name(const std::string &name)
|
||||||
|
{
|
||||||
|
if (is_reserved_word(name)) {
|
||||||
|
throw exception::reserved_word_error(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name.find("::") != std::string::npos) {
|
||||||
|
throw exception::illegal_name_error(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/// Signature of module entry point that all binary loadable modules must implement.
|
/// Signature of module entry point that all binary loadable modules must implement.
|
||||||
typedef ModulePtr (*Create_Module_Func)();
|
typedef ModulePtr (*Create_Module_Func)();
|
||||||
|
|
||||||
|
|
||||||
/// Types of AST nodes available to the parser and eval
|
/// Types of AST nodes available to the parser and eval
|
||||||
class AST_Node_Type {
|
enum class AST_Node_Type { Id, Fun_Call, Unused_Return_Fun_Call, Arg_List, Equation, Var_Decl,
|
||||||
public:
|
Array_Call, Dot_Access,
|
||||||
enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Arg_List, Variable, Equation, Var_Decl,
|
Lambda, Block, Scopeless_Block, Def, While, If, For, Ranged_For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range,
|
||||||
Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
|
Inline_Range, Try, Catch, Finally, Method, Attr_Decl,
|
||||||
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range,
|
Logical_And, Logical_Or, Reference, Switch, Case, Default, Noop, Class, Binary, Arg, Global_Decl, Constant, Compiled
|
||||||
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, Binary, Arg, Global_Decl
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class Operator_Precidence { Ternary_Cond, Logical_Or,
|
||||||
|
Logical_And, Bitwise_Or, Bitwise_Xor, Bitwise_And,
|
||||||
|
Equality, Comparison, Shift, Addition, Multiplication, Prefix };
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
/// Helper lookup to get the name of each node type
|
/// Helper lookup to get the name of each node type
|
||||||
const char *ast_node_type_to_string(int ast_node_type) {
|
inline const char *ast_node_type_to_string(AST_Node_Type ast_node_type) {
|
||||||
const char *ast_node_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl",
|
static const char * const ast_node_types[] = { "Id", "Fun_Call", "Unused_Return_Fun_Call", "Arg_List", "Equation", "Var_Decl",
|
||||||
"Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String",
|
"Array_Call", "Dot_Access",
|
||||||
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range",
|
"Lambda", "Block", "Scopeless_Block", "Def", "While", "If", "For", "Ranged_For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range",
|
||||||
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or",
|
"Inline_Range", "Try", "Catch", "Finally", "Method", "Attr_Decl",
|
||||||
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary", "Arg"};
|
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Noop", "Class", "Binary", "Arg", "Global_Decl", "Constant", "Compiled"};
|
||||||
|
|
||||||
return ast_node_types[ast_node_type];
|
return ast_node_types[static_cast<int>(ast_node_type)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,6 +131,36 @@ namespace chaiscript
|
|||||||
/// \brief Classes which may be thrown during error cases when ChaiScript is executing.
|
/// \brief Classes which may be thrown during error cases when ChaiScript is executing.
|
||||||
namespace exception
|
namespace exception
|
||||||
{
|
{
|
||||||
|
/// \brief Thrown if an error occurs while attempting to load a binary module
|
||||||
|
struct load_module_error : std::runtime_error
|
||||||
|
{
|
||||||
|
explicit load_module_error(const std::string &t_reason) noexcept
|
||||||
|
: std::runtime_error(t_reason)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
load_module_error(const std::string &t_name, const std::vector<load_module_error> &t_errors)
|
||||||
|
: std::runtime_error(format_error(t_name, t_errors))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
load_module_error(const load_module_error &) = default;
|
||||||
|
~load_module_error() noexcept override = default;
|
||||||
|
|
||||||
|
static std::string format_error(const std::string &t_name, const std::vector<load_module_error> &t_errors)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "Error loading module '" << t_name << "'\n"
|
||||||
|
<< " The following locations were searched:\n";
|
||||||
|
|
||||||
|
for (const auto &err : t_errors) {
|
||||||
|
ss << " " << err.what() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Errors generated during parsing or evaluation
|
/// Errors generated during parsing or evaluation
|
||||||
struct eval_error : std::runtime_error {
|
struct eval_error : std::runtime_error {
|
||||||
@ -132,7 +192,7 @@ namespace chaiscript
|
|||||||
reason(t_why), start_position(t_where), filename(t_fname)
|
reason(t_why), start_position(t_where), filename(t_fname)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
eval_error(const std::string &t_why) noexcept
|
explicit eval_error(const std::string &t_why) noexcept
|
||||||
: std::runtime_error("Error: \"" + t_why + "\" "),
|
: std::runtime_error("Error: \"" + t_why + "\" "),
|
||||||
reason(t_why)
|
reason(t_why)
|
||||||
{}
|
{}
|
||||||
@ -144,7 +204,7 @@ namespace chaiscript
|
|||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
|
|
||||||
ss << what();
|
ss << what();
|
||||||
if (call_stack.size() > 0) {
|
if (!call_stack.empty()) {
|
||||||
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")\n";
|
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")\n";
|
||||||
ss << '\n' << detail << '\n';
|
ss << '\n' << detail << '\n';
|
||||||
ss << " " << fname(call_stack[0]) << " (" << startpos(call_stack[0]) << ") '" << pretty(call_stack[0]) << "'";
|
ss << " " << fname(call_stack[0]) << " (" << startpos(call_stack[0]) << ") '" << pretty(call_stack[0]) << "'";
|
||||||
@ -161,12 +221,12 @@ namespace chaiscript
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~eval_error() noexcept {}
|
~eval_error() noexcept override = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
static int id(const T& t)
|
static AST_Node_Type id(const T& t)
|
||||||
{
|
{
|
||||||
return t->identifier;
|
return t->identifier;
|
||||||
}
|
}
|
||||||
@ -420,12 +480,12 @@ namespace chaiscript
|
|||||||
|
|
||||||
/// Errors generated when loading a file
|
/// Errors generated when loading a file
|
||||||
struct file_not_found_error : std::runtime_error {
|
struct file_not_found_error : std::runtime_error {
|
||||||
file_not_found_error(const std::string &t_filename) noexcept
|
explicit file_not_found_error(const std::string &t_filename) noexcept
|
||||||
: std::runtime_error("File Not Found: " + t_filename)
|
: std::runtime_error("File Not Found: " + t_filename)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
file_not_found_error(const file_not_found_error &) = default;
|
file_not_found_error(const file_not_found_error &) = default;
|
||||||
virtual ~file_not_found_error() noexcept {}
|
~file_not_found_error() noexcept override = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -434,11 +494,9 @@ namespace chaiscript
|
|||||||
/// \brief Struct that doubles as both a parser ast_node and an AST node.
|
/// \brief Struct that doubles as both a parser ast_node and an AST node.
|
||||||
struct AST_Node : std::enable_shared_from_this<AST_Node> {
|
struct AST_Node : std::enable_shared_from_this<AST_Node> {
|
||||||
public:
|
public:
|
||||||
const int identifier; //< \todo shouldn't this be a strongly typed enum value?
|
const AST_Node_Type identifier;
|
||||||
const std::string text;
|
const std::string text;
|
||||||
Parse_Location location;
|
Parse_Location location;
|
||||||
std::vector<AST_NodePtr> children;
|
|
||||||
AST_NodePtr annotation;
|
|
||||||
|
|
||||||
const std::string &filename() const {
|
const std::string &filename() const {
|
||||||
return *location.filename;
|
return *location.filename;
|
||||||
@ -452,19 +510,22 @@ namespace chaiscript
|
|||||||
return location.end;
|
return location.end;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string pretty_print() const
|
std::string pretty_print() const
|
||||||
{
|
{
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
|
||||||
oss << text;
|
oss << text;
|
||||||
|
|
||||||
for (auto & elem : this->children) {
|
for (auto & elem : this->get_children()) {
|
||||||
oss << elem->pretty_print();
|
oss << elem->pretty_print() << ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual std::vector<AST_NodePtr> get_children() const = 0;
|
||||||
|
virtual Boxed_Value eval(const chaiscript::detail::Dispatch_State &t_e) const = 0;
|
||||||
|
|
||||||
|
|
||||||
/// Prints the contents of an AST node, including its children, recursively
|
/// Prints the contents of an AST node, including its children, recursively
|
||||||
std::string to_string(const std::string &t_prepend = "") const {
|
std::string to_string(const std::string &t_prepend = "") const {
|
||||||
@ -473,25 +534,16 @@ namespace chaiscript
|
|||||||
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
|
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
|
||||||
<< this->text << " : " << this->location.start.line << ", " << this->location.start.column << '\n';
|
<< this->text << " : " << this->location.start.line << ", " << this->location.start.column << '\n';
|
||||||
|
|
||||||
for (auto & elem : this->children) {
|
for (auto & elem : this->get_children()) {
|
||||||
oss << elem->to_string(t_prepend + " ");
|
oss << elem->to_string(t_prepend + " ");
|
||||||
}
|
}
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
Boxed_Value eval(const chaiscript::detail::Dispatch_State &t_e) const
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
return eval_internal(t_e);
|
|
||||||
} catch (exception::eval_error &ee) {
|
|
||||||
ee.call_stack.push_back(shared_from_this());
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool get_bool_condition(const Boxed_Value &t_bv) {
|
static bool get_bool_condition(const Boxed_Value &t_bv, const chaiscript::detail::Dispatch_State &t_ss) {
|
||||||
try {
|
try {
|
||||||
return boxed_cast<bool>(t_bv);
|
return t_ss->boxed_cast<bool>(t_bv);
|
||||||
}
|
}
|
||||||
catch (const exception::bad_boxed_cast &) {
|
catch (const exception::bad_boxed_cast &) {
|
||||||
throw exception::eval_error("Condition not boolean");
|
throw exception::eval_error("Condition not boolean");
|
||||||
@ -499,33 +551,47 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void replace_child(const AST_NodePtr &t_child, const AST_NodePtr &t_new_child)
|
virtual ~AST_Node() = default;
|
||||||
{
|
AST_Node(AST_Node &&) = default;
|
||||||
std::replace(children.begin(), children.end(), t_child, t_new_child);
|
AST_Node &operator=(AST_Node &&) = default;
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~AST_Node() {}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
AST_Node(std::string t_ast_node_text, int t_id, Parse_Location t_loc,
|
|
||||||
std::vector<AST_NodePtr> t_children = std::vector<AST_NodePtr>()) :
|
|
||||||
identifier(t_id), text(std::move(t_ast_node_text)),
|
|
||||||
location(std::move(t_loc)),
|
|
||||||
children(std::move(t_children))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Undispatched ast_node (internal error)");
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Copy and assignment explicitly unimplemented
|
|
||||||
AST_Node(const AST_Node &) = delete;
|
AST_Node(const AST_Node &) = delete;
|
||||||
AST_Node& operator=(const AST_Node &) = delete;
|
AST_Node& operator=(const AST_Node &) = delete;
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
AST_Node(std::string t_ast_node_text, AST_Node_Type t_id, Parse_Location t_loc)
|
||||||
|
: identifier(t_id), text(std::move(t_ast_node_text)),
|
||||||
|
location(std::move(t_loc))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace parser {
|
||||||
|
class ChaiScript_Parser_Base
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual AST_NodePtr parse(const std::string &t_input, const std::string &t_fname) = 0;
|
||||||
|
virtual void debug_print(AST_NodePtr t, std::string prepend = "") const = 0;
|
||||||
|
virtual void *get_tracer_ptr() = 0;
|
||||||
|
virtual ~ChaiScript_Parser_Base() = default;
|
||||||
|
ChaiScript_Parser_Base() = default;
|
||||||
|
ChaiScript_Parser_Base(ChaiScript_Parser_Base &&) = default;
|
||||||
|
ChaiScript_Parser_Base &operator=(ChaiScript_Parser_Base &&) = delete;
|
||||||
|
ChaiScript_Parser_Base &operator=(const ChaiScript_Parser_Base &&) = delete;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T &get_tracer()
|
||||||
|
{
|
||||||
|
// to do type check this somehow?
|
||||||
|
return *static_cast<T*>(get_tracer_ptr());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
ChaiScript_Parser_Base(const ChaiScript_Parser_Base &) = default;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
namespace eval
|
namespace eval
|
||||||
{
|
{
|
||||||
@ -535,96 +601,102 @@ namespace chaiscript
|
|||||||
struct Return_Value {
|
struct Return_Value {
|
||||||
Boxed_Value retval;
|
Boxed_Value retval;
|
||||||
|
|
||||||
Return_Value(Boxed_Value t_return_value) : retval(std::move(t_return_value)) { }
|
explicit Return_Value(Boxed_Value t_return_value) : retval(std::move(t_return_value)) { }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Special type indicating a call to 'break'
|
/// Special type indicating a call to 'break'
|
||||||
struct Break_Loop {
|
struct Break_Loop {
|
||||||
Break_Loop() { }
|
Break_Loop() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Special type indicating a call to 'continue'
|
/// Special type indicating a call to 'continue'
|
||||||
struct Continue_Loop {
|
struct Continue_Loop {
|
||||||
Continue_Loop() { }
|
Continue_Loop() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Creates a new scope then pops it on destruction
|
/// Creates a new scope then pops it on destruction
|
||||||
struct Scope_Push_Pop
|
struct Scope_Push_Pop
|
||||||
{
|
{
|
||||||
|
Scope_Push_Pop(Scope_Push_Pop &&) = default;
|
||||||
|
Scope_Push_Pop& operator=(Scope_Push_Pop &&) = default;
|
||||||
Scope_Push_Pop(const Scope_Push_Pop &) = delete;
|
Scope_Push_Pop(const Scope_Push_Pop &) = delete;
|
||||||
Scope_Push_Pop& operator=(const Scope_Push_Pop &) = delete;
|
Scope_Push_Pop& operator=(const Scope_Push_Pop &) = delete;
|
||||||
|
|
||||||
Scope_Push_Pop(const chaiscript::detail::Dispatch_State &t_ds)
|
explicit Scope_Push_Pop(const chaiscript::detail::Dispatch_State &t_ds)
|
||||||
: m_ds(t_ds)
|
: m_ds(t_ds)
|
||||||
{
|
{
|
||||||
m_ds.get()->new_scope(m_ds.get().stack_holder());
|
m_ds->new_scope(m_ds.stack_holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
~Scope_Push_Pop()
|
~Scope_Push_Pop()
|
||||||
{
|
{
|
||||||
m_ds.get()->pop_scope(m_ds.get().stack_holder());
|
m_ds->pop_scope(m_ds.stack_holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::reference_wrapper<const chaiscript::detail::Dispatch_State> m_ds;
|
const chaiscript::detail::Dispatch_State &m_ds;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Creates a new function call and pops it on destruction
|
/// Creates a new function call and pops it on destruction
|
||||||
struct Function_Push_Pop
|
struct Function_Push_Pop
|
||||||
{
|
{
|
||||||
|
Function_Push_Pop(Function_Push_Pop &&) = default;
|
||||||
|
Function_Push_Pop& operator=(Function_Push_Pop &&) = default;
|
||||||
Function_Push_Pop(const Function_Push_Pop &) = delete;
|
Function_Push_Pop(const Function_Push_Pop &) = delete;
|
||||||
Function_Push_Pop& operator=(const Function_Push_Pop &) = delete;
|
Function_Push_Pop& operator=(const Function_Push_Pop &) = delete;
|
||||||
|
|
||||||
Function_Push_Pop(const chaiscript::detail::Dispatch_State &t_ds)
|
explicit Function_Push_Pop(const chaiscript::detail::Dispatch_State &t_ds)
|
||||||
: m_ds(t_ds)
|
: m_ds(t_ds)
|
||||||
{
|
{
|
||||||
m_ds.get()->new_function_call(m_ds.get().stack_holder(), m_ds.get().conversion_saves());
|
m_ds->new_function_call(m_ds.stack_holder(), m_ds.conversion_saves());
|
||||||
}
|
}
|
||||||
|
|
||||||
~Function_Push_Pop()
|
~Function_Push_Pop()
|
||||||
{
|
{
|
||||||
m_ds.get()->pop_function_call(m_ds.get().stack_holder(), m_ds.get().conversion_saves());
|
m_ds->pop_function_call(m_ds.stack_holder(), m_ds.conversion_saves());
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_params(const std::vector<Boxed_Value> &t_params)
|
void save_params(const std::vector<Boxed_Value> &t_params)
|
||||||
{
|
{
|
||||||
m_ds.get()->save_function_params(t_params);
|
m_ds->save_function_params(t_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_params(std::initializer_list<Boxed_Value> t_params)
|
void save_params(std::initializer_list<Boxed_Value> t_params)
|
||||||
{
|
{
|
||||||
m_ds.get()->save_function_params(std::move(t_params));
|
m_ds->save_function_params(t_params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::reference_wrapper<const chaiscript::detail::Dispatch_State> m_ds;
|
const chaiscript::detail::Dispatch_State &m_ds;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Creates a new scope then pops it on destruction
|
/// Creates a new scope then pops it on destruction
|
||||||
struct Stack_Push_Pop
|
struct Stack_Push_Pop
|
||||||
{
|
{
|
||||||
|
Stack_Push_Pop(Stack_Push_Pop &&) = default;
|
||||||
|
Stack_Push_Pop& operator=(Stack_Push_Pop &&) = default;
|
||||||
Stack_Push_Pop(const Stack_Push_Pop &) = delete;
|
Stack_Push_Pop(const Stack_Push_Pop &) = delete;
|
||||||
Stack_Push_Pop& operator=(const Stack_Push_Pop &) = delete;
|
Stack_Push_Pop& operator=(const Stack_Push_Pop &) = delete;
|
||||||
|
|
||||||
Stack_Push_Pop(const chaiscript::detail::Dispatch_State &t_ds)
|
explicit Stack_Push_Pop(const chaiscript::detail::Dispatch_State &t_ds)
|
||||||
: m_ds(t_ds)
|
: m_ds(t_ds)
|
||||||
{
|
{
|
||||||
m_ds.get()->new_stack(m_ds.get().stack_holder());
|
m_ds->new_stack(m_ds.stack_holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
~Stack_Push_Pop()
|
~Stack_Push_Pop()
|
||||||
{
|
{
|
||||||
m_ds.get()->pop_stack(m_ds.get().stack_holder());
|
m_ds->pop_stack(m_ds.stack_holder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::reference_wrapper<const chaiscript::detail::Dispatch_State> m_ds;
|
const chaiscript::detail::Dispatch_State &m_ds;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_ENGINE_HPP_
|
#ifndef CHAISCRIPT_ENGINE_HPP_
|
||||||
#define CHAISCRIPT_ENGINE_HPP_
|
#define CHAISCRIPT_ENGINE_HPP_
|
||||||
|
|
||||||
@ -17,6 +21,7 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "../chaiscript_defines.hpp"
|
#include "../chaiscript_defines.hpp"
|
||||||
#include "../chaiscript_threading.hpp"
|
#include "../chaiscript_threading.hpp"
|
||||||
@ -31,229 +36,34 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(_POSIX_VERSION) && !defined(__CYGWIN__)
|
#if !defined(CHAISCRIPT_NO_DYNLOAD) && defined(_POSIX_VERSION) && !defined(__CYGWIN__)
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(CHAISCRIPT_NO_DYNLOAD)
|
||||||
|
#include "chaiscript_unknown.hpp"
|
||||||
|
#elif defined(CHAISCRIPT_WINDOWS)
|
||||||
|
#include "chaiscript_windows.hpp"
|
||||||
|
#elif _POSIX_VERSION
|
||||||
|
#include "chaiscript_posix.hpp"
|
||||||
#else
|
#else
|
||||||
#ifdef CHAISCRIPT_WINDOWS
|
#include "chaiscript_unknown.hpp"
|
||||||
#define VC_EXTRA_LEAN
|
|
||||||
#if !defined(WIN32_LEAN_AND_MEAN)
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#endif
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#include "../dispatchkit/exception_specification.hpp"
|
#include "../dispatchkit/exception_specification.hpp"
|
||||||
#include "chaiscript_parser.hpp"
|
|
||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
namespace exception
|
|
||||||
{
|
|
||||||
/// \brief Thrown if an error occurs while attempting to load a binary module
|
|
||||||
struct load_module_error : std::runtime_error
|
|
||||||
{
|
|
||||||
load_module_error(const std::string &t_reason) noexcept
|
|
||||||
: std::runtime_error(t_reason)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
load_module_error(const load_module_error &) = default;
|
|
||||||
virtual ~load_module_error() noexcept {}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace detail
|
namespace detail
|
||||||
{
|
{
|
||||||
#if defined(_POSIX_VERSION) && !defined(__CYGWIN__)
|
|
||||||
struct Loadable_Module
|
|
||||||
{
|
|
||||||
struct DLModule
|
|
||||||
{
|
|
||||||
DLModule(const std::string &t_filename)
|
|
||||||
: m_data(dlopen(t_filename.c_str(), RTLD_NOW))
|
|
||||||
{
|
|
||||||
if (!m_data)
|
|
||||||
{
|
|
||||||
throw chaiscript::exception::load_module_error(dlerror());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DLModule(const DLModule &); // Explicitly unimplemented copy constructor
|
|
||||||
DLModule &operator=(const DLModule &); // Explicitly unimplemented assignment operator
|
|
||||||
|
|
||||||
~DLModule()
|
|
||||||
{
|
|
||||||
dlclose(m_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *m_data;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct DLSym
|
|
||||||
{
|
|
||||||
DLSym(DLModule &t_mod, const std::string &t_symbol)
|
|
||||||
: m_symbol(cast_symbol(dlsym(t_mod.m_data, t_symbol.c_str())))
|
|
||||||
{
|
|
||||||
if (!m_symbol)
|
|
||||||
{
|
|
||||||
throw chaiscript::exception::load_module_error(dlerror());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static T cast_symbol(void *p)
|
|
||||||
{
|
|
||||||
union cast_union
|
|
||||||
{
|
|
||||||
T func_ptr;
|
|
||||||
void *in_ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
cast_union c;
|
|
||||||
c.in_ptr = p;
|
|
||||||
return c.func_ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
T m_symbol;
|
|
||||||
};
|
|
||||||
|
|
||||||
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
|
|
||||||
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
|
|
||||||
m_moduleptr(m_func.m_symbol())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
DLModule m_dlmodule;
|
|
||||||
DLSym<Create_Module_Func> m_func;
|
|
||||||
ModulePtr m_moduleptr;
|
|
||||||
};
|
|
||||||
#else
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
|
|
||||||
|
|
||||||
struct Loadable_Module
|
|
||||||
{
|
|
||||||
template<typename T>
|
|
||||||
static std::wstring to_wstring(const T &t_str)
|
|
||||||
{
|
|
||||||
return std::wstring(t_str.begin(), t_str.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static std::string to_string(const T &t_str)
|
|
||||||
{
|
|
||||||
return std::string(t_str.begin(), t_str.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(_UNICODE) || defined(UNICODE)
|
|
||||||
template<typename T>
|
|
||||||
static std::wstring to_proper_string(const T &t_str)
|
|
||||||
{
|
|
||||||
return to_wstring(t_str);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
template<typename T>
|
|
||||||
static std::string to_proper_string(const T &t_str)
|
|
||||||
{
|
|
||||||
return to_string(t_str);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static std::string get_error_message(DWORD t_err)
|
|
||||||
{
|
|
||||||
typedef LPTSTR StringType;
|
|
||||||
|
|
||||||
#if defined(_UNICODE) || defined(UNICODE)
|
|
||||||
std::wstring retval = L"Unknown Error";
|
|
||||||
#else
|
|
||||||
std::string retval = "Unknown Error";
|
|
||||||
#endif
|
|
||||||
StringType lpMsgBuf = nullptr;
|
|
||||||
|
|
||||||
if (FormatMessage(
|
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
||||||
nullptr,
|
|
||||||
t_err,
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
reinterpret_cast<StringType>(&lpMsgBuf),
|
|
||||||
0, nullptr ) != 0 && lpMsgBuf)
|
|
||||||
{
|
|
||||||
retval = lpMsgBuf;
|
|
||||||
LocalFree(lpMsgBuf);
|
|
||||||
}
|
|
||||||
|
|
||||||
return to_string(retval);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DLModule
|
|
||||||
{
|
|
||||||
DLModule(const std::string &t_filename)
|
|
||||||
: m_data(LoadLibrary(to_proper_string(t_filename).c_str()))
|
|
||||||
{
|
|
||||||
if (!m_data)
|
|
||||||
{
|
|
||||||
throw chaiscript::exception::load_module_error(get_error_message(GetLastError()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~DLModule()
|
|
||||||
{
|
|
||||||
FreeLibrary(m_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
HMODULE m_data;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct DLSym
|
|
||||||
{
|
|
||||||
DLSym(DLModule &t_mod, const std::string &t_symbol)
|
|
||||||
: m_symbol(reinterpret_cast<T>(GetProcAddress(t_mod.m_data, t_symbol.c_str())))
|
|
||||||
{
|
|
||||||
if (!m_symbol)
|
|
||||||
{
|
|
||||||
throw chaiscript::exception::load_module_error(get_error_message(GetLastError()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
T m_symbol;
|
|
||||||
};
|
|
||||||
|
|
||||||
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
|
|
||||||
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
|
|
||||||
m_moduleptr(m_func.m_symbol())
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
DLModule m_dlmodule;
|
|
||||||
DLSym<Create_Module_Func> m_func;
|
|
||||||
ModulePtr m_moduleptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
#else
|
|
||||||
struct Loadable_Module
|
|
||||||
{
|
|
||||||
Loadable_Module(const std::string &, const std::string &)
|
|
||||||
{
|
|
||||||
throw chaiscript::exception::load_module_error("Loadable module support not available for your platform");
|
|
||||||
}
|
|
||||||
|
|
||||||
ModulePtr m_moduleptr;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef std::shared_ptr<Loadable_Module> Loadable_Module_Ptr;
|
typedef std::shared_ptr<Loadable_Module> Loadable_Module_Ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// \brief The main object that the ChaiScript user will use.
|
/// \brief The main object that the ChaiScript user will use.
|
||||||
class ChaiScript {
|
class ChaiScript_Basic {
|
||||||
|
|
||||||
mutable chaiscript::detail::threading::shared_mutex m_mutex;
|
mutable chaiscript::detail::threading::shared_mutex m_mutex;
|
||||||
mutable chaiscript::detail::threading::recursive_mutex m_use_mutex;
|
mutable chaiscript::detail::threading::recursive_mutex m_use_mutex;
|
||||||
@ -265,19 +75,16 @@ namespace chaiscript
|
|||||||
std::vector<std::string> m_module_paths;
|
std::vector<std::string> m_module_paths;
|
||||||
std::vector<std::string> m_use_paths;
|
std::vector<std::string> m_use_paths;
|
||||||
|
|
||||||
|
std::unique_ptr<parser::ChaiScript_Parser_Base> m_parser;
|
||||||
|
|
||||||
chaiscript::detail::Dispatch_Engine m_engine;
|
chaiscript::detail::Dispatch_Engine m_engine;
|
||||||
|
|
||||||
/// Evaluates the given string in by parsing it and running the results through the evaluator
|
/// Evaluates the given string in by parsing it and running the results through the evaluator
|
||||||
Boxed_Value do_eval(const std::string &t_input, const std::string &t_filename = "__EVAL__", bool /* t_internal*/ = false)
|
Boxed_Value do_eval(const std::string &t_input, const std::string &t_filename = "__EVAL__", bool /* t_internal*/ = false)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
parser::ChaiScript_Parser parser;
|
const auto p = m_parser->parse(t_input, t_filename);
|
||||||
if (parser.parse(t_input, t_filename)) {
|
return p->eval(chaiscript::detail::Dispatch_State(m_engine));
|
||||||
//parser.show_match_stack();
|
|
||||||
return parser.optimized_ast()->eval(m_engine);
|
|
||||||
} else {
|
|
||||||
return Boxed_Value();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (chaiscript::eval::detail::Return_Value &rv) {
|
catch (chaiscript::eval::detail::Return_Value &rv) {
|
||||||
return rv.retval;
|
return rv.retval;
|
||||||
@ -286,8 +93,6 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Evaluates the given file and looks in the 'use' paths
|
/// Evaluates the given file and looks in the 'use' paths
|
||||||
const Boxed_Value internal_eval_file(const std::string &t_filename) {
|
const Boxed_Value internal_eval_file(const std::string &t_filename) {
|
||||||
for (const auto &path : m_use_paths)
|
for (const auto &path : m_use_paths)
|
||||||
@ -324,28 +129,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
|
/// Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
|
||||||
void build_eval_system(const ModulePtr &t_lib) {
|
void build_eval_system(const ModulePtr &t_lib, const std::vector<Options> &t_opts) {
|
||||||
m_engine.add_reserved_word("def");
|
|
||||||
m_engine.add_reserved_word("fun");
|
|
||||||
m_engine.add_reserved_word("while");
|
|
||||||
m_engine.add_reserved_word("for");
|
|
||||||
m_engine.add_reserved_word("if");
|
|
||||||
m_engine.add_reserved_word("else");
|
|
||||||
m_engine.add_reserved_word("&&");
|
|
||||||
m_engine.add_reserved_word("||");
|
|
||||||
m_engine.add_reserved_word(",");
|
|
||||||
m_engine.add_reserved_word("auto");
|
|
||||||
m_engine.add_reserved_word("return");
|
|
||||||
m_engine.add_reserved_word("break");
|
|
||||||
m_engine.add_reserved_word("true");
|
|
||||||
m_engine.add_reserved_word("false");
|
|
||||||
m_engine.add_reserved_word("class");
|
|
||||||
m_engine.add_reserved_word("attr");
|
|
||||||
m_engine.add_reserved_word("var");
|
|
||||||
m_engine.add_reserved_word("global");
|
|
||||||
m_engine.add_reserved_word("GLOBAL");
|
|
||||||
m_engine.add_reserved_word("_");
|
|
||||||
|
|
||||||
if (t_lib)
|
if (t_lib)
|
||||||
{
|
{
|
||||||
add(t_lib);
|
add(t_lib);
|
||||||
@ -366,9 +150,6 @@ namespace chaiscript
|
|||||||
})
|
})
|
||||||
, "call_exists");
|
, "call_exists");
|
||||||
|
|
||||||
// m_engine.add(fun<Boxed_Value (const dispatch::Proxy_Function_Base *, const std::vector<Boxed_Value> &)>(std::bind(&chaiscript::dispatch::Proxy_Function_Base::operator(), std::placeholders::_1, std::placeholders::_2, std::ref(m_engine.conversions()))), "call");
|
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
m_engine.add(fun(
|
m_engine.add(fun(
|
||||||
[=](const dispatch::Proxy_Function_Base &t_fun, const std::vector<Boxed_Value> &t_params) -> Boxed_Value {
|
[=](const dispatch::Proxy_Function_Base &t_fun, const std::vector<Boxed_Value> &t_params) -> Boxed_Value {
|
||||||
@ -390,23 +171,26 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
m_engine.add(fun([this](const std::string &t_module, const std::string &t_file){ return load_module(t_module, t_file); }), "load_module");
|
if (std::find(t_opts.begin(), t_opts.end(), Options::No_Load_Modules) == t_opts.end()
|
||||||
m_engine.add(fun([this](const std::string &t_module){ return load_module(t_module); }), "load_module");
|
&& std::find(t_opts.begin(), t_opts.end(), Options::Load_Modules) != t_opts.end())
|
||||||
|
{
|
||||||
|
m_engine.add(fun([this](const std::string &t_module, const std::string &t_file){ return load_module(t_module, t_file); }), "load_module");
|
||||||
|
m_engine.add(fun([this](const std::string &t_module){ return load_module(t_module); }), "load_module");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std::find(t_opts.begin(), t_opts.end(), Options::No_External_Scripts) == t_opts.end()
|
||||||
|
&& std::find(t_opts.begin(), t_opts.end(), Options::External_Scripts) != t_opts.end())
|
||||||
|
{
|
||||||
|
m_engine.add(fun([this](const std::string &t_file){ return use(t_file); }), "use");
|
||||||
|
m_engine.add(fun([this](const std::string &t_file){ return internal_eval_file(t_file); }), "eval_file");
|
||||||
|
}
|
||||||
|
|
||||||
m_engine.add(fun([this](const std::string &t_file){ return use(t_file); }), "use");
|
|
||||||
m_engine.add(fun([this](const std::string &t_file){ return internal_eval_file(t_file); }), "eval_file");
|
|
||||||
m_engine.add(fun([this](const std::string &t_str){ return internal_eval(t_str); }), "eval");
|
m_engine.add(fun([this](const std::string &t_str){ return internal_eval(t_str); }), "eval");
|
||||||
m_engine.add(fun([this](const AST_NodePtr &t_ast){ return eval(t_ast); }), "eval");
|
m_engine.add(fun([this](const AST_NodePtr &t_ast){ return eval(t_ast); }), "eval");
|
||||||
m_engine.add(fun(&parse), "parse");
|
|
||||||
|
|
||||||
m_engine.add(fun(&ChaiScript::version_major), "version_major");
|
m_engine.add(fun([this](const std::string &t_str, const bool t_dump){ return parse(t_str, t_dump); }), "parse");
|
||||||
m_engine.add(fun(&ChaiScript::version_minor), "version_minor");
|
m_engine.add(fun([this](const std::string &t_str){ return parse(t_str); }), "parse");
|
||||||
m_engine.add(fun(&ChaiScript::version_patch), "version_patch");
|
|
||||||
m_engine.add(fun(&ChaiScript::version), "version");
|
|
||||||
m_engine.add(fun(&ChaiScript::compiler_version), "compiler_version");
|
|
||||||
m_engine.add(fun(&ChaiScript::compiler_name), "compiler_name");
|
|
||||||
m_engine.add(fun(&ChaiScript::compiler_id), "compiler_id");
|
|
||||||
m_engine.add(fun(&ChaiScript::debug_build), "debug_build");
|
|
||||||
|
|
||||||
m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ add_global_const(t_bv, t_name); }), "add_global_const");
|
m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ add_global_const(t_bv, t_name); }), "add_global_const");
|
||||||
m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ add_global(t_bv, t_name); }), "add_global");
|
m_engine.add(fun([this](const Boxed_Value &t_bv, const std::string &t_name){ add_global(t_bv, t_name); }), "add_global");
|
||||||
@ -437,65 +221,43 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> ensure_minimum_path_vec(std::vector<std::string> paths)
|
||||||
|
{
|
||||||
|
if (paths.empty()) { return {""}; }
|
||||||
|
else { return paths; }
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// \brief Constructor for ChaiScript
|
/// \brief Constructor for ChaiScript
|
||||||
/// \param[in] t_lib Standard library to apply to this ChaiScript instance
|
/// \param[in] t_lib Standard library to apply to this ChaiScript instance
|
||||||
/// \param[in] t_modulepaths Vector of paths to search when attempting to load a binary module
|
/// \param[in] t_modulepaths Vector of paths to search when attempting to load a binary module
|
||||||
/// \param[in] t_usepaths Vector of paths to search when attempting to "use" an included ChaiScript file
|
/// \param[in] t_usepaths Vector of paths to search when attempting to "use" an included ChaiScript file
|
||||||
ChaiScript(const ModulePtr &t_lib,
|
ChaiScript_Basic(const ModulePtr &t_lib,
|
||||||
std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
std::unique_ptr<parser::ChaiScript_Parser_Base> &&parser,
|
||||||
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
std::vector<std::string> t_module_paths = {},
|
||||||
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths))
|
std::vector<std::string> t_use_paths = {},
|
||||||
|
const std::vector<chaiscript::Options> &t_opts = chaiscript::default_options())
|
||||||
|
: m_module_paths(ensure_minimum_path_vec(std::move(t_module_paths))),
|
||||||
|
m_use_paths(ensure_minimum_path_vec(std::move(t_use_paths))),
|
||||||
|
m_parser(std::move(parser)),
|
||||||
|
m_engine(*m_parser)
|
||||||
{
|
{
|
||||||
if (m_module_paths.empty())
|
#if !defined(CHAISCRIPT_NO_DYNLOAD) && defined(_POSIX_VERSION) && !defined(__CYGWIN__)
|
||||||
{
|
|
||||||
m_module_paths.push_back("");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_use_paths.empty())
|
|
||||||
{
|
|
||||||
m_use_paths.push_back("");
|
|
||||||
}
|
|
||||||
|
|
||||||
build_eval_system(t_lib);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// \brief Constructor for ChaiScript.
|
|
||||||
///
|
|
||||||
/// This version of the ChaiScript constructor attempts to find the stdlib module to load
|
|
||||||
/// at runtime generates an error if it cannot be found.
|
|
||||||
///
|
|
||||||
/// \param[in] t_modulepaths Vector of paths to search when attempting to load a binary module
|
|
||||||
/// \param[in] t_usepaths Vector of paths to search when attempting to "use" an included ChaiScript file
|
|
||||||
ChaiScript( std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
|
||||||
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
|
||||||
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths))
|
|
||||||
{
|
|
||||||
if (m_module_paths.empty())
|
|
||||||
{
|
|
||||||
m_module_paths.push_back("");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_use_paths.empty())
|
|
||||||
{
|
|
||||||
m_use_paths.push_back("");
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(_POSIX_VERSION) && !defined(__CYGWIN__)
|
|
||||||
// If on Unix, add the path of the current executable to the module search path
|
// If on Unix, add the path of the current executable to the module search path
|
||||||
// as windows would do
|
// as windows would do
|
||||||
|
|
||||||
union cast_union
|
union cast_union
|
||||||
{
|
{
|
||||||
Boxed_Value (ChaiScript::*in_ptr)(const std::string&);
|
Boxed_Value (ChaiScript_Basic::*in_ptr)(const std::string&);
|
||||||
void *out_ptr;
|
void *out_ptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
Dl_info rInfo;
|
Dl_info rInfo;
|
||||||
memset( &rInfo, 0, sizeof(rInfo) );
|
memset( &rInfo, 0, sizeof(rInfo) );
|
||||||
cast_union u;
|
cast_union u;
|
||||||
u.in_ptr = &ChaiScript::use;
|
u.in_ptr = &ChaiScript_Basic::use;
|
||||||
if ( dladdr(static_cast<void*>(u.out_ptr), &rInfo) && rInfo.dli_fname ) {
|
if ( (dladdr(static_cast<void*>(u.out_ptr), &rInfo) != 0) && (rInfo.dli_fname != nullptr) ) {
|
||||||
std::string dllpath(rInfo.dli_fname);
|
std::string dllpath(rInfo.dli_fname);
|
||||||
const size_t lastslash = dllpath.rfind('/');
|
const size_t lastslash = dllpath.rfind('/');
|
||||||
if (lastslash != std::string::npos)
|
if (lastslash != std::string::npos)
|
||||||
@ -514,83 +276,70 @@ namespace chaiscript
|
|||||||
m_module_paths.insert(m_module_paths.begin(), dllpath+"/");
|
m_module_paths.insert(m_module_paths.begin(), dllpath+"/");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
build_eval_system(t_lib, t_opts);
|
||||||
|
|
||||||
// attempt to load the stdlib
|
|
||||||
load_module("chaiscript_stdlib-" + version());
|
|
||||||
|
|
||||||
build_eval_system(ModulePtr());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
/// \brief Constructor for ChaiScript.
|
||||||
|
///
|
||||||
|
/// This version of the ChaiScript constructor attempts to find the stdlib module to load
|
||||||
|
/// at runtime generates an error if it cannot be found.
|
||||||
|
///
|
||||||
|
/// \param[in] t_modulepaths Vector of paths to search when attempting to load a binary module
|
||||||
|
/// \param[in] t_usepaths Vector of paths to search when attempting to "use" an included ChaiScript file
|
||||||
|
explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&parser,
|
||||||
|
std::vector<std::string> t_module_paths = {},
|
||||||
|
std::vector<std::string> t_use_paths = {},
|
||||||
|
const std::vector<chaiscript::Options> &t_opts = chaiscript::default_options())
|
||||||
|
: ChaiScript_Basic({}, std::move(parser), t_module_paths, t_use_paths, t_opts)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// attempt to load the stdlib
|
||||||
|
load_module("chaiscript_stdlib-" + Build_Info::version());
|
||||||
|
} catch (const exception::load_module_error &t_err) {
|
||||||
|
std::cout << "An error occured while trying to load the chaiscript standard library.\n"
|
||||||
|
<< "\n"
|
||||||
|
<< "You must either provide a standard library, or compile it in.\n"
|
||||||
|
<< "For an example of compiling the standard library in,\n"
|
||||||
|
<< "see: https://gist.github.com/lefticus/9456197\n"
|
||||||
|
<< "Compiling the stdlib in is the recommended and MOST SUPPORTED method.\n"
|
||||||
|
<< "\n"
|
||||||
|
<< "\n"
|
||||||
|
<< t_err.what();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else // CHAISCRIPT_NO_DYNLOAD
|
||||||
|
explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&parser,
|
||||||
|
std::vector<std::string> t_module_paths = {},
|
||||||
|
std::vector<std::string> t_use_paths = {},
|
||||||
|
const std::vector<chaiscript::Options> &t_opts = chaiscript::default_options()) = delete;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
parser::ChaiScript_Parser_Base &get_parser()
|
||||||
|
{
|
||||||
|
return *m_parser;
|
||||||
|
}
|
||||||
|
|
||||||
const Boxed_Value eval(const AST_NodePtr &t_ast)
|
const Boxed_Value eval(const AST_NodePtr &t_ast)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return t_ast->eval(m_engine);
|
return t_ast->eval(chaiscript::detail::Dispatch_State(m_engine));
|
||||||
} catch (const exception::eval_error &t_ee) {
|
} catch (const exception::eval_error &t_ee) {
|
||||||
throw Boxed_Value(t_ee);
|
throw Boxed_Value(t_ee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static AST_NodePtr parse(const std::string &t_input)
|
AST_NodePtr parse(const std::string &t_input, const bool t_debug_print = false)
|
||||||
{
|
{
|
||||||
parser::ChaiScript_Parser parser;
|
const auto ast = m_parser->parse(t_input, "PARSE");
|
||||||
if (parser.parse(t_input, "PARSE")) {
|
if (t_debug_print) {
|
||||||
//parser.show_match_stack();
|
m_parser->debug_print(ast);
|
||||||
return parser.optimized_ast();
|
|
||||||
} else {
|
|
||||||
throw chaiscript::exception::eval_error("Unknown error while parsing");
|
|
||||||
}
|
}
|
||||||
|
return ast;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int version_major()
|
|
||||||
{
|
|
||||||
return chaiscript::version_major;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int version_minor()
|
|
||||||
{
|
|
||||||
return chaiscript::version_minor;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int version_patch()
|
|
||||||
{
|
|
||||||
return chaiscript::version_patch;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string version()
|
|
||||||
{
|
|
||||||
return std::to_string(version_major()) + '.' + std::to_string(version_minor()) + '.' + std::to_string(version_patch());
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string compiler_id()
|
|
||||||
{
|
|
||||||
return compiler_name() + '-' + compiler_version();
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string build_id()
|
|
||||||
{
|
|
||||||
return compiler_id() + (debug_build()?"-Debug":"-Release");
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string compiler_version()
|
|
||||||
{
|
|
||||||
return chaiscript::compiler_version;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string compiler_name()
|
|
||||||
{
|
|
||||||
return chaiscript::compiler_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool debug_build()
|
|
||||||
{
|
|
||||||
return chaiscript::debug_build;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string get_type_name(const Type_Info &ti) const
|
std::string get_type_name(const Type_Info &ti) const
|
||||||
{
|
{
|
||||||
return m_engine.get_type_name(ti);
|
return m_engine.get_type_name(ti);
|
||||||
@ -643,8 +392,9 @@ namespace chaiscript
|
|||||||
/// \param[in] t_name Name of the value to add
|
/// \param[in] t_name Name of the value to add
|
||||||
/// \throw chaiscript::exception::global_non_const If t_bv is not a constant object
|
/// \throw chaiscript::exception::global_non_const If t_bv is not a constant object
|
||||||
/// \sa Boxed_Value::is_const
|
/// \sa Boxed_Value::is_const
|
||||||
ChaiScript &add_global_const(const Boxed_Value &t_bv, const std::string &t_name)
|
ChaiScript_Basic &add_global_const(const Boxed_Value &t_bv, const std::string &t_name)
|
||||||
{
|
{
|
||||||
|
Name_Validator::validate_object_name(t_name);
|
||||||
m_engine.add_global_const(t_bv, t_name);
|
m_engine.add_global_const(t_bv, t_name);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -654,14 +404,16 @@ namespace chaiscript
|
|||||||
/// \param[in] t_name Name of the value to add
|
/// \param[in] t_name Name of the value to add
|
||||||
/// \warning The user is responsible for making sure the object is thread-safe if necessary
|
/// \warning The user is responsible for making sure the object is thread-safe if necessary
|
||||||
/// ChaiScript is thread-safe but provides no threading locking mechanism to the script
|
/// ChaiScript is thread-safe but provides no threading locking mechanism to the script
|
||||||
ChaiScript &add_global(const Boxed_Value &t_bv, const std::string &t_name)
|
ChaiScript_Basic &add_global(const Boxed_Value &t_bv, const std::string &t_name)
|
||||||
{
|
{
|
||||||
|
Name_Validator::validate_object_name(t_name);
|
||||||
m_engine.add_global(t_bv, t_name);
|
m_engine.add_global(t_bv, t_name);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ChaiScript &set_global(const Boxed_Value &t_bv, const std::string &t_name)
|
ChaiScript_Basic &set_global(const Boxed_Value &t_bv, const std::string &t_name)
|
||||||
{
|
{
|
||||||
|
Name_Validator::validate_object_name(t_name);
|
||||||
m_engine.set_global(t_bv, t_name);
|
m_engine.set_global(t_bv, t_name);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -759,8 +511,9 @@ namespace chaiscript
|
|||||||
///
|
///
|
||||||
/// \sa \ref adding_items
|
/// \sa \ref adding_items
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ChaiScript &add(const T &t_t, const std::string &t_name)
|
ChaiScript_Basic &add(const T &t_t, const std::string &t_name)
|
||||||
{
|
{
|
||||||
|
Name_Validator::validate_object_name(t_name);
|
||||||
m_engine.add(t_t, t_name);
|
m_engine.add(t_t, t_name);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -774,7 +527,7 @@ namespace chaiscript
|
|||||||
/// chaiscript::ChaiScript chai;
|
/// chaiscript::ChaiScript chai;
|
||||||
/// chai.add(chaiscript::base_class<std::runtime_error, chaiscript::dispatch_error>());
|
/// chai.add(chaiscript::base_class<std::runtime_error, chaiscript::dispatch_error>());
|
||||||
/// \endcode
|
/// \endcode
|
||||||
ChaiScript &add(const Type_Conversion &d)
|
ChaiScript_Basic &add(const Type_Conversion &d)
|
||||||
{
|
{
|
||||||
m_engine.add(d);
|
m_engine.add(d);
|
||||||
return *this;
|
return *this;
|
||||||
@ -783,7 +536,7 @@ namespace chaiscript
|
|||||||
/// \brief Adds all elements of a module to ChaiScript runtime
|
/// \brief Adds all elements of a module to ChaiScript runtime
|
||||||
/// \param[in] t_p The module to add.
|
/// \param[in] t_p The module to add.
|
||||||
/// \sa chaiscript::Module
|
/// \sa chaiscript::Module
|
||||||
ChaiScript &add(const ModulePtr &t_p)
|
ChaiScript_Basic &add(const ModulePtr &t_p)
|
||||||
{
|
{
|
||||||
t_p->apply(*this, this->get_eval_engine());
|
t_p->apply(*this, this->get_eval_engine());
|
||||||
return *this;
|
return *this;
|
||||||
@ -803,9 +556,13 @@ namespace chaiscript
|
|||||||
/// \throw chaiscript::exception::load_module_error In the event that no matching module can be found.
|
/// \throw chaiscript::exception::load_module_error In the event that no matching module can be found.
|
||||||
std::string load_module(const std::string &t_module_name)
|
std::string load_module(const std::string &t_module_name)
|
||||||
{
|
{
|
||||||
|
#ifdef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
(void)t_module_name; // -Wunused-parameter
|
||||||
|
throw chaiscript::exception::load_module_error("Loadable module support was disabled (CHAISCRIPT_NO_DYNLOAD)");
|
||||||
|
#else
|
||||||
std::vector<exception::load_module_error> errors;
|
std::vector<exception::load_module_error> errors;
|
||||||
std::string version_stripped_name = t_module_name;
|
std::string version_stripped_name = t_module_name;
|
||||||
size_t version_pos = version_stripped_name.find("-"+version());
|
size_t version_pos = version_stripped_name.find("-" + Build_Info::version());
|
||||||
if (version_pos != std::string::npos)
|
if (version_pos != std::string::npos)
|
||||||
{
|
{
|
||||||
version_stripped_name.erase(version_pos);
|
version_stripped_name.erase(version_pos);
|
||||||
@ -835,21 +592,8 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string errstring;
|
throw chaiscript::exception::load_module_error(t_module_name, errors);
|
||||||
|
#endif
|
||||||
for (std::vector<exception::load_module_error>::const_iterator itr = errors.begin();
|
|
||||||
itr != errors.end();
|
|
||||||
++itr)
|
|
||||||
{
|
|
||||||
if (!errstring.empty())
|
|
||||||
{
|
|
||||||
errstring += "; ";
|
|
||||||
}
|
|
||||||
|
|
||||||
errstring += itr->what();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw chaiscript::exception::load_module_error("Unable to find module: " + t_module_name + " Errors: " + errstring);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Load a binary module from a dynamic library. Works on platforms that support
|
/// \brief Load a binary module from a dynamic library. Works on platforms that support
|
||||||
@ -886,14 +630,7 @@ namespace chaiscript
|
|||||||
/// \throw chaiscript::exception::eval_error In the case that evaluation fails.
|
/// \throw chaiscript::exception::eval_error In the case that evaluation fails.
|
||||||
Boxed_Value operator()(const std::string &t_script, const Exception_Handler &t_handler = Exception_Handler())
|
Boxed_Value operator()(const std::string &t_script, const Exception_Handler &t_handler = Exception_Handler())
|
||||||
{
|
{
|
||||||
try {
|
return eval(t_script, t_handler);
|
||||||
return do_eval(t_script);
|
|
||||||
} catch (Boxed_Value &bv) {
|
|
||||||
if (t_handler) {
|
|
||||||
t_handler->handle(bv, m_engine);
|
|
||||||
}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Evaluates a string and returns a typesafe result.
|
/// \brief Evaluates a string and returns a typesafe result.
|
||||||
@ -912,21 +649,14 @@ namespace chaiscript
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
T eval(const std::string &t_input, const Exception_Handler &t_handler = Exception_Handler(), const std::string &t_filename="__EVAL__")
|
T eval(const std::string &t_input, const Exception_Handler &t_handler = Exception_Handler(), const std::string &t_filename="__EVAL__")
|
||||||
{
|
{
|
||||||
try {
|
return m_engine.boxed_cast<T>(eval(t_input, t_handler, t_filename));
|
||||||
return m_engine.boxed_cast<T>(do_eval(t_input, t_filename));
|
|
||||||
} catch (Boxed_Value &bv) {
|
|
||||||
if (t_handler) {
|
|
||||||
t_handler->handle(bv, m_engine);
|
|
||||||
}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief casts an object while applying any Dynamic_Conversion available
|
/// \brief casts an object while applying any Dynamic_Conversion available
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
typename detail::Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv) const
|
decltype(auto) boxed_cast(const Boxed_Value &bv) const
|
||||||
{
|
{
|
||||||
return m_engine.boxed_cast<Type>(bv);
|
return(m_engine.boxed_cast<Type>(bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -958,14 +688,7 @@ namespace chaiscript
|
|||||||
/// \return result of the script execution
|
/// \return result of the script execution
|
||||||
/// \throw chaiscript::exception::eval_error In the case that evaluation fails.
|
/// \throw chaiscript::exception::eval_error In the case that evaluation fails.
|
||||||
Boxed_Value eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
|
Boxed_Value eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
|
||||||
try {
|
return eval(load_file(t_filename), t_handler, t_filename);
|
||||||
return do_eval(load_file(t_filename), t_filename);
|
|
||||||
} catch (Boxed_Value &bv) {
|
|
||||||
if (t_handler) {
|
|
||||||
t_handler->handle(bv, m_engine);
|
|
||||||
}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Loads the file specified by filename, evaluates it, and returns the type safe result.
|
/// \brief Loads the file specified by filename, evaluates it, and returns the type safe result.
|
||||||
@ -978,14 +701,7 @@ namespace chaiscript
|
|||||||
/// to the requested type.
|
/// to the requested type.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
|
T eval_file(const std::string &t_filename, const Exception_Handler &t_handler = Exception_Handler()) {
|
||||||
try {
|
return m_engine.boxed_cast<T>(eval_file(t_filename, t_handler));
|
||||||
return m_engine.boxed_cast<T>(do_eval(load_file(t_filename), t_filename));
|
|
||||||
} catch (Boxed_Value &bv) {
|
|
||||||
if (t_handler) {
|
|
||||||
t_handler->handle(bv, m_engine);
|
|
||||||
}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
427
include/chaiscript/language/chaiscript_optimizer.hpp
Normal file
427
include/chaiscript/language/chaiscript_optimizer.hpp
Normal file
@ -0,0 +1,427 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_OPTIMIZER_HPP_
|
||||||
|
#define CHAISCRIPT_OPTIMIZER_HPP_
|
||||||
|
|
||||||
|
#include "chaiscript_eval.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace chaiscript {
|
||||||
|
namespace optimizer {
|
||||||
|
|
||||||
|
template<typename ... T>
|
||||||
|
struct Optimizer : T...
|
||||||
|
{
|
||||||
|
Optimizer() = default;
|
||||||
|
explicit Optimizer(T ... t)
|
||||||
|
: T(std::move(t))...
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Tracer>
|
||||||
|
auto optimize(eval::AST_Node_Impl_Ptr<Tracer> p) {
|
||||||
|
(void)std::initializer_list<int>{ (p = static_cast<T&>(*this).optimize(p), 0)... };
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
auto child_at(const eval::AST_Node_Impl_Ptr<T> &node, const size_t offset) {
|
||||||
|
if (node->children[offset]->identifier == AST_Node_Type::Compiled) {
|
||||||
|
return dynamic_cast<const eval::Compiled_AST_Node<T>&>(*node->children[offset]).m_original_node;
|
||||||
|
} else {
|
||||||
|
return node->children[offset];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
if (node->identifier == AST_Node_Type::Compiled) {
|
||||||
|
return dynamic_cast<const eval::Compiled_AST_Node<T>&>(*node).m_original_node->children[offset];
|
||||||
|
} else {
|
||||||
|
return node->children[offset];
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
auto child_count(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
if (node->identifier == AST_Node_Type::Compiled) {
|
||||||
|
return dynamic_cast<const eval::Compiled_AST_Node<T>&>(*node).m_original_node->children.size();
|
||||||
|
} else {
|
||||||
|
return node->children.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename Callable>
|
||||||
|
auto make_compiled_node(const eval::AST_Node_Impl_Ptr<T> &original_node, std::vector<eval::AST_Node_Impl_Ptr<T>> children, Callable callable)
|
||||||
|
{
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Compiled_AST_Node<T>>(original_node, std::move(children), std::move(callable));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct Return {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &p)
|
||||||
|
{
|
||||||
|
if ( (p->identifier == AST_Node_Type::Def || p->identifier == AST_Node_Type::Lambda)
|
||||||
|
&& !p->children.empty())
|
||||||
|
{
|
||||||
|
auto &last_child = p->children.back();
|
||||||
|
if (last_child->identifier == AST_Node_Type::Block) {
|
||||||
|
auto &block_last_child = last_child->children.back();
|
||||||
|
if (block_last_child->identifier == AST_Node_Type::Return) {
|
||||||
|
if (block_last_child->children.size() == 1) {
|
||||||
|
last_child->children.back() = block_last_child->children[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
bool contains_var_decl_in_scope(const T &node)
|
||||||
|
{
|
||||||
|
if (node->identifier == AST_Node_Type::Var_Decl) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto num = child_count(node);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < num; ++i) {
|
||||||
|
const auto &child = child_at(node, i);
|
||||||
|
if (child->identifier != AST_Node_Type::Block
|
||||||
|
&& child->identifier != AST_Node_Type::For
|
||||||
|
&& contains_var_decl_in_scope(child)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Block {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
if (node->identifier == AST_Node_Type::Block)
|
||||||
|
{
|
||||||
|
if (!contains_var_decl_in_scope(node))
|
||||||
|
{
|
||||||
|
if (node->children.size() == 1) {
|
||||||
|
return node->children[0];
|
||||||
|
} else {
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Scopeless_Block_AST_Node<T>>(node->text, node->location, node->children);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Dead_Code {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
if (node->identifier == AST_Node_Type::Block)
|
||||||
|
{
|
||||||
|
std::vector<size_t> keepers;
|
||||||
|
const auto num_children = node->children.size();
|
||||||
|
keepers.reserve(num_children);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < num_children; ++i) {
|
||||||
|
auto child = node->children[i];
|
||||||
|
if ( (child->identifier != AST_Node_Type::Id
|
||||||
|
&& child->identifier != AST_Node_Type::Constant
|
||||||
|
&& child->identifier != AST_Node_Type::Noop)
|
||||||
|
|| i == num_children - 1) {
|
||||||
|
keepers.push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keepers.size() == num_children) {
|
||||||
|
return node;
|
||||||
|
} else {
|
||||||
|
std::vector<eval::AST_Node_Impl_Ptr<T>> new_children;
|
||||||
|
for (const auto x : keepers)
|
||||||
|
{
|
||||||
|
new_children.push_back(node->children[x]);
|
||||||
|
}
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Block_AST_Node<T>>(node->text, node->location, new_children);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Unused_Return {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
if ((node->identifier == AST_Node_Type::Block
|
||||||
|
|| node->identifier == AST_Node_Type::Scopeless_Block)
|
||||||
|
&& !node->children.empty())
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < node->children.size()-1; ++i) {
|
||||||
|
auto child = node->children[i];
|
||||||
|
if (child->identifier == AST_Node_Type::Fun_Call) {
|
||||||
|
node->children[i] = chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Unused_Return_Fun_Call_AST_Node<T>>(child->text, child->location, std::move(child->children));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if ((node->identifier == AST_Node_Type::For
|
||||||
|
|| node->identifier == AST_Node_Type::While)
|
||||||
|
&& child_count(node) > 0) {
|
||||||
|
auto child = child_at(node, child_count(node) - 1);
|
||||||
|
if (child->identifier == AST_Node_Type::Block
|
||||||
|
|| child->identifier == AST_Node_Type::Scopeless_Block)
|
||||||
|
{
|
||||||
|
auto num_sub_children = child_count(child);
|
||||||
|
for (size_t i = 0; i < num_sub_children; ++i) {
|
||||||
|
auto sub_child = child_at(child, i);
|
||||||
|
if (sub_child->identifier == AST_Node_Type::Fun_Call) {
|
||||||
|
child->children[i] = chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Unused_Return_Fun_Call_AST_Node<T>>(sub_child->text, sub_child->location, std::move(sub_child->children));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct If {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
if ((node->identifier == AST_Node_Type::If)
|
||||||
|
&& node->children.size() >= 2
|
||||||
|
&& node->children[0]->identifier == AST_Node_Type::Constant)
|
||||||
|
{
|
||||||
|
const auto condition = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[0])->m_value;
|
||||||
|
if (condition.get_type_info().bare_equal_type_info(typeid(bool))) {
|
||||||
|
if (boxed_cast<bool>(condition)) {
|
||||||
|
return node->children[1];
|
||||||
|
} else if (node->children.size() == 3) {
|
||||||
|
return node->children[2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Partial_Fold {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
|
||||||
|
// Fold right side
|
||||||
|
if (node->identifier == AST_Node_Type::Binary
|
||||||
|
&& node->children.size() == 2
|
||||||
|
&& node->children[0]->identifier != AST_Node_Type::Constant
|
||||||
|
&& node->children[1]->identifier == AST_Node_Type::Constant)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
const auto &oper = node->text;
|
||||||
|
const auto parsed = Operators::to_operator(oper);
|
||||||
|
if (parsed != Operators::Opers::invalid) {
|
||||||
|
const auto rhs = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[1])->m_value;
|
||||||
|
if (rhs.get_type_info().is_arithmetic()) {
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Fold_Right_Binary_Operator_AST_Node<T>>(node->text, node->location, node->children, rhs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (const std::exception &) {
|
||||||
|
//failure to fold, that's OK
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Constant_Fold {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &node) {
|
||||||
|
|
||||||
|
if (node->identifier == AST_Node_Type::Prefix
|
||||||
|
&& node->children.size() == 1
|
||||||
|
&& node->children[0]->identifier == AST_Node_Type::Constant)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
const auto &oper = node->text;
|
||||||
|
const auto parsed = Operators::to_operator(oper, true);
|
||||||
|
const auto lhs = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[0])->m_value;
|
||||||
|
const auto match = oper + node->children[0]->text;
|
||||||
|
|
||||||
|
if (parsed != Operators::Opers::invalid && parsed != Operators::Opers::bitwise_and && lhs.get_type_info().is_arithmetic()) {
|
||||||
|
const auto val = Boxed_Number::do_oper(parsed, lhs);
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Constant_AST_Node<T>>(std::move(match), node->location, std::move(val));
|
||||||
|
} else if (lhs.get_type_info().bare_equal_type_info(typeid(bool)) && oper == "!") {
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Constant_AST_Node<T>>(std::move(match), node->location, Boxed_Value(!boxed_cast<bool>(lhs)));
|
||||||
|
}
|
||||||
|
} catch (const std::exception &) {
|
||||||
|
//failure to fold, that's OK
|
||||||
|
}
|
||||||
|
} else if ((node->identifier == AST_Node_Type::Logical_And || node->identifier == AST_Node_Type::Logical_Or)
|
||||||
|
&& node->children.size() == 2
|
||||||
|
&& node->children[0]->identifier == AST_Node_Type::Constant
|
||||||
|
&& node->children[1]->identifier == AST_Node_Type::Constant)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
const auto lhs = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[0])->m_value;
|
||||||
|
const auto rhs = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[1])->m_value;
|
||||||
|
if (lhs.get_type_info().bare_equal_type_info(typeid(bool)) && rhs.get_type_info().bare_equal_type_info(typeid(bool))) {
|
||||||
|
const auto match = node->children[0]->text + " " + node->text + " " + node->children[1]->text;
|
||||||
|
const auto val = [lhs_val = boxed_cast<bool>(lhs), rhs_val = boxed_cast<bool>(rhs), id = node->identifier] {
|
||||||
|
if (id == AST_Node_Type::Logical_And) { return Boxed_Value(lhs_val && rhs_val); }
|
||||||
|
else { return Boxed_Value(lhs_val || rhs_val); }
|
||||||
|
}();
|
||||||
|
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Constant_AST_Node<T>>(std::move(match), node->location, std::move(val));
|
||||||
|
}
|
||||||
|
} catch (const std::exception &) {
|
||||||
|
//failure to fold, that's OK
|
||||||
|
}
|
||||||
|
} else if (node->identifier == AST_Node_Type::Binary
|
||||||
|
&& node->children.size() == 2
|
||||||
|
&& node->children[0]->identifier == AST_Node_Type::Constant
|
||||||
|
&& node->children[1]->identifier == AST_Node_Type::Constant)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
const auto &oper = node->text;
|
||||||
|
const auto parsed = Operators::to_operator(oper);
|
||||||
|
if (parsed != Operators::Opers::invalid) {
|
||||||
|
const auto lhs = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[0])->m_value;
|
||||||
|
const auto rhs = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[1])->m_value;
|
||||||
|
if (lhs.get_type_info().is_arithmetic() && rhs.get_type_info().is_arithmetic()) {
|
||||||
|
const auto val = Boxed_Number::do_oper(parsed, lhs, rhs);
|
||||||
|
const auto match = node->children[0]->text + " " + oper + " " + node->children[1]->text;
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Constant_AST_Node<T>>(std::move(match), node->location, std::move(val));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (const std::exception &) {
|
||||||
|
//failure to fold, that's OK
|
||||||
|
}
|
||||||
|
} else if (node->identifier == AST_Node_Type::Fun_Call
|
||||||
|
&& node->children.size() == 2
|
||||||
|
&& node->children[0]->identifier == AST_Node_Type::Id
|
||||||
|
&& node->children[1]->identifier == AST_Node_Type::Arg_List
|
||||||
|
&& node->children[1]->children.size() == 1
|
||||||
|
&& node->children[1]->children[0]->identifier == AST_Node_Type::Constant) {
|
||||||
|
|
||||||
|
const auto arg = std::dynamic_pointer_cast<eval::Constant_AST_Node<T>>(node->children[1]->children[0])->m_value;
|
||||||
|
if (arg.get_type_info().is_arithmetic()) {
|
||||||
|
const auto &fun_name = node->children[0]->text;
|
||||||
|
|
||||||
|
const auto make_constant = [&node, &fun_name](auto val){
|
||||||
|
const auto match = fun_name + "(" + node->children[1]->children[0]->text + ")";
|
||||||
|
return chaiscript::make_shared<eval::AST_Node_Impl<T>, eval::Constant_AST_Node<T>>(std::move(match), node->location, Boxed_Value(val));
|
||||||
|
};
|
||||||
|
|
||||||
|
if (fun_name == "double") {
|
||||||
|
return make_constant(Boxed_Number(arg).get_as<double>());
|
||||||
|
} else if (fun_name == "int") {
|
||||||
|
return make_constant(Boxed_Number(arg).get_as<int>());
|
||||||
|
} else if (fun_name == "float") {
|
||||||
|
return make_constant(Boxed_Number(arg).get_as<float>());
|
||||||
|
} else if (fun_name == "long") {
|
||||||
|
return make_constant(Boxed_Number(arg).get_as<long>());
|
||||||
|
} else if (fun_name == "size_t") {
|
||||||
|
return make_constant(Boxed_Number(arg).get_as<size_t>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct For_Loop {
|
||||||
|
template<typename T>
|
||||||
|
auto optimize(const eval::AST_Node_Impl_Ptr<T> &for_node) {
|
||||||
|
|
||||||
|
if (for_node->identifier != AST_Node_Type::For) {
|
||||||
|
return for_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto eq_node = child_at(for_node, 0);
|
||||||
|
const auto binary_node = child_at(for_node, 1);
|
||||||
|
const auto prefix_node = child_at(for_node, 2);
|
||||||
|
|
||||||
|
if (eq_node->identifier == AST_Node_Type::Equation
|
||||||
|
&& child_count(eq_node) == 2
|
||||||
|
&& child_at(eq_node, 0)->identifier == AST_Node_Type::Var_Decl
|
||||||
|
&& child_at(eq_node, 1)->identifier == AST_Node_Type::Constant
|
||||||
|
&& binary_node->identifier == AST_Node_Type::Binary
|
||||||
|
&& binary_node->text == "<"
|
||||||
|
&& child_count(binary_node) == 2
|
||||||
|
&& child_at(binary_node, 0)->identifier == AST_Node_Type::Id
|
||||||
|
&& child_at(binary_node, 0)->text == child_at(child_at(eq_node,0), 0)->text
|
||||||
|
&& child_at(binary_node, 1)->identifier == AST_Node_Type::Constant
|
||||||
|
&& prefix_node->identifier == AST_Node_Type::Prefix
|
||||||
|
&& prefix_node->text == "++"
|
||||||
|
&& child_count(prefix_node) == 1
|
||||||
|
&& child_at(prefix_node, 0)->identifier == AST_Node_Type::Id
|
||||||
|
&& child_at(prefix_node, 0)->text == child_at(child_at(eq_node,0), 0)->text)
|
||||||
|
{
|
||||||
|
const Boxed_Value &begin = std::dynamic_pointer_cast<const eval::Constant_AST_Node<T>>(child_at(eq_node, 1))->m_value;
|
||||||
|
const Boxed_Value &end = std::dynamic_pointer_cast<const eval::Constant_AST_Node<T>>(child_at(binary_node, 1))->m_value;
|
||||||
|
const std::string &id = child_at(prefix_node, 0)->text;
|
||||||
|
|
||||||
|
if (begin.get_type_info().bare_equal(user_type<int>())
|
||||||
|
&& end.get_type_info().bare_equal(user_type<int>())) {
|
||||||
|
|
||||||
|
const auto start_int = boxed_cast<int>(begin);
|
||||||
|
const auto end_int = boxed_cast<int>(end);
|
||||||
|
|
||||||
|
const auto body = child_at(for_node, 3);
|
||||||
|
|
||||||
|
return make_compiled_node(for_node, {body},
|
||||||
|
[id, start_int, end_int](const std::vector<eval::AST_Node_Impl_Ptr<T>> &children, const chaiscript::detail::Dispatch_State &t_ss) {
|
||||||
|
assert(children.size() == 1);
|
||||||
|
chaiscript::eval::detail::Scope_Push_Pop spp(t_ss);
|
||||||
|
|
||||||
|
int i = start_int;
|
||||||
|
t_ss.add_object(id, var(&i));
|
||||||
|
|
||||||
|
try {
|
||||||
|
for (; i < end_int; ++i) {
|
||||||
|
try {
|
||||||
|
// Body of Loop
|
||||||
|
children[0]->eval(t_ss);
|
||||||
|
} catch (eval::detail::Continue_Loop &) {
|
||||||
|
// we got a continue exception, which means all of the remaining
|
||||||
|
// loop implementation is skipped and we just need to continue to
|
||||||
|
// the next iteration step
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (eval::detail::Break_Loop &) {
|
||||||
|
// loop broken
|
||||||
|
}
|
||||||
|
|
||||||
|
return void_var();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return for_node;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return for_node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef Optimizer<optimizer::Partial_Fold, optimizer::Unused_Return, optimizer::Constant_Fold,
|
||||||
|
optimizer::If, optimizer::Return, optimizer::Dead_Code, optimizer::Block, optimizer::For_Loop> Optimizer_Default;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
File diff suppressed because it is too large
Load Diff
81
include/chaiscript/language/chaiscript_posix.hpp
Normal file
81
include/chaiscript/language/chaiscript_posix.hpp
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_POSIX_HPP_
|
||||||
|
#define CHAISCRIPT_POSIX_HPP_
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
struct Loadable_Module
|
||||||
|
{
|
||||||
|
struct DLModule
|
||||||
|
{
|
||||||
|
explicit DLModule(const std::string &t_filename)
|
||||||
|
: m_data(dlopen(t_filename.c_str(), RTLD_NOW))
|
||||||
|
{
|
||||||
|
if (m_data == nullptr)
|
||||||
|
{
|
||||||
|
throw chaiscript::exception::load_module_error(dlerror());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DLModule(DLModule &&) = default;
|
||||||
|
DLModule &operator=(DLModule &&) = default;
|
||||||
|
DLModule(const DLModule &) = delete;
|
||||||
|
DLModule &operator=(const DLModule &) = delete;
|
||||||
|
|
||||||
|
~DLModule()
|
||||||
|
{
|
||||||
|
dlclose(m_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *m_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct DLSym
|
||||||
|
{
|
||||||
|
DLSym(DLModule &t_mod, const std::string &t_symbol)
|
||||||
|
: m_symbol(cast_symbol(dlsym(t_mod.m_data, t_symbol.c_str())))
|
||||||
|
{
|
||||||
|
if (!m_symbol)
|
||||||
|
{
|
||||||
|
throw chaiscript::exception::load_module_error(dlerror());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static T cast_symbol(void *p)
|
||||||
|
{
|
||||||
|
union cast_union
|
||||||
|
{
|
||||||
|
T func_ptr;
|
||||||
|
void *in_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
cast_union c;
|
||||||
|
c.in_ptr = p;
|
||||||
|
return c.func_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
T m_symbol;
|
||||||
|
};
|
||||||
|
|
||||||
|
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
|
||||||
|
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
|
||||||
|
m_moduleptr(m_func.m_symbol())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
DLModule m_dlmodule;
|
||||||
|
DLSym<Create_Module_Func> m_func;
|
||||||
|
ModulePtr m_moduleptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// and 2009-2016, Jason Turner (jason@emptycrate.com)
|
// and 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_PRELUDE_HPP_
|
#ifndef CHAISCRIPT_PRELUDE_HPP_
|
||||||
|
46
include/chaiscript/language/chaiscript_tracer.hpp
Normal file
46
include/chaiscript/language/chaiscript_tracer.hpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_TRACER_HPP_
|
||||||
|
#define CHAISCRIPT_TRACER_HPP_
|
||||||
|
|
||||||
|
namespace chaiscript {
|
||||||
|
namespace eval {
|
||||||
|
|
||||||
|
|
||||||
|
struct Noop_Tracer_Detail
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
void trace(const chaiscript::detail::Dispatch_State &, const AST_Node_Impl<T> *)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename ... T>
|
||||||
|
struct Tracer : T...
|
||||||
|
{
|
||||||
|
Tracer() = default;
|
||||||
|
explicit Tracer(T ... t)
|
||||||
|
: T(std::move(t))...
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_trace(const chaiscript::detail::Dispatch_State &ds, const AST_Node_Impl<Tracer<T...>> *node) {
|
||||||
|
(void)std::initializer_list<int>{ (static_cast<T&>(*this).trace(ds, node), 0)... };
|
||||||
|
}
|
||||||
|
|
||||||
|
static void trace(const chaiscript::detail::Dispatch_State &ds, const AST_Node_Impl<Tracer<T...>> *node) {
|
||||||
|
ds->get_parser().get_tracer<Tracer<T...>>().do_trace(ds, node);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef Tracer<Noop_Tracer_Detail> Noop_Tracer;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
31
include/chaiscript/language/chaiscript_unknown.hpp
Normal file
31
include/chaiscript/language/chaiscript_unknown.hpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_UNKNOWN_HPP_
|
||||||
|
#define CHAISCRIPT_UNKNOWN_HPP_
|
||||||
|
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
struct Loadable_Module
|
||||||
|
{
|
||||||
|
Loadable_Module(const std::string &, const std::string &)
|
||||||
|
{
|
||||||
|
#ifdef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
throw chaiscript::exception::load_module_error("Loadable module support was disabled (CHAISCRIPT_NO_DYNLOAD)");
|
||||||
|
#else
|
||||||
|
throw chaiscript::exception::load_module_error("Loadable module support not available for your platform");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
ModulePtr m_moduleptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
133
include/chaiscript/language/chaiscript_windows.hpp
Normal file
133
include/chaiscript/language/chaiscript_windows.hpp
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_WINDOWS_HPP_
|
||||||
|
#define CHAISCRIPT_WINDOWS_HPP_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_WINDOWS
|
||||||
|
#define VC_EXTRA_LEAN
|
||||||
|
#if !defined(WIN32_LEAN_AND_MEAN)
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
struct Loadable_Module
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static std::wstring to_wstring(const T &t_str)
|
||||||
|
{
|
||||||
|
return std::wstring(t_str.begin(), t_str.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::string to_string(const T &t_str)
|
||||||
|
{
|
||||||
|
return std::string(t_str.begin(), t_str.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_UNICODE) || defined(UNICODE)
|
||||||
|
template<typename T>
|
||||||
|
static std::wstring to_proper_string(const T &t_str)
|
||||||
|
{
|
||||||
|
return to_wstring(t_str);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
template<typename T>
|
||||||
|
static std::string to_proper_string(const T &t_str)
|
||||||
|
{
|
||||||
|
return to_string(t_str);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static std::string get_error_message(DWORD t_err)
|
||||||
|
{
|
||||||
|
typedef LPTSTR StringType;
|
||||||
|
|
||||||
|
#if defined(_UNICODE) || defined(UNICODE)
|
||||||
|
std::wstring retval = L"Unknown Error";
|
||||||
|
#else
|
||||||
|
std::string retval = "Unknown Error";
|
||||||
|
#endif
|
||||||
|
StringType lpMsgBuf = nullptr;
|
||||||
|
|
||||||
|
if (FormatMessage(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
nullptr,
|
||||||
|
t_err,
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
|
reinterpret_cast<StringType>(&lpMsgBuf),
|
||||||
|
0, nullptr ) != 0 && lpMsgBuf)
|
||||||
|
{
|
||||||
|
retval = lpMsgBuf;
|
||||||
|
LocalFree(lpMsgBuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return to_string(retval);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DLModule
|
||||||
|
{
|
||||||
|
explicit DLModule(const std::string &t_filename)
|
||||||
|
: m_data(LoadLibrary(to_proper_string(t_filename).c_str()))
|
||||||
|
{
|
||||||
|
if (!m_data)
|
||||||
|
{
|
||||||
|
throw chaiscript::exception::load_module_error(get_error_message(GetLastError()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DLModule(DLModule &&) = default;
|
||||||
|
DLModule &operator=(DLModule &&) = default;
|
||||||
|
DLModule(const DLModule &) = delete;
|
||||||
|
DLModule &operator=(const DLModule &) = delete;
|
||||||
|
|
||||||
|
~DLModule()
|
||||||
|
{
|
||||||
|
FreeLibrary(m_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
HMODULE m_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct DLSym
|
||||||
|
{
|
||||||
|
DLSym(DLModule &t_mod, const std::string &t_symbol)
|
||||||
|
: m_symbol(reinterpret_cast<T>(GetProcAddress(t_mod.m_data, t_symbol.c_str())))
|
||||||
|
{
|
||||||
|
if (!m_symbol)
|
||||||
|
{
|
||||||
|
throw chaiscript::exception::load_module_error(get_error_message(GetLastError()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
T m_symbol;
|
||||||
|
};
|
||||||
|
|
||||||
|
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
|
||||||
|
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
|
||||||
|
m_moduleptr(m_func.m_symbol())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
DLModule m_dlmodule;
|
||||||
|
DLSym<Create_Module_Func> m_func;
|
||||||
|
ModulePtr m_moduleptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
50
include/chaiscript/utility/fnv1a.hpp
Normal file
50
include/chaiscript/utility/fnv1a.hpp
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_UTILITY_FNV1A_HPP_
|
||||||
|
#define CHAISCRIPT_UTILITY_FNV1A_HPP_
|
||||||
|
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include "../chaiscript_defines.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
namespace utility
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
static constexpr std::uint32_t fnv1a_32(const char *s, std::uint32_t h = 0x811c9dc5) {
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4307)
|
||||||
|
#endif
|
||||||
|
return (*s == 0) ? h : fnv1a_32(s+1, ((h ^ (*s)) * 0x01000193));
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -47,6 +47,79 @@ class JSON
|
|||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
struct QuickFlatMap
|
||||||
|
{
|
||||||
|
auto find(const std::string &s) {
|
||||||
|
return std::find_if(std::begin(data), std::end(data), [&s](const auto &d) { return d.first == s; });
|
||||||
|
}
|
||||||
|
|
||||||
|
auto find(const std::string &s) const {
|
||||||
|
return std::find_if(std::begin(data), std::end(data), [&s](const auto &d) { return d.first == s; });
|
||||||
|
}
|
||||||
|
|
||||||
|
auto size() const {
|
||||||
|
return data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto begin() const {
|
||||||
|
return data.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto end() const {
|
||||||
|
return data.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auto begin() {
|
||||||
|
return data.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto end() {
|
||||||
|
return data.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
JSON &operator[](const std::string &s) {
|
||||||
|
const auto itr = find(s);
|
||||||
|
if (itr != data.end()) {
|
||||||
|
return itr->second;
|
||||||
|
} else {
|
||||||
|
data.emplace_back(s, JSON());
|
||||||
|
return data.back().second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
JSON &at(const std::string &s) {
|
||||||
|
const auto itr = find(s);
|
||||||
|
if (itr != data.end()) {
|
||||||
|
return itr->second;
|
||||||
|
} else {
|
||||||
|
throw std::out_of_range("Unknown key: " + s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const JSON &at(const std::string &s) const {
|
||||||
|
const auto itr = find(s);
|
||||||
|
if (itr != data.end()) {
|
||||||
|
return itr->second;
|
||||||
|
} else {
|
||||||
|
throw std::out_of_range("Unknown key: " + s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t count(const std::string &s) const {
|
||||||
|
return (find(s) != data.end())?1:0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::pair<std::string, JSON>> data;
|
||||||
|
|
||||||
|
using iterator = decltype(data)::iterator;
|
||||||
|
using const_iterator = decltype(data)::const_iterator;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
struct Internal {
|
struct Internal {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto clone(const std::unique_ptr<T> &ptr) {
|
auto clone(const std::unique_ptr<T> &ptr) {
|
||||||
@ -100,7 +173,7 @@ class JSON
|
|||||||
String.reset();
|
String.reset();
|
||||||
|
|
||||||
switch( type ) {
|
switch( type ) {
|
||||||
case Class::Object: Map = std::make_unique<std::map<std::string,JSON>>(); break;
|
case Class::Object: Map = std::make_unique<QuickFlatMap>(); break;
|
||||||
case Class::Array: List = std::make_unique<std::vector<JSON>>(); break;
|
case Class::Array: List = std::make_unique<std::vector<JSON>>(); break;
|
||||||
case Class::String: String = std::make_unique<std::string>(); break;
|
case Class::String: String = std::make_unique<std::string>(); break;
|
||||||
case Class::Floating: Float = 0.0; break;
|
case Class::Floating: Float = 0.0; break;
|
||||||
@ -116,7 +189,7 @@ class JSON
|
|||||||
Internal &operator=(Internal &&) = default;
|
Internal &operator=(Internal &&) = default;
|
||||||
|
|
||||||
std::unique_ptr<std::vector<JSON>> List;
|
std::unique_ptr<std::vector<JSON>> List;
|
||||||
std::unique_ptr<std::map<std::string,JSON>> Map;
|
std::unique_ptr<QuickFlatMap> Map;
|
||||||
std::unique_ptr<std::string> String;
|
std::unique_ptr<std::string> String;
|
||||||
double Float = 0;
|
double Float = 0;
|
||||||
long Int = 0;
|
long Int = 0;
|
||||||
@ -229,7 +302,7 @@ class JSON
|
|||||||
|
|
||||||
bool has_key( const std::string &key ) const {
|
bool has_key( const std::string &key ) const {
|
||||||
if( internal.Type == Class::Object ) {
|
if( internal.Type == Class::Object ) {
|
||||||
return internal.Map->find( key ) != internal.Map->end();
|
return internal.Map->count(key) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -274,11 +347,11 @@ class JSON
|
|||||||
return ok ? internal.Bool : false;
|
return ok ? internal.Bool : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONWrapper<std::map<std::string,JSON>> object_range() {
|
JSONWrapper<QuickFlatMap> object_range() {
|
||||||
if( internal.Type == Class::Object ) {
|
if( internal.Type == Class::Object ) {
|
||||||
return JSONWrapper<std::map<std::string,JSON>>( internal.Map.get() );
|
return JSONWrapper<QuickFlatMap>( internal.Map.get() );
|
||||||
} else {
|
} else {
|
||||||
return JSONWrapper<std::map<std::string,JSON>>( nullptr );
|
return JSONWrapper<QuickFlatMap>( nullptr );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,11 +363,11 @@ class JSON
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONConstWrapper<std::map<std::string,JSON>> object_range() const {
|
JSONConstWrapper<QuickFlatMap> object_range() const {
|
||||||
if( internal.Type == Class::Object ) {
|
if( internal.Type == Class::Object ) {
|
||||||
return JSONConstWrapper<std::map<std::string,JSON>>( internal.Map.get() );
|
return JSONConstWrapper<QuickFlatMap>( internal.Map.get() );
|
||||||
} else {
|
} else {
|
||||||
return JSONConstWrapper<std::map<std::string,JSON>>( nullptr );
|
return JSONConstWrapper<QuickFlatMap>( nullptr );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -313,12 +386,12 @@ class JSON
|
|||||||
return "null";
|
return "null";
|
||||||
case Class::Object: {
|
case Class::Object: {
|
||||||
std::string pad = "";
|
std::string pad = "";
|
||||||
for( long i = 0; i < depth; ++i, pad += tab );
|
for( long i = 0; i < depth; ++i, pad += tab ) { }
|
||||||
|
|
||||||
std::string s = "{\n";
|
std::string s = "{\n";
|
||||||
bool skip = true;
|
bool skip = true;
|
||||||
for( auto &p : *internal.Map ) {
|
for( auto &p : *internal.Map ) {
|
||||||
if( !skip ) s += ",\n";
|
if( !skip ) { s += ",\n"; }
|
||||||
s += ( pad + "\"" + p.first + "\" : " + p.second.dump( depth + 1, tab ) );
|
s += ( pad + "\"" + p.first + "\" : " + p.second.dump( depth + 1, tab ) );
|
||||||
skip = false;
|
skip = false;
|
||||||
}
|
}
|
||||||
@ -329,7 +402,7 @@ class JSON
|
|||||||
std::string s = "[";
|
std::string s = "[";
|
||||||
bool skip = true;
|
bool skip = true;
|
||||||
for( auto &p : *internal.List ) {
|
for( auto &p : *internal.List ) {
|
||||||
if( !skip ) s += ", ";
|
if( !skip ) { s += ", "; }
|
||||||
s += p.dump( depth + 1, tab );
|
s += p.dump( depth + 1, tab );
|
||||||
skip = false;
|
skip = false;
|
||||||
}
|
}
|
||||||
@ -353,8 +426,8 @@ class JSON
|
|||||||
private:
|
private:
|
||||||
static std::string json_escape( const std::string &str ) {
|
static std::string json_escape( const std::string &str ) {
|
||||||
std::string output;
|
std::string output;
|
||||||
for( size_t i = 0; i < str.length(); ++i )
|
for(char i : str) {
|
||||||
switch( str[i] ) {
|
switch( i ) {
|
||||||
case '\"': output += "\\\""; break;
|
case '\"': output += "\\\""; break;
|
||||||
case '\\': output += "\\\\"; break;
|
case '\\': output += "\\\\"; break;
|
||||||
case '\b': output += "\\b"; break;
|
case '\b': output += "\\b"; break;
|
||||||
@ -362,8 +435,9 @@ class JSON
|
|||||||
case '\n': output += "\\n"; break;
|
case '\n': output += "\\n"; break;
|
||||||
case '\r': output += "\\r"; break;
|
case '\r': output += "\\r"; break;
|
||||||
case '\t': output += "\\t"; break;
|
case '\t': output += "\\t"; break;
|
||||||
default : output += str[i]; break;
|
default : output += i; break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -389,7 +463,7 @@ struct JSONParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void consume_ws( const std::string &str, size_t &offset ) {
|
static void consume_ws( const std::string &str, size_t &offset ) {
|
||||||
while( isspace( str[offset] ) && offset <= str.size() ) ++offset;
|
while( isspace( str[offset] ) && offset <= str.size() ) { ++offset; }
|
||||||
}
|
}
|
||||||
|
|
||||||
static JSON parse_object( const std::string &str, size_t &offset ) {
|
static JSON parse_object( const std::string &str, size_t &offset ) {
|
||||||
@ -471,9 +545,9 @@ struct JSONParser {
|
|||||||
val += "\\u" ;
|
val += "\\u" ;
|
||||||
for( size_t i = 1; i <= 4; ++i ) {
|
for( size_t i = 1; i <= 4; ++i ) {
|
||||||
c = str[offset+i];
|
c = str[offset+i];
|
||||||
if( (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') )
|
if( (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ) {
|
||||||
val += c;
|
val += c;
|
||||||
else {
|
} else {
|
||||||
throw std::runtime_error(std::string("JSON ERROR: String: Expected hex character in unicode escape, found '") + c + "'");
|
throw std::runtime_error(std::string("JSON ERROR: String: Expected hex character in unicode escape, found '") + c + "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -481,9 +555,9 @@ struct JSONParser {
|
|||||||
} break;
|
} break;
|
||||||
default : val += '\\'; break;
|
default : val += '\\'; break;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
val += c;
|
val += c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
++offset;
|
++offset;
|
||||||
return JSON(val);
|
return JSON(val);
|
||||||
@ -493,33 +567,43 @@ struct JSONParser {
|
|||||||
std::string val, exp_str;
|
std::string val, exp_str;
|
||||||
char c = '\0';
|
char c = '\0';
|
||||||
bool isDouble = false;
|
bool isDouble = false;
|
||||||
|
bool isNegative = false;
|
||||||
long exp = 0;
|
long exp = 0;
|
||||||
|
if( offset < str.size() && str[offset] == '-' ) {
|
||||||
|
isNegative = true;
|
||||||
|
++offset;
|
||||||
|
}
|
||||||
for (; offset < str.size() ;) {
|
for (; offset < str.size() ;) {
|
||||||
c = str[offset++];
|
c = str[offset++];
|
||||||
if( (c == '-') || (c >= '0' && c <= '9') )
|
if( c >= '0' && c <= '9' ) {
|
||||||
val += c;
|
val += c;
|
||||||
else if( c == '.' ) {
|
} else if( c == '.' && !isDouble ) {
|
||||||
val += c;
|
val += c;
|
||||||
isDouble = true;
|
isDouble = true;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if( offset < str.size() && (c == 'E' || c == 'e' )) {
|
if( offset < str.size() && (c == 'E' || c == 'e' )) {
|
||||||
c = str[ offset++ ];
|
c = str[ offset++ ];
|
||||||
if( c == '-' ) { exp_str += '-';}
|
if( c == '-' ) {
|
||||||
else if( c == '+' ) { }
|
exp_str += '-';
|
||||||
else --offset;
|
} else if( c == '+' ) {
|
||||||
|
// do nothing
|
||||||
|
} else {
|
||||||
|
--offset;
|
||||||
|
}
|
||||||
|
|
||||||
for (; offset < str.size() ;) {
|
for (; offset < str.size() ;) {
|
||||||
c = str[ offset++ ];
|
c = str[ offset++ ];
|
||||||
if( c >= '0' && c <= '9' )
|
if( c >= '0' && c <= '9' ) {
|
||||||
exp_str += c;
|
exp_str += c;
|
||||||
else if( !isspace( c ) && c != ',' && c != ']' && c != '}' ) {
|
} else if( !isspace( c ) && c != ',' && c != ']' && c != '}' ) {
|
||||||
throw std::runtime_error(std::string("JSON ERROR: Number: Expected a number for exponent, found '") + c + "'");
|
throw std::runtime_error(std::string("JSON ERROR: Number: Expected a number for exponent, found '") + c + "'");
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exp = chaiscript::parse_num<long>( exp_str );
|
exp = chaiscript::parse_num<long>( exp_str );
|
||||||
}
|
}
|
||||||
@ -529,12 +613,12 @@ struct JSONParser {
|
|||||||
--offset;
|
--offset;
|
||||||
|
|
||||||
if( isDouble ) {
|
if( isDouble ) {
|
||||||
return JSON(chaiscript::parse_num<double>( val ) * std::pow( 10, exp ));
|
return JSON((isNegative?-1:1) * chaiscript::parse_num<double>( val ) * std::pow( 10, exp ));
|
||||||
} else {
|
} else {
|
||||||
if( !exp_str.empty() ) {
|
if( !exp_str.empty() ) {
|
||||||
return JSON(static_cast<double>(chaiscript::parse_num<long>( val )) * std::pow( 10, exp ));
|
return JSON((isNegative?-1:1) * static_cast<double>(chaiscript::parse_num<long>( val )) * std::pow( 10, exp ));
|
||||||
} else {
|
} else {
|
||||||
return JSON(chaiscript::parse_num<long>( val ));
|
return JSON((isNegative?-1:1) * chaiscript::parse_num<long>( val ));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -570,8 +654,9 @@ struct JSONParser {
|
|||||||
case 't' :
|
case 't' :
|
||||||
case 'f' : return parse_bool( str, offset );
|
case 'f' : return parse_bool( str, offset );
|
||||||
case 'n' : return parse_null( str, offset );
|
case 'n' : return parse_null( str, offset );
|
||||||
default : if( ( value <= '9' && value >= '0' ) || value == '-' )
|
default : if( ( value <= '9' && value >= '0' ) || value == '-' ) {
|
||||||
return parse_number( str, offset );
|
return parse_number( str, offset );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw std::runtime_error(std::string("JSON ERROR: Parse: Unexpected starting character '") + value + "'");
|
throw std::runtime_error(std::string("JSON ERROR: Parse: Unexpected starting character '") + value + "'");
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,11 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
static ModulePtr library(ModulePtr m = std::make_shared<Module>())
|
static Module& library(Module& m)
|
||||||
{
|
{
|
||||||
|
|
||||||
m->add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json");
|
m.add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json");
|
||||||
m->add(chaiscript::fun(&json_wrap::to_json), "to_json");
|
m.add(chaiscript::fun(&json_wrap::to_json), "to_json");
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
|
|
||||||
|
37
include/chaiscript/utility/static_string.hpp
Normal file
37
include/chaiscript/utility/static_string.hpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_UTILITY_STATIC_STRING_HPP_
|
||||||
|
#define CHAISCRIPT_UTILITY_STATIC_STRING_HPP_
|
||||||
|
|
||||||
|
namespace chaiscript
|
||||||
|
{
|
||||||
|
namespace utility
|
||||||
|
{
|
||||||
|
|
||||||
|
struct Static_String
|
||||||
|
{
|
||||||
|
template<size_t N>
|
||||||
|
constexpr Static_String(const char (&str)[N])
|
||||||
|
: m_size(N-1), data(&str[0])
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr size_t size() const {
|
||||||
|
return m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const char *c_str() const {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t m_size;
|
||||||
|
const char *data = nullptr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAISCRIPT_UTILITY_UTILITY_HPP_
|
#ifndef CHAISCRIPT_UTILITY_UTILITY_HPP_
|
||||||
#define CHAISCRIPT_UTILITY_UTILITY_HPP_
|
#define CHAISCRIPT_UTILITY_UTILITY_HPP_
|
||||||
|
|
||||||
@ -12,9 +16,8 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "../chaiscript.hpp"
|
#include "../language/chaiscript_common.hpp"
|
||||||
#include "../dispatchkit/proxy_functions.hpp"
|
#include "../dispatchkit/register_function.hpp"
|
||||||
#include "../dispatchkit/type_info.hpp"
|
|
||||||
#include "../dispatchkit/operators.hpp"
|
#include "../dispatchkit/operators.hpp"
|
||||||
|
|
||||||
|
|
||||||
@ -78,10 +81,9 @@ namespace chaiscript
|
|||||||
t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
|
t_module.add(chaiscript::constructor<Enum (const Enum &)>(), t_class_name);
|
||||||
|
|
||||||
using namespace chaiscript::bootstrap::operators;
|
using namespace chaiscript::bootstrap::operators;
|
||||||
t_module.add([](){
|
equal<Enum>(t_module);
|
||||||
// add some comparison and assignment operators
|
not_equal<Enum>(t_module);
|
||||||
return assign<Enum>(not_equal<Enum>(equal<Enum>()));
|
assign<Enum>(t_module);
|
||||||
}());
|
|
||||||
|
|
||||||
t_module.add(chaiscript::fun([](const Enum &e, const int &i) { return e == i; }), "==");
|
t_module.add(chaiscript::fun([](const Enum &e, const int &i) { return e == i; }), "==");
|
||||||
t_module.add(chaiscript::fun([](const int &i, const Enum &e) { return i == e; }), "==");
|
t_module.add(chaiscript::fun([](const int &i, const Enum &e) { return i == e; }), "==");
|
||||||
@ -91,6 +93,29 @@ namespace chaiscript
|
|||||||
t_module.add_global_const(chaiscript::const_var(Enum(constant.first)), constant.second);
|
t_module.add_global_const(chaiscript::const_var(Enum(constant.first)), constant.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename EnumClass, typename ModuleType>
|
||||||
|
typename std::enable_if<std::is_enum<EnumClass>::value, void>::type
|
||||||
|
add_class(ModuleType &t_module,
|
||||||
|
const std::string &t_class_name,
|
||||||
|
const std::vector<std::pair<EnumClass, std::string>> &t_constants
|
||||||
|
)
|
||||||
|
{
|
||||||
|
t_module.add(chaiscript::user_type<EnumClass>(), t_class_name);
|
||||||
|
|
||||||
|
t_module.add(chaiscript::constructor<EnumClass()>(), t_class_name);
|
||||||
|
t_module.add(chaiscript::constructor<EnumClass(const EnumClass &)>(), t_class_name);
|
||||||
|
|
||||||
|
using namespace chaiscript::bootstrap::operators;
|
||||||
|
equal<EnumClass>(t_module);
|
||||||
|
not_equal<EnumClass>(t_module);
|
||||||
|
assign<EnumClass>(t_module);
|
||||||
|
|
||||||
|
for (const auto &constant : t_constants)
|
||||||
|
{
|
||||||
|
t_module.add_global_const(chaiscript::const_var(EnumClass(constant.first)), constant.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
62
readme.md
62
readme.md
@ -17,7 +17,7 @@ ChaiScript
|
|||||||
http://www.chaiscript.com
|
http://www.chaiscript.com
|
||||||
|
|
||||||
(c) 2009-2012 Jonathan Turner
|
(c) 2009-2012 Jonathan Turner
|
||||||
(c) 2009-2016 Jason Turner
|
(c) 2009-2017 Jason Turner
|
||||||
|
|
||||||
Release under the BSD license, see "license.txt" for details.
|
Release under the BSD license, see "license.txt" for details.
|
||||||
|
|
||||||
@ -43,10 +43,9 @@ languages:
|
|||||||
Requirements
|
Requirements
|
||||||
============
|
============
|
||||||
|
|
||||||
ChaiScript requires a C++11 compiler to build with support for variadic
|
ChaiScript requires a C++14 compiler to build with support for variadic
|
||||||
templates. It has been tested with gcc 4.6 and clang 3.1 (with libcxx). MacOS
|
templates. It has been tested with gcc 4.9 and clang 3.6 (with libcxx).
|
||||||
10.8 (Mountain Lion) is also known to support the C++11 build with Apple's
|
For more information see the build
|
||||||
clang 4.0. MSVC 2013 or newer is supports also. For more information see the build
|
|
||||||
[dashboard](http://chaiscript.com/ChaiScript-BuildResults/index.html).
|
[dashboard](http://chaiscript.com/ChaiScript-BuildResults/index.html).
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
@ -80,7 +79,7 @@ directory, and for more in-depth look at the language, the unit tests in the
|
|||||||
"unittests" directory cover the most ground.
|
"unittests" directory cover the most ground.
|
||||||
|
|
||||||
For examples of how to register parts of your C++ application, see
|
For examples of how to register parts of your C++ application, see
|
||||||
"example.cpp" in the "src" directory. Example.cpp is verbose and shows every
|
"example.cpp" in the "samples" directory. Example.cpp is verbose and shows every
|
||||||
possible way of working with the library. For further documentation generate
|
possible way of working with the library. For further documentation generate
|
||||||
the doxygen documentation in the build folder or see the website
|
the doxygen documentation in the build folder or see the website
|
||||||
http://www.chaiscript.com.
|
http://www.chaiscript.com.
|
||||||
@ -88,44 +87,21 @@ http://www.chaiscript.com.
|
|||||||
|
|
||||||
The shortest complete example possible follows:
|
The shortest complete example possible follows:
|
||||||
|
|
||||||
/// main.cpp
|
```C++
|
||||||
|
/// main.cpp
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
|
|
||||||
double function(int i, double j)
|
double function(int i, double j)
|
||||||
{
|
{
|
||||||
return i * j;
|
return i * j;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
chaiscript::ChaiScript chai;
|
|
||||||
chai.add(chaiscript::fun(&function), "function");
|
|
||||||
|
|
||||||
double d = chai.eval<double>("function(3, 4.75);");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Or, if you want to compile the std lib into your code, which reduces
|
|
||||||
runtime requirements.
|
|
||||||
|
|
||||||
/// main.cpp
|
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
|
||||||
|
|
||||||
double function(int i, double j)
|
|
||||||
{
|
|
||||||
return i * j;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
|
|
||||||
chai.add(chaiscript::fun(&function), "function");
|
|
||||||
|
|
||||||
double d = chai.eval<double>("function(3, 4.75);");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript chai;
|
||||||
|
chai.add(chaiscript::fun(&function), "function");
|
||||||
|
|
||||||
|
double d = chai.eval<double>("function(3, 4.75);");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@ -1,6 +1,67 @@
|
|||||||
Notes:
|
Notes:
|
||||||
=======
|
=======
|
||||||
Current Version: 5.8.2
|
Current Version: 6.0.0
|
||||||
|
|
||||||
|
### Changes since 5.8.6
|
||||||
|
|
||||||
|
*6.0.0 is a massive rework compared to 5.x. It now requires a C++14 enabled compiler*
|
||||||
|
|
||||||
|
#### Compiler Requirements
|
||||||
|
|
||||||
|
* MSVC 2015 or greater
|
||||||
|
* g++ 4.9 or greater
|
||||||
|
* clang 3.6 or greater
|
||||||
|
|
||||||
|
#### Breaking Changes
|
||||||
|
|
||||||
|
* Instantiating a ChaiScript object now, by default, builds the stdlib in
|
||||||
|
* This was done to address the most common support issues of loading stdlib dynamically at runtime
|
||||||
|
* If you want the old behavior, use include/chaiscript/chaiscript_basic.hpp
|
||||||
|
* Headers have been reorganized to fully separate stdlib/parser/engine from each other (some faster builds)
|
||||||
|
* Bootstrap functions no longer return a reference to the module added to (compile time savings)
|
||||||
|
* It's now no longer possible modify AST_Nodes (compile time, runtime efficiency)
|
||||||
|
* Function annotations no longer exist (simplifies code, reduces compile time, compile size)
|
||||||
|
|
||||||
|
#### New Features Added
|
||||||
|
|
||||||
|
* Modular optimization system; this can be accessed via the ChaiScript_Basic interface
|
||||||
|
* Execution tracing capability; also accessed via ChaiScript_Basic interface
|
||||||
|
* range-based for loops `for( id : container ) { }` (much better performance than other loop types)
|
||||||
|
* If-init expressions (ala C++17)
|
||||||
|
* Support for passing r-value references to functions
|
||||||
|
* Support for containing unique_ptr
|
||||||
|
* Add helpers for exposing enum classes to ChaiScript
|
||||||
|
* Allow typed ChaiScript defined functions to perform conversions on call #303
|
||||||
|
|
||||||
|
#### Improvements
|
||||||
|
|
||||||
|
* Compile time improvements
|
||||||
|
* Compile size improvements
|
||||||
|
* Significant runtime improvements (see "Modular optimization system")
|
||||||
|
* Significant parser improvements, both with parse-time and parser initialization time (Thanks @niXman)
|
||||||
|
* Fix type conversion to bool in conditionals
|
||||||
|
|
||||||
|
#### Improvements Still Need To Be Made
|
||||||
|
|
||||||
|
* File location tracking has been rewritten; this currently means error location reporting is not as good as it was
|
||||||
|
* Tracing capability needs to be tested and vetted
|
||||||
|
|
||||||
|
### Changes since 5.8.5
|
||||||
|
* Optimize away `return` statements in lambdas also
|
||||||
|
* Allow conversions to bool in conditionals
|
||||||
|
* Don't allow `class` statements inside of scopes
|
||||||
|
* Properly error when a dynamic object non-function member is called
|
||||||
|
|
||||||
|
### Changes since 5.8.4
|
||||||
|
* Fix order of operations for prefix operators
|
||||||
|
* Make sure atomics are initialized properly
|
||||||
|
* Remove parsing of unused prefix `&` operator
|
||||||
|
|
||||||
|
### Changes since 5.8.3
|
||||||
|
* Fix case with some numeric conversions mixed with numerics that do not need conversion
|
||||||
|
|
||||||
|
### Changes since 5.8.2
|
||||||
|
* Add support for reference of pointer return types
|
||||||
|
|
||||||
### Changes since 5.8.1
|
### Changes since 5.8.1
|
||||||
* Allow casting to non-const & std::shared_ptr<T>
|
* Allow casting to non-const & std::shared_ptr<T>
|
||||||
|
@ -73,7 +73,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
|||||||
|
|
||||||
//Create a new system object and share it with the chaiscript engine
|
//Create a new system object and share it with the chaiscript engine
|
||||||
System system;
|
System system;
|
||||||
chai.add(var(&system), "system");
|
chai.add_global(var(&system), "system");
|
||||||
|
|
||||||
//Add a bound callback method
|
//Add a bound callback method
|
||||||
chai.add(fun(&System::add_callback, std::ref(system)), "add_callback_bound");
|
chai.add(fun(&System::add_callback, std::ref(system)), "add_callback_bound");
|
||||||
@ -108,9 +108,9 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
|||||||
// A shortcut to using eval is just to use the chai operator()
|
// A shortcut to using eval is just to use the chai operator()
|
||||||
chai("log(\"Test Module\", \"Test Message\");");
|
chai("log(\"Test Module\", \"Test Message\");");
|
||||||
|
|
||||||
//Finally, it is possible to register any std::function as a system function, in this
|
//Finally, it is possible to register a lambda as a system function, in this
|
||||||
//way, we can, for instance add a bound member function to the system
|
//way, we can, for instance add a bound member function to the system
|
||||||
chai.add(fun(&System::do_callbacks, std::ref(system), std::string("Bound Test")), "do_callbacks");
|
chai.add(fun([&system](){ return system.do_callbacks("Bound Test"); }), "do_callbacks");
|
||||||
|
|
||||||
//Call bound version of do_callbacks
|
//Call bound version of do_callbacks
|
||||||
chai("do_callbacks()");
|
chai("do_callbacks()");
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
|
||||||
|
|
||||||
class Entity
|
class Entity
|
||||||
{
|
{
|
||||||
@ -57,7 +56,7 @@ class Factory
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
|
chaiscript::ChaiScript chai;
|
||||||
|
|
||||||
chai.add(chaiscript::fun(&Entity::width), "width");
|
chai.add(chaiscript::fun(&Entity::width), "width");
|
||||||
chai.add(chaiscript::fun(&Entity::height), "height");
|
chai.add(chaiscript::fun(&Entity::height), "height");
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
@ -13,7 +17,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
|
||||||
|
|
||||||
#ifdef READLINE_AVAILABLE
|
#ifdef READLINE_AVAILABLE
|
||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
@ -65,6 +68,7 @@ std::vector<std::string> default_search_paths()
|
|||||||
{
|
{
|
||||||
std::vector<std::string> paths;
|
std::vector<std::string> paths;
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_NO_DYNLOAD
|
||||||
#ifdef CHAISCRIPT_WINDOWS // force no unicode
|
#ifdef CHAISCRIPT_WINDOWS // force no unicode
|
||||||
CHAR path[4096];
|
CHAR path[4096];
|
||||||
int size = GetModuleFileNameA(0, path, sizeof(path) - 1);
|
int size = GetModuleFileNameA(0, path, sizeof(path) - 1);
|
||||||
@ -134,6 +138,7 @@ std::vector<std::string> default_search_paths()
|
|||||||
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
|
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#endif // ifndef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
@ -291,7 +296,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
//chaiscript::ChaiScript chai(modulepaths, usepaths);
|
//chaiscript::ChaiScript chai(modulepaths, usepaths);
|
||||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library(), usepaths);
|
chaiscript::ChaiScript chai(usepaths);
|
||||||
|
|
||||||
chai.add(chaiscript::fun(&myexit), "exit");
|
chai.add(chaiscript::fun(&myexit), "exit");
|
||||||
chai.add(chaiscript::fun(&myexit), "quit");
|
chai.add(chaiscript::fun(&myexit), "quit");
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
|
||||||
|
|
||||||
class BaseClass
|
class BaseClass
|
||||||
{
|
{
|
||||||
@ -70,7 +69,7 @@ class ChaiScriptDerived : public BaseClass
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
|
chaiscript::ChaiScript chai;
|
||||||
chai.add(chaiscript::fun(&BaseClass::doSomething), "doSomething");
|
chai.add(chaiscript::fun(&BaseClass::doSomething), "doSomething");
|
||||||
chai.add(chaiscript::fun(&BaseClass::setValue), "setValue");
|
chai.add(chaiscript::fun(&BaseClass::setValue), "setValue");
|
||||||
chai.add(chaiscript::fun(&BaseClass::getValue), "getValue");
|
chai.add(chaiscript::fun(&BaseClass::getValue), "getValue");
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
|
||||||
|
|
||||||
#ifdef READLINE_AVAILABLE
|
#ifdef READLINE_AVAILABLE
|
||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
@ -32,16 +31,9 @@ void function(void)
|
|||||||
class test
|
class test
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript chai;
|
||||||
chaiscript::ChaiScript::State backupState;
|
chaiscript::ChaiScript::State backupState = chai.get_state();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
test()
|
|
||||||
: chai(chaiscript::Std_Lib::library())
|
|
||||||
{
|
|
||||||
backupState = chai.get_state();
|
|
||||||
}
|
|
||||||
~test(){}
|
|
||||||
|
|
||||||
void ResetState()
|
void ResetState()
|
||||||
{
|
{
|
||||||
chai.set_state(backupState);
|
chai.set_state(backupState);
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
|
||||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||||
#include <chaiscript/dispatchkit/function_call.hpp>
|
#include <chaiscript/dispatchkit/function_call.hpp>
|
||||||
|
|
||||||
int main( int /*argc*/ , char * /*argv*/[] )
|
int main( int /*argc*/ , char * /*argv*/[] )
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript ch( chaiscript::Std_Lib::library( ) );
|
chaiscript::ChaiScript ch;
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
|
39
src/main.cpp
39
src/main.cpp
@ -1,9 +1,13 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
@ -12,7 +16,10 @@
|
|||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
#include "../static_libs/chaiscript_parser.hpp"
|
||||||
|
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||||
|
|
||||||
|
|
||||||
#ifdef READLINE_AVAILABLE
|
#ifdef READLINE_AVAILABLE
|
||||||
#include <readline/readline.h>
|
#include <readline/readline.h>
|
||||||
@ -22,7 +29,7 @@
|
|||||||
char *mystrdup (const char *s) {
|
char *mystrdup (const char *s) {
|
||||||
size_t len = strlen(s); // Space for length plus nul
|
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
|
if (d == nullptr) { return nullptr; } // No memory
|
||||||
#ifdef CHAISCRIPT_MSVC
|
#ifdef CHAISCRIPT_MSVC
|
||||||
strcpy_s(d, len+1, s); // Copy the characters
|
strcpy_s(d, len+1, s); // Copy the characters
|
||||||
#else
|
#else
|
||||||
@ -41,7 +48,7 @@ char* readline(const char* p)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void add_history(const char*){}
|
void add_history(const char* /*unused*/){}
|
||||||
void using_history(){}
|
void using_history(){}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -64,6 +71,7 @@ std::vector<std::string> default_search_paths()
|
|||||||
{
|
{
|
||||||
std::vector<std::string> paths;
|
std::vector<std::string> paths;
|
||||||
|
|
||||||
|
#ifndef CHAISCRIPT_NO_DYNLOAD
|
||||||
#ifdef CHAISCRIPT_WINDOWS // force no unicode
|
#ifdef CHAISCRIPT_WINDOWS // force no unicode
|
||||||
CHAR path[4096];
|
CHAR path[4096];
|
||||||
int size = GetModuleFileNameA(nullptr, path, sizeof(path)-1);
|
int size = GetModuleFileNameA(nullptr, path, sizeof(path)-1);
|
||||||
@ -113,7 +121,7 @@ std::vector<std::string> default_search_paths()
|
|||||||
{
|
{
|
||||||
Dl_info rInfo;
|
Dl_info rInfo;
|
||||||
memset( &rInfo, 0, sizeof(rInfo) );
|
memset( &rInfo, 0, sizeof(rInfo) );
|
||||||
if ( !dladdr(cast_module_symbol(&default_search_paths), &rInfo) || !rInfo.dli_fname ) {
|
if ( dladdr(cast_module_symbol(&default_search_paths), &rInfo) == 0 || rInfo.dli_fname == nullptr ) {
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,7 +133,7 @@ std::vector<std::string> default_search_paths()
|
|||||||
size_t secondtolastslash = exepath.rfind('/', lastslash - 1);
|
size_t secondtolastslash = exepath.rfind('/', lastslash - 1);
|
||||||
if (lastslash != std::string::npos)
|
if (lastslash != std::string::npos)
|
||||||
{
|
{
|
||||||
paths.push_back(exepath.substr(0, lastslash));
|
paths.push_back(exepath.substr(0, lastslash+1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (secondtolastslash != std::string::npos)
|
if (secondtolastslash != std::string::npos)
|
||||||
@ -133,6 +141,7 @@ std::vector<std::string> default_search_paths()
|
|||||||
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
|
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#endif // ifndef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
@ -181,7 +190,7 @@ std::string get_next_command() {
|
|||||||
std::string retval("quit");
|
std::string retval("quit");
|
||||||
if ( ! std::cin.eof() ) {
|
if ( ! std::cin.eof() ) {
|
||||||
char *input_raw = readline("eval> ");
|
char *input_raw = readline("eval> ");
|
||||||
if ( input_raw ) {
|
if ( input_raw != nullptr ) {
|
||||||
add_history(input_raw);
|
add_history(input_raw);
|
||||||
|
|
||||||
std::string val(input_raw);
|
std::string val(input_raw);
|
||||||
@ -217,7 +226,7 @@ void myexit(int return_val) {
|
|||||||
exit(return_val);
|
exit(return_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
void interactive(chaiscript::ChaiScript& chai)
|
void interactive(chaiscript::ChaiScript_Basic& chai)
|
||||||
{
|
{
|
||||||
using_history();
|
using_history();
|
||||||
|
|
||||||
@ -237,7 +246,7 @@ void interactive(chaiscript::ChaiScript& chai)
|
|||||||
}
|
}
|
||||||
catch (const chaiscript::exception::eval_error &ee) {
|
catch (const chaiscript::exception::eval_error &ee) {
|
||||||
std::cout << ee.what();
|
std::cout << ee.what();
|
||||||
if (ee.call_stack.size() > 0) {
|
if ( !ee.call_stack.empty() ) {
|
||||||
std::cout << "during evaluation at (" << ee.call_stack[0]->start().line << ", " << ee.call_stack[0]->start().column << ")";
|
std::cout << "during evaluation at (" << ee.call_stack[0]->start().line << ", " << ee.call_stack[0]->start().column << ")";
|
||||||
}
|
}
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
@ -274,7 +283,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
std::vector<std::string> usepaths;
|
std::vector<std::string> usepaths;
|
||||||
usepaths.push_back("");
|
usepaths.push_back("");
|
||||||
if (usepath)
|
if (usepath != nullptr)
|
||||||
{
|
{
|
||||||
usepaths.push_back(usepath);
|
usepaths.push_back(usepath);
|
||||||
}
|
}
|
||||||
@ -283,12 +292,12 @@ int main(int argc, char *argv[])
|
|||||||
std::vector<std::string> searchpaths = default_search_paths();
|
std::vector<std::string> searchpaths = default_search_paths();
|
||||||
modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
|
modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
|
||||||
modulepaths.push_back("");
|
modulepaths.push_back("");
|
||||||
if (modulepath)
|
if (modulepath != nullptr)
|
||||||
{
|
{
|
||||||
modulepaths.push_back(modulepath);
|
modulepaths.push_back(modulepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
chaiscript::ChaiScript chai(modulepaths,usepaths);
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(),modulepaths,usepaths);
|
||||||
|
|
||||||
chai.add(chaiscript::fun(&myexit), "exit");
|
chai.add(chaiscript::fun(&myexit), "exit");
|
||||||
chai.add(chaiscript::fun(&myexit), "quit");
|
chai.add(chaiscript::fun(&myexit), "quit");
|
||||||
@ -305,7 +314,7 @@ int main(int argc, char *argv[])
|
|||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string arg( i ? argv[i] : "--interactive" );
|
std::string arg( i != 0 ? argv[i] : "--interactive" );
|
||||||
|
|
||||||
enum { eInteractive
|
enum { eInteractive
|
||||||
, eCommand
|
, eCommand
|
||||||
@ -316,9 +325,9 @@ int main(int argc, char *argv[])
|
|||||||
if ( (i+1) >= argc ) {
|
if ( (i+1) >= argc ) {
|
||||||
std::cout << "insufficient input following " << arg << '\n';
|
std::cout << "insufficient input following " << arg << '\n';
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} else {
|
|
||||||
arg = argv[++i];
|
|
||||||
}
|
}
|
||||||
|
arg = argv[++i];
|
||||||
|
|
||||||
} else if ( arg == "-" || arg == "--stdin" ) {
|
} else if ( arg == "-" || arg == "--stdin" ) {
|
||||||
arg = "" ;
|
arg = "" ;
|
||||||
std::string line;
|
std::string line;
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
// This file is distributed under the BSD License.
|
// This file is distributed under the BSD License.
|
||||||
// See "license.txt" for details.
|
// See "license.txt" for details.
|
||||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
|
||||||
// http://www.chaiscript.com
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
||||||
|
|
||||||
|
|
||||||
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -23,9 +27,9 @@
|
|||||||
|
|
||||||
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra()
|
CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra()
|
||||||
{
|
{
|
||||||
|
auto module = std::make_shared<chaiscript::Module>();
|
||||||
auto module = chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List");
|
chaiscript::bootstrap::standard_library::list_type<std::list<chaiscript::Boxed_Value> >("List", *module);
|
||||||
module->add(chaiscript::bootstrap::standard_library::vector_type<std::vector<uint16_t> >("u16vector"));
|
chaiscript::bootstrap::standard_library::vector_type<std::vector<uint16_t> >("u16vector", *module);
|
||||||
module->add(chaiscript::vector_conversion<std::vector<uint16_t>>());
|
module->add(chaiscript::vector_conversion<std::vector<uint16_t>>());
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
#include <chaiscript/dispatchkit/bootstrap.hpp>
|
#include <chaiscript/dispatchkit/bootstrap.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -189,9 +189,9 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo
|
|||||||
m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val");
|
m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val");
|
||||||
|
|
||||||
m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray");
|
m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray");
|
||||||
m->add(chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5"));
|
chaiscript::bootstrap::array<int[2][3][5]>("IntArray_2_3_5", *m);
|
||||||
m->add(chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5"));
|
chaiscript::bootstrap::array<int[3][5]>("IntArray_3_5", *m);
|
||||||
m->add(chaiscript::bootstrap::array<int[5]>("IntArray_5"));
|
chaiscript::bootstrap::array<int[5]>("IntArray_5", *m);
|
||||||
|
|
||||||
// member that is a function
|
// member that is a function
|
||||||
m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");
|
m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");
|
||||||
|
8
static_libs/chaiscript_parser.cpp
Normal file
8
static_libs/chaiscript_parser.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "../include/chaiscript/language/chaiscript_parser.hpp"
|
||||||
|
#include "chaiscript_parser.hpp"
|
||||||
|
|
||||||
|
std::unique_ptr<chaiscript::parser::ChaiScript_Parser_Base> create_chaiscript_parser()
|
||||||
|
{
|
||||||
|
return std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>();
|
||||||
|
}
|
||||||
|
|
13
static_libs/chaiscript_parser.hpp
Normal file
13
static_libs/chaiscript_parser.hpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
#ifndef CHAISCRIPT_PARSER_LIB
|
||||||
|
#define CHAISCRIPT_PARSER_LIB
|
||||||
|
|
||||||
|
namespace chaiscript {
|
||||||
|
namespace parser {
|
||||||
|
class ChaiScript_Parser_Base;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<chaiscript::parser::ChaiScript_Parser_Base> create_chaiscript_parser();
|
||||||
|
|
||||||
|
#endif
|
8
static_libs/chaiscript_stdlib.cpp
Normal file
8
static_libs/chaiscript_stdlib.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include "../include/chaiscript/chaiscript_stdlib.hpp"
|
||||||
|
#include "chaiscript_stdlib.hpp"
|
||||||
|
|
||||||
|
std::shared_ptr<chaiscript::Module> create_chaiscript_stdlib()
|
||||||
|
{
|
||||||
|
return chaiscript::Std_Lib::library();
|
||||||
|
}
|
||||||
|
|
11
static_libs/chaiscript_stdlib.hpp
Normal file
11
static_libs/chaiscript_stdlib.hpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
#ifndef CHAISCRIPT_STDLIB
|
||||||
|
#define CHAISCRIPT_STDLIB
|
||||||
|
|
||||||
|
namespace chaiscript {
|
||||||
|
class Module;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<chaiscript::Module> create_chaiscript_stdlib();
|
||||||
|
|
||||||
|
#endif
|
@ -1,7 +1,6 @@
|
|||||||
assert_equal(`==`, `==`);
|
assert_equal(`==`, `==`);
|
||||||
assert_not_equal(`==`, `<`);
|
assert_not_equal(`==`, `<`);
|
||||||
assert_equal(`<`.get_arity(), 2);
|
assert_equal(`<`.get_arity(), 2);
|
||||||
assert_equal(`+`.get_annotation(), "Multiple method dispatch function wrapper.");
|
|
||||||
assert_equal(get_arity.get_contained_functions().size(), 0);
|
assert_equal(get_arity.get_contained_functions().size(), 0);
|
||||||
assert_equal(get_arity.get_arity(), 1);
|
assert_equal(get_arity.get_arity(), 1);
|
||||||
assert_equal(get_arity.get_param_types().size(), 2);
|
assert_equal(get_arity.get_param_types().size(), 2);
|
||||||
|
@ -35,4 +35,4 @@ def while_doing()
|
|||||||
|
|
||||||
var f = fun() { while_doing(); }
|
var f = fun() { while_doing(); }
|
||||||
|
|
||||||
assert_equal(get_eval_error(f).call_stack.size(), 16)
|
assert_true(get_eval_error(f).call_stack.size() <= 16)
|
||||||
|
@ -10,7 +10,6 @@ def test_function(a)
|
|||||||
|
|
||||||
// test_function tests
|
// test_function tests
|
||||||
assert_equal(test_function.get_arity(), 1);
|
assert_equal(test_function.get_arity(), 1);
|
||||||
assert_equal(trim(test_function.get_annotation()), "#Test Function Description");
|
|
||||||
assert_equal(test_function.get_contained_functions().size(), 0);
|
assert_equal(test_function.get_contained_functions().size(), 0);
|
||||||
assert_equal(test_function.get_param_types().size(), 2);
|
assert_equal(test_function.get_param_types().size(), 2);
|
||||||
|
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
var parser := ChaiScript_Parser()
|
|
||||||
var parse_success = parser.parse("3 + 4", "INPUT")
|
|
||||||
var a := parser.ast()
|
|
||||||
|
|
||||||
assert_equal(eval(a), 7)
|
|
||||||
|
|
||||||
var childs := a.children.front().children
|
|
||||||
var node := childs[0]
|
|
||||||
|
|
||||||
var parser2 := ChaiScript_Parser()
|
|
||||||
parser2.parse("9", "INPUT")
|
|
||||||
|
|
||||||
|
|
||||||
a.children.front().replace_child(childs[0], parser2.ast())
|
|
||||||
|
|
||||||
assert_equal(eval(a), 13)
|
|
||||||
assert_equal(node.filename, "INPUT")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def my_fun()
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
assert_equal(true, my_fun.has_parse_tree());
|
|
||||||
assert_equal(false, `+`.has_parse_tree());
|
|
||||||
|
|
||||||
assert_throws("Function does not have a parse tree", fun() { `+`.get_parse_tree(); } );
|
|
||||||
|
|
||||||
var parsetree := my_fun.get_parse_tree();
|
|
||||||
|
|
||||||
assert_equal(1, eval(parsetree));
|
|
||||||
|
|
||||||
print(parsetree.text());
|
|
@ -1,4 +1,6 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
#include "../static_libs/chaiscript_parser.hpp"
|
||||||
|
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||||
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
@ -11,8 +13,7 @@ extern "C"
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chaiscript::ChaiScript chai;
|
|
||||||
chai.add(chaiscript::fun(&do_something), "do_something");
|
chai.add(chaiscript::fun(&do_something), "do_something");
|
||||||
|
|
||||||
return chai.eval<int>("do_something(101)") == 101 % 2?EXIT_SUCCESS:EXIT_FAILURE;
|
return chai.eval<int>("do_something(101)") == 101 % 2?EXIT_SUCCESS:EXIT_FAILURE;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Catch v1.3.5
|
* Catch v1.5.7
|
||||||
* Generated: 2016-02-29 08:16:42.342094
|
* Generated: 2016-09-27 10:45:46.824849
|
||||||
* ----------------------------------------------------------
|
* ----------------------------------------------------------
|
||||||
* This file has been merged from multiple headers. Please don't edit it directly
|
* This file has been merged from multiple headers. Please don't edit it directly
|
||||||
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
|
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
|
||||||
@ -62,7 +62,11 @@
|
|||||||
|
|
||||||
#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line
|
#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line
|
||||||
#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
|
#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
|
||||||
#define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ )
|
#ifdef CATCH_CONFIG_COUNTER
|
||||||
|
# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ )
|
||||||
|
#else
|
||||||
|
# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ )
|
||||||
|
#endif
|
||||||
|
|
||||||
#define INTERNAL_CATCH_STRINGIFY2( expr ) #expr
|
#define INTERNAL_CATCH_STRINGIFY2( expr ) #expr
|
||||||
#define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
|
#define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr )
|
||||||
@ -89,7 +93,7 @@
|
|||||||
// CATCH_CONFIG_CPP11_OR_GREATER : Is C++11 supported?
|
// CATCH_CONFIG_CPP11_OR_GREATER : Is C++11 supported?
|
||||||
|
|
||||||
// CATCH_CONFIG_VARIADIC_MACROS : are variadic macros supported?
|
// CATCH_CONFIG_VARIADIC_MACROS : are variadic macros supported?
|
||||||
|
// CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported?
|
||||||
// ****************
|
// ****************
|
||||||
// Note to maintainers: if new toggles are added please document them
|
// Note to maintainers: if new toggles are added please document them
|
||||||
// in configuration.md, too
|
// in configuration.md, too
|
||||||
@ -102,8 +106,16 @@
|
|||||||
|
|
||||||
// All the C++11 features can be disabled with CATCH_CONFIG_NO_CPP11
|
// All the C++11 features can be disabled with CATCH_CONFIG_NO_CPP11
|
||||||
|
|
||||||
#if defined(__cplusplus) && __cplusplus >= 201103L
|
#ifdef __cplusplus
|
||||||
# define CATCH_CPP11_OR_GREATER
|
|
||||||
|
# if __cplusplus >= 201103L
|
||||||
|
# define CATCH_CPP11_OR_GREATER
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# if __cplusplus >= 201402L
|
||||||
|
# define CATCH_CPP14_OR_GREATER
|
||||||
|
# endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __clang__
|
#ifdef __clang__
|
||||||
@ -185,6 +197,15 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Use __COUNTER__ if the compiler supports it
|
||||||
|
#if ( defined _MSC_VER && _MSC_VER >= 1300 ) || \
|
||||||
|
( defined __GNUC__ && __GNUC__ >= 4 && __GNUC_MINOR__ >= 3 ) || \
|
||||||
|
( defined __clang__ && __clang_major__ >= 3 )
|
||||||
|
|
||||||
|
#define CATCH_INTERNAL_CONFIG_COUNTER
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// C++ language feature support
|
// C++ language feature support
|
||||||
|
|
||||||
@ -256,6 +277,9 @@
|
|||||||
#if defined(CATCH_INTERNAL_CONFIG_CPP11_UNIQUE_PTR) && !defined(CATCH_CONFIG_NO_UNIQUE_PTR) && !defined(CATCH_CONFIG_CPP11_UNIQUE_PTR) && !defined(CATCH_CONFIG_NO_CPP11)
|
#if defined(CATCH_INTERNAL_CONFIG_CPP11_UNIQUE_PTR) && !defined(CATCH_CONFIG_NO_UNIQUE_PTR) && !defined(CATCH_CONFIG_CPP11_UNIQUE_PTR) && !defined(CATCH_CONFIG_NO_CPP11)
|
||||||
# define CATCH_CONFIG_CPP11_UNIQUE_PTR
|
# define CATCH_CONFIG_CPP11_UNIQUE_PTR
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER)
|
||||||
|
# define CATCH_CONFIG_COUNTER
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS)
|
#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS)
|
||||||
# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS
|
# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS
|
||||||
@ -686,24 +710,28 @@ void registerTestCaseFunction
|
|||||||
|
|
||||||
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
#ifdef CATCH_CONFIG_VARIADIC_MACROS
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
#define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \
|
||||||
|
static void TestName(); \
|
||||||
|
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &TestName, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); }\
|
||||||
|
static void TestName()
|
||||||
#define INTERNAL_CATCH_TESTCASE( ... ) \
|
#define INTERNAL_CATCH_TESTCASE( ... ) \
|
||||||
static void INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )(); \
|
INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), __VA_ARGS__ )
|
||||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); }\
|
|
||||||
static void INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )()
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \
|
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \
|
||||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); }
|
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); }
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, ... )\
|
#define INTERNAL_CATCH_TEST_CASE_METHOD2( TestName, ClassName, ... )\
|
||||||
namespace{ \
|
namespace{ \
|
||||||
struct INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ) : ClassName{ \
|
struct TestName : ClassName{ \
|
||||||
void test(); \
|
void test(); \
|
||||||
}; \
|
}; \
|
||||||
Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )::test, #ClassName, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); \
|
Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &TestName::test, #ClassName, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); \
|
||||||
} \
|
} \
|
||||||
void INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )::test()
|
void TestName::test()
|
||||||
|
#define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, ... ) \
|
||||||
|
INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, __VA_ARGS__ )
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \
|
#define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \
|
||||||
@ -711,24 +739,28 @@ void registerTestCaseFunction
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
#define INTERNAL_CATCH_TESTCASE2( TestName, Name, Desc ) \
|
||||||
|
static void TestName(); \
|
||||||
|
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &TestName, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( Name, Desc ) ); }\
|
||||||
|
static void TestName()
|
||||||
#define INTERNAL_CATCH_TESTCASE( Name, Desc ) \
|
#define INTERNAL_CATCH_TESTCASE( Name, Desc ) \
|
||||||
static void INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )(); \
|
INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), Name, Desc )
|
||||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( Name, Desc ) ); }\
|
|
||||||
static void INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )()
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
|
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
|
||||||
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( Name, Desc ), CATCH_INTERNAL_LINEINFO ); }
|
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( Name, Desc ), CATCH_INTERNAL_LINEINFO ); }
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, TestName, Desc )\
|
#define INTERNAL_CATCH_TEST_CASE_METHOD2( TestCaseName, ClassName, TestName, Desc )\
|
||||||
namespace{ \
|
namespace{ \
|
||||||
struct INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ) : ClassName{ \
|
struct TestCaseName : ClassName{ \
|
||||||
void test(); \
|
void test(); \
|
||||||
}; \
|
}; \
|
||||||
Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )::test, #ClassName, Catch::NameAndDesc( TestName, Desc ), CATCH_INTERNAL_LINEINFO ); \
|
Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &TestCaseName::test, #ClassName, Catch::NameAndDesc( TestName, Desc ), CATCH_INTERNAL_LINEINFO ); \
|
||||||
} \
|
} \
|
||||||
void INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ )::test()
|
void TestCaseName::test()
|
||||||
|
#define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, TestName, Desc )\
|
||||||
|
INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, TestName, Desc )
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_REGISTER_TESTCASE( Function, Name, Desc ) \
|
#define INTERNAL_CATCH_REGISTER_TESTCASE( Function, Name, Desc ) \
|
||||||
@ -2041,7 +2073,7 @@ namespace Catch {
|
|||||||
__catchResult.useActiveException( Catch::ResultDisposition::Normal ); \
|
__catchResult.useActiveException( Catch::ResultDisposition::Normal ); \
|
||||||
} \
|
} \
|
||||||
INTERNAL_CATCH_REACT( __catchResult ) \
|
INTERNAL_CATCH_REACT( __catchResult ) \
|
||||||
} while( Catch::isTrue( false && static_cast<bool>(expr) ) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look
|
} while( Catch::isTrue( false && !!(expr) ) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_IF( expr, resultDisposition, macroName ) \
|
#define INTERNAL_CATCH_IF( expr, resultDisposition, macroName ) \
|
||||||
@ -2578,10 +2610,12 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) \
|
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \
|
||||||
static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature ); \
|
static std::string translatorName( signature ); \
|
||||||
namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ) ); }\
|
namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); }\
|
||||||
static std::string INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator )( signature )
|
static std::string translatorName( signature )
|
||||||
|
|
||||||
|
#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature )
|
||||||
|
|
||||||
// #included from: internal/catch_approx.hpp
|
// #included from: internal/catch_approx.hpp
|
||||||
#define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
|
#define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED
|
||||||
@ -3189,10 +3223,11 @@ namespace Catch {
|
|||||||
|
|
||||||
bool matches( TestCaseInfo const& testCase ) const {
|
bool matches( TestCaseInfo const& testCase ) const {
|
||||||
// All patterns in a filter must match for the filter to be a match
|
// All patterns in a filter must match for the filter to be a match
|
||||||
for( std::vector<Ptr<Pattern> >::const_iterator it = m_patterns.begin(), itEnd = m_patterns.end(); it != itEnd; ++it )
|
for( std::vector<Ptr<Pattern> >::const_iterator it = m_patterns.begin(), itEnd = m_patterns.end(); it != itEnd; ++it ) {
|
||||||
if( !(*it)->matches( testCase ) )
|
if( !(*it)->matches( testCase ) )
|
||||||
return false;
|
return false;
|
||||||
return true;
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -3424,7 +3459,7 @@ namespace Catch {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class DebugOutStream : public IStream {
|
class DebugOutStream : public IStream {
|
||||||
std::auto_ptr<StreamBufBase> m_streamBuf;
|
CATCH_AUTO_PTR( StreamBufBase ) m_streamBuf;
|
||||||
mutable std::ostream m_os;
|
mutable std::ostream m_os;
|
||||||
public:
|
public:
|
||||||
DebugOutStream();
|
DebugOutStream();
|
||||||
@ -3572,7 +3607,7 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
ConfigData m_data;
|
ConfigData m_data;
|
||||||
|
|
||||||
std::auto_ptr<IStream const> m_stream;
|
CATCH_AUTO_PTR( IStream const ) m_stream;
|
||||||
TestSpec m_testSpec;
|
TestSpec m_testSpec;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -3592,7 +3627,7 @@ namespace Catch {
|
|||||||
#define STITCH_CLARA_OPEN_NAMESPACE namespace Catch {
|
#define STITCH_CLARA_OPEN_NAMESPACE namespace Catch {
|
||||||
// #included from: ../external/clara.h
|
// #included from: ../external/clara.h
|
||||||
|
|
||||||
// Version 0.0.1.1
|
// Version 0.0.2.4
|
||||||
|
|
||||||
// Only use header guard if we are not using an outer namespace
|
// Only use header guard if we are not using an outer namespace
|
||||||
#if !defined(TWOBLUECUBES_CLARA_H_INCLUDED) || defined(STITCH_CLARA_OPEN_NAMESPACE)
|
#if !defined(TWOBLUECUBES_CLARA_H_INCLUDED) || defined(STITCH_CLARA_OPEN_NAMESPACE)
|
||||||
@ -3908,6 +3943,10 @@ namespace Tbc {
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER)
|
||||||
|
#define CLARA_PLATFORM_WINDOWS
|
||||||
|
#endif
|
||||||
|
|
||||||
// Use optional outer namespace
|
// Use optional outer namespace
|
||||||
#ifdef STITCH_CLARA_OPEN_NAMESPACE
|
#ifdef STITCH_CLARA_OPEN_NAMESPACE
|
||||||
STITCH_CLARA_OPEN_NAMESPACE
|
STITCH_CLARA_OPEN_NAMESPACE
|
||||||
@ -3931,9 +3970,6 @@ namespace Clara {
|
|||||||
const unsigned int consoleWidth = 80;
|
const unsigned int consoleWidth = 80;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Use this to try and stop compiler from warning about unreachable code
|
|
||||||
inline bool isTrue( bool value ) { return value; }
|
|
||||||
|
|
||||||
using namespace Tbc;
|
using namespace Tbc;
|
||||||
|
|
||||||
inline bool startsWith( std::string const& str, std::string const& prefix ) {
|
inline bool startsWith( std::string const& str, std::string const& prefix ) {
|
||||||
@ -3969,14 +4005,6 @@ namespace Clara {
|
|||||||
else
|
else
|
||||||
throw std::runtime_error( "Expected a boolean value but did not recognise:\n '" + _source + "'" );
|
throw std::runtime_error( "Expected a boolean value but did not recognise:\n '" + _source + "'" );
|
||||||
}
|
}
|
||||||
inline void convertInto( bool _source, bool& _dest ) {
|
|
||||||
_dest = _source;
|
|
||||||
}
|
|
||||||
template<typename T>
|
|
||||||
inline void convertInto( bool, T& ) {
|
|
||||||
if( isTrue( true ) )
|
|
||||||
throw std::runtime_error( "Invalid conversion" );
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ConfigT>
|
template<typename ConfigT>
|
||||||
struct IArgFunction {
|
struct IArgFunction {
|
||||||
@ -3986,7 +4014,6 @@ namespace Clara {
|
|||||||
IArgFunction( IArgFunction const& ) = default;
|
IArgFunction( IArgFunction const& ) = default;
|
||||||
#endif
|
#endif
|
||||||
virtual void set( ConfigT& config, std::string const& value ) const = 0;
|
virtual void set( ConfigT& config, std::string const& value ) const = 0;
|
||||||
virtual void setFlag( ConfigT& config ) const = 0;
|
|
||||||
virtual bool takesArg() const = 0;
|
virtual bool takesArg() const = 0;
|
||||||
virtual IArgFunction* clone() const = 0;
|
virtual IArgFunction* clone() const = 0;
|
||||||
};
|
};
|
||||||
@ -4008,9 +4035,6 @@ namespace Clara {
|
|||||||
void set( ConfigT& config, std::string const& value ) const {
|
void set( ConfigT& config, std::string const& value ) const {
|
||||||
functionObj->set( config, value );
|
functionObj->set( config, value );
|
||||||
}
|
}
|
||||||
void setFlag( ConfigT& config ) const {
|
|
||||||
functionObj->setFlag( config );
|
|
||||||
}
|
|
||||||
bool takesArg() const { return functionObj->takesArg(); }
|
bool takesArg() const { return functionObj->takesArg(); }
|
||||||
|
|
||||||
bool isSet() const {
|
bool isSet() const {
|
||||||
@ -4023,7 +4047,6 @@ namespace Clara {
|
|||||||
template<typename C>
|
template<typename C>
|
||||||
struct NullBinder : IArgFunction<C>{
|
struct NullBinder : IArgFunction<C>{
|
||||||
virtual void set( C&, std::string const& ) const {}
|
virtual void set( C&, std::string const& ) const {}
|
||||||
virtual void setFlag( C& ) const {}
|
|
||||||
virtual bool takesArg() const { return true; }
|
virtual bool takesArg() const { return true; }
|
||||||
virtual IArgFunction<C>* clone() const { return new NullBinder( *this ); }
|
virtual IArgFunction<C>* clone() const { return new NullBinder( *this ); }
|
||||||
};
|
};
|
||||||
@ -4034,9 +4057,6 @@ namespace Clara {
|
|||||||
virtual void set( C& p, std::string const& stringValue ) const {
|
virtual void set( C& p, std::string const& stringValue ) const {
|
||||||
convertInto( stringValue, p.*member );
|
convertInto( stringValue, p.*member );
|
||||||
}
|
}
|
||||||
virtual void setFlag( C& p ) const {
|
|
||||||
convertInto( true, p.*member );
|
|
||||||
}
|
|
||||||
virtual bool takesArg() const { return !IsBool<M>::value; }
|
virtual bool takesArg() const { return !IsBool<M>::value; }
|
||||||
virtual IArgFunction<C>* clone() const { return new BoundDataMember( *this ); }
|
virtual IArgFunction<C>* clone() const { return new BoundDataMember( *this ); }
|
||||||
M C::* member;
|
M C::* member;
|
||||||
@ -4049,11 +4069,6 @@ namespace Clara {
|
|||||||
convertInto( stringValue, value );
|
convertInto( stringValue, value );
|
||||||
(p.*member)( value );
|
(p.*member)( value );
|
||||||
}
|
}
|
||||||
virtual void setFlag( C& p ) const {
|
|
||||||
typename RemoveConstRef<M>::type value;
|
|
||||||
convertInto( true, value );
|
|
||||||
(p.*member)( value );
|
|
||||||
}
|
|
||||||
virtual bool takesArg() const { return !IsBool<M>::value; }
|
virtual bool takesArg() const { return !IsBool<M>::value; }
|
||||||
virtual IArgFunction<C>* clone() const { return new BoundUnaryMethod( *this ); }
|
virtual IArgFunction<C>* clone() const { return new BoundUnaryMethod( *this ); }
|
||||||
void (C::*member)( M );
|
void (C::*member)( M );
|
||||||
@ -4067,9 +4082,6 @@ namespace Clara {
|
|||||||
if( value )
|
if( value )
|
||||||
(p.*member)();
|
(p.*member)();
|
||||||
}
|
}
|
||||||
virtual void setFlag( C& p ) const {
|
|
||||||
(p.*member)();
|
|
||||||
}
|
|
||||||
virtual bool takesArg() const { return false; }
|
virtual bool takesArg() const { return false; }
|
||||||
virtual IArgFunction<C>* clone() const { return new BoundNullaryMethod( *this ); }
|
virtual IArgFunction<C>* clone() const { return new BoundNullaryMethod( *this ); }
|
||||||
void (C::*member)();
|
void (C::*member)();
|
||||||
@ -4084,9 +4096,6 @@ namespace Clara {
|
|||||||
if( value )
|
if( value )
|
||||||
function( obj );
|
function( obj );
|
||||||
}
|
}
|
||||||
virtual void setFlag( C& p ) const {
|
|
||||||
function( p );
|
|
||||||
}
|
|
||||||
virtual bool takesArg() const { return false; }
|
virtual bool takesArg() const { return false; }
|
||||||
virtual IArgFunction<C>* clone() const { return new BoundUnaryFunction( *this ); }
|
virtual IArgFunction<C>* clone() const { return new BoundUnaryFunction( *this ); }
|
||||||
void (*function)( C& );
|
void (*function)( C& );
|
||||||
@ -4100,11 +4109,6 @@ namespace Clara {
|
|||||||
convertInto( stringValue, value );
|
convertInto( stringValue, value );
|
||||||
function( obj, value );
|
function( obj, value );
|
||||||
}
|
}
|
||||||
virtual void setFlag( C& obj ) const {
|
|
||||||
typename RemoveConstRef<T>::type value;
|
|
||||||
convertInto( true, value );
|
|
||||||
function( obj, value );
|
|
||||||
}
|
|
||||||
virtual bool takesArg() const { return !IsBool<T>::value; }
|
virtual bool takesArg() const { return !IsBool<T>::value; }
|
||||||
virtual IArgFunction<C>* clone() const { return new BoundBinaryFunction( *this ); }
|
virtual IArgFunction<C>* clone() const { return new BoundBinaryFunction( *this ); }
|
||||||
void (*function)( C&, T );
|
void (*function)( C&, T );
|
||||||
@ -4112,8 +4116,20 @@ namespace Clara {
|
|||||||
|
|
||||||
} // namespace Detail
|
} // namespace Detail
|
||||||
|
|
||||||
struct Parser {
|
inline std::vector<std::string> argsToVector( int argc, char const* const* const argv ) {
|
||||||
Parser() : separators( " \t=:" ) {}
|
std::vector<std::string> args( static_cast<std::size_t>( argc ) );
|
||||||
|
for( std::size_t i = 0; i < static_cast<std::size_t>( argc ); ++i )
|
||||||
|
args[i] = argv[i];
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Parser {
|
||||||
|
enum Mode { None, MaybeShortOpt, SlashOpt, ShortOpt, LongOpt, Positional };
|
||||||
|
Mode mode;
|
||||||
|
std::size_t from;
|
||||||
|
bool inQuotes;
|
||||||
|
public:
|
||||||
|
|
||||||
struct Token {
|
struct Token {
|
||||||
enum Type { Positional, ShortOpt, LongOpt };
|
enum Type { Positional, ShortOpt, LongOpt };
|
||||||
@ -4122,38 +4138,75 @@ namespace Clara {
|
|||||||
std::string data;
|
std::string data;
|
||||||
};
|
};
|
||||||
|
|
||||||
void parseIntoTokens( int argc, char const* const argv[], std::vector<Parser::Token>& tokens ) const {
|
Parser() : mode( None ), from( 0 ), inQuotes( false ){}
|
||||||
|
|
||||||
|
void parseIntoTokens( std::vector<std::string> const& args, std::vector<Token>& tokens ) {
|
||||||
const std::string doubleDash = "--";
|
const std::string doubleDash = "--";
|
||||||
for( int i = 1; i < argc && argv[i] != doubleDash; ++i )
|
for( std::size_t i = 1; i < args.size() && args[i] != doubleDash; ++i )
|
||||||
parseIntoTokens( argv[i] , tokens);
|
parseIntoTokens( args[i], tokens);
|
||||||
}
|
}
|
||||||
void parseIntoTokens( std::string arg, std::vector<Parser::Token>& tokens ) const {
|
|
||||||
while( !arg.empty() ) {
|
void parseIntoTokens( std::string const& arg, std::vector<Token>& tokens ) {
|
||||||
Parser::Token token( Parser::Token::Positional, arg );
|
for( std::size_t i = 0; i <= arg.size(); ++i ) {
|
||||||
arg = "";
|
char c = arg[i];
|
||||||
if( token.data[0] == '-' ) {
|
if( c == '"' )
|
||||||
if( token.data.size() > 1 && token.data[1] == '-' ) {
|
inQuotes = !inQuotes;
|
||||||
token = Parser::Token( Parser::Token::LongOpt, token.data.substr( 2 ) );
|
mode = handleMode( i, c, arg, tokens );
|
||||||
}
|
|
||||||
else {
|
|
||||||
token = Parser::Token( Parser::Token::ShortOpt, token.data.substr( 1 ) );
|
|
||||||
if( token.data.size() > 1 && separators.find( token.data[1] ) == std::string::npos ) {
|
|
||||||
arg = "-" + token.data.substr( 1 );
|
|
||||||
token.data = token.data.substr( 0, 1 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( token.type != Parser::Token::Positional ) {
|
|
||||||
std::size_t pos = token.data.find_first_of( separators );
|
|
||||||
if( pos != std::string::npos ) {
|
|
||||||
arg = token.data.substr( pos+1 );
|
|
||||||
token.data = token.data.substr( 0, pos );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tokens.push_back( token );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string separators;
|
Mode handleMode( std::size_t i, char c, std::string const& arg, std::vector<Token>& tokens ) {
|
||||||
|
switch( mode ) {
|
||||||
|
case None: return handleNone( i, c );
|
||||||
|
case MaybeShortOpt: return handleMaybeShortOpt( i, c );
|
||||||
|
case ShortOpt:
|
||||||
|
case LongOpt:
|
||||||
|
case SlashOpt: return handleOpt( i, c, arg, tokens );
|
||||||
|
case Positional: return handlePositional( i, c, arg, tokens );
|
||||||
|
default: throw std::logic_error( "Unknown mode" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Mode handleNone( std::size_t i, char c ) {
|
||||||
|
if( inQuotes ) {
|
||||||
|
from = i;
|
||||||
|
return Positional;
|
||||||
|
}
|
||||||
|
switch( c ) {
|
||||||
|
case '-': return MaybeShortOpt;
|
||||||
|
#ifdef CLARA_PLATFORM_WINDOWS
|
||||||
|
case '/': from = i+1; return SlashOpt;
|
||||||
|
#endif
|
||||||
|
default: from = i; return Positional;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode handleMaybeShortOpt( std::size_t i, char c ) {
|
||||||
|
switch( c ) {
|
||||||
|
case '-': from = i+1; return LongOpt;
|
||||||
|
default: from = i; return ShortOpt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode handleOpt( std::size_t i, char c, std::string const& arg, std::vector<Token>& tokens ) {
|
||||||
|
if( std::string( ":=\0", 3 ).find( c ) == std::string::npos )
|
||||||
|
return mode;
|
||||||
|
|
||||||
|
std::string optName = arg.substr( from, i-from );
|
||||||
|
if( mode == ShortOpt )
|
||||||
|
for( std::size_t j = 0; j < optName.size(); ++j )
|
||||||
|
tokens.push_back( Token( Token::ShortOpt, optName.substr( j, 1 ) ) );
|
||||||
|
else if( mode == SlashOpt && optName.size() == 1 )
|
||||||
|
tokens.push_back( Token( Token::ShortOpt, optName ) );
|
||||||
|
else
|
||||||
|
tokens.push_back( Token( Token::LongOpt, optName ) );
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Mode handlePositional( std::size_t i, char c, std::string const& arg, std::vector<Token>& tokens ) {
|
||||||
|
if( inQuotes || std::string( "\0", 1 ).find( c ) == std::string::npos )
|
||||||
|
return mode;
|
||||||
|
|
||||||
|
std::string data = arg.substr( from, i-from );
|
||||||
|
tokens.push_back( Token( Token::Positional, data ) );
|
||||||
|
return None;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ConfigT>
|
template<typename ConfigT>
|
||||||
@ -4456,21 +4509,21 @@ namespace Clara {
|
|||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigT parse( int argc, char const* const argv[] ) const {
|
ConfigT parse( std::vector<std::string> const& args ) const {
|
||||||
ConfigT config;
|
ConfigT config;
|
||||||
parseInto( argc, argv, config );
|
parseInto( args, config );
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Parser::Token> parseInto( int argc, char const* argv[], ConfigT& config ) const {
|
std::vector<Parser::Token> parseInto( std::vector<std::string> const& args, ConfigT& config ) const {
|
||||||
std::string processName = argv[0];
|
std::string processName = args[0];
|
||||||
std::size_t lastSlash = processName.find_last_of( "/\\" );
|
std::size_t lastSlash = processName.find_last_of( "/\\" );
|
||||||
if( lastSlash != std::string::npos )
|
if( lastSlash != std::string::npos )
|
||||||
processName = processName.substr( lastSlash+1 );
|
processName = processName.substr( lastSlash+1 );
|
||||||
m_boundProcessName.set( config, processName );
|
m_boundProcessName.set( config, processName );
|
||||||
std::vector<Parser::Token> tokens;
|
std::vector<Parser::Token> tokens;
|
||||||
Parser parser;
|
Parser parser;
|
||||||
parser.parseIntoTokens( argc, argv, tokens );
|
parser.parseIntoTokens( args, tokens );
|
||||||
return populate( tokens, config );
|
return populate( tokens, config );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4501,7 +4554,7 @@ namespace Clara {
|
|||||||
arg.boundField.set( config, tokens[++i].data );
|
arg.boundField.set( config, tokens[++i].data );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
arg.boundField.setFlag( config );
|
arg.boundField.set( config, "true" );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -4667,8 +4720,11 @@ namespace Catch {
|
|||||||
std::string line;
|
std::string line;
|
||||||
while( std::getline( f, line ) ) {
|
while( std::getline( f, line ) ) {
|
||||||
line = trim(line);
|
line = trim(line);
|
||||||
if( !line.empty() && !startsWith( line, "#" ) )
|
if( !line.empty() && !startsWith( line, "#" ) ) {
|
||||||
addTestOrTags( config, "\"" + line + "\"," );
|
if( !startsWith( line, "\"" ) )
|
||||||
|
line = "\"" + line + "\"";
|
||||||
|
addTestOrTags( config, line + "," );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5209,6 +5265,8 @@ namespace Catch
|
|||||||
bool aborting;
|
bool aborting;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class MultipleReporters;
|
||||||
|
|
||||||
struct IStreamingReporter : IShared {
|
struct IStreamingReporter : IShared {
|
||||||
virtual ~IStreamingReporter();
|
virtual ~IStreamingReporter();
|
||||||
|
|
||||||
@ -5236,6 +5294,8 @@ namespace Catch
|
|||||||
virtual void testRunEnded( TestRunStats const& testRunStats ) = 0;
|
virtual void testRunEnded( TestRunStats const& testRunStats ) = 0;
|
||||||
|
|
||||||
virtual void skipTest( TestCaseInfo const& testInfo ) = 0;
|
virtual void skipTest( TestCaseInfo const& testInfo ) = 0;
|
||||||
|
|
||||||
|
virtual MultipleReporters* tryAsMulti() { return CATCH_NULL; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct IReporterFactory : IShared {
|
struct IReporterFactory : IShared {
|
||||||
@ -5312,7 +5372,10 @@ namespace Catch {
|
|||||||
++it ) {
|
++it ) {
|
||||||
matchedTests++;
|
matchedTests++;
|
||||||
TestCaseInfo const& testCaseInfo = it->getTestCaseInfo();
|
TestCaseInfo const& testCaseInfo = it->getTestCaseInfo();
|
||||||
Catch::cout() << testCaseInfo.name << std::endl;
|
if( startsWith( testCaseInfo.name, "#" ) )
|
||||||
|
Catch::cout() << "\"" << testCaseInfo.name << "\"" << std::endl;
|
||||||
|
else
|
||||||
|
Catch::cout() << testCaseInfo.name << std::endl;
|
||||||
}
|
}
|
||||||
return matchedTests;
|
return matchedTests;
|
||||||
}
|
}
|
||||||
@ -5453,6 +5516,10 @@ namespace TestCaseTracking {
|
|||||||
virtual void addChild( Ptr<ITracker> const& child ) = 0;
|
virtual void addChild( Ptr<ITracker> const& child ) = 0;
|
||||||
virtual ITracker* findChild( std::string const& name ) = 0;
|
virtual ITracker* findChild( std::string const& name ) = 0;
|
||||||
virtual void openChild() = 0;
|
virtual void openChild() = 0;
|
||||||
|
|
||||||
|
// Debug/ checking
|
||||||
|
virtual bool isSectionTracker() const = 0;
|
||||||
|
virtual bool isIndexTracker() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TrackerContext {
|
class TrackerContext {
|
||||||
@ -5577,6 +5644,10 @@ namespace TestCaseTracking {
|
|||||||
m_parent->openChild();
|
m_parent->openChild();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual bool isSectionTracker() const CATCH_OVERRIDE { return false; }
|
||||||
|
virtual bool isIndexTracker() const CATCH_OVERRIDE { return false; }
|
||||||
|
|
||||||
void open() {
|
void open() {
|
||||||
m_runState = Executing;
|
m_runState = Executing;
|
||||||
moveToThis();
|
moveToThis();
|
||||||
@ -5640,13 +5711,16 @@ namespace TestCaseTracking {
|
|||||||
{}
|
{}
|
||||||
virtual ~SectionTracker();
|
virtual ~SectionTracker();
|
||||||
|
|
||||||
|
virtual bool isSectionTracker() const CATCH_OVERRIDE { return true; }
|
||||||
|
|
||||||
static SectionTracker& acquire( TrackerContext& ctx, std::string const& name ) {
|
static SectionTracker& acquire( TrackerContext& ctx, std::string const& name ) {
|
||||||
SectionTracker* section = CATCH_NULL;
|
SectionTracker* section = CATCH_NULL;
|
||||||
|
|
||||||
ITracker& currentTracker = ctx.currentTracker();
|
ITracker& currentTracker = ctx.currentTracker();
|
||||||
if( ITracker* childTracker = currentTracker.findChild( name ) ) {
|
if( ITracker* childTracker = currentTracker.findChild( name ) ) {
|
||||||
section = dynamic_cast<SectionTracker*>( childTracker );
|
assert( childTracker );
|
||||||
assert( section );
|
assert( childTracker->isSectionTracker() );
|
||||||
|
section = static_cast<SectionTracker*>( childTracker );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
section = new SectionTracker( name, ctx, ¤tTracker );
|
section = new SectionTracker( name, ctx, ¤tTracker );
|
||||||
@ -5671,13 +5745,16 @@ namespace TestCaseTracking {
|
|||||||
{}
|
{}
|
||||||
virtual ~IndexTracker();
|
virtual ~IndexTracker();
|
||||||
|
|
||||||
|
virtual bool isIndexTracker() const CATCH_OVERRIDE { return true; }
|
||||||
|
|
||||||
static IndexTracker& acquire( TrackerContext& ctx, std::string const& name, int size ) {
|
static IndexTracker& acquire( TrackerContext& ctx, std::string const& name, int size ) {
|
||||||
IndexTracker* tracker = CATCH_NULL;
|
IndexTracker* tracker = CATCH_NULL;
|
||||||
|
|
||||||
ITracker& currentTracker = ctx.currentTracker();
|
ITracker& currentTracker = ctx.currentTracker();
|
||||||
if( ITracker* childTracker = currentTracker.findChild( name ) ) {
|
if( ITracker* childTracker = currentTracker.findChild( name ) ) {
|
||||||
tracker = dynamic_cast<IndexTracker*>( childTracker );
|
assert( childTracker );
|
||||||
assert( tracker );
|
assert( childTracker->isIndexTracker() );
|
||||||
|
tracker = static_cast<IndexTracker*>( childTracker );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
tracker = new IndexTracker( name, ctx, ¤tTracker, size );
|
tracker = new IndexTracker( name, ctx, ¤tTracker, size );
|
||||||
@ -5884,6 +5961,11 @@ namespace Catch {
|
|||||||
while( getCurrentContext().advanceGeneratorsForCurrentTest() && !aborting() );
|
while( getCurrentContext().advanceGeneratorsForCurrentTest() && !aborting() );
|
||||||
|
|
||||||
Totals deltaTotals = m_totals.delta( prevTotals );
|
Totals deltaTotals = m_totals.delta( prevTotals );
|
||||||
|
if( testInfo.expectedToFail() && deltaTotals.testCases.passed > 0 ) {
|
||||||
|
deltaTotals.assertions.failed++;
|
||||||
|
deltaTotals.testCases.passed--;
|
||||||
|
deltaTotals.testCases.failed++;
|
||||||
|
}
|
||||||
m_totals.testCases += deltaTotals.testCases;
|
m_totals.testCases += deltaTotals.testCases;
|
||||||
m_reporter->testCaseEnded( TestCaseStats( testInfo,
|
m_reporter->testCaseEnded( TestCaseStats( testInfo,
|
||||||
deltaTotals,
|
deltaTotals,
|
||||||
@ -6275,10 +6357,10 @@ namespace Catch {
|
|||||||
Catch::cout() << "For more detail usage please see the project docs\n" << std::endl;
|
Catch::cout() << "For more detail usage please see the project docs\n" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int applyCommandLine( int argc, char const* argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) {
|
int applyCommandLine( int argc, char const* const* const argv, OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) {
|
||||||
try {
|
try {
|
||||||
m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail );
|
m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail );
|
||||||
m_unusedTokens = m_cli.parseInto( argc, argv, m_configData );
|
m_unusedTokens = m_cli.parseInto( Clara::argsToVector( argc, argv ), m_configData );
|
||||||
if( m_configData.showHelp )
|
if( m_configData.showHelp )
|
||||||
showHelp( m_configData.processName );
|
showHelp( m_configData.processName );
|
||||||
m_config.reset();
|
m_config.reset();
|
||||||
@ -6302,16 +6384,13 @@ namespace Catch {
|
|||||||
m_config.reset();
|
m_config.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
int run( int argc, char const* argv[] ) {
|
int run( int argc, char const* const* const argv ) {
|
||||||
|
|
||||||
int returnCode = applyCommandLine( argc, argv );
|
int returnCode = applyCommandLine( argc, argv );
|
||||||
if( returnCode == 0 )
|
if( returnCode == 0 )
|
||||||
returnCode = run();
|
returnCode = run();
|
||||||
return returnCode;
|
return returnCode;
|
||||||
}
|
}
|
||||||
int run( int argc, char* argv[] ) {
|
|
||||||
return run( argc, const_cast<char const**>( argv ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
int run() {
|
int run() {
|
||||||
if( m_configData.showHelp )
|
if( m_configData.showHelp )
|
||||||
@ -6375,13 +6454,31 @@ namespace Catch {
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#ifdef CATCH_CPP14_OR_GREATER
|
||||||
|
#include <random>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
struct LexSort {
|
|
||||||
bool operator() (TestCase i,TestCase j) const { return (i<j);}
|
|
||||||
};
|
|
||||||
struct RandomNumberGenerator {
|
struct RandomNumberGenerator {
|
||||||
int operator()( int n ) const { return std::rand() % n; }
|
typedef int result_type;
|
||||||
|
|
||||||
|
result_type operator()( result_type n ) const { return std::rand() % n; }
|
||||||
|
|
||||||
|
#ifdef CATCH_CPP14_OR_GREATER
|
||||||
|
static constexpr result_type min() { return 0; }
|
||||||
|
static constexpr result_type max() { return 1000000; }
|
||||||
|
result_type operator()() const { return std::rand() % max(); }
|
||||||
|
#endif
|
||||||
|
template<typename V>
|
||||||
|
static void shuffle( V& vector ) {
|
||||||
|
RandomNumberGenerator rng;
|
||||||
|
#ifdef CATCH_CPP14_OR_GREATER
|
||||||
|
std::shuffle( vector.begin(), vector.end(), rng );
|
||||||
|
#else
|
||||||
|
std::random_shuffle( vector.begin(), vector.end(), rng );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) {
|
inline std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) {
|
||||||
@ -6390,14 +6487,12 @@ namespace Catch {
|
|||||||
|
|
||||||
switch( config.runOrder() ) {
|
switch( config.runOrder() ) {
|
||||||
case RunTests::InLexicographicalOrder:
|
case RunTests::InLexicographicalOrder:
|
||||||
std::sort( sorted.begin(), sorted.end(), LexSort() );
|
std::sort( sorted.begin(), sorted.end() );
|
||||||
break;
|
break;
|
||||||
case RunTests::InRandomOrder:
|
case RunTests::InRandomOrder:
|
||||||
{
|
{
|
||||||
seedRng( config );
|
seedRng( config );
|
||||||
|
RandomNumberGenerator::shuffle( sorted );
|
||||||
RandomNumberGenerator rng;
|
|
||||||
std::random_shuffle( sorted.begin(), sorted.end(), rng );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case RunTests::InDeclarationOrder:
|
case RunTests::InDeclarationOrder:
|
||||||
@ -6416,13 +6511,15 @@ namespace Catch {
|
|||||||
it != itEnd;
|
it != itEnd;
|
||||||
++it ) {
|
++it ) {
|
||||||
std::pair<std::set<TestCase>::const_iterator, bool> prev = seenFunctions.insert( *it );
|
std::pair<std::set<TestCase>::const_iterator, bool> prev = seenFunctions.insert( *it );
|
||||||
if( !prev.second ){
|
if( !prev.second ) {
|
||||||
Catch::cerr()
|
std::ostringstream ss;
|
||||||
<< Colour( Colour::Red )
|
|
||||||
<< "error: TEST_CASE( \"" << it->name << "\" ) already defined.\n"
|
ss << Colour( Colour::Red )
|
||||||
<< "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << "\n"
|
<< "error: TEST_CASE( \"" << it->name << "\" ) already defined.\n"
|
||||||
<< "\tRedefined at " << it->getTestCaseInfo().lineInfo << std::endl;
|
<< "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << "\n"
|
||||||
exit(1);
|
<< "\tRedefined at " << it->getTestCaseInfo().lineInfo << std::endl;
|
||||||
|
|
||||||
|
throw std::runtime_error(ss.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -7481,7 +7578,7 @@ namespace Catch {
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
Version libraryVersion( 1, 3, 5, "", 0 );
|
Version libraryVersion( 1, 5, 7, "", 0 );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -8460,13 +8557,18 @@ public: // IStreamingReporter
|
|||||||
++it )
|
++it )
|
||||||
(*it)->skipTest( testInfo );
|
(*it)->skipTest( testInfo );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual MultipleReporters* tryAsMulti() CATCH_OVERRIDE {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Ptr<IStreamingReporter> addReporter( Ptr<IStreamingReporter> const& existingReporter, Ptr<IStreamingReporter> const& additionalReporter ) {
|
Ptr<IStreamingReporter> addReporter( Ptr<IStreamingReporter> const& existingReporter, Ptr<IStreamingReporter> const& additionalReporter ) {
|
||||||
Ptr<IStreamingReporter> resultingReporter;
|
Ptr<IStreamingReporter> resultingReporter;
|
||||||
|
|
||||||
if( existingReporter ) {
|
if( existingReporter ) {
|
||||||
MultipleReporters* multi = dynamic_cast<MultipleReporters*>( existingReporter.get() );
|
MultipleReporters* multi = existingReporter->tryAsMulti();
|
||||||
if( !multi ) {
|
if( !multi ) {
|
||||||
multi = new MultipleReporters;
|
multi = new MultipleReporters;
|
||||||
resultingReporter = Ptr<IStreamingReporter>( multi );
|
resultingReporter = Ptr<IStreamingReporter>( multi );
|
||||||
@ -8646,7 +8748,7 @@ namespace Catch {
|
|||||||
|
|
||||||
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE {}
|
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE {}
|
||||||
|
|
||||||
virtual bool assertionEnded( AssertionStats const& assertionStats ) {
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE {
|
||||||
assert( !m_sectionStack.empty() );
|
assert( !m_sectionStack.empty() );
|
||||||
SectionNode& sectionNode = *m_sectionStack.back();
|
SectionNode& sectionNode = *m_sectionStack.back();
|
||||||
sectionNode.assertions.push_back( assertionStats );
|
sectionNode.assertions.push_back( assertionStats );
|
||||||
@ -8856,9 +8958,10 @@ namespace Catch {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Escape control chars - based on contribution by @espenalb in PR #465
|
// Escape control chars - based on contribution by @espenalb in PR #465 and
|
||||||
|
// by @mrpi PR #588
|
||||||
if ( ( c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' )
|
if ( ( c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' )
|
||||||
os << "&#x" << std::uppercase << std::hex << static_cast<int>( c );
|
os << "&#x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>( c ) << ';';
|
||||||
else
|
else
|
||||||
os << c;
|
os << c;
|
||||||
}
|
}
|
||||||
@ -8913,13 +9016,20 @@ namespace Catch {
|
|||||||
: m_tagIsOpen( false ),
|
: m_tagIsOpen( false ),
|
||||||
m_needsNewline( false ),
|
m_needsNewline( false ),
|
||||||
m_os( &Catch::cout() )
|
m_os( &Catch::cout() )
|
||||||
{}
|
{
|
||||||
|
// We encode control characters, which requires
|
||||||
|
// XML 1.1
|
||||||
|
// see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0
|
||||||
|
*m_os << "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n";
|
||||||
|
}
|
||||||
|
|
||||||
XmlWriter( std::ostream& os )
|
XmlWriter( std::ostream& os )
|
||||||
: m_tagIsOpen( false ),
|
: m_tagIsOpen( false ),
|
||||||
m_needsNewline( false ),
|
m_needsNewline( false ),
|
||||||
m_os( &os )
|
m_os( &os )
|
||||||
{}
|
{
|
||||||
|
*m_os << "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n";
|
||||||
|
}
|
||||||
|
|
||||||
~XmlWriter() {
|
~XmlWriter() {
|
||||||
while( !m_tags.empty() )
|
while( !m_tags.empty() )
|
||||||
@ -9086,7 +9196,7 @@ namespace Catch {
|
|||||||
|
|
||||||
virtual void testCaseStarting( TestCaseInfo const& testInfo ) CATCH_OVERRIDE {
|
virtual void testCaseStarting( TestCaseInfo const& testInfo ) CATCH_OVERRIDE {
|
||||||
StreamingReporterBase::testCaseStarting(testInfo);
|
StreamingReporterBase::testCaseStarting(testInfo);
|
||||||
m_xml.startElement( "TestCase" ).writeAttribute( "name", trim( testInfo.name ) );
|
m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.name );
|
||||||
|
|
||||||
if ( m_config->showDurations() == ShowDurations::Always )
|
if ( m_config->showDurations() == ShowDurations::Always )
|
||||||
m_testCaseTimer.start();
|
m_testCaseTimer.start();
|
||||||
@ -9777,7 +9887,7 @@ namespace Catch {
|
|||||||
if( totals.testCases.total() == 0 ) {
|
if( totals.testCases.total() == 0 ) {
|
||||||
stream << Colour( Colour::Warning ) << "No tests ran\n";
|
stream << Colour( Colour::Warning ) << "No tests ran\n";
|
||||||
}
|
}
|
||||||
else if( totals.assertions.total() > 0 && totals.assertions.allPassed() ) {
|
else if( totals.assertions.total() > 0 && totals.testCases.allPassed() ) {
|
||||||
stream << Colour( Colour::ResultSuccess ) << "All tests passed";
|
stream << Colour( Colour::ResultSuccess ) << "All tests passed";
|
||||||
stream << " ("
|
stream << " ("
|
||||||
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
|
<< pluralise( totals.assertions.passed, "assertion" ) << " in "
|
||||||
|
8
unittests/class_inside_scope.chai
Normal file
8
unittests/class_inside_scope.chai
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
parse("{ class C { var data; def C() {} } }")
|
||||||
|
assert_false(true)
|
||||||
|
} catch (e) {
|
||||||
|
assert_true(true)
|
||||||
|
}
|
@ -16,9 +16,12 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
#include <chaiscript/utility/utility.hpp>
|
#include <chaiscript/utility/utility.hpp>
|
||||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||||
|
|
||||||
|
#include "../static_libs/chaiscript_parser.hpp"
|
||||||
|
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -34,7 +37,7 @@ TEST_CASE("C++11 Lambdas Can Be Registered")
|
|||||||
|
|
||||||
// We cannot deduce the type of a lambda expression, you must either wrap it
|
// We cannot deduce the type of a lambda expression, you must either wrap it
|
||||||
// in an std::function or provide the signature
|
// in an std::function or provide the signature
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
chai.add(chaiscript::fun([]()->std::string { return "hello"; } ), "f1");
|
chai.add(chaiscript::fun([]()->std::string { return "hello"; } ), "f1");
|
||||||
|
|
||||||
@ -49,7 +52,7 @@ TEST_CASE("C++11 Lambdas Can Be Registered")
|
|||||||
// dynamic_object tests
|
// dynamic_object tests
|
||||||
TEST_CASE("Dynamic_Object attributes can be shared with C++")
|
TEST_CASE("Dynamic_Object attributes can be shared with C++")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
chai("attr bob::z; def bob::bob() { this.z = 10 }; auto x = bob()");
|
chai("attr bob::z; def bob::bob() { this.z = 10 }; auto x = bob()");
|
||||||
|
|
||||||
@ -77,7 +80,7 @@ TEST_CASE("Dynamic_Object attributes can be shared with C++")
|
|||||||
TEST_CASE("Function objects can be created from chaiscript functions")
|
TEST_CASE("Function objects can be created from chaiscript functions")
|
||||||
{
|
{
|
||||||
|
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
chai.eval("def func() { print(\"Hello World\"); } ");
|
chai.eval("def func() { print(\"Hello World\"); } ");
|
||||||
|
|
||||||
@ -91,7 +94,7 @@ TEST_CASE("Function objects can be created from chaiscript functions")
|
|||||||
|
|
||||||
TEST_CASE("ChaiScript can be created and destroyed on heap")
|
TEST_CASE("ChaiScript can be created and destroyed on heap")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript *chai = new chaiscript::ChaiScript();
|
auto *chai = new chaiscript::ChaiScript_Basic(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
delete chai;
|
delete chai;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +126,7 @@ void arithmetic_conversions_f_func_return(const std::function<unsigned int (unsi
|
|||||||
|
|
||||||
TEST_CASE("Test automatic arithmetic conversions")
|
TEST_CASE("Test automatic arithmetic conversions")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f1");
|
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f1");
|
||||||
chai.add(chaiscript::fun(&arithmetic_conversions_f2), "f2");
|
chai.add(chaiscript::fun(&arithmetic_conversions_f2), "f2");
|
||||||
@ -164,7 +167,7 @@ TEST_CASE("Test automatic arithmetic conversions")
|
|||||||
|
|
||||||
TEST_CASE("Generic exception handling with C++")
|
TEST_CASE("Generic exception handling with C++")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
chai.eval("throw(runtime_error(\"error\"));");
|
chai.eval("throw(runtime_error(\"error\"));");
|
||||||
@ -177,7 +180,7 @@ TEST_CASE("Generic exception handling with C++")
|
|||||||
|
|
||||||
TEST_CASE("Throw an int")
|
TEST_CASE("Throw an int")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
chai.eval("throw(1)", chaiscript::exception_specification<int>());
|
chai.eval("throw(1)", chaiscript::exception_specification<int>());
|
||||||
@ -189,19 +192,19 @@ TEST_CASE("Throw an int")
|
|||||||
|
|
||||||
TEST_CASE("Throw int or double")
|
TEST_CASE("Throw int or double")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
chai.eval("throw(1.0)", chaiscript::exception_specification<int, double>());
|
chai.eval("throw(1.0)", chaiscript::exception_specification<int, double>());
|
||||||
REQUIRE(false);
|
REQUIRE(false);
|
||||||
} catch (const double e) {
|
} catch (const double e) {
|
||||||
CHECK(e == 1.0);
|
CHECK(e == Approx(1.0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Throw a runtime_error")
|
TEST_CASE("Throw a runtime_error")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
|
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
|
||||||
@ -221,7 +224,7 @@ TEST_CASE("Throw a runtime_error")
|
|||||||
|
|
||||||
TEST_CASE("Throw unhandled type")
|
TEST_CASE("Throw unhandled type")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
|
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
|
||||||
@ -250,7 +253,7 @@ int expected_eval_errors_test_one(const int &)
|
|||||||
|
|
||||||
TEST_CASE("No unexpected exceptions leak")
|
TEST_CASE("No unexpected exceptions leak")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::fun(&expected_eval_errors_test_one), "test_fun");
|
chai.add(chaiscript::fun(&expected_eval_errors_test_one), "test_fun");
|
||||||
|
|
||||||
chai.eval("def guard_fun(i) : i.get_type_info().is_type_arithmetic() {} ");
|
chai.eval("def guard_fun(i) : i.get_type_info().is_type_arithmetic() {} ");
|
||||||
@ -313,7 +316,7 @@ int function_ordering_test_two(int &)
|
|||||||
|
|
||||||
TEST_CASE("Function ordering")
|
TEST_CASE("Function ordering")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.eval("def test_fun(x) { return 3; }");
|
chai.eval("def test_fun(x) { return 3; }");
|
||||||
chai.eval("def test_fun(x) : x == \"hi\" { return 4; }");
|
chai.eval("def test_fun(x) : x == \"hi\" { return 4; }");
|
||||||
// chai.eval("def test_fun(x) { return 5; }");
|
// chai.eval("def test_fun(x) { return 5; }");
|
||||||
@ -337,7 +340,7 @@ int functor_cast_test_call(const std::function<int (int)> &f, int val)
|
|||||||
TEST_CASE("Functor cast")
|
TEST_CASE("Functor cast")
|
||||||
{
|
{
|
||||||
|
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
chai.add(chaiscript::fun(&functor_cast_test_call), "test_call");
|
chai.add(chaiscript::fun(&functor_cast_test_call), "test_call");
|
||||||
|
|
||||||
@ -356,10 +359,10 @@ int set_state_test_myfun()
|
|||||||
|
|
||||||
TEST_CASE("Set and restore chai state")
|
TEST_CASE("Set and restore chai state")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
// save the initial state of globals and locals
|
// save the initial state of globals and locals
|
||||||
chaiscript::ChaiScript::State firststate = chai.get_state();
|
auto firststate = chai.get_state();
|
||||||
std::map<std::string, chaiscript::Boxed_Value> locals = chai.get_locals();
|
std::map<std::string, chaiscript::Boxed_Value> locals = chai.get_locals();
|
||||||
|
|
||||||
// add some new globals and locals
|
// add some new globals and locals
|
||||||
@ -402,7 +405,7 @@ class Short_Comparison_Test {
|
|||||||
|
|
||||||
TEST_CASE("Short comparison with int")
|
TEST_CASE("Short comparison with int")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::user_type<Short_Comparison_Test>(), "Test");
|
chai.add(chaiscript::user_type<Short_Comparison_Test>(), "Test");
|
||||||
chai.add(chaiscript::constructor<Short_Comparison_Test()>(), "Test");
|
chai.add(chaiscript::constructor<Short_Comparison_Test()>(), "Test");
|
||||||
|
|
||||||
@ -425,7 +428,7 @@ class Type_Name_MyClass
|
|||||||
|
|
||||||
TEST_CASE("Test lookup of type names")
|
TEST_CASE("Test lookup of type names")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
auto type = chaiscript::user_type<Type_Name_MyClass>();
|
auto type = chaiscript::user_type<Type_Name_MyClass>();
|
||||||
chai.add(type, "MyClass");
|
chai.add(type, "MyClass");
|
||||||
|
|
||||||
@ -451,13 +454,13 @@ int simultaneous_chaiscript_do_something_else(int i)
|
|||||||
|
|
||||||
TEST_CASE("Simultaneous ChaiScript tests")
|
TEST_CASE("Simultaneous ChaiScript tests")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::fun(&simultaneous_chaiscript_do_something), "do_something");
|
chai.add(chaiscript::fun(&simultaneous_chaiscript_do_something), "do_something");
|
||||||
chai.add(chaiscript::var(1), "i");
|
chai.add(chaiscript::var(1), "i");
|
||||||
|
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai2;
|
chaiscript::ChaiScript_Basic chai2(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai2.add(chaiscript::fun(&simultaneous_chaiscript_do_something_else), "do_something_else");
|
chai2.add(chaiscript::fun(&simultaneous_chaiscript_do_something_else), "do_something_else");
|
||||||
|
|
||||||
CHECK(chai.eval<int>("do_something(" + std::to_string(i) + ")") == i + 2);
|
CHECK(chai.eval<int>("do_something(" + std::to_string(i) + ")") == i + 2);
|
||||||
@ -506,7 +509,7 @@ TEST_CASE("Utility_Test utility class wrapper")
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(m);
|
chai.add(m);
|
||||||
|
|
||||||
CHECK(chai.eval<std::string>("auto t = Utility_Test(); t.function2(); ") == "Function2");
|
CHECK(chai.eval<std::string>("auto t = Utility_Test(); t.function2(); ") == "Function2");
|
||||||
@ -549,7 +552,7 @@ TEST_CASE("Utility_Test utility class wrapper for enum")
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(m);
|
chai.add(m);
|
||||||
|
|
||||||
CHECK(chai.eval<Utility_Test_Numbers>("ONE ") == 0);
|
CHECK(chai.eval<Utility_Test_Numbers>("ONE ") == 0);
|
||||||
@ -644,7 +647,7 @@ TEST_CASE("Object copy counts")
|
|||||||
m->add(chaiscript::constructor<Object_Copy_Count_Test(const Object_Copy_Count_Test &)>(), "Object_Copy_Count_Test");
|
m->add(chaiscript::constructor<Object_Copy_Count_Test(const Object_Copy_Count_Test &)>(), "Object_Copy_Count_Test");
|
||||||
m->add(chaiscript::fun(&object_copy_count_create), "create");
|
m->add(chaiscript::fun(&object_copy_count_create), "create");
|
||||||
|
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(m);
|
chai.add(m);
|
||||||
|
|
||||||
chai.eval(" { auto i = create(); } ");
|
chai.eval(" { auto i = create(); } ");
|
||||||
@ -700,7 +703,7 @@ TEST_CASE("Object lifetime tests")
|
|||||||
m->add(chaiscript::constructor<Object_Lifetime_Test(const Object_Lifetime_Test &)>(), "Object_Lifetime_Test");
|
m->add(chaiscript::constructor<Object_Lifetime_Test(const Object_Lifetime_Test &)>(), "Object_Lifetime_Test");
|
||||||
m->add(chaiscript::fun(&Object_Lifetime_Test::count), "count");
|
m->add(chaiscript::fun(&Object_Lifetime_Test::count), "count");
|
||||||
|
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(m);
|
chai.add(m);
|
||||||
|
|
||||||
CHECK(chai.eval<int>("count()") == 0);
|
CHECK(chai.eval<int>("count()") == 0);
|
||||||
@ -753,7 +756,7 @@ Object_Lifetime_Vector2<float> Object_Lifetime_Vector2_GetValue()
|
|||||||
|
|
||||||
TEST_CASE("Object lifetime test 2")
|
TEST_CASE("Object lifetime test 2")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript _script;
|
chaiscript::ChaiScript_Basic _script(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
//Registering stuff
|
//Registering stuff
|
||||||
_script.add(chaiscript::user_type<Object_Lifetime_Vector2<float>>(), "Object_Lifetime_Vector2f");
|
_script.add(chaiscript::user_type<Object_Lifetime_Vector2<float>>(), "Object_Lifetime_Vector2f");
|
||||||
@ -794,7 +797,7 @@ int myfunction(Non_Poly_Base *)
|
|||||||
|
|
||||||
TEST_CASE("Test Derived->Base with non-polymorphic classes")
|
TEST_CASE("Test Derived->Base with non-polymorphic classes")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::base_class<Non_Poly_Base, Non_Poly_Derived>());
|
chai.add(chaiscript::base_class<Non_Poly_Base, Non_Poly_Derived>());
|
||||||
Non_Poly_Derived d;
|
Non_Poly_Derived d;
|
||||||
chai.add(chaiscript::var(&d), "d");
|
chai.add(chaiscript::var(&d), "d");
|
||||||
@ -813,7 +816,7 @@ struct TestCppVariableScope
|
|||||||
|
|
||||||
TEST_CASE("Variable Scope When Calling From C++")
|
TEST_CASE("Variable Scope When Calling From C++")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::user_type<TestCppVariableScope>(), "Test");
|
chai.add(chaiscript::user_type<TestCppVariableScope>(), "Test");
|
||||||
chai.add(chaiscript::constructor<TestCppVariableScope()>(), "Test");
|
chai.add(chaiscript::constructor<TestCppVariableScope()>(), "Test");
|
||||||
chai.add(chaiscript::fun(&TestCppVariableScope::print), "print");
|
chai.add(chaiscript::fun(&TestCppVariableScope::print), "print");
|
||||||
@ -836,7 +839,7 @@ TEST_CASE("Variable Scope When Calling From C++")
|
|||||||
|
|
||||||
TEST_CASE("Variable Scope When Calling From C++ 2")
|
TEST_CASE("Variable Scope When Calling From C++ 2")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.eval("var obj = 2;");
|
chai.eval("var obj = 2;");
|
||||||
auto func = chai.eval<std::function<void()>>("fun(){ return obj; }");
|
auto func = chai.eval<std::function<void()>>("fun(){ return obj; }");
|
||||||
CHECK_THROWS(func());
|
CHECK_THROWS(func());
|
||||||
@ -853,7 +856,7 @@ void longlong(long long i) {
|
|||||||
|
|
||||||
TEST_CASE("Test long long dispatch")
|
TEST_CASE("Test long long dispatch")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::fun(&longlong), "longlong");
|
chai.add(chaiscript::fun(&longlong), "longlong");
|
||||||
chai.add(chaiscript::fun(&ulonglong), "ulonglong");
|
chai.add(chaiscript::fun(&ulonglong), "ulonglong");
|
||||||
chai.eval("longlong(15)");
|
chai.eval("longlong(15)");
|
||||||
@ -873,7 +876,7 @@ struct Returned_Converted_Config
|
|||||||
|
|
||||||
TEST_CASE("Return of converted type from script")
|
TEST_CASE("Return of converted type from script")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
chai.add(chaiscript::constructor<Returned_Converted_Config ()>(), "Returned_Converted_Config");
|
chai.add(chaiscript::constructor<Returned_Converted_Config ()>(), "Returned_Converted_Config");
|
||||||
chai.add(chaiscript::fun(&Returned_Converted_Config::num_iterations), "num_iterations");
|
chai.add(chaiscript::fun(&Returned_Converted_Config::num_iterations), "num_iterations");
|
||||||
@ -921,7 +924,7 @@ int get_value_a(const std::map<std::string, int> &t_m)
|
|||||||
|
|
||||||
TEST_CASE("Map conversions")
|
TEST_CASE("Map conversions")
|
||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
chai.add(chaiscript::map_conversion<std::map<std::string, int>>());
|
chai.add(chaiscript::map_conversion<std::map<std::string, int>>());
|
||||||
chai.add(chaiscript::fun(&get_value_a), "get_value_a");
|
chai.add(chaiscript::fun(&get_value_a), "get_value_a");
|
||||||
|
|
||||||
@ -941,7 +944,7 @@ TEST_CASE("Parse floats with non-posix locale")
|
|||||||
#else
|
#else
|
||||||
std::setlocale(LC_ALL, "en_ZA.utf8");
|
std::setlocale(LC_ALL, "en_ZA.utf8");
|
||||||
#endif
|
#endif
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
const double parsed = chai.eval<double>("print(1.3); 1.3");
|
const double parsed = chai.eval<double>("print(1.3); 1.3");
|
||||||
CHECK(parsed == Approx(1.3));
|
CHECK(parsed == Approx(1.3));
|
||||||
const std::string str = chai.eval<std::string>("to_string(1.3)");
|
const std::string str = chai.eval<std::string>("to_string(1.3)");
|
||||||
@ -950,4 +953,320 @@ TEST_CASE("Parse floats with non-posix locale")
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool FindBitmap(int &ox, int &oy, long) {
|
||||||
|
ox = 1;
|
||||||
|
oy = 2;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Mismatched numeric types only convert necessary params")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun(&FindBitmap), "FindBitmap");
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
chai.add(chaiscript::var(&x), "x");
|
||||||
|
chai.add(chaiscript::var(&y), "y");
|
||||||
|
chai.eval( "if ( FindBitmap ( x, y, 0) ) { print(\"found at \" + to_string(x) + \", \" + to_string(y))}" );
|
||||||
|
CHECK(x == 1);
|
||||||
|
CHECK(y == 2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("type_conversion to bool")
|
||||||
|
{
|
||||||
|
auto module = std::make_shared<chaiscript::Module>();
|
||||||
|
struct T {
|
||||||
|
operator bool() const { return true; }
|
||||||
|
};
|
||||||
|
module->add(chaiscript::type_conversion<T, bool>());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Make sure ChaiScript object still compiles / executes")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript chai;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Count_Tracer
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
template<typename T>
|
||||||
|
void trace(const chaiscript::detail::Dispatch_State &, const chaiscript::eval::AST_Node_Impl<T> *)
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("Test count tracer")
|
||||||
|
{
|
||||||
|
typedef chaiscript::parser::ChaiScript_Parser< chaiscript::eval::Tracer<Count_Tracer>, chaiscript::optimizer::Optimizer_Default > Parser_Type;
|
||||||
|
|
||||||
|
chaiscript::ChaiScript_Basic chai(chaiscript::Std_Lib::library(),
|
||||||
|
std::make_unique<Parser_Type>());
|
||||||
|
|
||||||
|
Parser_Type &parser = dynamic_cast<Parser_Type &>(chai.get_parser());
|
||||||
|
|
||||||
|
const auto count = parser.get_tracer().count;
|
||||||
|
|
||||||
|
chai.eval("");
|
||||||
|
|
||||||
|
CHECK(parser.get_tracer().count > count);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("Test stdlib options")
|
||||||
|
{
|
||||||
|
const auto test_has_external_scripts = [](chaiscript::ChaiScript_Basic &chai) {
|
||||||
|
CHECK_NOTHROW(chai.eval("`use`"));
|
||||||
|
CHECK_NOTHROW(chai.eval("`eval_file`"));
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto test_no_external_scripts = [](chaiscript::ChaiScript_Basic &chai) {
|
||||||
|
CHECK_THROWS(chai.eval("`use`"));
|
||||||
|
CHECK_THROWS(chai.eval("`eval_file`"));
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto test_has_load_modules = [](chaiscript::ChaiScript_Basic &chai) {
|
||||||
|
CHECK_NOTHROW(chai.eval("`load_module`"));
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto test_no_load_modules = [](chaiscript::ChaiScript_Basic &chai) {
|
||||||
|
CHECK_THROWS(chai.eval("`load_module`"));
|
||||||
|
};
|
||||||
|
|
||||||
|
SECTION( "Defaults" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
test_has_external_scripts(chai);
|
||||||
|
test_has_load_modules(chai);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION( "Load_Modules, External_Scripts" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(), {}, {},
|
||||||
|
{chaiscript::Options::Load_Modules, chaiscript::Options::External_Scripts} );
|
||||||
|
test_has_external_scripts(chai);
|
||||||
|
test_has_load_modules(chai);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION( "No_Load_Modules, No_External_Scripts" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(), {}, {},
|
||||||
|
{chaiscript::Options::No_Load_Modules, chaiscript::Options::No_External_Scripts} );
|
||||||
|
test_no_external_scripts(chai);
|
||||||
|
test_no_load_modules(chai);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION( "No_Load_Modules, Load_Modules" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(), {}, {},
|
||||||
|
{chaiscript::Options::No_Load_Modules, chaiscript::Options::Load_Modules} );
|
||||||
|
test_no_external_scripts(chai);
|
||||||
|
test_no_load_modules(chai);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION( "No_External_Scripts, External_Scripts" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(), {}, {},
|
||||||
|
{chaiscript::Options::No_External_Scripts, chaiscript::Options::External_Scripts} );
|
||||||
|
test_no_external_scripts(chai);
|
||||||
|
test_no_load_modules(chai);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION( "No_External_Scripts, Load_Modules" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(), {}, {},
|
||||||
|
{chaiscript::Options::No_External_Scripts, chaiscript::Options::Load_Modules} );
|
||||||
|
test_no_external_scripts(chai);
|
||||||
|
test_has_load_modules(chai);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION( "External_Scripts, No_Load_Modules" ) {
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(), {}, {},
|
||||||
|
{chaiscript::Options::External_Scripts, chaiscript::Options::No_Load_Modules} );
|
||||||
|
test_has_external_scripts(chai);
|
||||||
|
test_no_load_modules(chai);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void uservalueref(int &&)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void usemoveonlytype(std::unique_ptr<int> &&)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("Pass r-value reference to func")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun(&uservalueref), "uservalueref");
|
||||||
|
chai.add(chaiscript::fun(&usemoveonlytype), "usemoveonlytype");
|
||||||
|
|
||||||
|
chai.add(chaiscript::var(std::make_unique<int>(1)), "iptr");
|
||||||
|
chai.eval("usemoveonlytype(iptr)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Use unique_ptr")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun([](int &i){ ++i; }), "inci");
|
||||||
|
chai.add(chaiscript::fun([](int i){ ++i; }), "copyi");
|
||||||
|
chai.add(chaiscript::fun([](int *i){ ++(*i); }), "derefi");
|
||||||
|
chai.add(chaiscript::fun([](const std::unique_ptr<int> &i){ ++(*i); }), "constrefuniqptri");
|
||||||
|
chai.add(chaiscript::fun([](std::unique_ptr<int> &i){ ++(*i); }), "refuniqptri");
|
||||||
|
chai.add(chaiscript::fun([](std::unique_ptr<int> &&i){ ++(*i); }), "rvaluniqptri");
|
||||||
|
chai.add(chaiscript::var(std::make_unique<int>(1)), "iptr");
|
||||||
|
|
||||||
|
|
||||||
|
CHECK(chai.eval<int>("iptr") == 1);
|
||||||
|
chai.eval("inci(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 2);
|
||||||
|
chai.eval("copyi(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 2);
|
||||||
|
chai.eval("derefi(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 3);
|
||||||
|
chai.eval("constrefuniqptri(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 4);
|
||||||
|
chai.eval("refuniqptri(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 5);
|
||||||
|
chai.eval("rvaluniqptri(iptr)");
|
||||||
|
CHECK(chai.eval<int>("iptr") == 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Unique_Ptr_Test_Class
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Unique_Ptr_Test_Class() = default;
|
||||||
|
Unique_Ptr_Test_Class(const Unique_Ptr_Test_Class&) = default;
|
||||||
|
Unique_Ptr_Test_Class(Unique_Ptr_Test_Class &&) = default;
|
||||||
|
Unique_Ptr_Test_Class &operator=(const Unique_Ptr_Test_Class&) = default;
|
||||||
|
Unique_Ptr_Test_Class &operator=(Unique_Ptr_Test_Class&&) = default;
|
||||||
|
virtual ~Unique_Ptr_Test_Class() = default;
|
||||||
|
|
||||||
|
int getI() const {return 5;}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
std::unique_ptr<Unique_Ptr_Test_Class> make_Unique_Ptr_Test_Class()
|
||||||
|
{
|
||||||
|
return std::make_unique<Unique_Ptr_Test_Class>();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Call methods through unique_ptr")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
|
chai.add(chaiscript::var(std::make_unique<Unique_Ptr_Test_Class>()), "uptr");
|
||||||
|
chai.add(chaiscript::fun(make_Unique_Ptr_Test_Class), "make_Unique_Ptr_Test_Class");
|
||||||
|
chai.add(chaiscript::fun(&Unique_Ptr_Test_Class::getI), "getI");
|
||||||
|
CHECK(chai.eval<int>("uptr.getI()") == 5);
|
||||||
|
CHECK(chai.eval<int>("var uptr2 = make_Unique_Ptr_Test_Class(); uptr2.getI()") == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Unique_Ptr_Test_Base_Class
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int getI() const {return 5;}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Unique_Ptr_Test_Derived_Class : public Unique_Ptr_Test_Base_Class
|
||||||
|
{};
|
||||||
|
|
||||||
|
std::unique_ptr<Unique_Ptr_Test_Derived_Class> make_Unique_Ptr_Test_Derived_Class()
|
||||||
|
{
|
||||||
|
return std::make_unique<Unique_Ptr_Test_Derived_Class>();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Call methods on base class through unique_ptr<derived>")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
|
chai.add(chaiscript::var(std::make_unique<Unique_Ptr_Test_Derived_Class>()), "uptr");
|
||||||
|
chai.add(chaiscript::fun(make_Unique_Ptr_Test_Derived_Class), "make_Unique_Ptr_Test_Derived_Class");
|
||||||
|
chai.add(chaiscript::fun(&Unique_Ptr_Test_Base_Class::getI), "getI");
|
||||||
|
chai.add(chaiscript::base_class<Unique_Ptr_Test_Base_Class, Unique_Ptr_Test_Derived_Class>());
|
||||||
|
CHECK(chai.eval<int>("uptr.getI()") == 5);
|
||||||
|
CHECK(chai.eval<int>("var uptr2 = make_Unique_Ptr_Test_Derived_Class(); uptr2.getI()") == 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
A() = default;
|
||||||
|
A(const A&) = default;
|
||||||
|
A(A &&) = default;
|
||||||
|
A &operator=(const A&) = default;
|
||||||
|
A &operator=(A&&) = default;
|
||||||
|
virtual ~A() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
class B : public A
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
B() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("Test typed chaiscript functions to perform conversions")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
chai.add(chaiscript::user_type<A>(), "A");
|
||||||
|
|
||||||
|
chai.add(chaiscript::user_type<B>(), "B");
|
||||||
|
chai.add(chaiscript::base_class<A, B>());
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun([](const B &)
|
||||||
|
{
|
||||||
|
}), "CppFunctWithBArg");
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun([]() -> std::shared_ptr<A>
|
||||||
|
{
|
||||||
|
return (std::shared_ptr<A>(new B()));
|
||||||
|
}), "Create");
|
||||||
|
|
||||||
|
chai.eval(R"(
|
||||||
|
var inst = Create() // A*
|
||||||
|
|
||||||
|
// it prints "A"
|
||||||
|
inst.type_name().print()
|
||||||
|
|
||||||
|
// Ok it is casted using conversion
|
||||||
|
CppFunctWithBArg(inst)
|
||||||
|
|
||||||
|
// Define a function with B as argument
|
||||||
|
def ChaiFuncWithBArg(B inst)
|
||||||
|
{
|
||||||
|
print("ok")
|
||||||
|
}
|
||||||
|
|
||||||
|
// don't work
|
||||||
|
ChaiFuncWithBArg(inst)
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Reference_MyClass
|
||||||
|
{
|
||||||
|
Reference_MyClass(double& t_x) : x(t_x) {}
|
||||||
|
double& x;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("Test reference member being registered")
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
|
// Note, C++ will not allow us to do this:
|
||||||
|
// chai.add(chaiscript::fun(&Reference_MyClass::x) , "x");
|
||||||
|
chai.add(chaiscript::fun([](Reference_MyClass &r) -> decltype(auto) { return (r.x); }), "x");
|
||||||
|
chai.add(chaiscript::fun([](const Reference_MyClass &r) -> decltype(auto) { return (r.x); }), "x");
|
||||||
|
double d;
|
||||||
|
chai.add(chaiscript::var(Reference_MyClass(d)), "ref");
|
||||||
|
chai.eval("ref.x = 2.3");
|
||||||
|
CHECK(d == Approx(2.3));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
23
unittests/conversion_to_bool.chai
Normal file
23
unittests/conversion_to_bool.chai
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
// all we care is that this executes, really
|
||||||
|
|
||||||
|
add_type_conversion(type("int"), type("bool"), fun(int i) { return i != 0; });
|
||||||
|
|
||||||
|
if (0) {
|
||||||
|
assert_true(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!0) {
|
||||||
|
assert_true(true);
|
||||||
|
} else {
|
||||||
|
assert_true(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (0) {
|
||||||
|
assert_true(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; 0; ) {
|
||||||
|
assert_true(false);
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,6 @@
|
|||||||
assert_equal(`==`, `==`);
|
assert_equal(`==`, `==`);
|
||||||
assert_not_equal(`==`, `<`);
|
assert_not_equal(`==`, `<`);
|
||||||
assert_equal(`<`.get_arity(), 2);
|
assert_equal(`<`.get_arity(), 2);
|
||||||
assert_equal(`+`.get_annotation(), "Multiple method dispatch function wrapper.");
|
|
||||||
assert_equal(get_arity.get_contained_functions().size(), 0);
|
assert_equal(get_arity.get_contained_functions().size(), 0);
|
||||||
assert_equal(get_arity.get_arity(), 1);
|
assert_equal(get_arity.get_arity(), 1);
|
||||||
assert_equal(get_arity.get_param_types().size(), 2);
|
assert_equal(get_arity.get_param_types().size(), 2);
|
||||||
|
@ -35,4 +35,4 @@ def while_doing()
|
|||||||
|
|
||||||
auto f = fun() { while_doing(); }
|
auto f = fun() { while_doing(); }
|
||||||
|
|
||||||
assert_equal(get_eval_error(f).call_stack.size(), 16)
|
assert_true(get_eval_error(f).call_stack.size() <= 16)
|
||||||
|
42
unittests/execution_context.chai
Normal file
42
unittests/execution_context.chai
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
|
||||||
|
|
||||||
|
assert_equal(__LINE__, 3)
|
||||||
|
|
||||||
|
def f() {
|
||||||
|
[__LINE__, __CLASS__, __FUNC__]
|
||||||
|
}
|
||||||
|
|
||||||
|
var res = f()
|
||||||
|
|
||||||
|
assert_equal(res[0], 6)
|
||||||
|
assert_equal(res[1], "NOT_IN_CLASS")
|
||||||
|
assert_equal(res[2], "f")
|
||||||
|
|
||||||
|
assert_equal(__CLASS__, "NOT_IN_CLASS")
|
||||||
|
assert_equal(__FUNC__, "NOT_IN_FUNCTION")
|
||||||
|
|
||||||
|
class C
|
||||||
|
{
|
||||||
|
def C() {}
|
||||||
|
def member() { [__LINE__, __CLASS__, __FUNC__]; }
|
||||||
|
}
|
||||||
|
|
||||||
|
var c = C();
|
||||||
|
|
||||||
|
var res2 = c.member();
|
||||||
|
|
||||||
|
assert_equal(res2[0], 21)
|
||||||
|
assert_equal(res2[1], "C")
|
||||||
|
assert_equal(res2[2], "member")
|
||||||
|
|
||||||
|
def C::member2() { [__LINE__, __CLASS__, __FUNC__]; }
|
||||||
|
|
||||||
|
var res3 = c.member2();
|
||||||
|
|
||||||
|
assert_equal(res3[0], 32)
|
||||||
|
assert_equal(res3[1], "C")
|
||||||
|
assert_equal(res3[2], "member2")
|
||||||
|
|
||||||
|
assert_true(__FILE__.find("execution_context.chai") != -1)
|
||||||
|
|
||||||
|
|
26
unittests/explicit_this_capture.chai
Normal file
26
unittests/explicit_this_capture.chai
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
class Test
|
||||||
|
{
|
||||||
|
def Test()
|
||||||
|
{}
|
||||||
|
|
||||||
|
def a(Function f)
|
||||||
|
{
|
||||||
|
f("test");
|
||||||
|
// f();
|
||||||
|
}
|
||||||
|
|
||||||
|
def b()
|
||||||
|
{
|
||||||
|
var l = fun [this](x) { this.thing(); }
|
||||||
|
this.a(l)
|
||||||
|
}
|
||||||
|
|
||||||
|
def thing()
|
||||||
|
{
|
||||||
|
print("hello world");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var t = Test()
|
||||||
|
t.b()
|
||||||
|
|
||||||
|
|
@ -10,7 +10,6 @@ def test_function(a)
|
|||||||
|
|
||||||
// test_function tests
|
// test_function tests
|
||||||
assert_equal(test_function.get_arity(), 1);
|
assert_equal(test_function.get_arity(), 1);
|
||||||
assert_equal(trim(test_function.get_annotation()), "#Test Function Description");
|
|
||||||
assert_equal(test_function.get_contained_functions().size(), 0);
|
assert_equal(test_function.get_contained_functions().size(), 0);
|
||||||
assert_equal(test_function.get_param_types().size(), 2);
|
assert_equal(test_function.get_param_types().size(), 2);
|
||||||
|
|
||||||
|
Binary file not shown.
BIN
unittests/fuzzy_tests-2016-06-29.tar.bz2
Normal file
BIN
unittests/fuzzy_tests-2016-06-29.tar.bz2
Normal file
Binary file not shown.
@ -16,3 +16,8 @@ def f() {
|
|||||||
|
|
||||||
f();
|
f();
|
||||||
|
|
||||||
|
|
||||||
|
global &h = v;
|
||||||
|
assert_true(h == v)
|
||||||
|
v = 3;
|
||||||
|
assert_true(h == 3)
|
||||||
|
7
unittests/init_if.chai
Normal file
7
unittests/init_if.chai
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
if (var x = 2; x == 3) {
|
||||||
|
assert_true(false);
|
||||||
|
} else if (var y = 3; y == 6) {
|
||||||
|
assert_true(false);
|
||||||
|
} else {
|
||||||
|
assert_equal(5, y + x);
|
||||||
|
}
|
@ -1,122 +1,284 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
#include "../static_libs/chaiscript_parser.hpp"
|
||||||
|
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
// ignore errors about negative unsigned integer literals
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4146)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define TEST_LITERAL(v) test_literal(v, #v)
|
#define TEST_LITERAL(v) test_literal(v, #v)
|
||||||
|
#define TEST_LITERAL_SIGNED(v) test_literal(v, #v, true)
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool test_literal(T val, const std::string &str)
|
bool test_literal(T val, const std::string &str, bool use_boxed_number = false)
|
||||||
{
|
{
|
||||||
std::cout << "Comparing : " << val;
|
std::cout << '(' << str << ") Comparing : C++ '" << val;
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||||
T val2 = chai.eval<T>(str);
|
|
||||||
std::cout << " " << val2 << '\n';
|
// Note, after applying the `-` it's possible that chaiscript has internally converted
|
||||||
|
// between two equivalently sized types (ie, unsigned long and unsigned long long on a 64bit system)
|
||||||
|
// so we're going to allow some leeway with the signed tests
|
||||||
|
T val2 = [&](){
|
||||||
|
if (!use_boxed_number) {
|
||||||
|
return chai.eval<T>(str);
|
||||||
|
} else {
|
||||||
|
return chai.eval<chaiscript::Boxed_Number>(str).get_as_checked<T>();
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
|
||||||
|
std::cout << "' chai '" << val2 << "'\n";
|
||||||
return val == val2;
|
return val == val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
if( TEST_LITERAL(0xF)
|
if(
|
||||||
&& TEST_LITERAL(0xFF)
|
TEST_LITERAL(0xF)
|
||||||
&& TEST_LITERAL(0xFFF)
|
&& TEST_LITERAL(0xFF)
|
||||||
&& TEST_LITERAL(0xFFFF)
|
&& TEST_LITERAL(0xFFF)
|
||||||
&& TEST_LITERAL(0xFFFFF)
|
&& TEST_LITERAL(0xFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFF)
|
&& TEST_LITERAL(0xFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFFFFFFF)
|
||||||
&& TEST_LITERAL(0xFFFFFFFFFFFFFFFF)
|
&& TEST_LITERAL(0xFFFFFFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL(0xFFFFFFFFFFFFFFFF)
|
||||||
|
|
||||||
|
|
||||||
&& TEST_LITERAL(01)
|
&& TEST_LITERAL(01)
|
||||||
&& TEST_LITERAL(017)
|
&& TEST_LITERAL(017)
|
||||||
&& TEST_LITERAL(0177)
|
&& TEST_LITERAL(0177)
|
||||||
&& TEST_LITERAL(01777)
|
&& TEST_LITERAL(01777)
|
||||||
&& TEST_LITERAL(017777)
|
&& TEST_LITERAL(017777)
|
||||||
&& TEST_LITERAL(0177777)
|
&& TEST_LITERAL(0177777)
|
||||||
&& TEST_LITERAL(01777777)
|
&& TEST_LITERAL(01777777)
|
||||||
&& TEST_LITERAL(017777777)
|
&& TEST_LITERAL(017777777)
|
||||||
&& TEST_LITERAL(0177777777)
|
&& TEST_LITERAL(0177777777)
|
||||||
&& TEST_LITERAL(01777777777)
|
&& TEST_LITERAL(01777777777)
|
||||||
&& TEST_LITERAL(017777777777)
|
&& TEST_LITERAL(017777777777)
|
||||||
&& TEST_LITERAL(0177777777777)
|
&& TEST_LITERAL(0177777777777)
|
||||||
&& TEST_LITERAL(01777777777777)
|
&& TEST_LITERAL(01777777777777)
|
||||||
&& TEST_LITERAL(017777777777777)
|
&& TEST_LITERAL(017777777777777)
|
||||||
&& TEST_LITERAL(0177777777777777)
|
&& TEST_LITERAL(0177777777777777)
|
||||||
&& TEST_LITERAL(01777777777777777)
|
&& TEST_LITERAL(01777777777777777)
|
||||||
&& TEST_LITERAL(017777777777777777)
|
&& TEST_LITERAL(017777777777777777)
|
||||||
&& TEST_LITERAL(0177777777777777777)
|
&& TEST_LITERAL(0177777777777777777)
|
||||||
&& TEST_LITERAL(01777777777777777777)
|
&& TEST_LITERAL(01777777777777777777)
|
||||||
&& TEST_LITERAL(017777777777777777777)
|
&& TEST_LITERAL(017777777777777777777)
|
||||||
&& TEST_LITERAL(0177777777777777777777)
|
&& TEST_LITERAL(0177777777777777777777)
|
||||||
&& TEST_LITERAL(01777777777777777777777)
|
&& TEST_LITERAL(01777777777777777777777)
|
||||||
|
|
||||||
&& TEST_LITERAL(1)
|
&& TEST_LITERAL(1)
|
||||||
&& TEST_LITERAL(17)
|
&& TEST_LITERAL(17)
|
||||||
&& TEST_LITERAL(177)
|
&& TEST_LITERAL(177)
|
||||||
&& TEST_LITERAL(1777)
|
&& TEST_LITERAL(1777)
|
||||||
&& TEST_LITERAL(17777)
|
&& TEST_LITERAL(17777)
|
||||||
&& TEST_LITERAL(177777)
|
&& TEST_LITERAL(177777)
|
||||||
&& TEST_LITERAL(1777777)
|
&& TEST_LITERAL(1777777)
|
||||||
&& TEST_LITERAL(17777777)
|
&& TEST_LITERAL(17777777)
|
||||||
&& TEST_LITERAL(177777777)
|
&& TEST_LITERAL(177777777)
|
||||||
&& TEST_LITERAL(1777777777)
|
&& TEST_LITERAL(1777777777)
|
||||||
&& TEST_LITERAL(17777777777)
|
&& TEST_LITERAL(17777777777)
|
||||||
&& TEST_LITERAL(177777777777)
|
&& TEST_LITERAL(177777777777)
|
||||||
&& TEST_LITERAL(1777777777777)
|
&& TEST_LITERAL(1777777777777)
|
||||||
&& TEST_LITERAL(17777777777777)
|
&& TEST_LITERAL(17777777777777)
|
||||||
&& TEST_LITERAL(177777777777777)
|
&& TEST_LITERAL(177777777777777)
|
||||||
&& TEST_LITERAL(1777777777777777)
|
&& TEST_LITERAL(1777777777777777)
|
||||||
&& TEST_LITERAL(17777777777777777)
|
&& TEST_LITERAL(17777777777777777)
|
||||||
&& TEST_LITERAL(177777777777777777)
|
&& TEST_LITERAL(177777777777777777)
|
||||||
&& TEST_LITERAL(1777777777777777777)
|
&& TEST_LITERAL(1777777777777777777)
|
||||||
|
|
||||||
&& test_literal(0xF, "0b1111")
|
&& test_literal(0xF, "0b1111")
|
||||||
&& test_literal(0xFF, "0b11111111")
|
&& test_literal(0xFF, "0b11111111")
|
||||||
&& test_literal(0xFFF, "0b111111111111")
|
&& test_literal(0xFFF, "0b111111111111")
|
||||||
&& test_literal(0xFFFF, "0b1111111111111111")
|
&& test_literal(0xFFFF, "0b1111111111111111")
|
||||||
&& test_literal(0xFFFFF, "0b11111111111111111111")
|
&& test_literal(0xFFFFF, "0b11111111111111111111")
|
||||||
&& test_literal(0xFFFFFF, "0b111111111111111111111111")
|
&& test_literal(0xFFFFFF, "0b111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFF, "0b1111111111111111111111111111")
|
&& test_literal(0xFFFFFFF, "0b1111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFF, "0b11111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFF, "0b11111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFF, "0b111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFF, "0b111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFF, "0b1111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFF, "0b1111111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFFF, "0b11111111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFFF, "0b11111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFFFFF, "0b1111111111111111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFFFFF, "0b1111111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFFFFFF, "0b11111111111111111111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFFFFFF, "0b11111111111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0xFFFFFFFFFFFFFFFF, "0b1111111111111111111111111111111111111111111111111111111111111111")
|
&& test_literal(0xFFFFFFFFFFFFFFFF, "0b1111111111111111111111111111111111111111111111111111111111111111")
|
||||||
|
|
||||||
&& test_literal(0x7, "0b111")
|
&& test_literal(0x7, "0b111")
|
||||||
&& test_literal(0x7F, "0b1111111")
|
&& test_literal(0x7F, "0b1111111")
|
||||||
&& test_literal(0x7FF, "0b11111111111")
|
&& test_literal(0x7FF, "0b11111111111")
|
||||||
&& test_literal(0x7FFF, "0b111111111111111")
|
&& test_literal(0x7FFF, "0b111111111111111")
|
||||||
&& test_literal(0x7FFFF, "0b1111111111111111111")
|
&& test_literal(0x7FFFF, "0b1111111111111111111")
|
||||||
&& test_literal(0x7FFFFF, "0b11111111111111111111111")
|
&& test_literal(0x7FFFFF, "0b11111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFF, "0b111111111111111111111111111")
|
&& test_literal(0x7FFFFFF, "0b111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFF, "0b1111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFF, "0b1111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFF, "0b11111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFF, "0b11111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFF, "0b111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFF, "0b111111111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFFF, "0b1111111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFFF, "0b1111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFFFF, "0b11111111111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFFFF, "0b11111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFFFFFF, "0b1111111111111111111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFFFFFF, "0b1111111111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFFFFFFF, "0b11111111111111111111111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFFFFFFF, "0b11111111111111111111111111111111111111111111111111111111111")
|
||||||
&& test_literal(0x7FFFFFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111111111111111111")
|
&& test_literal(0x7FFFFFFFFFFFFFFF, "0b111111111111111111111111111111111111111111111111111111111111111")
|
||||||
)
|
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFFFFFFF)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0xFFFFFFFFFFFFFFFF)
|
||||||
|
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-01)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-017777777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-0177777777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-01777777777777777777777)
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-1)
|
||||||
|
&& TEST_LITERAL_SIGNED(-17)
|
||||||
|
&& TEST_LITERAL_SIGNED(-177)
|
||||||
|
&& TEST_LITERAL_SIGNED(-1777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-17777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-177777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-1777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-17777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-177777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-1777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-17777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-177777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-1777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-17777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-177777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-1777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-17777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-177777777777777777)
|
||||||
|
&& TEST_LITERAL_SIGNED(-1777777777777777777)
|
||||||
|
|
||||||
|
// Test 8/16/24/32 bit boundaries for various types
|
||||||
|
|
||||||
|
&& TEST_LITERAL(255)
|
||||||
|
&& TEST_LITERAL(65535)
|
||||||
|
&& TEST_LITERAL(16777215)
|
||||||
|
#ifndef CHAISCRIPT_MSVC
|
||||||
|
// bug in cl.exe causes this to be incorrectly parsed as an unsigned
|
||||||
|
&& TEST_LITERAL(4294967295)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-255)
|
||||||
|
&& TEST_LITERAL_SIGNED(-65535)
|
||||||
|
&& TEST_LITERAL_SIGNED(-16777215)
|
||||||
|
#ifndef CHAISCRIPT_MSVC
|
||||||
|
// bug in cl.exe causes this to be incorrectly parsed as an unsigned
|
||||||
|
&& TEST_LITERAL_SIGNED(-4294967295)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
&& TEST_LITERAL(255u)
|
||||||
|
&& TEST_LITERAL(65535u)
|
||||||
|
&& TEST_LITERAL(16777215u)
|
||||||
|
&& TEST_LITERAL(4294967295u)
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-255u)
|
||||||
|
&& TEST_LITERAL_SIGNED(-65535u)
|
||||||
|
&& TEST_LITERAL_SIGNED(-16777215u)
|
||||||
|
&& TEST_LITERAL_SIGNED(-4294967295u)
|
||||||
|
|
||||||
|
&& TEST_LITERAL(255l)
|
||||||
|
&& TEST_LITERAL(65535l)
|
||||||
|
&& TEST_LITERAL(16777215l)
|
||||||
|
#ifndef CHAISCRIPT_MSVC
|
||||||
|
// bug in cl.exe causes this to be incorrectly parsed as an unsigned
|
||||||
|
&& TEST_LITERAL(4294967295l)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-255l)
|
||||||
|
&& TEST_LITERAL_SIGNED(-65535l)
|
||||||
|
&& TEST_LITERAL_SIGNED(-16777215l)
|
||||||
|
#ifndef CHAISCRIPT_MSVC
|
||||||
|
// bug in cl.exe causes this to be incorrectly parsed as an unsigned
|
||||||
|
&& TEST_LITERAL_SIGNED(-4294967295l)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
&& TEST_LITERAL(255ul)
|
||||||
|
&& TEST_LITERAL(65535ul)
|
||||||
|
&& TEST_LITERAL(16777215ul)
|
||||||
|
&& TEST_LITERAL(4294967295ul)
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-255ul)
|
||||||
|
&& TEST_LITERAL_SIGNED(-65535ul)
|
||||||
|
&& TEST_LITERAL_SIGNED(-16777215ul)
|
||||||
|
&& TEST_LITERAL_SIGNED(-4294967295ul)
|
||||||
|
|
||||||
|
&& TEST_LITERAL(255ull)
|
||||||
|
&& TEST_LITERAL(65535ull)
|
||||||
|
&& TEST_LITERAL(16777215ull)
|
||||||
|
&& TEST_LITERAL(4294967295ull)
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-255ull)
|
||||||
|
&& TEST_LITERAL_SIGNED(-65535ull)
|
||||||
|
&& TEST_LITERAL_SIGNED(-16777215ull)
|
||||||
|
&& TEST_LITERAL_SIGNED(-4294967295ull)
|
||||||
|
|
||||||
|
&& TEST_LITERAL(255ll)
|
||||||
|
&& TEST_LITERAL(65535ll)
|
||||||
|
&& TEST_LITERAL(16777215ll)
|
||||||
|
&& TEST_LITERAL(4294967295ll)
|
||||||
|
|
||||||
|
&& TEST_LITERAL_SIGNED(-255ll)
|
||||||
|
&& TEST_LITERAL_SIGNED(-65535ll)
|
||||||
|
&& TEST_LITERAL_SIGNED(-16777215ll)
|
||||||
|
&& TEST_LITERAL_SIGNED(-4294967295ll)
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
{
|
{
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
@ -1 +1,2 @@
|
|||||||
assert_equal(from_json("100"), 100)
|
assert_equal(from_json("100"), 100)
|
||||||
|
assert_equal(from_json("-100"), -100)
|
||||||
|
@ -1 +1,22 @@
|
|||||||
assert_equal(from_json("1.234"), 1.234)
|
assert_equal(from_json("1.234"), 1.234)
|
||||||
|
assert_equal(from_json("-1.234"), -1.234)
|
||||||
|
|
||||||
|
auto caught = false;
|
||||||
|
try {
|
||||||
|
from_json("-1-5.3");
|
||||||
|
}
|
||||||
|
catch(e) {
|
||||||
|
assert_equal("JSON ERROR: Number: unexpected character '-'", e.what());
|
||||||
|
caught = true;
|
||||||
|
}
|
||||||
|
assert_equal(caught, true);
|
||||||
|
|
||||||
|
caught = false;
|
||||||
|
try {
|
||||||
|
from_json("-15.3.2");
|
||||||
|
}
|
||||||
|
catch(e) {
|
||||||
|
assert_equal("JSON ERROR: Number: unexpected character '.'", e.what());
|
||||||
|
caught = true;
|
||||||
|
}
|
||||||
|
assert_equal(caught, true);
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
assert_equal(from_json("[1,2,3]"), [1,2,3])
|
assert_equal(from_json("[1,-2,3]"), [1,-2,3])
|
||||||
|
|
||||||
|
13
unittests/list_resize.chai
Normal file
13
unittests/list_resize.chai
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
load_module("stl_extra");
|
||||||
|
|
||||||
|
auto list = List();
|
||||||
|
|
||||||
|
list.resize(2);
|
||||||
|
assert_equal(list.size(), 2);
|
||||||
|
|
||||||
|
list.resize(6, "AAA");
|
||||||
|
assert_equal(list.back(), "AAA");
|
||||||
|
|
||||||
|
list.resize(0);
|
||||||
|
assert_equal(list.size(), 0);
|
||||||
|
|
@ -1,14 +1,16 @@
|
|||||||
#include "multifile_test_chai.hpp"
|
#include "multifile_test_chai.hpp"
|
||||||
|
|
||||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||||
|
#include <chaiscript/language/chaiscript_parser.hpp>
|
||||||
|
|
||||||
Multi_Test_Chai::Multi_Test_Chai()
|
Multi_Test_Chai::Multi_Test_Chai()
|
||||||
: m_chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
|
: m_chai(new chaiscript::ChaiScript_Basic(chaiscript::Std_Lib::library(),
|
||||||
|
std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>()))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<chaiscript::ChaiScript> Multi_Test_Chai::get_chai()
|
std::shared_ptr<chaiscript::ChaiScript_Basic> Multi_Test_Chai::get_chai()
|
||||||
{
|
{
|
||||||
return m_chai;
|
return m_chai;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
|
||||||
class Multi_Test_Chai
|
class Multi_Test_Chai
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Multi_Test_Chai();
|
Multi_Test_Chai();
|
||||||
|
|
||||||
std::shared_ptr<chaiscript::ChaiScript> get_chai();
|
std::shared_ptr<chaiscript::ChaiScript_Basic> get_chai();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<chaiscript::ChaiScript> m_chai;
|
std::shared_ptr<chaiscript::ChaiScript_Basic> m_chai;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
#include "multifile_test_chai.hpp"
|
#include "multifile_test_chai.hpp"
|
||||||
#include "multifile_test_module.hpp"
|
#include "multifile_test_module.hpp"
|
||||||
|
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Multi_Test_Chai chaitest;
|
Multi_Test_Chai chaitest;
|
||||||
Multi_Test_Module chaimodule;
|
Multi_Test_Module chaimodule;
|
||||||
|
|
||||||
std::shared_ptr<chaiscript::ChaiScript> chai = chaitest.get_chai();
|
std::shared_ptr<chaiscript::ChaiScript_Basic> chai = chaitest.get_chai();
|
||||||
chai->add(chaimodule.get_module());
|
chai->add(chaimodule.get_module());
|
||||||
return chai->eval<int>("get_module_value()");
|
return chai->eval<int>("get_module_value()");
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
|
||||||
#include "multifile_test_module.hpp"
|
#include "multifile_test_module.hpp"
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
|
||||||
class Multi_Test_Module
|
class Multi_Test_Module
|
||||||
{
|
{
|
||||||
|
@ -2,7 +2,11 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_NO_DYNLOAD
|
||||||
#include <chaiscript/chaiscript.hpp>
|
#include <chaiscript/chaiscript.hpp>
|
||||||
|
#endif
|
||||||
|
#include <chaiscript/chaiscript_basic.hpp>
|
||||||
|
#include <chaiscript/language/chaiscript_parser.hpp>
|
||||||
|
|
||||||
int expected_value(int num_iters)
|
int expected_value(int num_iters)
|
||||||
{
|
{
|
||||||
@ -15,7 +19,7 @@ int expected_value(int num_iters)
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
void do_work(chaiscript::ChaiScript &c, const size_t id)
|
void do_work(chaiscript::ChaiScript_Basic &c, const size_t id)
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
@ -56,13 +60,22 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> modulepaths;
|
std::vector<std::string> modulepaths;
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_NO_DYNLOAD
|
||||||
|
chaiscript::ChaiScript chai(/* unused */modulepaths, usepaths);
|
||||||
|
#else
|
||||||
modulepaths.push_back("");
|
modulepaths.push_back("");
|
||||||
if (modulepath)
|
if (modulepath)
|
||||||
{
|
{
|
||||||
modulepaths.push_back(modulepath);
|
modulepaths.push_back(modulepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
chaiscript::ChaiScript chai(modulepaths,usepaths);
|
// For this test we are going to load the dynamic stdlib
|
||||||
|
// to make sure it continues to work
|
||||||
|
chaiscript::ChaiScript_Basic chai(
|
||||||
|
std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>(),
|
||||||
|
modulepaths,usepaths);
|
||||||
|
#endif
|
||||||
|
|
||||||
std::vector<std::shared_ptr<std::thread> > threads;
|
std::vector<std::shared_ptr<std::thread> > threads;
|
||||||
|
|
||||||
|
12
unittests/negative_numbers.chai
Normal file
12
unittests/negative_numbers.chai
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
def numFunc(x)
|
||||||
|
{
|
||||||
|
return x + 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Note that unary prefix `-` has lower precedence than `.`
|
||||||
|
// this is why these values are correct, even if counterintuitive
|
||||||
|
|
||||||
|
assert_true(-5.numFunc() == -15);
|
||||||
|
assert_true((-5).numFunc() == 5);
|
||||||
|
|
4
unittests/precedence_4.chai
Normal file
4
unittests/precedence_4.chai
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
var i = 2;
|
||||||
|
|
||||||
|
assert_equal(++i * i, 9)
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user