Added 'clear'. Added 'push_back' to string. Added char. Added simple reverse

This commit is contained in:
Jonathan Turner
2009-07-23 17:01:07 +00:00
parent 4e412c0f6a
commit 4127a6ed41
9 changed files with 61 additions and 18 deletions

View File

@@ -205,6 +205,7 @@ namespace chaiscript
m->add(fun(&ContainerType::size), "size"); m->add(fun(&ContainerType::size), "size");
m->add(fun(&ContainerType::max_size), "max_size"); m->add(fun(&ContainerType::max_size), "max_size");
m->add(fun(&ContainerType::empty), "empty"); m->add(fun(&ContainerType::empty), "empty");
m->add(fun(&ContainerType::clear), "clear");
return m; return m;
} }
@@ -450,6 +451,17 @@ namespace chaiscript
opers_comparison<String>(m); opers_comparison<String>(m);
random_access_container_type<String>(type, m); random_access_container_type<String>(type, m);
sequence_type<String>(type, m); sequence_type<String>(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; 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::find)), "find");
m->add(fun(find_func(&String::rfind)), "rfind"); m->add(fun(find_func(&String::rfind)), "rfind");

View File

@@ -102,7 +102,12 @@ namespace chaiscript
*/ */
template <typename Eval_System> template <typename Eval_System>
Boxed_Value eval_single_quoted_string(Eval_System &, TokenPtr node) { 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]));
}
} }
/** /**

View File

@@ -12,6 +12,7 @@
#define CODE_STRING(x, y) #x ", " #y #define CODE_STRING(x, y) #x ", " #y
#define chaiscript_prelude CODE_STRING(\ #define chaiscript_prelude CODE_STRING(\
def new(x) { var retval = clone(x); clear(x); x } \
# to_string for Pair()\n\ # to_string for Pair()\n\
def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ def to_string(x) : call_exists(first, x) && call_exists(second, x) { \
"<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ "<" + 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\ # Basic to_string function\n\
def to_string(x) { \ def to_string(x) { \
return internal_to_string(x); \ internal_to_string(x); \
}\ }\
# Prints to console with no carriage return\n\ # Prints to console with no carriage return\n\
def puts(x) { \ 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\ # Inserts the third value at the position of the second value into the container of the first\n\
# while making a clone. \n\ # while making a clone. \n\
def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \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\ # Performs the second value function over the container first value\n\
def for_each(container, func) : call_exists(range, container) { \ def for_each(container, func) : call_exists(range, container) { \
var range = range(container); \ var range = range(container); \
@@ -54,7 +65,7 @@ def for_each(container, func) : call_exists(range, container) { \
} \ } \
} \ } \
def back_inserter(container) { \ def back_inserter(container) { \
return bind(push_back, container, _); \ bind(push_back, container, _); \
}\ }\
\ \
def map(container, func, inserter) : call_exists(range, 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\ # Performs the second value function over the container first value. Creates a new Vector with the results\n\
def map(container, func) { \ def map(container, func) { \
var retval = Vector();\ var retval = Vector(); \
map(container, func, back_inserter(retval));\ 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\ # 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){ \ 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) {\ def take(container, num) {\
var retval = Vector(); \ var retval = Vector(); \
take(container, num, back_inserter(retval)); \ take(container, num, back_inserter(retval)); \
return retval; \ retval; \
}\ }\
def take_while(container, f, inserter) : call_exists(range, container) { \ def take_while(container, f, inserter) : call_exists(range, container) { \
var r = 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) {\ def take_while(container, f) {\
var retval = Vector(); \ var retval = Vector(); \
take_while(container, f, back_inserter(retval)); \ take_while(container, f, back_inserter(retval)); \
return retval;\ retval;\
}\ }\
def drop(container, num, inserter) : call_exists(range, container) { \ def drop(container, num, inserter) : call_exists(range, container) { \
var r = range(container); \ var r = range(container); \
@@ -139,7 +150,7 @@ def drop(container, num, inserter) : call_exists(range, container) { \
def drop(container, num) {\ def drop(container, num) {\
var retval = Vector(); \ var retval = Vector(); \
drop(container, num, back_inserter(retval)); \ drop(container, num, back_inserter(retval)); \
return retval; \ retval; \
}\ }\
def drop_while(container, f, inserter) : call_exists(range, container) { \ def drop_while(container, f, inserter) : call_exists(range, container) { \
var r = 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) {\ def drop_while(container, f) {\
var retval = Vector(); \ var retval = Vector(); \
drop_while(container, f, back_inserter(retval)); \ 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\ # 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) { \ 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) { \ def filter(container, f) { \
var retval = Vector(); \ var retval = Vector(); \
filter(container, f, back_inserter(retval));\ filter(container, f, back_inserter(retval));\
return retval;\ retval;\
}\ }\
def generate_range(x, y, inserter) { \ def generate_range(x, y, inserter) { \
var i = x; \ var i = x; \
@@ -211,7 +222,7 @@ def generate_range(x, y, inserter) { \
def generate_range(x, y) { \ def generate_range(x, y) { \
var retval = Vector(); \ var retval = Vector(); \
generate_range(x,y,back_inserter(retval)); \ 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\ # Returns a new Vector with the first value to the second value as its elements\n\
def collate(x, y) { \ 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) { \ def zip_with(f, x, y) { \
var retval = Vector(); \ var retval = Vector(); \
zip_with(f,x,y,back_inserter(retval)); \ 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\ # Returns a new Vector which joins matching elements of the first and second\n\
def zip(x, y) { \ 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\ # Returns the position of the second value string in the first value string\n\
def find(str, substr) { \ 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\ # Returns the position of last match of the second value string in the first value string\n\
def rfind(str, substr) { \ 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\ # 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) { \ 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\ # 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) { \ 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\ # 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) { \ 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\ # 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) { \ 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))); \
} \ } \
) )

4
unittests/range.chai Normal file
View File

@@ -0,0 +1,4 @@
var x = [1, 2, 3, 4]
var r = range(x)
r.pop_front()
print(r.front())

1
unittests/range.txt Normal file
View File

@@ -0,0 +1 @@
2

View File

@@ -0,0 +1,4 @@
var x = [1, 2, 3, 4]
var r = range(x)
r.pop_back()
print(r.back())

1
unittests/range_back.txt Normal file
View File

@@ -0,0 +1 @@
3

4
unittests/retro.chai Normal file
View File

@@ -0,0 +1,4 @@
var x = [1, 2, 3, 4]
var r = retro(range(x))
r.pop_front()
print(r.front())

1
unittests/retro.txt Normal file
View File

@@ -0,0 +1 @@
3