Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7478d57264 | ||
![]() |
bab3701c2f | ||
![]() |
e225654289 | ||
![]() |
019ea57cb6 | ||
![]() |
60fe242fb6 | ||
![]() |
6c10f18e4c | ||
![]() |
d5a221a468 | ||
![]() |
6ec3afc687 | ||
![]() |
049cd12127 | ||
![]() |
8b34066dd5 | ||
![]() |
9fb74762ad | ||
![]() |
7b7e7176f5 | ||
![]() |
81146d6b0f | ||
![]() |
423e872720 | ||
![]() |
1e8c0ab93e | ||
![]() |
c90fe16858 | ||
![]() |
161652b5d9 | ||
![]() |
cc5bf45b3b | ||
![]() |
28124e4b33 | ||
![]() |
db207b345b | ||
![]() |
684d724103 | ||
![]() |
283152a880 | ||
![]() |
dc3ef087e2 | ||
![]() |
032ba63b8a | ||
![]() |
cf49b1b30c | ||
![]() |
63a083b47b |
@@ -1,12 +1,24 @@
|
|||||||
compilers:
|
compilers:
|
||||||
- name: "clang"
|
- name: "clang"
|
||||||
version: "3.5"
|
version: "3.5"
|
||||||
|
skip_packaging: true
|
||||||
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
||||||
|
- name: "clang"
|
||||||
|
build_tag: AddressSanitizer
|
||||||
|
version: "3.5"
|
||||||
|
skip_packaging: true
|
||||||
|
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DENABLE_ADDRESS_SANITIZER:BOOL=ON
|
||||||
|
- name: "clang"
|
||||||
|
build_tag: ThreadSanitizer
|
||||||
|
version: "3.5"
|
||||||
|
skip_packaging: true
|
||||||
|
cmake_extra_flags: -DUSE_LIBCXX:BOOL=OFF -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DENABLE_THREAD_SANITIZER:BOOL=ON
|
||||||
- name: "gcc"
|
- name: "gcc"
|
||||||
version: "4.8"
|
version: "4.8"
|
||||||
cmake_extra_flags: -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
cmake_extra_flags: -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
||||||
- name: "gcc"
|
- name: "gcc"
|
||||||
version: "4.6"
|
version: "4.6"
|
||||||
|
skip_packaging: true
|
||||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON
|
||||||
- name: cppcheck
|
- name: cppcheck
|
||||||
compiler_extra_flags: --enable=all -I include --inline-suppr
|
compiler_extra_flags: --enable=all -I include --inline-suppr
|
||||||
|
@@ -4,4 +4,5 @@ compilers:
|
|||||||
- name: clang
|
- name: clang
|
||||||
build_type: Debug
|
build_type: Debug
|
||||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=$COMMIT_SHA
|
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=$COMMIT_SHA
|
||||||
|
skip_packaging: true
|
||||||
|
|
||||||
|
@@ -3,11 +3,13 @@ compilers:
|
|||||||
version: 14
|
version: 14
|
||||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||||
compiler_extra_flags: /ANALYZE
|
compiler_extra_flags: /ANALYZE
|
||||||
|
skip_packaging: true
|
||||||
- name: Visual Studio
|
- name: Visual Studio
|
||||||
version: 14
|
version: 14
|
||||||
architecture: Win64
|
architecture: Win64
|
||||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||||
compiler_extra_flags: /ANALYZE
|
compiler_extra_flags: /ANALYZE
|
||||||
|
skip_packaging: true
|
||||||
- name: Visual Studio
|
- name: Visual Studio
|
||||||
version: 12
|
version: 12
|
||||||
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
cmake_extra_flags: -DBUILD_SAMPLES:BOOL=ON -DBUILD_PACKAGE:BOOL=ON -DBUILD_TESTING:BOOL=ON -DCOMMIT_SHA=%COMMIT_SHA%
|
||||||
|
@@ -82,7 +82,7 @@ set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/description.txt"
|
|||||||
|
|
||||||
set(CPACK_PACKAGE_VERSION_MAJOR 5)
|
set(CPACK_PACKAGE_VERSION_MAJOR 5)
|
||||||
set(CPACK_PACKAGE_VERSION_MINOR 5)
|
set(CPACK_PACKAGE_VERSION_MINOR 5)
|
||||||
set(CPACK_PACKAGE_VERSION_PATCH 0)
|
set(CPACK_PACKAGE_VERSION_PATCH 1)
|
||||||
|
|
||||||
set(CPACK_PACKAGE_EXECUTABLES "chai;ChaiScript Eval")
|
set(CPACK_PACKAGE_EXECUTABLES "chai;ChaiScript Eval")
|
||||||
set(CPACK_PACKAGE_VENDOR "ChaiScript.com")
|
set(CPACK_PACKAGE_VENDOR "ChaiScript.com")
|
||||||
@@ -232,6 +232,8 @@ if(BUILD_SAMPLES)
|
|||||||
target_link_libraries(memory_leak_test ${LIBS})
|
target_link_libraries(memory_leak_test ${LIBS})
|
||||||
add_executable(inheritance samples/inheritance.cpp)
|
add_executable(inheritance samples/inheritance.cpp)
|
||||||
target_link_libraries(inheritance ${LIBS})
|
target_link_libraries(inheritance ${LIBS})
|
||||||
|
add_executable(fun_call_performance samples/fun_call_performance.cpp)
|
||||||
|
target_link_libraries(fun_call_performance ${LIBS})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
@@ -294,6 +296,10 @@ if(BUILD_TESTING)
|
|||||||
target_link_libraries(object_lifetime_test ${LIBS})
|
target_link_libraries(object_lifetime_test ${LIBS})
|
||||||
add_test(NAME Object_Lifetime_Test COMMAND object_lifetime_test)
|
add_test(NAME Object_Lifetime_Test COMMAND object_lifetime_test)
|
||||||
|
|
||||||
|
add_executable(object_lifetime_test2 unittests/object_lifetime_test2.cpp)
|
||||||
|
target_link_libraries(object_lifetime_test2 ${LIBS})
|
||||||
|
add_test(NAME Object_Lifetime_Test2 COMMAND object_lifetime_test2)
|
||||||
|
|
||||||
add_executable(function_ordering_test unittests/function_ordering_test.cpp)
|
add_executable(function_ordering_test unittests/function_ordering_test.cpp)
|
||||||
target_link_libraries(function_ordering_test ${LIBS})
|
target_link_libraries(function_ordering_test ${LIBS})
|
||||||
add_test(NAME Function_Ordering_Test COMMAND function_ordering_test)
|
add_test(NAME Function_Ordering_Test COMMAND function_ordering_test)
|
||||||
|
@@ -46,7 +46,7 @@
|
|||||||
namespace chaiscript {
|
namespace chaiscript {
|
||||||
static const int version_major = 5;
|
static const int version_major = 5;
|
||||||
static const int version_minor = 5;
|
static const int version_minor = 5;
|
||||||
static const int version_patch = 0;
|
static const int version_patch = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -202,12 +202,12 @@ namespace chaiscript
|
|||||||
|
|
||||||
static void print(const std::string &s)
|
static void print(const std::string &s)
|
||||||
{
|
{
|
||||||
std::cout << s;
|
fwrite(s.c_str(), 1, s.size(), stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void println(const std::string &s)
|
static void println(const std::string &s)
|
||||||
{
|
{
|
||||||
std::cout << s << std::endl;
|
puts(s.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -443,7 +443,7 @@ namespace chaiscript
|
|||||||
operators::assign<bool>(m);
|
operators::assign<bool>(m);
|
||||||
operators::equal<bool>(m);
|
operators::equal<bool>(m);
|
||||||
|
|
||||||
m->add(fun(&to_string<const std::string &>), "to_string");
|
m->add(fun<std::string (const std::string &t_ss)>([](const std::string &s) -> std::string { return s; }), "to_string");
|
||||||
m->add(fun(&Bootstrap::bool_to_string), "to_string");
|
m->add(fun(&Bootstrap::bool_to_string), "to_string");
|
||||||
m->add(fun(&unknown_assign), "=");
|
m->add(fun(&unknown_assign), "=");
|
||||||
m->add(fun(&throw_exception), "throw");
|
m->add(fun(&throw_exception), "throw");
|
||||||
|
@@ -89,13 +89,13 @@ namespace chaiscript
|
|||||||
if (t_conversions && t_conversions->convertable_type<Type>())
|
if (t_conversions && t_conversions->convertable_type<Type>())
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// std::cout << "trying an up conversion " << typeid(Type).name() << std::endl;
|
// std::cout << "trying an up conversion " << typeid(Type).name() << '\n';
|
||||||
// We will not catch any bad_boxed_dynamic_cast that is thrown, let the user get it
|
// 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>(bv), t_conversions);
|
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_conversion<Type>(bv), t_conversions);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
try {
|
try {
|
||||||
// std::cout << "trying a down conversion " << typeid(Type).name() << std::endl;
|
// std::cout << "trying a down conversion " << typeid(Type).name() << '\n';
|
||||||
// try going the other way - down the inheritance graph
|
// try going the other way - down the inheritance graph
|
||||||
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_down_conversion<Type>(bv), t_conversions);
|
return detail::Cast_Helper<Type>::cast(t_conversions->boxed_type_down_conversion<Type>(bv), t_conversions);
|
||||||
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
} catch (const chaiscript::detail::exception::bad_any_cast &) {
|
||||||
|
@@ -443,17 +443,13 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
/// 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
|
||||||
|
/// it is meant for internal use only
|
||||||
void add_object(const std::string &name, const Boxed_Value &obj) const
|
void add_object(const std::string &name, const Boxed_Value &obj) const
|
||||||
{
|
{
|
||||||
auto &stack = get_stack_data();
|
if (!get_stack_data().back().insert(std::make_pair(name, obj)).second)
|
||||||
validate_object_name(name);
|
|
||||||
|
|
||||||
auto &scope = stack.back();
|
|
||||||
if (scope.find(name) != scope.end())
|
|
||||||
{
|
{
|
||||||
throw chaiscript::exception::name_conflict_error(name);
|
throw chaiscript::exception::name_conflict_error(name);
|
||||||
} else {
|
|
||||||
scope.insert(std::make_pair(name, obj));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -497,11 +493,13 @@ namespace chaiscript
|
|||||||
void new_scope()
|
void new_scope()
|
||||||
{
|
{
|
||||||
get_stack_data().emplace_back();
|
get_stack_data().emplace_back();
|
||||||
|
m_stack_holder->call_params.emplace_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pops the current scope from the stack
|
/// Pops the current scope from the stack
|
||||||
void pop_scope()
|
void pop_scope()
|
||||||
{
|
{
|
||||||
|
m_stack_holder->call_params.pop_back();
|
||||||
StackData &stack = get_stack_data();
|
StackData &stack = get_stack_data();
|
||||||
if (stack.size() > 1)
|
if (stack.size() > 1)
|
||||||
{
|
{
|
||||||
@@ -796,7 +794,7 @@ namespace chaiscript
|
|||||||
/// Dump object info to stdout
|
/// Dump object info to stdout
|
||||||
void dump_object(const Boxed_Value &o) const
|
void dump_object(const Boxed_Value &o) const
|
||||||
{
|
{
|
||||||
std::cout << (o.is_const()?"const ":"") << type_name(o) << std::endl;
|
std::cout << (o.is_const()?"const ":"") << type_name(o) << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dump type info to stdout
|
/// Dump type info to stdout
|
||||||
@@ -830,7 +828,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << ") " << std::endl;
|
std::cout << ") \n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if a call can be made that consists of the first parameter
|
/// Returns true if a call can be made that consists of the first parameter
|
||||||
@@ -850,7 +848,7 @@ namespace chaiscript
|
|||||||
/// Dump all system info to stdout
|
/// Dump all system info to stdout
|
||||||
void dump_system() const
|
void dump_system() const
|
||||||
{
|
{
|
||||||
std::cout << "Registered Types: " << std::endl;
|
std::cout << "Registered Types: \n";
|
||||||
std::vector<std::pair<std::string, Type_Info> > types = get_types();
|
std::vector<std::pair<std::string, Type_Info> > types = get_types();
|
||||||
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
|
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
|
||||||
itr != types.end();
|
itr != types.end();
|
||||||
@@ -858,20 +856,20 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
std::cout << itr->first << ": ";
|
std::cout << itr->first << ": ";
|
||||||
std::cout << itr->second.bare_name();
|
std::cout << itr->second.bare_name();
|
||||||
std::cout << std::endl;
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << std::endl;
|
std::cout << '\n';
|
||||||
std::vector<std::pair<std::string, Proxy_Function > > funcs = get_functions();
|
std::vector<std::pair<std::string, Proxy_Function > > funcs = get_functions();
|
||||||
|
|
||||||
std::cout << "Functions: " << std::endl;
|
std::cout << "Functions: \n";
|
||||||
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
|
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
|
||||||
itr != funcs.end();
|
itr != funcs.end();
|
||||||
++itr)
|
++itr)
|
||||||
{
|
{
|
||||||
dump_function(*itr);
|
dump_function(*itr);
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return true if the Boxed_Value matches the registered type by name
|
/// return true if the Boxed_Value matches the registered type by name
|
||||||
@@ -918,7 +916,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)
|
||||||
{
|
{
|
||||||
Stack_Holder &s = *m_stack_holder;
|
Stack_Holder &s = *m_stack_holder;
|
||||||
s.call_params.insert(s.call_params.begin(), std::move(t_params));
|
s.call_params.back().insert(s.call_params.back().begin(), std::move(t_params));
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_function_params(std::vector<Boxed_Value> &&t_params)
|
void save_function_params(std::vector<Boxed_Value> &&t_params)
|
||||||
@@ -927,14 +925,14 @@ namespace chaiscript
|
|||||||
|
|
||||||
for (auto &¶m : t_params)
|
for (auto &¶m : t_params)
|
||||||
{
|
{
|
||||||
s.call_params.insert(s.call_params.begin(), std::move(param));
|
s.call_params.back().insert(s.call_params.back().begin(), std::move(param));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_function_params(const std::vector<Boxed_Value> &t_params)
|
void save_function_params(const std::vector<Boxed_Value> &t_params)
|
||||||
{
|
{
|
||||||
Stack_Holder &s = *m_stack_holder;
|
Stack_Holder &s = *m_stack_holder;
|
||||||
s.call_params.insert(s.call_params.begin(), t_params.begin(), t_params.end());
|
s.call_params.back().insert(s.call_params.back().begin(), t_params.begin(), t_params.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void new_function_call()
|
void new_function_call()
|
||||||
@@ -945,7 +943,7 @@ namespace chaiscript
|
|||||||
m_conversions.enable_conversion_saves(true);
|
m_conversions.enable_conversion_saves(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
++m_stack_holder->call_depth;
|
++s.call_depth;
|
||||||
|
|
||||||
save_function_params(m_conversions.take_saves());
|
save_function_params(m_conversions.take_saves());
|
||||||
}
|
}
|
||||||
@@ -959,9 +957,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
if (s.call_depth == 0)
|
if (s.call_depth == 0)
|
||||||
{
|
{
|
||||||
/// \todo Critical: this needs to be smarter, memory can expand quickly
|
s.call_params.back().clear();
|
||||||
/// in tight loops involving function calls
|
|
||||||
s.call_params.clear();
|
|
||||||
m_conversions.enable_conversion_saves(false);
|
m_conversions.enable_conversion_saves(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1153,11 +1149,12 @@ namespace chaiscript
|
|||||||
: call_depth(0)
|
: call_depth(0)
|
||||||
{
|
{
|
||||||
stacks.emplace_back(1);
|
stacks.emplace_back(1);
|
||||||
|
call_params.emplace_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::deque<StackData> stacks;
|
std::deque<StackData> stacks;
|
||||||
|
|
||||||
std::list<Boxed_Value> call_params;
|
std::deque<std::list<Boxed_Value>> call_params;
|
||||||
int call_depth;
|
int call_depth;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -429,11 +429,7 @@ namespace chaiscript
|
|||||||
static_assert(std::is_convertible<From, To>::value, "Types are not automatically convertible");
|
static_assert(std::is_convertible<From, To>::value, "Types are not automatically convertible");
|
||||||
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
|
auto func = [](const Boxed_Value &t_bv) -> Boxed_Value {
|
||||||
// not even attempting to call boxed_cast so that we don't get caught in some call recursion
|
// not even attempting to call boxed_cast so that we don't get caught in some call recursion
|
||||||
std::cout << " Type conversion to : " << typeid(To).name() << " from " << typeid(From).name() << std::endl;
|
|
||||||
auto &&from = detail::Cast_Helper<From>::cast(t_bv, nullptr);
|
auto &&from = detail::Cast_Helper<From>::cast(t_bv, nullptr);
|
||||||
std::cout << "Ptr" << static_cast<const void *>(from) << std::endl;
|
|
||||||
std::cout << "Ptr" << from << std::endl;
|
|
||||||
|
|
||||||
To to(from);
|
To to(from);
|
||||||
return chaiscript::Boxed_Value(to);
|
return chaiscript::Boxed_Value(to);
|
||||||
};
|
};
|
||||||
|
@@ -37,7 +37,7 @@ namespace chaiscript
|
|||||||
Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
|
Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
|
||||||
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range,
|
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range,
|
||||||
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
|
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
|
||||||
Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class
|
Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class, Binary
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ namespace chaiscript
|
|||||||
"Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String",
|
"Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String",
|
||||||
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range",
|
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range",
|
||||||
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or",
|
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or",
|
||||||
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop"};
|
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary"};
|
||||||
|
|
||||||
return ast_node_types[ast_node_type];
|
return ast_node_types[ast_node_type];
|
||||||
}
|
}
|
||||||
@@ -118,19 +118,19 @@ namespace chaiscript
|
|||||||
|
|
||||||
ss << what();
|
ss << what();
|
||||||
if (call_stack.size() > 0) {
|
if (call_stack.size() > 0) {
|
||||||
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")" << std::endl;
|
ss << "during evaluation at (" << fname(call_stack[0]) << " " << startpos(call_stack[0]) << ")\n";
|
||||||
ss << std::endl << detail << std::endl;
|
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]) << "'";
|
||||||
for (size_t j = 1; j < call_stack.size(); ++j) {
|
for (size_t j = 1; j < call_stack.size(); ++j) {
|
||||||
if (id(call_stack[j]) != chaiscript::AST_Node_Type::Block
|
if (id(call_stack[j]) != chaiscript::AST_Node_Type::Block
|
||||||
&& id(call_stack[j]) != chaiscript::AST_Node_Type::File)
|
&& id(call_stack[j]) != chaiscript::AST_Node_Type::File)
|
||||||
{
|
{
|
||||||
ss << std::endl;
|
ss << '\n';
|
||||||
ss << " from " << fname(call_stack[j]) << " (" << startpos(call_stack[j]) << ") '" << pretty(call_stack[j]) << "'";
|
ss << " from " << fname(call_stack[j]) << " (" << startpos(call_stack[j]) << ") '" << pretty(call_stack[j]) << "'";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ss << std::endl;
|
ss << '\n';
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,13 +264,13 @@ namespace chaiscript
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
if (t_functions.size() == 1)
|
if (t_functions.size() == 1)
|
||||||
{
|
{
|
||||||
ss << " Expected: " << format_types(t_functions[0], t_dot_notation, t_ss) << std::endl;
|
ss << " Expected: " << format_types(t_functions[0], t_dot_notation, t_ss) << '\n';
|
||||||
} else {
|
} else {
|
||||||
ss << " " << t_functions.size() << " overloads available:" << std::endl;
|
ss << " " << t_functions.size() << " overloads available:\n";
|
||||||
|
|
||||||
for (const auto & t_function : t_functions)
|
for (const auto & t_function : t_functions)
|
||||||
{
|
{
|
||||||
ss << " " << format_types((t_function), t_dot_notation, t_ss) << std::endl;
|
ss << " " << format_types((t_function), t_dot_notation, t_ss) << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -430,7 +430,7 @@ namespace chaiscript
|
|||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
|
|
||||||
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
|
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
|
||||||
<< this->text << " : " << this->start.line << ", " << this->start.column << std::endl;
|
<< this->text << " : " << this->start.line << ", " << this->start.column << '\n';
|
||||||
|
|
||||||
for (size_t j = 0; j < this->children.size(); ++j) {
|
for (size_t j = 0; j < this->children.size(); ++j) {
|
||||||
oss << this->children[j]->to_string(t_prepend + " ");
|
oss << this->children[j]->to_string(t_prepend + " ");
|
||||||
|
@@ -735,11 +735,11 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
const auto name = elem + prefix + t_module_name + postfix;
|
const auto name = elem + prefix + t_module_name + postfix;
|
||||||
// std::cerr << "trying location: " << name << std::endl;
|
// std::cerr << "trying location: " << name << '\n';
|
||||||
load_module(version_stripped_name, name);
|
load_module(version_stripped_name, name);
|
||||||
return name;
|
return name;
|
||||||
} catch (const chaiscript::exception::load_module_error &e) {
|
} catch (const chaiscript::exception::load_module_error &e) {
|
||||||
// std::cerr << "error: " << e.what() << std::endl;
|
// std::cerr << "error: " << e.what() << '\n';
|
||||||
errors.push_back(e);
|
errors.push_back(e);
|
||||||
// Try next set
|
// Try next set
|
||||||
}
|
}
|
||||||
|
@@ -63,25 +63,21 @@ namespace chaiscript
|
|||||||
|
|
||||||
struct Binary_Operator_AST_Node : public AST_Node {
|
struct Binary_Operator_AST_Node : public AST_Node {
|
||||||
public:
|
public:
|
||||||
Binary_Operator_AST_Node(std::string t_ast_node_text, int t_id, const std::shared_ptr<std::string> &t_fname, int t_start_line, int t_start_col, int t_end_line, int t_end_col) :
|
Binary_Operator_AST_Node(const std::string &t_oper) :
|
||||||
AST_Node(std::move(t_ast_node_text), t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
|
AST_Node(t_oper, AST_Node_Type::Binary, std::make_shared<std::string>(""), 0, 0, 0, 0),
|
||||||
|
m_oper(Operators::to_operator(t_oper))
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
virtual ~Binary_Operator_AST_Node() {}
|
virtual ~Binary_Operator_AST_Node() {}
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||||
return do_oper(t_ss, Operators::to_operator(children[1]->text), children[1]->text,
|
return do_oper(t_ss, m_oper, text,
|
||||||
this->children[0]->eval(t_ss),
|
this->children[0]->eval(t_ss),
|
||||||
this->children[2]->eval(t_ss));
|
this->children[1]->eval(t_ss));
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE
|
virtual std::string pretty_print() const CHAISCRIPT_OVERRIDE
|
||||||
{
|
{
|
||||||
if (children.size() == 3)
|
return "(" + this->children[0]->pretty_print() + " " + text + " " + this->children[1]->pretty_print() + ")";
|
||||||
{
|
|
||||||
return "(" + this->children[0]->pretty_print() + " " + this->children[1]->text + " " + this->children[2]->pretty_print() + ")";
|
|
||||||
} else {
|
|
||||||
return "(" + this->children[0]->pretty_print() + " " + text + " " + this->children[1]->pretty_print() + ")";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -110,6 +106,9 @@ namespace chaiscript
|
|||||||
throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, e.functions, false, t_ss);
|
throw exception::eval_error("Can not find appropriate '" + t_oper_string + "' operator.", e.parameters, e.functions, false, t_ss);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Operators::Opers m_oper;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Int_AST_Node : public AST_Node {
|
struct Int_AST_Node : public AST_Node {
|
||||||
@@ -316,8 +315,6 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
catch(const exception::bad_boxed_cast &){
|
catch(const exception::bad_boxed_cast &){
|
||||||
// handle the case where there is only 1 function to try to call and dispatch fails on it
|
// handle the case where there is only 1 function to try to call and dispatch fails on it
|
||||||
std::vector<Const_Proxy_Function> funcs;
|
|
||||||
funcs.push_back(fn);
|
|
||||||
throw exception::eval_error("Error calling function '" + this->children[0]->text + "'", params, {fn}, false, t_ss);
|
throw exception::eval_error("Error calling function '" + this->children[0]->text + "'", params, {fn}, false, t_ss);
|
||||||
}
|
}
|
||||||
catch(const exception::arity_error &e){
|
catch(const exception::arity_error &e){
|
||||||
@@ -382,6 +379,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
virtual ~Equation_AST_Node() {}
|
virtual ~Equation_AST_Node() {}
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
||||||
|
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
||||||
Boxed_Value retval = this->children.back()->eval(t_ss);
|
Boxed_Value retval = this->children.back()->eval(t_ss);
|
||||||
|
|
||||||
|
|
||||||
@@ -454,7 +452,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
return this->children[0]->eval(t_ss);
|
return this->children[0]->eval(t_ss);
|
||||||
} else {
|
} else {
|
||||||
std::string idname = this->children[0]->text;
|
const std::string &idname = this->children[0]->text;
|
||||||
|
|
||||||
Boxed_Value bv;
|
Boxed_Value bv;
|
||||||
try {
|
try {
|
||||||
@@ -477,72 +475,6 @@ namespace chaiscript
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Comparison_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Comparison_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Comparison, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Comparison_AST_Node() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Addition_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Addition_AST_Node(std::string t_ast_node_text = "+",
|
|
||||||
const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(),
|
|
||||||
int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Addition, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Addition_AST_Node() {}
|
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
|
||||||
return do_oper(t_ss, Operators::sum, "+", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Subtraction_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Subtraction_AST_Node(std::string t_ast_node_text = "-",
|
|
||||||
const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0,
|
|
||||||
int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Subtraction, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Subtraction_AST_Node() {}
|
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
|
||||||
return do_oper(t_ss, Operators::difference, "-", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Multiplication_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Multiplication_AST_Node(std::string t_ast_node_text = "*",
|
|
||||||
const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0,
|
|
||||||
int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Multiplication, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Multiplication_AST_Node() {}
|
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
|
||||||
return do_oper(t_ss, Operators::product, "*", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Division_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Division_AST_Node(std::string t_ast_node_text = "/",
|
|
||||||
const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0,
|
|
||||||
int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Division, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Division_AST_Node() {}
|
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
|
||||||
return do_oper(t_ss, Operators::quotient, "/", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Modulus_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Modulus_AST_Node(std::string t_ast_node_text = "%",
|
|
||||||
const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0,
|
|
||||||
int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(std::move(t_ast_node_text), AST_Node_Type::Modulus, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Modulus_AST_Node() {}
|
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE {
|
|
||||||
return do_oper(t_ss, Operators::remainder, "%", this->children[0]->eval(t_ss), this->children[1]->eval(t_ss));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Array_Call_AST_Node : public AST_Node {
|
struct Array_Call_AST_Node : public AST_Node {
|
||||||
public:
|
public:
|
||||||
@@ -595,8 +527,8 @@ namespace chaiscript
|
|||||||
AST_Node(t_ast_node_text, AST_Node_Type::Dot_Access, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
AST_Node(t_ast_node_text, AST_Node_Type::Dot_Access, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
||||||
virtual ~Dot_Access_AST_Node() {}
|
virtual ~Dot_Access_AST_Node() {}
|
||||||
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &t_ss) const CHAISCRIPT_OVERRIDE{
|
||||||
Boxed_Value retval = this->children[0]->eval(t_ss);
|
|
||||||
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
||||||
|
Boxed_Value retval = this->children[0]->eval(t_ss);
|
||||||
|
|
||||||
if (this->children.size() > 1) {
|
if (this->children.size() > 1) {
|
||||||
for (size_t i = 2; i < this->children.size(); i+=2) {
|
for (size_t i = 2; i < this->children.size(); i+=2) {
|
||||||
@@ -1144,8 +1076,9 @@ namespace chaiscript
|
|||||||
|
|
||||||
struct Prefix_AST_Node : public AST_Node {
|
struct Prefix_AST_Node : public AST_Node {
|
||||||
public:
|
public:
|
||||||
Prefix_AST_Node(std::string t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
Prefix_AST_Node(Operators::Opers t_oper) :
|
||||||
AST_Node(std::move(t_ast_node_text), AST_Node_Type::Prefix, t_fname, t_start_line, t_start_col, t_end_line, t_end_col)
|
AST_Node("", AST_Node_Type::Prefix, std::make_shared<std::string>(""), 0, 0, 0, 0),
|
||||||
|
m_oper(t_oper)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
virtual ~Prefix_AST_Node() {}
|
virtual ~Prefix_AST_Node() {}
|
||||||
@@ -1153,12 +1086,11 @@ namespace chaiscript
|
|||||||
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
chaiscript::eval::detail::Function_Push_Pop fpp(t_ss);
|
||||||
Boxed_Value bv(this->children[1]->eval(t_ss));
|
Boxed_Value bv(this->children[1]->eval(t_ss));
|
||||||
|
|
||||||
Operators::Opers oper = Operators::to_operator(children[0]->text, true);
|
|
||||||
try {
|
try {
|
||||||
// short circuit arithmetic operations
|
// short circuit arithmetic operations
|
||||||
if (bv.get_type_info().is_arithmetic() && oper != Operators::invalid)
|
if (m_oper != Operators::invalid && bv.get_type_info().is_arithmetic())
|
||||||
{
|
{
|
||||||
return Boxed_Number::do_oper(oper, std::move(bv));
|
return Boxed_Number::do_oper(m_oper, std::move(bv));
|
||||||
} else {
|
} else {
|
||||||
chaiscript::eval::detail::Stack_Push_Pop spp(t_ss);
|
chaiscript::eval::detail::Stack_Push_Pop spp(t_ss);
|
||||||
fpp.save_params({bv});
|
fpp.save_params({bv});
|
||||||
@@ -1169,6 +1101,8 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Operators::Opers m_oper;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Break_AST_Node : public AST_Node {
|
struct Break_AST_Node : public AST_Node {
|
||||||
@@ -1512,40 +1446,6 @@ namespace chaiscript
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Shift_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Shift_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Shift, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Shift_AST_Node() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Equality_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Equality_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Equality, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Equality_AST_Node() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Bitwise_And_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Bitwise_And_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_And, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Bitwise_And_AST_Node() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Bitwise_Xor_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Bitwise_Xor_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_Xor, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Bitwise_Xor_AST_Node() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Bitwise_Or_AST_Node : public Binary_Operator_AST_Node {
|
|
||||||
public:
|
|
||||||
Bitwise_Or_AST_Node(const std::string &t_ast_node_text = "", const std::shared_ptr<std::string> &t_fname=std::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
|
|
||||||
Binary_Operator_AST_Node(t_ast_node_text, AST_Node_Type::Bitwise_Or, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { }
|
|
||||||
virtual ~Bitwise_Or_AST_Node() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Logical_And_AST_Node : public AST_Node {
|
struct Logical_And_AST_Node : public AST_Node {
|
||||||
public:
|
public:
|
||||||
|
@@ -171,7 +171,7 @@ namespace chaiscript
|
|||||||
/// Prints the parsed ast_nodes as a tree
|
/// Prints the parsed ast_nodes as a tree
|
||||||
/*
|
/*
|
||||||
void debug_print(AST_NodePtr t, std::string prepend = "") {
|
void debug_print(AST_NodePtr t, std::string prepend = "") {
|
||||||
std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << std::endl;
|
std::cout << prepend << "(" << ast_node_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << '\n';
|
||||||
for (unsigned int j = 0; j < t->children.size(); ++j) {
|
for (unsigned int j = 0; j < t->children.size(); ++j) {
|
||||||
debug_print(t->children[j], prepend + " ");
|
debug_print(t->children[j], prepend + " ");
|
||||||
}
|
}
|
||||||
@@ -787,7 +787,7 @@ namespace chaiscript
|
|||||||
//If we've seen previous interpolation, add on instead of making a new one
|
//If we've seen previous interpolation, add on instead of making a new one
|
||||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
build_match(std::make_shared<eval::Binary_Operator_AST_Node>("+"), prev_stack_top);
|
||||||
} else {
|
} else {
|
||||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||||
}
|
}
|
||||||
@@ -824,7 +824,7 @@ namespace chaiscript
|
|||||||
build_match(std::make_shared<eval::Inplace_Fun_Call_AST_Node>(), ev_stack_top);
|
build_match(std::make_shared<eval::Inplace_Fun_Call_AST_Node>(), ev_stack_top);
|
||||||
build_match(std::make_shared<eval::Arg_List_AST_Node>(), ev_stack_top);
|
build_match(std::make_shared<eval::Arg_List_AST_Node>(), ev_stack_top);
|
||||||
build_match(std::make_shared<eval::Fun_Call_AST_Node>(), tostr_stack_top);
|
build_match(std::make_shared<eval::Fun_Call_AST_Node>(), tostr_stack_top);
|
||||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
build_match(std::make_shared<eval::Binary_Operator_AST_Node>("+"), prev_stack_top);
|
||||||
} else {
|
} else {
|
||||||
throw exception::eval_error("Unclosed in-string eval", File_Position(prev_line, prev_col), *m_filename);
|
throw exception::eval_error("Unclosed in-string eval", File_Position(prev_line, prev_col), *m_filename);
|
||||||
}
|
}
|
||||||
@@ -867,7 +867,7 @@ namespace chaiscript
|
|||||||
if (is_interpolated) {
|
if (is_interpolated) {
|
||||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
build_match(std::make_shared<eval::Binary_Operator_AST_Node>("+"), prev_stack_top);
|
||||||
} else {
|
} else {
|
||||||
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
m_match_stack.push_back(std::make_shared<eval::Quoted_String_AST_Node>(match, m_filename, prev_line, prev_col, m_line, m_col));
|
||||||
}
|
}
|
||||||
@@ -1835,72 +1835,24 @@ namespace chaiscript
|
|||||||
|
|
||||||
/// Reads a unary prefixed expression from input
|
/// Reads a unary prefixed expression from input
|
||||||
bool Prefix() {
|
bool Prefix() {
|
||||||
bool retval = false;
|
|
||||||
|
|
||||||
const auto prev_stack_top = m_match_stack.size();
|
const auto prev_stack_top = m_match_stack.size();
|
||||||
|
const std::vector<std::string> prefix_opers{"++", "--", "-", "+", "!", "~", "&"};
|
||||||
|
|
||||||
if (Symbol("++", true)) {
|
for (const auto &oper : prefix_opers)
|
||||||
retval = true;
|
{
|
||||||
|
bool is_char = oper.size() == 1;
|
||||||
|
if ((is_char && Char(oper[0], true)) || (!is_char && Symbol(oper.c_str(), true)))
|
||||||
|
{
|
||||||
|
if (!Operator(m_operators.size()-1)) {
|
||||||
|
throw exception::eval_error("Incomplete prefix '" + oper + "' expression", File_Position(m_line, m_col), *m_filename);
|
||||||
|
}
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
build_match(std::make_shared<eval::Prefix_AST_Node>(Operators::to_operator(oper, true)), prev_stack_top);
|
||||||
throw exception::eval_error("Incomplete '++' expression", File_Position(m_line, m_col), *m_filename);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
} else if (Symbol("--", true)) {
|
|
||||||
retval = true;
|
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
|
||||||
throw exception::eval_error("Incomplete '--' expression", File_Position(m_line, m_col), *m_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
} else if (Char('-', true)) {
|
|
||||||
retval = true;
|
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
|
||||||
throw exception::eval_error("Incomplete unary '-' expression", File_Position(m_line, m_col), *m_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
} else if (Char('+', true)) {
|
|
||||||
retval = true;
|
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
|
||||||
throw exception::eval_error("Incomplete unary '+' expression", File_Position(m_line, m_col), *m_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
else if (Char('!', true)) {
|
|
||||||
retval = true;
|
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
|
||||||
throw exception::eval_error("Incomplete '!' expression", File_Position(m_line, m_col), *m_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
else if (Char('~', true)) {
|
|
||||||
retval = true;
|
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
|
||||||
throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
else if (Char('&', true)) {
|
|
||||||
retval = true;
|
|
||||||
|
|
||||||
if (!Operator(m_operators.size()-1)) {
|
|
||||||
throw exception::eval_error("Incomplete '~' expression", File_Position(m_line, m_col), *m_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
build_match(std::make_shared<eval::Prefix_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses any of a group of 'value' style ast_node groups from input
|
/// Parses any of a group of 'value' style ast_node groups from input
|
||||||
@@ -1923,7 +1875,6 @@ namespace chaiscript
|
|||||||
|
|
||||||
bool Operator(const size_t t_precedence = 0) {
|
bool Operator(const size_t t_precedence = 0) {
|
||||||
bool retval = false;
|
bool retval = false;
|
||||||
AST_NodePtr oper;
|
|
||||||
const auto prev_stack_top = m_match_stack.size();
|
const auto prev_stack_top = m_match_stack.size();
|
||||||
|
|
||||||
if (t_precedence < m_operators.size()) {
|
if (t_precedence < m_operators.size()) {
|
||||||
@@ -1938,10 +1889,9 @@ namespace chaiscript
|
|||||||
File_Position(m_line, m_col), *m_filename);
|
File_Position(m_line, m_col), *m_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AST_NodePtr oper = m_match_stack.at(m_match_stack.size()-2);
|
||||||
|
|
||||||
switch (m_operators[t_precedence]) {
|
switch (m_operators[t_precedence]) {
|
||||||
case(AST_Node_Type::Comparison) :
|
|
||||||
build_match(std::make_shared<eval::Comparison_AST_Node>(), prev_stack_top);
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Ternary_Cond) :
|
case(AST_Node_Type::Ternary_Cond) :
|
||||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
||||||
m_match_stack.begin() + m_match_stack.size() - 1);
|
m_match_stack.begin() + m_match_stack.size() - 1);
|
||||||
@@ -1959,52 +1909,26 @@ namespace chaiscript
|
|||||||
File_Position(m_line, m_col), *m_filename);
|
File_Position(m_line, m_col), *m_filename);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case(AST_Node_Type::Addition) :
|
case(AST_Node_Type::Addition) :
|
||||||
oper = m_match_stack.at(m_match_stack.size()-2);
|
|
||||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
|
||||||
m_match_stack.begin() + m_match_stack.size() - 1);
|
|
||||||
if (oper->text == "+") {
|
|
||||||
build_match(std::make_shared<eval::Addition_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
else if (oper->text == "-") {
|
|
||||||
build_match(std::make_shared<eval::Subtraction_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Multiplication) :
|
case(AST_Node_Type::Multiplication) :
|
||||||
oper = m_match_stack.at(m_match_stack.size()-2);
|
|
||||||
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2,
|
|
||||||
m_match_stack.begin() + m_match_stack.size() - 1);
|
|
||||||
if (oper->text == "*") {
|
|
||||||
build_match(std::make_shared<eval::Multiplication_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
else if (oper->text == "/") {
|
|
||||||
build_match(std::make_shared<eval::Division_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
else if (oper->text == "%") {
|
|
||||||
build_match(std::make_shared<eval::Modulus_AST_Node>(), prev_stack_top);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Shift) :
|
case(AST_Node_Type::Shift) :
|
||||||
build_match(std::make_shared<eval::Shift_AST_Node>(), prev_stack_top);
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Equality) :
|
case(AST_Node_Type::Equality) :
|
||||||
build_match(std::make_shared<eval::Equality_AST_Node>(), prev_stack_top);
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Bitwise_And) :
|
case(AST_Node_Type::Bitwise_And) :
|
||||||
build_match(std::make_shared<eval::Bitwise_And_AST_Node>(), prev_stack_top);
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Bitwise_Xor) :
|
case(AST_Node_Type::Bitwise_Xor) :
|
||||||
build_match(std::make_shared<eval::Bitwise_Xor_AST_Node>(), prev_stack_top);
|
|
||||||
break;
|
|
||||||
case(AST_Node_Type::Bitwise_Or) :
|
case(AST_Node_Type::Bitwise_Or) :
|
||||||
build_match(std::make_shared<eval::Bitwise_Or_AST_Node>(), prev_stack_top);
|
case(AST_Node_Type::Comparison) :
|
||||||
|
m_match_stack.erase(m_match_stack.begin() + m_match_stack.size() - 2, m_match_stack.begin() + m_match_stack.size() - 1);
|
||||||
|
build_match(std::make_shared<eval::Binary_Operator_AST_Node>(oper->text), prev_stack_top);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case(AST_Node_Type::Logical_And) :
|
case(AST_Node_Type::Logical_And) :
|
||||||
build_match(std::make_shared<eval::Logical_And_AST_Node>(), prev_stack_top);
|
build_match(std::make_shared<eval::Logical_And_AST_Node>(), prev_stack_top);
|
||||||
break;
|
break;
|
||||||
case(AST_Node_Type::Logical_Or) :
|
case(AST_Node_Type::Logical_Or) :
|
||||||
build_match(std::make_shared<eval::Logical_Or_AST_Node>(), prev_stack_top);
|
build_match(std::make_shared<eval::Logical_Or_AST_Node>(), prev_stack_top);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_line, m_col), *m_filename);
|
throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_line, m_col), *m_filename);
|
||||||
}
|
}
|
||||||
@@ -2111,14 +2035,7 @@ namespace chaiscript
|
|||||||
while (has_more) {
|
while (has_more) {
|
||||||
const auto prev_line = m_line;
|
const auto prev_line = m_line;
|
||||||
const auto prev_col = m_col;
|
const auto prev_col = m_col;
|
||||||
if (Def(true)) {
|
if (Def(true) || Var_Decl(true)) {
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
} else if (Var_Decl(true)) {
|
|
||||||
if (!saw_eol) {
|
if (!saw_eol) {
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||||
}
|
}
|
||||||
@@ -2147,7 +2064,7 @@ namespace chaiscript
|
|||||||
while (has_more) {
|
while (has_more) {
|
||||||
int prev_line = m_line;
|
int prev_line = m_line;
|
||||||
int prev_col = m_col;
|
int prev_col = m_col;
|
||||||
if (Def()) {
|
if (Def() || Try() || If() || While() || Class() || For() || Switch()) {
|
||||||
if (!saw_eol) {
|
if (!saw_eol) {
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||||
}
|
}
|
||||||
@@ -2155,55 +2072,7 @@ namespace chaiscript
|
|||||||
retval = true;
|
retval = true;
|
||||||
saw_eol = true;
|
saw_eol = true;
|
||||||
}
|
}
|
||||||
else if (Try()) {
|
else if (Return() || Break() || Continue() || Equation()) {
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (If()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (While()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (Class()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (For()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (Switch()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two function definitions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (Return()) {
|
|
||||||
if (!saw_eol) {
|
if (!saw_eol) {
|
||||||
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
||||||
}
|
}
|
||||||
@@ -2211,36 +2080,7 @@ namespace chaiscript
|
|||||||
retval = true;
|
retval = true;
|
||||||
saw_eol = false;
|
saw_eol = false;
|
||||||
}
|
}
|
||||||
else if (Break()) {
|
else if (Block() || Eol()) {
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = false;
|
|
||||||
}
|
|
||||||
else if (Continue()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = false;
|
|
||||||
}
|
|
||||||
else if (Block()) {
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = true;
|
|
||||||
}
|
|
||||||
else if (Equation()) {
|
|
||||||
if (!saw_eol) {
|
|
||||||
throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename);
|
|
||||||
}
|
|
||||||
has_more = true;
|
|
||||||
retval = true;
|
|
||||||
saw_eol = false;
|
|
||||||
}
|
|
||||||
else if (Eol()) {
|
|
||||||
has_more = true;
|
has_more = true;
|
||||||
retval = true;
|
retval = true;
|
||||||
saw_eol = true;
|
saw_eol = true;
|
||||||
|
@@ -16,6 +16,8 @@ Release under the BSD license, see "license.txt" for details.
|
|||||||
Introduction
|
Introduction
|
||||||
============
|
============
|
||||||
|
|
||||||
|
[](https://gitter.im/ChaiScript/ChaiScript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
|
||||||
ChaiScript is one of the only embedded scripting language designed from the
|
ChaiScript is one of the only embedded scripting language designed from the
|
||||||
ground up to directly target C++ and take advantage of modern C++ development
|
ground up to directly target C++ and take advantage of modern C++ development
|
||||||
techniques, working with the developer like he expects it to work. Being a
|
techniques, working with the developer like he expects it to work. Being a
|
||||||
|
@@ -1,6 +1,11 @@
|
|||||||
Notes:
|
Notes:
|
||||||
=======
|
=======
|
||||||
Current Version: 5.5.0
|
Current Version: 5.5.1
|
||||||
|
|
||||||
|
### Changes since 5.5.0
|
||||||
|
* 30% performance increase
|
||||||
|
* Fix handling of object stack, resulting in greatly reduced memory usage
|
||||||
|
* Code cleanups
|
||||||
|
|
||||||
### Changes since 5.4.0
|
### Changes since 5.4.0
|
||||||
* 2x performance increase
|
* 2x performance increase
|
||||||
|
@@ -13,12 +13,12 @@
|
|||||||
|
|
||||||
void log(const std::string &msg)
|
void log(const std::string &msg)
|
||||||
{
|
{
|
||||||
std::cout << "[" << time(nullptr) << "] " << msg << std::endl;
|
std::cout << "[" << time(nullptr) << "] " << msg << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(const std::string &module, const std::string &msg)
|
void log(const std::string &module, const std::string &msg)
|
||||||
{
|
{
|
||||||
std::cout << "[" << time(nullptr) << "] <" << module << "> " << msg << std::endl;
|
std::cout << "[" << time(nullptr) << "] <" << module << "> " << msg << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
void bound_log(const std::string &msg)
|
void bound_log(const std::string &msg)
|
||||||
@@ -28,12 +28,12 @@ void bound_log(const std::string &msg)
|
|||||||
|
|
||||||
void hello_world(const chaiscript::Boxed_Value & /*o*/)
|
void hello_world(const chaiscript::Boxed_Value & /*o*/)
|
||||||
{
|
{
|
||||||
std::cout << "Hello World" << std::endl;
|
std::cout << "Hello World\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void hello_constructor(const chaiscript::Boxed_Value & /*o*/)
|
void hello_constructor(const chaiscript::Boxed_Value & /*o*/)
|
||||||
{
|
{
|
||||||
std::cout << "Hello Constructor" << std::endl;
|
std::cout << "Hello Constructor\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ struct System
|
|||||||
|
|
||||||
void take_shared_ptr(const std::shared_ptr<const std::string> &p)
|
void take_shared_ptr(const std::shared_ptr<const std::string> &p)
|
||||||
{
|
{
|
||||||
std::cout << *p << std::endl;
|
std::cout << *p << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int /*argc*/, char * /*argv*/[]) {
|
int main(int /*argc*/, char * /*argv*/[]) {
|
||||||
@@ -122,7 +122,7 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
|||||||
//the templated version of eval:
|
//the templated version of eval:
|
||||||
int i = chai.eval<int>("5+5");
|
int i = chai.eval<int>("5+5");
|
||||||
|
|
||||||
std::cout << "5+5: " << i << std::endl;
|
std::cout << "5+5: " << i << '\n';
|
||||||
|
|
||||||
//Add a new variable
|
//Add a new variable
|
||||||
chai("var scripti = 15");
|
chai("var scripti = 15");
|
||||||
@@ -130,9 +130,9 @@ int main(int /*argc*/, char * /*argv*/[]) {
|
|||||||
//We can even get a handle to the variables in the system
|
//We can even get a handle to the variables in the system
|
||||||
int &scripti = chai.eval<int &>("scripti");
|
int &scripti = chai.eval<int &>("scripti");
|
||||||
|
|
||||||
std::cout << "scripti: " << scripti << std::endl;
|
std::cout << "scripti: " << scripti << '\n';
|
||||||
scripti *= 2;
|
scripti *= 2;
|
||||||
std::cout << "scripti (updated): " << scripti << std::endl;
|
std::cout << "scripti (updated): " << scripti << '\n';
|
||||||
chai("print(\"Scripti from chai: \" + to_string(scripti))");
|
chai("print(\"Scripti from chai: \" + to_string(scripti))");
|
||||||
|
|
||||||
//To do: Add examples of handling Boxed_Values directly when needed
|
//To do: Add examples of handling Boxed_Values directly when needed
|
||||||
|
4
samples/fun_call_performance.chai
Normal file
4
samples/fun_call_performance.chai
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
for (var i = 0; i < 1000; ++i) {
|
||||||
|
puts(helloWorld("Bob12345"))
|
||||||
|
}
|
||||||
|
|
407
samples/fun_call_performance.cpp
Normal file
407
samples/fun_call_performance.cpp
Normal file
@@ -0,0 +1,407 @@
|
|||||||
|
// This file is distributed under the BSD License.
|
||||||
|
// See "license.txt" for details.
|
||||||
|
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||||
|
// Copyright 2009-2014, Jason Turner (jason@emptycrate.com)
|
||||||
|
// http://www.chaiscript.com
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <list>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
#include <chaiscript/chaiscript.hpp>
|
||||||
|
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||||
|
|
||||||
|
#ifdef READLINE_AVAILABLE
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
#else
|
||||||
|
|
||||||
|
char *mystrdup(const char *s) {
|
||||||
|
size_t len = strlen(s); // Space for length plus nul
|
||||||
|
char *d = static_cast<char*>(malloc(len + 1));
|
||||||
|
if (d == nullptr) return nullptr; // No memory
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
strcpy_s(d, len + 1, s); // Copy the characters
|
||||||
|
#else
|
||||||
|
strncpy(d, s, len); // Copy the characters
|
||||||
|
#endif
|
||||||
|
d[len] = '\0';
|
||||||
|
return d; // Return the new string
|
||||||
|
}
|
||||||
|
|
||||||
|
char* readline(const char* p)
|
||||||
|
{
|
||||||
|
std::string retval;
|
||||||
|
std::cout << p;
|
||||||
|
std::getline(std::cin, retval);
|
||||||
|
return std::cin.eof() ? nullptr : mystrdup(retval.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void add_history(const char*){}
|
||||||
|
void using_history(){}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void *cast_module_symbol(std::vector<std::string>(*t_path)())
|
||||||
|
{
|
||||||
|
union cast_union
|
||||||
|
{
|
||||||
|
std::vector<std::string>(*in_ptr)();
|
||||||
|
void *out_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
cast_union c;
|
||||||
|
c.in_ptr = t_path;
|
||||||
|
return c.out_ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> default_search_paths()
|
||||||
|
{
|
||||||
|
std::vector<std::string> paths;
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_WINDOWS // force no unicode
|
||||||
|
CHAR path[4096];
|
||||||
|
int size = GetModuleFileNameA(0, path, sizeof(path) - 1);
|
||||||
|
|
||||||
|
std::string exepath(path, size);
|
||||||
|
|
||||||
|
size_t lastslash = exepath.rfind('\\');
|
||||||
|
size_t secondtolastslash = exepath.rfind('\\', lastslash - 1);
|
||||||
|
if (lastslash != std::string::npos)
|
||||||
|
{
|
||||||
|
paths.push_back(exepath.substr(0, lastslash));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secondtolastslash != std::string::npos)
|
||||||
|
{
|
||||||
|
return{ exepath.substr(0, secondtolastslash) + "\\lib\\chaiscript\\" };
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
std::string exepath;
|
||||||
|
|
||||||
|
std::vector<char> buf(2048);
|
||||||
|
ssize_t size = -1;
|
||||||
|
|
||||||
|
if ((size = readlink("/proc/self/exe", &buf.front(), buf.size())) != -1)
|
||||||
|
{
|
||||||
|
exepath = std::string(&buf.front(), size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exepath.empty())
|
||||||
|
{
|
||||||
|
if ((size = readlink("/proc/curproc/file", &buf.front(), buf.size())) != -1)
|
||||||
|
{
|
||||||
|
exepath = std::string(&buf.front(), size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exepath.empty())
|
||||||
|
{
|
||||||
|
if ((size = readlink("/proc/self/path/a.out", &buf.front(), buf.size())) != -1)
|
||||||
|
{
|
||||||
|
exepath = std::string(&buf.front(), size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exepath.empty())
|
||||||
|
{
|
||||||
|
Dl_info rInfo;
|
||||||
|
memset(&rInfo, 0, sizeof(rInfo));
|
||||||
|
if (!dladdr(cast_module_symbol(&default_search_paths), &rInfo) || !rInfo.dli_fname) {
|
||||||
|
return paths;
|
||||||
|
}
|
||||||
|
|
||||||
|
exepath = std::string(rInfo.dli_fname);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t lastslash = exepath.rfind('/');
|
||||||
|
|
||||||
|
size_t secondtolastslash = exepath.rfind('/', lastslash - 1);
|
||||||
|
if (lastslash != std::string::npos)
|
||||||
|
{
|
||||||
|
paths.push_back(exepath.substr(0, lastslash));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (secondtolastslash != std::string::npos)
|
||||||
|
{
|
||||||
|
paths.push_back(exepath.substr(0, secondtolastslash) + "/lib/chaiscript/");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return paths;
|
||||||
|
}
|
||||||
|
|
||||||
|
void help(int n) {
|
||||||
|
if (n >= 0) {
|
||||||
|
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl;
|
||||||
|
std::cout << "Additionally, you can inspect the runtime system using:" << std::endl;
|
||||||
|
std::cout << " dump_system() - outputs all functions registered to the system" << std::endl;
|
||||||
|
std::cout << " dump_object(x) - dumps information about the given symbol" << std::endl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "usage : chai [option]+" << std::endl;
|
||||||
|
std::cout << "option:" << std::endl;
|
||||||
|
std::cout << " -h | --help" << std::endl;
|
||||||
|
std::cout << " -i | --interactive" << std::endl;
|
||||||
|
std::cout << " -c | --command cmd" << std::endl;
|
||||||
|
std::cout << " -v | --version" << std::endl;
|
||||||
|
std::cout << " - --stdin" << std::endl;
|
||||||
|
std::cout << " filepath" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void version(int){
|
||||||
|
std::cout << "chai: compiled " << __TIME__ << " " << __DATE__ << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string helloWorld(const std::string &t_name)
|
||||||
|
{
|
||||||
|
return "Hello " + t_name + "!";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool throws_exception(const std::function<void()> &f)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
chaiscript::exception::eval_error get_eval_error(const std::function<void()> &f)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
catch (const chaiscript::exception::eval_error &e) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw std::runtime_error("no exception throw");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string get_next_command() {
|
||||||
|
std::string retval("quit");
|
||||||
|
if (!std::cin.eof()) {
|
||||||
|
char *input_raw = readline("eval> ");
|
||||||
|
if (input_raw) {
|
||||||
|
add_history(input_raw);
|
||||||
|
|
||||||
|
std::string val(input_raw);
|
||||||
|
size_t pos = val.find_first_not_of("\t \n");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
val.erase(0, pos);
|
||||||
|
}
|
||||||
|
pos = val.find_last_not_of("\t \n");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
{
|
||||||
|
val.erase(pos + 1, std::string::npos);
|
||||||
|
}
|
||||||
|
|
||||||
|
retval = val;
|
||||||
|
|
||||||
|
::free(input_raw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (retval == "quit"
|
||||||
|
|| retval == "exit"
|
||||||
|
|| retval == "help"
|
||||||
|
|| retval == "version")
|
||||||
|
{
|
||||||
|
retval += "(0)";
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have to wrap exit with our own because Clang has a hard time with
|
||||||
|
// function pointers to functions with special attributes (system exit being marked NORETURN)
|
||||||
|
void myexit(int return_val) {
|
||||||
|
exit(return_val);
|
||||||
|
}
|
||||||
|
|
||||||
|
void interactive(chaiscript::ChaiScript& chai)
|
||||||
|
{
|
||||||
|
using_history();
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
std::string input = get_next_command();
|
||||||
|
try {
|
||||||
|
// evaluate input
|
||||||
|
chaiscript::Boxed_Value val = chai.eval(input);
|
||||||
|
|
||||||
|
//Then, we try to print the result of the evaluation to the user
|
||||||
|
if (!val.get_type_info().bare_equal(chaiscript::user_type<void>())) {
|
||||||
|
try {
|
||||||
|
std::cout << chai.eval<std::function<std::string(const chaiscript::Boxed_Value &bv)> >("to_string")(val) << std::endl;
|
||||||
|
}
|
||||||
|
catch (...) {} //If we can't, do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const chaiscript::exception::eval_error &ee) {
|
||||||
|
std::cout << ee.what();
|
||||||
|
if (ee.call_stack.size() > 0) {
|
||||||
|
std::cout << "during evaluation at (" << ee.call_stack[0]->start.line << ", " << ee.call_stack[0]->start.column << ")";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
catch (const std::exception &e) {
|
||||||
|
std::cout << e.what();
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
// Disable deprecation warning for getenv call.
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable : 4996)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const char *usepath = getenv("CHAI_USE_PATH");
|
||||||
|
const char *modulepath = getenv("CHAI_MODULE_PATH");
|
||||||
|
|
||||||
|
#ifdef CHAISCRIPT_MSVC
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
std::vector<std::string> usepaths;
|
||||||
|
usepaths.push_back("");
|
||||||
|
if (usepath)
|
||||||
|
{
|
||||||
|
usepaths.push_back(usepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> modulepaths;
|
||||||
|
std::vector<std::string> searchpaths = default_search_paths();
|
||||||
|
modulepaths.insert(modulepaths.end(), searchpaths.begin(), searchpaths.end());
|
||||||
|
modulepaths.push_back("");
|
||||||
|
if (modulepath)
|
||||||
|
{
|
||||||
|
modulepaths.push_back(modulepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
//chaiscript::ChaiScript chai(modulepaths, usepaths);
|
||||||
|
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library(), usepaths);
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun(&myexit), "exit");
|
||||||
|
chai.add(chaiscript::fun(&myexit), "quit");
|
||||||
|
chai.add(chaiscript::fun(&help), "help");
|
||||||
|
chai.add(chaiscript::fun(&version), "version");
|
||||||
|
chai.add(chaiscript::fun(&throws_exception), "throws_exception");
|
||||||
|
chai.add(chaiscript::fun(&get_eval_error), "get_eval_error");
|
||||||
|
|
||||||
|
chai.add(chaiscript::fun(&helloWorld), "helloWorld");
|
||||||
|
|
||||||
|
clock_t begin = clock();
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; i++)
|
||||||
|
{
|
||||||
|
std::string str = helloWorld("Bob12345");
|
||||||
|
fwrite(str.c_str(), 1, str.size(), stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
clock_t end = clock();
|
||||||
|
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
//begin = clock();
|
||||||
|
|
||||||
|
////for (int i = 0; i < 1000; i++)
|
||||||
|
////{
|
||||||
|
//// chai.eval("puts(helloWorld(\"Bob12345\"));");
|
||||||
|
////}
|
||||||
|
//chai.eval_file("E:\\C++\\ChaiScript - 5.4.0\\samples\forx.chai");
|
||||||
|
|
||||||
|
//end = clock();
|
||||||
|
//elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
|
||||||
|
//printf("**MyProgram::time= %lf\n", elapsed_secs);
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; ++i) {
|
||||||
|
if (i == 0 && argc > 1) {
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string arg(i ? argv[i] : "--interactive");
|
||||||
|
|
||||||
|
enum {
|
||||||
|
eInteractive
|
||||||
|
, eCommand
|
||||||
|
, eFile
|
||||||
|
} mode = eCommand;
|
||||||
|
|
||||||
|
if (arg == "-c" || arg == "--command") {
|
||||||
|
if ((i + 1) >= argc) {
|
||||||
|
std::cout << "insufficient input following " << arg << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
arg = argv[++i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (arg == "-" || arg == "--stdin") {
|
||||||
|
arg = "";
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(std::cin, line)) {
|
||||||
|
arg += line + '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (arg == "-v" || arg == "--version") {
|
||||||
|
arg = "version(0)";
|
||||||
|
}
|
||||||
|
else if (arg == "-h" || arg == "--help") {
|
||||||
|
arg = "help(-1)";
|
||||||
|
}
|
||||||
|
else if (arg == "-i" || arg == "--interactive") {
|
||||||
|
mode = eInteractive;
|
||||||
|
}
|
||||||
|
else if (arg.find('-') == 0) {
|
||||||
|
std::cout << "unrecognised argument " << arg << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mode = eFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
chaiscript::Boxed_Value val;
|
||||||
|
try {
|
||||||
|
switch (mode) {
|
||||||
|
case eInteractive: interactive(chai); break;
|
||||||
|
case eCommand: val = chai.eval(arg); break;
|
||||||
|
case eFile: {
|
||||||
|
begin = clock();
|
||||||
|
|
||||||
|
val = chai.eval_file(arg);
|
||||||
|
|
||||||
|
end = clock();
|
||||||
|
double elapsed_secs1 = double(end - begin) / CLOCKS_PER_SEC;
|
||||||
|
printf("**C++::time= %.10f\n", elapsed_secs);
|
||||||
|
printf("**ChaiScript::time= %.10f\n", elapsed_secs1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: std::cout << "Unrecognized execution mode" << std::endl; return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const chaiscript::exception::eval_error &ee) {
|
||||||
|
std::cout << ee.pretty_print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
std::cout << e.what() << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
@@ -54,7 +54,7 @@ class test
|
|||||||
chaiscript::Boxed_Value val = chai.eval_file(sFile);
|
chaiscript::Boxed_Value val = chai.eval_file(sFile);
|
||||||
}
|
}
|
||||||
catch (std::exception &e) {
|
catch (std::exception &e) {
|
||||||
std::cout << e.what() << std::endl;
|
std::cout << e.what() << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
42
src/main.cpp
42
src/main.cpp
@@ -136,24 +136,24 @@ std::vector<std::string> default_search_paths()
|
|||||||
|
|
||||||
void help(int n) {
|
void help(int n) {
|
||||||
if ( n >= 0 ) {
|
if ( n >= 0 ) {
|
||||||
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl;
|
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>.\n";
|
||||||
std::cout << "Additionally, you can inspect the runtime system using:" << std::endl;
|
std::cout << "Additionally, you can inspect the runtime system using:\n";
|
||||||
std::cout << " dump_system() - outputs all functions registered to the system" << std::endl;
|
std::cout << " dump_system() - outputs all functions registered to the system\n";
|
||||||
std::cout << " dump_object(x) - dumps information about the given symbol" << std::endl;
|
std::cout << " dump_object(x) - dumps information about the given symbol\n";
|
||||||
} else {
|
} else {
|
||||||
std::cout << "usage : chai [option]+" << std::endl;
|
std::cout << "usage : chai [option]+\n";
|
||||||
std::cout << "option:" << std::endl;
|
std::cout << "option:" << '\n';
|
||||||
std::cout << " -h | --help" << std::endl;
|
std::cout << " -h | --help" << '\n';
|
||||||
std::cout << " -i | --interactive" << std::endl;
|
std::cout << " -i | --interactive" << '\n';
|
||||||
std::cout << " -c | --command cmd" << std::endl;
|
std::cout << " -c | --command cmd" << '\n';
|
||||||
std::cout << " -v | --version" << std::endl;
|
std::cout << " -v | --version" << '\n';
|
||||||
std::cout << " - --stdin" << std::endl;
|
std::cout << " - --stdin" << '\n';
|
||||||
std::cout << " filepath" << std::endl;
|
std::cout << " filepath" << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void version(int){
|
void version(int){
|
||||||
std::cout << "chai: compiled " << __TIME__ << " " << __DATE__ << std::endl;
|
std::cout << "chai: compiled " << __TIME__ << " " << __DATE__ << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
bool throws_exception(const std::function<void ()> &f)
|
bool throws_exception(const std::function<void ()> &f)
|
||||||
@@ -231,7 +231,7 @@ void interactive(chaiscript::ChaiScript& chai)
|
|||||||
//Then, we try to print the result of the evaluation to the user
|
//Then, we try to print the result of the evaluation to the user
|
||||||
if (!val.get_type_info().bare_equal(chaiscript::user_type<void>())) {
|
if (!val.get_type_info().bare_equal(chaiscript::user_type<void>())) {
|
||||||
try {
|
try {
|
||||||
std::cout << chai.eval<std::function<std::string (const chaiscript::Boxed_Value &bv)> >("to_string")(val) << std::endl;
|
std::cout << chai.eval<std::function<std::string (const chaiscript::Boxed_Value &bv)> >("to_string")(val) << '\n';
|
||||||
}
|
}
|
||||||
catch (...) {} //If we can't, do nothing
|
catch (...) {} //If we can't, do nothing
|
||||||
}
|
}
|
||||||
@@ -241,11 +241,11 @@ void interactive(chaiscript::ChaiScript& chai)
|
|||||||
if (ee.call_stack.size() > 0) {
|
if (ee.call_stack.size() > 0) {
|
||||||
std::cout << "during evaluation at (" << ee.call_stack[0]->start.line << ", " << ee.call_stack[0]->start.column << ")";
|
std::cout << "during evaluation at (" << ee.call_stack[0]->start.line << ", " << ee.call_stack[0]->start.column << ")";
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
catch (const std::exception &e) {
|
catch (const std::exception &e) {
|
||||||
std::cout << e.what();
|
std::cout << e.what();
|
||||||
std::cout << std::endl;
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,7 +305,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if ( arg == "-c" || arg == "--command" ) {
|
if ( arg == "-c" || arg == "--command" ) {
|
||||||
if ( (i+1) >= argc ) {
|
if ( (i+1) >= argc ) {
|
||||||
std::cout << "insufficient input following " << arg << std::endl;
|
std::cout << "insufficient input following " << arg << '\n';
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} else {
|
} else {
|
||||||
arg = argv[++i];
|
arg = argv[++i];
|
||||||
@@ -323,7 +323,7 @@ int main(int argc, char *argv[])
|
|||||||
} else if ( arg == "-i" || arg == "--interactive" ) {
|
} else if ( arg == "-i" || arg == "--interactive" ) {
|
||||||
mode = eInteractive ;
|
mode = eInteractive ;
|
||||||
} else if ( arg.find('-') == 0 ) {
|
} else if ( arg.find('-') == 0 ) {
|
||||||
std::cout << "unrecognised argument " << arg << std::endl;
|
std::cout << "unrecognised argument " << arg << '\n';
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} else {
|
} else {
|
||||||
mode = eFile;
|
mode = eFile;
|
||||||
@@ -335,16 +335,16 @@ int main(int argc, char *argv[])
|
|||||||
case eInteractive : interactive(chai); break;
|
case eInteractive : interactive(chai); break;
|
||||||
case eCommand : val = chai.eval(arg); break;
|
case eCommand : val = chai.eval(arg); break;
|
||||||
case eFile : val = chai.eval_file(arg); break;
|
case eFile : val = chai.eval_file(arg); break;
|
||||||
default : std::cout << "Unrecognized execution mode" << std::endl; return EXIT_FAILURE;
|
default : std::cout << "Unrecognized execution mode\n"; return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const chaiscript::exception::eval_error &ee) {
|
catch (const chaiscript::exception::eval_error &ee) {
|
||||||
std::cout << ee.pretty_print();
|
std::cout << ee.pretty_print();
|
||||||
std::cout << std::endl;
|
std::cout << '\n';
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
catch (std::exception &e) {
|
catch (std::exception &e) {
|
||||||
std::cout << e.what() << std::endl;
|
std::cout << e.what() << '\n';
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,16 +15,16 @@ bool run_test_type_conversion(const Boxed_Value &bv, bool expectedpass)
|
|||||||
use(ret);
|
use(ret);
|
||||||
} catch (const chaiscript::exception::bad_boxed_cast &/*e*/) {
|
} catch (const chaiscript::exception::bad_boxed_cast &/*e*/) {
|
||||||
if (expectedpass) {
|
if (expectedpass) {
|
||||||
// std::cerr << "Failure in run_test_type_conversion: " << e.what() << std::endl;
|
// std::cerr << "Failure in run_test_type_conversion: " << e.what() << '\n';
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
std::cerr << "Unexpected standard exception when attempting cast_conversion: " << e.what() << std::endl;
|
std::cerr << "Unexpected standard exception when attempting cast_conversion: " << e.what() << '\n';
|
||||||
return false;
|
return false;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::cerr << "Unexpected unknown exception when attempting cast_conversion." << std::endl;
|
std::cerr << "Unexpected unknown exception when attempting cast_conversion.\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ bool test_type_conversion(const Boxed_Value &bv, bool expectedpass)
|
|||||||
<< (bv.is_const()?(std::string("const ")):(std::string())) << bv.get_type_info().name()
|
<< (bv.is_const()?(std::string("const ")):(std::string())) << bv.get_type_info().name()
|
||||||
<< " To: "
|
<< " To: "
|
||||||
<< (std::is_const<To>::value?(std::string("const ")):(std::string())) << typeid(To).name()
|
<< (std::is_const<To>::value?(std::string("const ")):(std::string())) << typeid(To).name()
|
||||||
<< " test was expected to " << ((expectedpass)?(std::string("succeed")):(std::string("fail"))) << " but did not" << std::endl;
|
<< " test was expected to " << ((expectedpass)?(std::string("succeed")):(std::string("fail"))) << " but did not\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -264,21 +264,21 @@ bool pointer_test(const T& default_value, const T& new_value)
|
|||||||
|
|
||||||
|
|
||||||
if (p != (*result) ) {
|
if (p != (*result) ) {
|
||||||
std::cerr << "Pointer passed in different than one returned" << std::endl;
|
std::cerr << "Pointer passed in different than one returned\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*p != *(*result) ) {
|
if (*p != *(*result) ) {
|
||||||
std::cerr << "Somehow dereferenced pointer values are not the same?" << std::endl;
|
std::cerr << "Somehow dereferenced pointer values are not the same?\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (const exception::bad_boxed_cast &) {
|
} catch (const exception::bad_boxed_cast &) {
|
||||||
std::cerr << "Bad boxed cast performing ** to ** test" << std::endl;
|
std::cerr << "Bad boxed cast performing ** to ** test\n";
|
||||||
return false;
|
return false;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::cerr << "Unknown exception performing ** to ** test" << std::endl;
|
std::cerr << "Unknown exception performing ** to ** test\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ void assert_equal(const LHS &lhs, const RHS &rhs)
|
|||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Got: " << lhs << " expected " << rhs << std::endl;
|
std::cout << "Got: " << lhs << " expected " << rhs << '\n';
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,7 @@ int test_generic()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "test_generic failed" << std::endl;
|
std::cout << "test_generic failed\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +33,7 @@ int test_1()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "test_1 failed" << std::endl;
|
std::cout << "test_1 failed\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ int test_2()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "test_2 failed" << std::endl;
|
std::cout << "test_2 failed\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,22 +61,22 @@ int test_5()
|
|||||||
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 &>());
|
||||||
} catch (const double) {
|
} catch (const double) {
|
||||||
std::cout << "test_5 failed with double" << std::endl;
|
std::cout << "test_5 failed with double\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (int) {
|
} catch (int) {
|
||||||
std::cout << "test_5 failed with int" << std::endl;
|
std::cout << "test_5 failed with int\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (float) {
|
} catch (float) {
|
||||||
std::cout << "test_5 failed with float" << std::endl;
|
std::cout << "test_5 failed with float\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (const std::string &) {
|
} catch (const std::string &) {
|
||||||
std::cout << "test_5 failed with string" << std::endl;
|
std::cout << "test_5 failed with string\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception &) {
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "test_5 failed" << std::endl;
|
std::cout << "test_5 failed\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,22 +87,22 @@ int test_unhandled()
|
|||||||
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 &>());
|
||||||
} catch (double) {
|
} catch (double) {
|
||||||
std::cout << "test_unhandled failed with double" << std::endl;
|
std::cout << "test_unhandled failed with double\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (int) {
|
} catch (int) {
|
||||||
std::cout << "test_unhandled failed with int" << std::endl;
|
std::cout << "test_unhandled failed with int\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (float) {
|
} catch (float) {
|
||||||
std::cout << "test_unhandled failed with float" << std::endl;
|
std::cout << "test_unhandled failed with float\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (const std::exception &) {
|
} catch (const std::exception &) {
|
||||||
std::cout << "test_unhandled failed with std::exception" << std::endl;
|
std::cout << "test_unhandled failed with std::exception\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
} catch (const chaiscript::Boxed_Value &) {
|
} catch (const chaiscript::Boxed_Value &) {
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "test_unhandled failed" << std::endl;
|
std::cout << "test_unhandled failed\n";
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@ bool test_literal(T val, const std::string &str)
|
|||||||
{
|
{
|
||||||
chaiscript::ChaiScript chai;
|
chaiscript::ChaiScript chai;
|
||||||
T val2 = chai.eval<T>(str);
|
T val2 = chai.eval<T>(str);
|
||||||
std::cout << "Comparing : " << val << " " << val2 << std::endl;
|
std::cout << "Comparing : " << val << " " << val2 << '\n';
|
||||||
return val == val2;
|
return val == val2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
69
unittests/object_lifetime_test2.cpp
Normal file
69
unittests/object_lifetime_test2.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include <chaiscript/chaiscript.hpp>
|
||||||
|
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Vector2
|
||||||
|
{
|
||||||
|
Vector2() : x(0), y(0) {};
|
||||||
|
Vector2(T px, T py) : x(px), y(py) {};
|
||||||
|
Vector2(const Vector2& cp) : x(cp.x), y(cp.y) {};
|
||||||
|
|
||||||
|
Vector2& operator+=(const Vector2& vec_r)
|
||||||
|
{
|
||||||
|
x += vec_r.x;
|
||||||
|
y += vec_r.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 operator+(const Vector2& vec_r)
|
||||||
|
{
|
||||||
|
return Vector2(*this += vec_r);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator=(const Vector2& ver_r)
|
||||||
|
{
|
||||||
|
x = ver_r.x;
|
||||||
|
y = ver_r.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
T x;
|
||||||
|
T y;
|
||||||
|
};
|
||||||
|
|
||||||
|
Vector2<float> GetValue()
|
||||||
|
{
|
||||||
|
return Vector2<float>(10,15);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
chaiscript::ChaiScript _script(chaiscript::Std_Lib::library());
|
||||||
|
|
||||||
|
//Registering stuff
|
||||||
|
_script.add(chaiscript::user_type<Vector2<float>>(), "Vector2f");
|
||||||
|
_script.add(chaiscript::constructor<Vector2<float> ()>(), "Vector2f");
|
||||||
|
_script.add(chaiscript::constructor<Vector2<float> (float, float)>(), "Vector2f");
|
||||||
|
_script.add(chaiscript::constructor<Vector2<float> (const Vector2<float>&)>(), "Vector2f");
|
||||||
|
_script.add(chaiscript::fun(&Vector2<float>::x), "x");
|
||||||
|
_script.add(chaiscript::fun(&Vector2<float>::y), "y");
|
||||||
|
_script.add(chaiscript::fun(&Vector2<float>::operator +), "+");
|
||||||
|
_script.add(chaiscript::fun(&Vector2<float>::operator +=), "+=");
|
||||||
|
_script.add(chaiscript::fun(&Vector2<float>::operator =), "=");
|
||||||
|
_script.add(chaiscript::fun(&GetValue), "getValue");
|
||||||
|
|
||||||
|
_script.eval(R"(
|
||||||
|
var test = 0.0
|
||||||
|
var test2 = Vector2f(10,10)
|
||||||
|
|
||||||
|
test = getValue().x
|
||||||
|
print(test)
|
||||||
|
print(test2.x)
|
||||||
|
)");
|
||||||
|
|
||||||
|
if (_script.eval<std::string>("to_string(test)") != "10") { return EXIT_FAILURE; }
|
||||||
|
if (_script.eval<std::string>("to_string(test2.x)") != "10") { return EXIT_FAILURE; }
|
||||||
|
|
||||||
|
|
||||||
|
//_script.eval_file("object_lifetime_test2.inc");
|
||||||
|
}
|
@@ -31,7 +31,7 @@ int main()
|
|||||||
test_type(chaiscript::user_type<const int *>(), true, true, false, false, false);
|
test_type(chaiscript::user_type<const int *>(), true, true, false, false, false);
|
||||||
test_type(chaiscript::Type_Info(), false, false, false, false, true);
|
test_type(chaiscript::Type_Info(), false, false, false, false, true);
|
||||||
|
|
||||||
std::cout << "Size of Type_Info " << sizeof(chaiscript::Type_Info) << std::endl;
|
std::cout << "Size of Type_Info " << sizeof(chaiscript::Type_Info) << '\n';
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user