diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 1b8d1c8..8e74115 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -205,6 +205,7 @@ namespace chaiscript m->add(fun(&ContainerType::size), "size"); m->add(fun(&ContainerType::max_size), "max_size"); m->add(fun(&ContainerType::empty), "empty"); + m->add(fun(&ContainerType::clear), "clear"); return m; } @@ -450,6 +451,17 @@ namespace chaiscript opers_comparison(m); random_access_container_type(type, m); sequence_type(type, m); + + //Special case: add push_back to string (which doesn't support other back_insertion operations + std::string push_back_name; + if (typeid(typename String::value_type) == typeid(Boxed_Value)) + { + push_back_name = "push_back_ref"; + } else { + push_back_name = "push_back"; + } + m->add(fun(&String::push_back), push_back_name); + typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const; m->add(fun(find_func(&String::find)), "find"); m->add(fun(find_func(&String::rfind)), "rfind"); diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 14c14b2..2ef56cc 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -102,7 +102,12 @@ namespace chaiscript */ template Boxed_Value eval_single_quoted_string(Eval_System &, TokenPtr node) { - return Boxed_Value(node->text); + if (node->text.size() == 1) { + return Boxed_Value(char(node->text[0])); + } + else { + return Boxed_Value(char((int)node->text[0] * 0xff + (int)node->text[0])); + } } /** diff --git a/include/chaiscript/language/chaiscript_prelude.hpp b/include/chaiscript/language/chaiscript_prelude.hpp index 4c4402e..bfcb406 100644 --- a/include/chaiscript/language/chaiscript_prelude.hpp +++ b/include/chaiscript/language/chaiscript_prelude.hpp @@ -12,6 +12,7 @@ #define CODE_STRING(x, y) #x ", " #y #define chaiscript_prelude CODE_STRING(\ +def new(x) { var retval = clone(x); clear(x); x } \ # to_string for Pair()\n\ def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ @@ -22,7 +23,7 @@ def to_string(x) : call_exists(range, x) && !x.is_type("string"){ \ }\ # Basic to_string function\n\ def to_string(x) { \ - return internal_to_string(x); \ + internal_to_string(x); \ }\ # Prints to console with no carriage return\n\ def puts(x) { \ @@ -45,6 +46,16 @@ def push_back(container, x) : call_exists(push_back_ref, container, x) { contain # 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\ +# Returns the reverse of the given container\n\ +def reverse(container) {\ + var retval = Vector(); \ + var r = retro(range(container)); \ + while (!r.empty()) { \ + retval.push_back(r.front()); \ + r.pop_front(); \ + } \ + retval; \ +} \ # Performs the second value function over the container first value\n\ def for_each(container, func) : call_exists(range, container) { \ var range = range(container); \ @@ -54,7 +65,7 @@ def for_each(container, func) : call_exists(range, container) { \ } \ } \ def back_inserter(container) { \ - return bind(push_back, container, _); \ + bind(push_back, container, _); \ }\ \ def map(container, func, inserter) : call_exists(range, container) { \ @@ -66,9 +77,9 @@ def map(container, func, inserter) : call_exists(range, container) { \ } \ # Performs the second value function over the container first value. Creates a new Vector with the results\n\ def map(container, func) { \ - var retval = Vector();\ + var retval = Vector(); \ map(container, func, back_inserter(retval));\ - return retval;\ + retval;\ }\ # Performs the second value function over the container first value. Starts with initial and continues with each element.\n\ def foldl(container, func, initial) : call_exists(range, container){ \ @@ -108,7 +119,7 @@ def take(container, num, inserter) : call_exists(range, container) { \ def take(container, num) {\ var retval = Vector(); \ take(container, num, back_inserter(retval)); \ - return retval; \ + retval; \ }\ def take_while(container, f, inserter) : call_exists(range, container) { \ var r = range(container); \ @@ -121,7 +132,7 @@ def take_while(container, f, inserter) : call_exists(range, container) { \ def take_while(container, f) {\ var retval = Vector(); \ take_while(container, f, back_inserter(retval)); \ - return retval;\ + retval;\ }\ def drop(container, num, inserter) : call_exists(range, container) { \ var r = range(container); \ @@ -139,7 +150,7 @@ def drop(container, num, inserter) : call_exists(range, container) { \ def drop(container, num) {\ var retval = Vector(); \ drop(container, num, back_inserter(retval)); \ - return retval; \ + retval; \ }\ def drop_while(container, f, inserter) : call_exists(range, container) { \ var r = range(container); \ @@ -155,7 +166,7 @@ def drop_while(container, f, inserter) : call_exists(range, container) { \ def drop_while(container, f) {\ var retval = Vector(); \ drop_while(container, f, back_inserter(retval)); \ - return retval; \ + retval; \ }\ # Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements.\n\ def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \ @@ -198,7 +209,7 @@ def filter(container, f, inserter) : call_exists(range, container) { \ def filter(container, f) { \ var retval = Vector(); \ filter(container, f, back_inserter(retval));\ - return retval;\ + retval;\ }\ def generate_range(x, y, inserter) { \ var i = x; \ @@ -211,7 +222,7 @@ def generate_range(x, y, inserter) { \ def generate_range(x, y) { \ var retval = Vector(); \ generate_range(x,y,back_inserter(retval)); \ - return retval; \ + retval; \ }\ # Returns a new Vector with the first value to the second value as its elements\n\ def collate(x, y) { \ @@ -230,7 +241,7 @@ def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) def zip_with(f, x, y) { \ var retval = Vector(); \ zip_with(f,x,y,back_inserter(retval)); \ - return retval;\ + retval;\ }\ # Returns a new Vector which joins matching elements of the first and second\n\ def zip(x, y) { \ @@ -238,27 +249,27 @@ def zip(x, y) { \ }\ # Returns the position of the second value string in the first value string\n\ def find(str, substr) { \ - return int(find(str, substr, size_t(0))); \ + int(find(str, substr, size_t(0))); \ } \ # Returns the position of last match of the second value string in the first value string\n\ def rfind(str, substr) { \ - return int(rfind(str, substr, size_t(-1))); \ + int(rfind(str, substr, size_t(-1))); \ } \ # Returns the position of the first match of elements in the second value string in the first value string\n\ def find_first_of(str, list) { \ - return int(find_first_of(str, list, size_t(0))); \ + int(find_first_of(str, list, size_t(0))); \ } \ # Returns the position of the last match of elements in the second value string in the first value string\n\ def find_last_of(str, list) { \ - return int(find_last_of(str, list, size_t(-1))); \ + int(find_last_of(str, list, size_t(-1))); \ } \ # Returns the position of the first non-matching element in the second value string in the first value string\n\ def find_first_not_of(str, list) { \ - return int(find_first_not_of(str, list, size_t(0))); \ + int(find_first_not_of(str, list, size_t(0))); \ } \ # Returns the position of the last non-matching element in the second value string in the first value string\n\ def find_last_not_of(str, list) { \ - return int(find_last_not_of(str, list, size_t(-1))); \ + int(find_last_not_of(str, list, size_t(-1))); \ } \ ) diff --git a/unittests/range.chai b/unittests/range.chai new file mode 100644 index 0000000..f939f31 --- /dev/null +++ b/unittests/range.chai @@ -0,0 +1,4 @@ +var x = [1, 2, 3, 4] +var r = range(x) +r.pop_front() +print(r.front()) diff --git a/unittests/range.txt b/unittests/range.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/unittests/range.txt @@ -0,0 +1 @@ +2 diff --git a/unittests/range_back.chai b/unittests/range_back.chai new file mode 100644 index 0000000..1edf803 --- /dev/null +++ b/unittests/range_back.chai @@ -0,0 +1,4 @@ +var x = [1, 2, 3, 4] +var r = range(x) +r.pop_back() +print(r.back()) diff --git a/unittests/range_back.txt b/unittests/range_back.txt new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/unittests/range_back.txt @@ -0,0 +1 @@ +3 diff --git a/unittests/retro.chai b/unittests/retro.chai new file mode 100644 index 0000000..904e561 --- /dev/null +++ b/unittests/retro.chai @@ -0,0 +1,4 @@ +var x = [1, 2, 3, 4] +var r = retro(range(x)) +r.pop_front() +print(r.front()) diff --git a/unittests/retro.txt b/unittests/retro.txt new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/unittests/retro.txt @@ -0,0 +1 @@ +3