7 #ifndef CHAISCRIPT_PRELUDE_HPP_ 8 #define CHAISCRIPT_PRELUDE_HPP_ 12 static std::string chaiscript_prelude() {
16 if (call_exists(`<`, l, r)) { 19 type_name(l) < type_name(r) 25 if (call_exists(`>`, l, r)) { 28 type_name(l) > type_name(r) 33 if (call_exists(`==`, l, r)) { 45 double(x).clone_var_attrs(x) 49 string(x).clone_var_attrs(x) 53 vector(x).clone_var_attrs(x) 58 int(x).clone_var_attrs(x) 61 def clone(x) : function_exists(type_name(x)) && call_exists(eval(type_name(x)), x) 63 eval(type_name(x))(x).clone_var_attrs(x); 67 # to_string for Pair() 68 def to_string(x) : call_exists(first, x) && call_exists(second, x) { 69 "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; 72 # to_string for containers 73 def to_string(x) : call_exists(range, x) && !x.is_type("string"){ 74 "[" + x.join(", ") + "]"; 77 # Prints to console with no carriage return 79 print_string(x.to_string()); 82 # Prints to console with carriage return 84 println_string(x.to_string()); 87 # Returns the maximum value of two numbers 96 # Returns the minimum value of two numbers 108 # Returns true if the value is odd 119 # Returns true if the value is even 131 # Inserts the third value at the position of the second value into the container of the first 132 # while making a clone. 133 def insert_at(container, pos, x) 135 container.insert_ref_at(pos, clone(x)); 138 # Returns the reverse of the given container 139 def reverse(container) { 140 auto retval := new(container); 141 auto r := range(container); 143 retval.push_back(r.back()); 150 def range(r) : call_exists(range_internal, r) 152 var ri := range_internal(r); 153 ri.get_var_attr("internal_obj") := r; 157 # Return a range from a range 158 def range(r) : call_exists(empty, r) && call_exists(pop_front, r) && call_exists(pop_back, r) && call_exists(back, r) && call_exists(front, r) 164 # The retro attribute that contains the underlying range 167 # Creates a retro from a retro by returning the original range 168 def retro(r) : call_exists(get_type_name, r) && get_type_name(r) == "retro" 174 # Creates a retro range from a range 175 def retro::retro(r) : call_exists(empty, r) && call_exists(pop_front, r) && call_exists(pop_back, r) && call_exists(back, r) && call_exists(front, r) 180 # Returns the first value of a retro 186 # Returns the last value of a retro 192 # Moves the back iterator of a retro towards the front by one 193 def retro::pop_back() 195 pop_front(this.m_range) 198 # Moves the front iterator of a retro towards the back by one 199 def retro::pop_front() 201 pop_back(this.m_range) 204 # returns true if the retro is out of elements 210 # Performs the second value function over the container first value 211 def for_each(container, func) : call_exists(range, container) { 212 var t_range := range(container); 213 while (!t_range.empty()) { 214 func(t_range.front()); 219 def any_of(container, func) : call_exists(range, container) { 220 var t_range := range(container); 221 while (!t_range.empty()) { 222 if (func(t_range.front())) { 230 def all_of(container, func) : call_exists(range, container) { 231 var t_range := range(container); 232 while (!t_range.empty()) { 233 if (!func(t_range.front())) { 242 def back_inserter(container) { 243 bind(push_back, container, _); 246 def contains(container, item, compare_func) : call_exists(range, container) { 247 auto t_range := range(container); 248 while (!t_range.empty()) { 249 if ( compare_func(t_range.front(), item) ) { 258 def contains(container, item) { 259 contains(container, item, eq) 262 def map(container, func, inserter) : call_exists(range, container) { 263 auto range := range(container); 264 while (!range.empty()) { 265 inserter(func(range.front())); 270 # Performs the second value function over the container first value. Creates a new container with the results 271 def map(container, func) { 272 auto retval := new(container); 273 map(container, func, back_inserter(retval)); 277 # Performs the second value function over the container first value. Starts with initial and continues with each element. 278 def foldl(container, func, initial) : call_exists(range, container){ 279 auto retval = initial; 280 auto range := range(container); 281 while (!range.empty()) { 282 retval = (func(range.front(), retval)); 288 # Returns the sum of the elements of the given value 290 foldl(container, `+`, 0.0) 293 # Returns the product of the elements of the given value 294 def product(container) { 295 foldl(container, `*`, 1.0) 298 # Returns a new container with the elements of the first value concatenated with the elements of the second value 299 def concat(x, y) : call_exists(clone, x) { 301 auto inserter := back_inserter(retval); 302 auto range := range(y); 303 while (!range.empty()) { 304 inserter(range.front()); 311 def take(container, num, inserter) : call_exists(range, container) { 312 auto r := range(container); 314 while ((i > 0) && (!r.empty())) { 322 # Returns a new container with the given number of elements taken from the container 323 def take(container, num) { 324 auto retval := new(container); 325 take(container, num, back_inserter(retval)); 330 def take_while(container, f, inserter) : call_exists(range, container) { 331 auto r := range(container); 332 while ((!r.empty()) && f(r.front())) { 339 # Returns a new container with the given elements match the second value function 340 def take_while(container, f) { 341 auto retval := new(container); 342 take_while(container, f, back_inserter(retval)); 347 def drop(container, num, inserter) : call_exists(range, container) { 348 auto r := range(container); 350 while ((i > 0) && (!r.empty())) { 361 # Returns a new container with the given number of elements dropped from the given container 362 def drop(container, num) { 363 auto retval := new(container); 364 drop(container, num, back_inserter(retval)); 369 def drop_while(container, f, inserter) : call_exists(range, container) { 370 auto r := range(container); 371 while ((!r.empty())&& f(r.front())) { 381 # Returns a new container with the given elements dropped that match the second value function 382 def drop_while(container, f) { 383 auto retval := new(container); 384 drop_while(container, f, back_inserter(retval)); 389 # Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements. 390 def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { 391 auto r := range(container); 392 auto retval = r.front(); 394 retval = func(retval, r.front()); 397 retval = func(retval, r.front()); 404 # Returns a string of the elements in container delimited by the second value string 405 def join(container, delim) { 407 auto range := range(container); 408 if (!range.empty()) { 409 retval += to_string(range.front()); 411 while (!range.empty()) { 413 retval += to_string(range.front()); 421 def filter(container, f, inserter) : call_exists(range, container) { 422 auto r := range(container); 432 # Returns a new Vector which match the second value function 433 def filter(container, f) { 434 auto retval := new(container); 435 filter(container, f, back_inserter(retval)); 440 def generate_range(x, y, inserter) { 449 # Returns a new Vector which represents the range from the first value to the second value 450 def generate_range(x, y) { 451 auto retval := Vector(); 452 generate_range(x,y,back_inserter(retval)); 457 # Returns a new Vector with the first value to the second value as its elements 463 def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) { 464 auto r_x := range(x); 465 auto r_y := range(y); 466 while (!r_x.empty() && !r_y.empty()) { 467 inserter(f(r_x.front(), r_y.front())); 474 # Returns a new Vector which joins matching elements of the second and third value with the first value function 475 def zip_with(f, x, y) { 476 auto retval := Vector(); 477 zip_with(f,x,y,back_inserter(retval)); 482 # Returns a new Vector which joins matching elements of the first and second 484 zip_with(collate, x, y); 488 # Returns the position of the second value string in the first value string 489 def string::find(string substr) { 490 find(this, substr, size_t(0)); 494 # Returns the position of last match of the second value string in the first value string 495 def string::rfind(string substr) { 496 rfind(this, substr, size_t(-1)); 500 # Returns the position of the first match of elements in the second value string in the first value string 501 def string::find_first_of(string list) { 502 find_first_of(this, list, size_t(0)); 506 # Returns the position of the last match of elements in the second value string in the first value string 507 def string::find_last_of(string list) { 508 find_last_of(this, list, size_t(-1)); 512 # Returns the position of the first non-matching element in the second value string in the first value string 513 def string::find_first_not_of(string list) { 514 find_first_not_of(this, list, size_t(0)); 518 # Returns the position of the last non-matching element in the second value string in the first value string 519 def string::find_last_not_of(string list) { 520 find_last_not_of(this, list, size_t(-1)); 524 def string::ltrim() { 525 drop_while(this, fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'}); 529 def string::rtrim() { 530 reverse(drop_while(reverse(this), fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'})); 539 def find(container, value, Function compare_func) : call_exists(range, container) { 540 auto range := range(container); 541 while (!range.empty()) { 542 if (compare_func(range.front(), value)) { 552 def find(container, value) { 553 find(container, value, eq) Namespace chaiscript contains every API call that the average user will be concerned with...
Definition: chaiscript_prelude.hpp:11