diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index b542f99..3aed1e5 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -322,6 +322,45 @@ namespace chaiscript return m; } + + /** + *Front insertion sequence + *http://www.sgi.com/tech/stl/FrontInsertionSequence.html + */ + template + ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) + { + sequence_type(type, m); + + typedef typename ContainerType::reference (ContainerType::*frontptr)(); + + m->add(fun(frontptr(&ContainerType::front)), "front"); + + std::string push_front_name; + if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) + { + push_front_name = "push_front_ref"; + } else { + push_front_name = "push_front"; + } + m->add(fun(&ContainerType::push_front), push_front_name); + m->add(fun(&ContainerType::pop_front), "pop_front"); + return m; + } + + /** + * hopefully working List type + * http://www.sgi.com/tech/stl/List.html + */ + template + ModulePtr list_type(const std::string &type, ModulePtr m = ModulePtr(new Module())) + { + m->add(user_type(), type); + front_insertion_sequence_type(type, m); + back_insertion_sequence_type(type, m); + return m; + } + /** * Create a vector type with associated concepts * http://www.sgi.com/tech/stl/Vector.html diff --git a/include/chaiscript/language/chaiscript_prelude.hpp b/include/chaiscript/language/chaiscript_prelude.hpp index e495847..092e106 100644 --- a/include/chaiscript/language/chaiscript_prelude.hpp +++ b/include/chaiscript/language/chaiscript_prelude.hpp @@ -44,6 +44,8 @@ def odd(x) { if (x % 2 == 1) { true } else { false } } \ def even(x) { if (x % 2 == 0) { true } else { false } } \ # Pushes the second value onto the container first value while making a clone of the value\n\ def push_back(container, x) : call_exists(push_back_ref, container, x) { container.push_back_ref(clone(x)) } \n\ +# Pushes the second value onto the front of the container first value while making a clone of the value\n\ +def push_front(container, x) : call_exists(push_front_ref, container, x) { container.push_front_ref(clone(x)) } \n\ # Inserts the third value at the position of the second value into the container of the first\n\ # while making a clone. \n\ def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\ diff --git a/src/main.cpp b/src/main.cpp index 3c050ff..9c1fbc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,8 @@ #include +#include + #include void print_help() { @@ -19,6 +21,8 @@ int main(int argc, char *argv[]) { std::string input; chaiscript::ChaiScript chai; + chai.add(chaiscript::bootstrap::list_type >("List")); + if (argc < 2) { std::cout << "eval> "; std::getline(std::cin, input); diff --git a/unittests/list_push_back.chai b/unittests/list_push_back.chai new file mode 100644 index 0000000..537e0cc --- /dev/null +++ b/unittests/list_push_back.chai @@ -0,0 +1,4 @@ +var x = List() +x.push_back(3) +x.push_back("A") +print(x) diff --git a/unittests/list_push_back.txt b/unittests/list_push_back.txt new file mode 100644 index 0000000..3bb0d0c --- /dev/null +++ b/unittests/list_push_back.txt @@ -0,0 +1 @@ +[3, A] diff --git a/unittests/list_push_front.chai b/unittests/list_push_front.chai new file mode 100644 index 0000000..a977622 --- /dev/null +++ b/unittests/list_push_front.chai @@ -0,0 +1,4 @@ +var x = List() +x.push_front(3) +x.push_front("A") +print(x) diff --git a/unittests/list_push_front.txt b/unittests/list_push_front.txt new file mode 100644 index 0000000..6f91e7c --- /dev/null +++ b/unittests/list_push_front.txt @@ -0,0 +1 @@ +[A, 3]