Fix a bug which prevented _ from being visible in all places, enhance prelude to take an inserter algorithm for each function that generates a new list
This commit is contained in:
parent
36208b401e
commit
c974ab0034
@ -4,6 +4,9 @@
|
|||||||
#ifndef CHAISCRIPT_PRELUDE_HPP_
|
#ifndef CHAISCRIPT_PRELUDE_HPP_
|
||||||
#define CHAISCRIPT_PRELUDE_HPP_
|
#define CHAISCRIPT_PRELUDE_HPP_
|
||||||
|
|
||||||
|
|
||||||
|
//Note, the expression "[x,y]" in "collate" is parsed as two separate expressions
|
||||||
|
//by C++, so CODE_STRING, takes two expressions and adds in the missing comma
|
||||||
#define CODE_STRING(x, y) #x ", " #y
|
#define CODE_STRING(x, y) #x ", " #y
|
||||||
|
|
||||||
#define chaiscript_prelude CODE_STRING(\
|
#define chaiscript_prelude CODE_STRING(\
|
||||||
@ -34,15 +37,22 @@ def for_each(container, func) : call_exists(range, container) { \
|
|||||||
range.pop_front(); \
|
range.pop_front(); \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
def map(container, func) : call_exists(range, container) { \
|
def back_inserter(container) { \
|
||||||
var retval = Vector(); \
|
return bind(push_back, container, _); \
|
||||||
|
}\
|
||||||
|
\
|
||||||
|
def map(container, func, inserter) : call_exists(range, container) { \
|
||||||
var range = range(container); \
|
var range = range(container); \
|
||||||
while (!range.empty()) { \
|
while (!range.empty()) { \
|
||||||
retval.push_back(func(range.front())); \
|
inserter(func(range.front())); \
|
||||||
range.pop_front(); \
|
range.pop_front(); \
|
||||||
} \
|
} \
|
||||||
retval \
|
|
||||||
} \
|
} \
|
||||||
|
def map(container, func) { \
|
||||||
|
var retval = Vector();\
|
||||||
|
map(container, func, back_inserter(retval));\
|
||||||
|
return retval;\
|
||||||
|
}\
|
||||||
def foldl(container, func, initial) : call_exists(range, container){ \
|
def foldl(container, func, initial) : call_exists(range, container){ \
|
||||||
var retval = initial; \
|
var retval = initial; \
|
||||||
var range = range(container); \
|
var range = range(container); \
|
||||||
@ -64,52 +74,64 @@ def concat(x, y) : call_exists(clone, x) { \
|
|||||||
} \
|
} \
|
||||||
retval; \
|
retval; \
|
||||||
} \
|
} \
|
||||||
def take(container, num) : call_exists(range, container) { \
|
def take(container, num, inserter) : call_exists(range, container) { \
|
||||||
var r = range(container); \
|
var r = range(container); \
|
||||||
var i = num; \
|
var i = num; \
|
||||||
var retval = Vector(); \
|
|
||||||
while ((i > 0) && (!r.empty())) { \
|
while ((i > 0) && (!r.empty())) { \
|
||||||
retval.push_back(r.front()); \
|
inserter(r.front()); \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
--i; \
|
--i; \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
def take_while(container, f) : call_exists(range, container) { \
|
def take(container, num) {\
|
||||||
var r = range(container); \
|
|
||||||
var retval = Vector(); \
|
var retval = Vector(); \
|
||||||
|
take(container, num, back_inserter(retval)); \
|
||||||
|
return retval; \
|
||||||
|
}\
|
||||||
|
def take_while(container, f, inserter) : call_exists(range, container) { \
|
||||||
|
var r = range(container); \
|
||||||
while ((!r.empty()) && f(r.front())) { \
|
while ((!r.empty()) && f(r.front())) { \
|
||||||
retval.push_back(r.front()); \
|
inserter(r.front()); \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
def drop(container, num) : call_exists(range, container) { \
|
def take_while(container, f) {\
|
||||||
|
var retval = Vector(); \
|
||||||
|
take_while(container, f, back_inserter(retval)); \
|
||||||
|
return retval;\
|
||||||
|
}\
|
||||||
|
def drop(container, num, inserter) : call_exists(range, container) { \
|
||||||
var r = range(container); \
|
var r = range(container); \
|
||||||
var i = num; \
|
var i = num; \
|
||||||
var retval = Vector(); \
|
|
||||||
while ((i > 0) && (!r.empty())) { \
|
while ((i > 0) && (!r.empty())) { \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
--i; \
|
--i; \
|
||||||
} \
|
} \
|
||||||
while (!r.empty()) { \
|
while (!r.empty()) { \
|
||||||
retval.push_back(r.front()); \
|
inserter(r.front()); \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
def drop_while(container, f) : call_exists(range, container) { \
|
def drop(container, num) {\
|
||||||
var r = range(container); \
|
|
||||||
var retval = Vector(); \
|
var retval = Vector(); \
|
||||||
|
drop(container, num, back_inserter(retval)); \
|
||||||
|
return retval; \
|
||||||
|
}\
|
||||||
|
def drop_while(container, f, inserter) : call_exists(range, container) { \
|
||||||
|
var r = range(container); \
|
||||||
while ((!r.empty())&& f(r.front())) { \
|
while ((!r.empty())&& f(r.front())) { \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
} \
|
} \
|
||||||
while (!r.empty()) { \
|
while (!r.empty()) { \
|
||||||
retval.push_back(r.front()); \
|
inserter(r.front()); \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
|
def drop_while(container, f) {\
|
||||||
|
var retval = Vector(); \
|
||||||
|
drop_while(container, f, back_inserter(retval)); \
|
||||||
|
return retval; \
|
||||||
|
}\
|
||||||
def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \
|
def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \
|
||||||
var r = range(container); \
|
var r = range(container); \
|
||||||
var retval = r.front(); \
|
var retval = r.front(); \
|
||||||
@ -136,40 +158,49 @@ def join(container, delim) { \
|
|||||||
} \
|
} \
|
||||||
retval; \
|
retval; \
|
||||||
} \
|
} \
|
||||||
def filter(container, f) : call_exists(range, container) { \
|
def filter(container, f, inserter) : call_exists(range, container) { \
|
||||||
var retval = Vector(); \
|
|
||||||
var r = range(container); \
|
var r = range(container); \
|
||||||
while (!r.empty()) { \
|
while (!r.empty()) { \
|
||||||
if (f(r.front())) { \
|
if (f(r.front())) { \
|
||||||
retval.push_back(r.front()); \
|
inserter(r.front()); \
|
||||||
} \
|
} \
|
||||||
r.pop_front(); \
|
r.pop_front(); \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
def generate_range(x, y) { \
|
def filter(container, f) { \
|
||||||
var i = x; \
|
|
||||||
var retval = Vector(); \
|
var retval = Vector(); \
|
||||||
|
filter(container, f, back_inserter(retval));\
|
||||||
|
return retval;\
|
||||||
|
}\
|
||||||
|
def generate_range(x, y, inserter) { \
|
||||||
|
var i = x; \
|
||||||
while (i <= y) { \
|
while (i <= y) { \
|
||||||
retval.push_back(i); \
|
inserter(i); \
|
||||||
++i; \
|
++i; \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
|
def generate_range(x, y) { \
|
||||||
|
var retval = Vector(); \
|
||||||
|
generate_range(x,y,back_inserter(retval)); \
|
||||||
|
return retval; \
|
||||||
|
}\
|
||||||
def collate(x, y) { \
|
def collate(x, y) { \
|
||||||
[x, y]; \
|
[x, y]; \
|
||||||
} \
|
} \
|
||||||
def zip_with(f, x, y) : call_exists(range, x) && call_exists(range, y) { \
|
def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) { \
|
||||||
var r_x = range(x); \
|
var r_x = range(x); \
|
||||||
var r_y = range(y); \
|
var r_y = range(y); \
|
||||||
var retval = Vector(); \
|
|
||||||
while (!r_x.empty() && !r_y.empty()) { \
|
while (!r_x.empty() && !r_y.empty()) { \
|
||||||
retval.push_back(f(r_x.front(), r_y.front())); \
|
inserter(f(r_x.front(), r_y.front())); \
|
||||||
r_x.pop_front(); \
|
r_x.pop_front(); \
|
||||||
r_y.pop_front(); \
|
r_y.pop_front(); \
|
||||||
} \
|
} \
|
||||||
retval; \
|
|
||||||
} \
|
} \
|
||||||
|
def zip_with(f, x, y) { \
|
||||||
|
var retval = Vector(); \
|
||||||
|
zip_with(f,x,y,back_inserter(retval)); \
|
||||||
|
return retval;\
|
||||||
|
}\
|
||||||
def zip(x, y) { \
|
def zip(x, y) { \
|
||||||
zip_with(collate, x, y); \
|
zip_with(collate, x, y); \
|
||||||
}\
|
}\
|
||||||
|
@ -530,8 +530,6 @@ namespace dispatchkit
|
|||||||
s.register_function(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1)),
|
s.register_function(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1)),
|
||||||
"is_type");
|
"is_type");
|
||||||
|
|
||||||
s.add_object("_", Placeholder_Object());
|
|
||||||
|
|
||||||
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
||||||
"bind");
|
"bind");
|
||||||
|
|
||||||
|
@ -77,6 +77,7 @@ namespace dispatchkit
|
|||||||
typedef std::deque<Scope> Stack;
|
typedef std::deque<Scope> Stack;
|
||||||
|
|
||||||
Dispatch_Engine()
|
Dispatch_Engine()
|
||||||
|
: m_place_holder(boost::shared_ptr<Placeholder_Object>(new Placeholder_Object()))
|
||||||
{
|
{
|
||||||
m_scopes.push_back(Scope());
|
m_scopes.push_back(Scope());
|
||||||
}
|
}
|
||||||
@ -144,6 +145,11 @@ namespace dispatchkit
|
|||||||
|
|
||||||
Boxed_Value get_object(const std::string &name) const
|
Boxed_Value get_object(const std::string &name) const
|
||||||
{
|
{
|
||||||
|
if (name == "_")
|
||||||
|
{
|
||||||
|
return m_place_holder;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = m_scopes.size()-1; i >= 0; --i)
|
for (int i = m_scopes.size()-1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
|
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
|
||||||
@ -244,6 +250,7 @@ namespace dispatchkit
|
|||||||
|
|
||||||
Function_Map m_functions;
|
Function_Map m_functions;
|
||||||
Type_Name_Map m_types;
|
Type_Name_Map m_types;
|
||||||
|
Boxed_Value m_place_holder;
|
||||||
};
|
};
|
||||||
|
|
||||||
void dump_object(Boxed_Value o)
|
void dump_object(Boxed_Value o)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user