diff --git a/chaiscript/chaiscript_prelude.hpp b/chaiscript/chaiscript_prelude.hpp index 46cca52..e614809 100644 --- a/chaiscript/chaiscript_prelude.hpp +++ b/chaiscript/chaiscript_prelude.hpp @@ -4,172 +4,175 @@ #ifndef CHAISCRIPT_PRELUDE_HPP_ #define CHAISCRIPT_PRELUDE_HPP_ -const char *chaiscript_prelude = " \ -def to_string(x) : call_exists(first, x) && call_exists(second, x) { \n\ - \"<\" + x.first.to_string() + \", \" + x.second.to_string() + \">\"\n\ -}\n\ -def to_string(x) : call_exists(range, x) && !x.is_type(\"string\"){ \n\ - \"[\" + x.join(\", \") + \"]\"\n\ -}\n\ -def to_string(x) { \n\ - return internal_to_string(x)\n\ -}\n\ -def puts(x) { \n\ - print_string(x.to_string()) \n\ -}; \n\ -def print(x) { \n\ - println_string(x.to_string()) \n\ -}; \n\ -def max(a, b) { if (a>b) { a } else { b } } \n\ -def min(a, b) { if (a 0) && (!r.empty())) { \n\ - retval.push_back(r.front()); \n\ - r.pop_front(); \n\ - --i; \n\ - } \n\ - retval \n\ -} \n\ -def take_while(container, f) : call_exists(range, container) { \n\ - var r = range(container); \n\ - var retval = Vector(); \n\ - while ((!r.empty()) && f(r.front())) { \n\ - retval.push_back(r.front()); \n\ - r.pop_front(); \n\ - } \n\ - retval \n\ -} \n\ -def drop(container, num) : call_exists(range, container) { \n\ - var r = range(container); \n\ - var i = num; \n\ - var retval = Vector(); \n\ - while ((i > 0) && (!r.empty())) { \n\ - r.pop_front(); \n\ - --i; \n\ - } \n\ - while (!r.empty()) { \n\ - retval.push_back(r.front()); \n\ - r.pop_front(); \n\ - } \n\ - retval \n\ -} \n\ -def drop_while(container, f) : call_exists(range, container) { \n\ - var r = range(container); \n\ - var retval = Vector(); \n\ - while ((!r.empty())&& f(r.front())) { \n\ - r.pop_front(); \n\ - } \n\ - while (!r.empty()) { \n\ - retval.push_back(r.front()); \n\ - r.pop_front(); \n\ - } \n\ - retval \n\ -} \n\ -def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \n\ - var r = range(container); \n\ - var retval = r.front(); \n\ - r.pop_front(); \n\ - retval = func(retval, r.front()); \n\ - r.pop_front(); \n\ - while (!r.empty()) { \n\ - retval = func(retval, r.front()); \n\ - r.pop_front(); \n\ - } \n\ - retval \n\ -} \n\ -def join(container, delim) { \n\ - var retval = \"\" \n\ - var range = range(container) \n\ - if (!range.empty()) { \n\ - retval += to_string(range.front()) \n\ - range.pop_front() \n\ - while (!range.empty()) { \n\ - retval += delim \n\ - retval += to_string(range.front()) \n\ - range.pop_front() \n\ - } \n\ - } \n\ - retval \n\ -} \n\ -def filter(container, f) : call_exists(range, container) { \n\ - var retval = Vector(); \n\ - var r = range(container); \n\ - while (!r.empty()) { \n\ - if (f(r.front())) { \n\ - retval.push_back(r.front()); \n\ - } \n\ - r.pop_front(); \n\ - } \n\ - retval \n\ -} \n\ -def generate_range(x, y) { \n\ - var i = x; \n\ - var retval = Vector(); \n\ - while (i <= y) { \n\ - retval.push_back(i); \n\ - ++i; \n\ - } \n\ - retval \n\ -} \n\ -def collate(x, y) { \n\ - [x, y] \n\ -} \n\ -def zip_with(f, x, y) : call_exists(range, x) && call_exists(range, y) { \n\ - var r_x = range(x); \n\ - var r_y = range(y); \n\ - var retval = Vector(); \n\ - while (!r_x.empty() && !r_y.empty()) { \n\ - retval.push_back(f(r_x.front(), r_y.front())); \n\ - r_x.pop_front(); \n\ - r_y.pop_front(); \n\ - } \n\ - retval \n\ -} \n\ -def zip(x, y) { \n\ - zip_with(collate, x, y) \n\ -}\n"; +#define CODE_STRING(x, y) #x ", " #y + +#define chaiscript_prelude CODE_STRING(\ +def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ + "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ +} \ +def to_string(x) : call_exists(range, x) && !x.is_type("string"){ \ + "[" + x.join(", ") + "]"; \ +} \ +def to_string(x) { \ + return internal_to_string(x); \ +} \ +def puts(x) { \ + print_string(x.to_string()); \ +} \ +def print(x) { \ + println_string(x.to_string()); \ +} \ +def max(a, b) { if (a>b) { a } else { b } } \ +def min(a, b) { if (a 0) && (!r.empty())) { \ + retval.push_back(r.front()); \ + r.pop_front(); \ + --i; \ + } \ + retval; \ +} \ +def take_while(container, f) : call_exists(range, container) { \ + var r = range(container); \ + var retval = Vector(); \ + while ((!r.empty()) && f(r.front())) { \ + retval.push_back(r.front()); \ + r.pop_front(); \ + } \ + retval; \ +} \ +def drop(container, num) : call_exists(range, container) { \ + var r = range(container); \ + var i = num; \ + var retval = Vector(); \ + while ((i > 0) && (!r.empty())) { \ + r.pop_front(); \ + --i; \ + } \ + while (!r.empty()) { \ + retval.push_back(r.front()); \ + r.pop_front(); \ + } \ + retval; \ +} \ +def drop_while(container, f) : call_exists(range, container) { \ + var r = range(container); \ + var retval = Vector(); \ + while ((!r.empty())&& f(r.front())) { \ + r.pop_front(); \ + } \ + while (!r.empty()) { \ + retval.push_back(r.front()); \ + r.pop_front(); \ + } \ + retval; \ +} \ +def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \ + var r = range(container); \ + var retval = r.front(); \ + r.pop_front(); \ + retval = func(retval, r.front()); \ + r.pop_front(); \ + while (!r.empty()) { \ + retval = func(retval, r.front()); \ + r.pop_front(); \ + } \ + retval; \ +} \ +def join(container, delim) { \ + var retval = ""; \ + var range = range(container); \ + if (!range.empty()) { \ + retval += to_string(range.front()); \ + range.pop_front(); \ + while (!range.empty()) { \ + retval += delim; \ + retval += to_string(range.front()); \ + range.pop_front(); \ + } \ + } \ + retval; \ +} \ +def filter(container, f) : call_exists(range, container) { \ + var retval = Vector(); \ + var r = range(container); \ + while (!r.empty()) { \ + if (f(r.front())) { \ + retval.push_back(r.front()); \ + } \ + r.pop_front(); \ + } \ + retval; \ +} \ +def generate_range(x, y) { \ + var i = x; \ + var retval = Vector(); \ + while (i <= y) { \ + retval.push_back(i); \ + ++i; \ + } \ + retval; \ +} \ +def collate(x, y) { \ + [x, y]; \ +} \ +def zip_with(f, x, y) : call_exists(range, x) && call_exists(range, y) { \ + var r_x = range(x); \ + var r_y = range(y); \ + var retval = Vector(); \ + while (!r_x.empty() && !r_y.empty()) { \ + retval.push_back(f(r_x.front(), r_y.front())); \ + r_x.pop_front(); \ + r_y.pop_front(); \ + } \ + retval; \ +} \ +def zip(x, y) { \ + zip_with(collate, x, y); \ +}\ +) #endif /* CHAISCRIPT_PRELUDE_HPP_ */