Formatting cleanup

This commit is contained in:
Jason Turner 2018-06-11 05:27:48 -06:00
parent 07fdb3bf6e
commit 2dfd444178

View File

@ -8,36 +8,36 @@
#define CHAISCRIPT_PRELUDE_HPP_ #define CHAISCRIPT_PRELUDE_HPP_
namespace chaiscript { namespace chaiscript {
struct ChaiScript_Prelude { struct ChaiScript_Prelude {
static std::string chaiscript_prelude() { return R"chaiscript( static std::string chaiscript_prelude() { return R"chaiscript(
def lt(l, r) { def lt(l, r) {
if (call_exists(`<`, l, r)) { if (call_exists(`<`, l, r)) {
l < r l < r
} else { } else {
type_name(l) < type_name(r) type_name(l) < type_name(r)
} }
} }
def gt(l, r) { def gt(l, r) {
if (call_exists(`>`, l, r)) { if (call_exists(`>`, l, r)) {
l > r l > r
} else { } else {
type_name(l) > type_name(r) type_name(l) > type_name(r)
} }
} }
def eq(l, r) { def eq(l, r) {
if (call_exists(`==`, l, r)) { if (call_exists(`==`, l, r)) {
l == r l == r
} else { } else {
false false
} }
} }
def new(x) { def new(x) {
eval(type_name(x))(); eval(type_name(x))();
} }
def clone(double x) { def clone(double x) {
@ -64,85 +64,85 @@ def clone(x) : function_exists(type_name(x)) && call_exists(eval(type_name(x)),
# to_string for Pair() # to_string for Pair()
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() + ">";
} }
# to_string for containers # to_string for containers
def to_string(x) : call_exists(range, x) && !x.is_type("string"){ def to_string(x) : call_exists(range, x) && !x.is_type("string"){
"[" + x.join(", ") + "]"; "[" + x.join(", ") + "]";
} }
# Prints to console with no carriage return # Prints to console with no carriage return
def puts(x) { def puts(x) {
print_string(x.to_string()); print_string(x.to_string());
} }
# Prints to console with carriage return # Prints to console with carriage return
def print(x) { def print(x) {
println_string(x.to_string()); println_string(x.to_string());
} }
# Returns the maximum value of two numbers # Returns the maximum value of two numbers
def max(a, b) { def max(a, b) {
if (a>b) { if (a>b) {
a a
} else { } else {
b b
} }
} }
# Returns the minimum value of two numbers # Returns the minimum value of two numbers
def min(a, b) def min(a, b)
{ {
if (a<b) if (a<b)
{ {
a a
} else { } else {
b b
} }
} }
# Returns true if the value is odd # Returns true if the value is odd
def odd(x) { def odd(x) {
if (x % 2 == 1) if (x % 2 == 1)
{ {
true true
} else { } else {
false false
} }
} }
# Returns true if the value is even # Returns true if the value is even
def even(x) def even(x)
{ {
if (x % 2 == 0) if (x % 2 == 0)
{ {
true true
} else { } else {
false false
} }
} }
# Inserts the third value at the position of the second value into the container of the first # Inserts the third value at the position of the second value into the container of the first
# while making a clone. # while making a clone.
def insert_at(container, pos, x) def insert_at(container, pos, x)
{ {
container.insert_ref_at(pos, clone(x)); container.insert_ref_at(pos, clone(x));
} }
# Returns the reverse of the given container # Returns the reverse of the given container
def reverse(container) { def reverse(container) {
auto retval := new(container); auto retval := new(container);
auto r := range(container); auto r := range(container);
while (!r.empty()) { while (!r.empty()) {
retval.push_back(r.back()); retval.push_back(r.back());
r.pop_back(); r.pop_back();
} }
retval; retval;
} }
@ -153,86 +153,86 @@ def range(r) : call_exists(range_internal, r)
ri; ri;
} }
# Return a range from a range # Return a range from a range
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) 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)
{ {
clone(r); clone(r);
} }
# The retro attribute that contains the underlying range # The retro attribute that contains the underlying range
attr retro::m_range; attr retro::m_range;
# Creates a retro from a retro by returning the original range # Creates a retro from a retro by returning the original range
def retro(r) : call_exists(get_type_name, r) && get_type_name(r) == "retro" def retro(r) : call_exists(get_type_name, r) && get_type_name(r) == "retro"
{ {
clone(r.m_range) clone(r.m_range)
} }
# Creates a retro range from a range # Creates a retro range from a range
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) 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)
{ {
this.m_range = r; this.m_range = r;
} }
# Returns the first value of a retro # Returns the first value of a retro
def retro::front() def retro::front()
{ {
back(this.m_range) back(this.m_range)
} }
# Returns the last value of a retro # Returns the last value of a retro
def retro::back() def retro::back()
{
front(this.m_range)
}
# Moves the back iterator of a retro towards the front by one
def retro::pop_back()
{ {
pop_front(this.m_range) front(this.m_range)
} }
# Moves the front iterator of a retro towards the back by one # Moves the back iterator of a retro towards the front by one
def retro::pop_front() def retro::pop_back()
{ {
pop_back(this.m_range) pop_front(this.m_range)
} }
# returns true if the retro is out of elements # Moves the front iterator of a retro towards the back by one
def retro::empty() def retro::pop_front()
{ {
empty(this.m_range); pop_back(this.m_range)
} }
# returns true if the retro is out of elements
def retro::empty()
{
empty(this.m_range);
}
# Performs the second value function over the container first value # Performs the second value function over the container first value
def for_each(container, func) : call_exists(range, container) { def for_each(container, func) : call_exists(range, container) {
var t_range := range(container); var t_range := range(container);
while (!t_range.empty()) { while (!t_range.empty()) {
func(t_range.front()); func(t_range.front());
t_range.pop_front(); t_range.pop_front();
} }
} }
def any_of(container, func) : call_exists(range, container) { def any_of(container, func) : call_exists(range, container) {
var t_range := range(container); var t_range := range(container);
while (!t_range.empty()) { while (!t_range.empty()) {
if (func(t_range.front())) { if (func(t_range.front())) {
return true; return true;
} }
t_range.pop_front(); t_range.pop_front();
} }
false; false;
} }
def all_of(container, func) : call_exists(range, container) { def all_of(container, func) : call_exists(range, container) {
var t_range := range(container); var t_range := range(container);
while (!t_range.empty()) { while (!t_range.empty()) {
if (!func(t_range.front())) { if (!func(t_range.front())) {
return false; return false;
} }
t_range.pop_front(); t_range.pop_front();
} }
true; true;
@ -242,315 +242,315 @@ def back_inserter(container) {
bind(push_back, container, _); bind(push_back, container, _);
} }
def contains(container, item, compare_func) : call_exists(range, container) { def contains(container, item, compare_func) : call_exists(range, container) {
auto t_range := range(container); auto t_range := range(container);
while (!t_range.empty()) { while (!t_range.empty()) {
if ( compare_func(t_range.front(), item) ) { if ( compare_func(t_range.front(), item) ) {
return true; return true;
} }
t_range.pop_front(); t_range.pop_front();
} }
false; false;
} }
def contains(container, item) { def contains(container, item) {
contains(container, item, eq) contains(container, item, eq)
} }
def map(container, func, inserter) : call_exists(range, container) { def map(container, func, inserter) : call_exists(range, container) {
auto range := range(container); auto range := range(container);
while (!range.empty()) { while (!range.empty()) {
inserter(func(range.front())); inserter(func(range.front()));
range.pop_front(); range.pop_front();
} }
} }
# Performs the second value function over the container first value. Creates a new container with the results # Performs the second value function over the container first value. Creates a new container with the results
def map(container, func) { def map(container, func) {
auto retval := new(container); auto retval := new(container);
map(container, func, back_inserter(retval)); map(container, func, back_inserter(retval));
retval; retval;
} }
# Performs the second value function over the container first value. Starts with initial and continues with each element. # Performs the second value function over the container first value. Starts with initial and continues with each element.
def foldl(container, func, initial) : call_exists(range, container){ def foldl(container, func, initial) : call_exists(range, container){
auto retval = initial; auto retval = initial;
auto range := range(container); auto range := range(container);
while (!range.empty()) { while (!range.empty()) {
retval = (func(range.front(), retval)); retval = (func(range.front(), retval));
range.pop_front(); range.pop_front();
} }
retval; retval;
} }
# Returns the sum of the elements of the given value # Returns the sum of the elements of the given value
def sum(container) { def sum(container) {
foldl(container, `+`, 0.0) foldl(container, `+`, 0.0)
} }
# Returns the product of the elements of the given value # Returns the product of the elements of the given value
def product(container) { def product(container) {
foldl(container, `*`, 1.0) foldl(container, `*`, 1.0)
} }
# Returns a new container with the elements of the first value concatenated with the elements of the second value # Returns a new container with the elements of the first value concatenated with the elements of the second value
def concat(x, y) : call_exists(clone, x) { def concat(x, y) : call_exists(clone, x) {
auto retval = x; auto retval = x;
auto inserter := back_inserter(retval); auto inserter := back_inserter(retval);
auto range := range(y); auto range := range(y);
while (!range.empty()) { while (!range.empty()) {
inserter(range.front()); inserter(range.front());
range.pop_front(); range.pop_front();
} }
retval; retval;
} }
def take(container, num, inserter) : call_exists(range, container) { def take(container, num, inserter) : call_exists(range, container) {
auto r := range(container); auto r := range(container);
auto i = num; auto i = num;
while ((i > 0) && (!r.empty())) { while ((i > 0) && (!r.empty())) {
inserter(r.front()); inserter(r.front());
r.pop_front(); r.pop_front();
--i; --i;
} }
} }
# Returns a new container with the given number of elements taken from the container # Returns a new container with the given number of elements taken from the container
def take(container, num) { def take(container, num) {
auto retval := new(container); auto retval := new(container);
take(container, num, back_inserter(retval)); take(container, num, back_inserter(retval));
retval;
}
def take_while(container, f, inserter) : call_exists(range, container) {
auto r := range(container);
while ((!r.empty()) && f(r.front())) {
inserter(r.front());
r.pop_front();
}
}
# Returns a new container with the given elements match the second value function
def take_while(container, f) {
auto retval := new(container);
take_while(container, f, back_inserter(retval));
retval; retval;
} }
def drop(container, num, inserter) : call_exists(range, container) { def take_while(container, f, inserter) : call_exists(range, container) {
auto r := range(container); auto r := range(container);
auto i = num; while ((!r.empty()) && f(r.front())) {
while ((i > 0) && (!r.empty())) { inserter(r.front());
r.pop_front(); r.pop_front();
--i; }
}
while (!r.empty()) {
inserter(r.front());
r.pop_front();
}
} }
# Returns a new container with the given number of elements dropped from the given container # Returns a new container with the given elements match the second value function
def take_while(container, f) {
auto retval := new(container);
take_while(container, f, back_inserter(retval));
retval;
}
def drop(container, num, inserter) : call_exists(range, container) {
auto r := range(container);
auto i = num;
while ((i > 0) && (!r.empty())) {
r.pop_front();
--i;
}
while (!r.empty()) {
inserter(r.front());
r.pop_front();
}
}
# Returns a new container with the given number of elements dropped from the given container
def drop(container, num) { def drop(container, num) {
auto retval := new(container); auto retval := new(container);
drop(container, num, back_inserter(retval)); drop(container, num, back_inserter(retval));
retval; retval;
} }
def drop_while(container, f, inserter) : call_exists(range, container) { def drop_while(container, f, inserter) : call_exists(range, container) {
auto r := range(container); auto 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()) {
inserter(r.front()); inserter(r.front());
r.pop_front(); r.pop_front();
} }
} }
# Returns a new container with the given elements dropped that match the second value function # Returns a new container with the given elements dropped that match the second value function
def drop_while(container, f) { def drop_while(container, f) {
auto retval := new(container); auto retval := new(container);
drop_while(container, f, back_inserter(retval)); drop_while(container, f, back_inserter(retval));
retval; retval;
} }
# Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements. # Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements.
def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { def reduce(container, func) : container.size() >= 2 && call_exists(range, container) {
auto r := range(container); auto r := range(container);
auto retval = r.front(); auto retval = r.front();
r.pop_front(); r.pop_front();
retval = func(retval, r.front()); retval = func(retval, r.front());
r.pop_front(); r.pop_front();
while (!r.empty()) { while (!r.empty()) {
retval = func(retval, r.front()); retval = func(retval, r.front());
r.pop_front(); r.pop_front();
} }
retval; retval;
} }
# Returns a string of the elements in container delimited by the second value string # Returns a string of the elements in container delimited by the second value string
def join(container, delim) { def join(container, delim) {
auto retval = ""; auto retval = "";
auto range := range(container); auto range := range(container);
if (!range.empty()) { if (!range.empty()) {
retval += to_string(range.front()); retval += to_string(range.front());
range.pop_front(); range.pop_front();
while (!range.empty()) { while (!range.empty()) {
retval += delim; retval += delim;
retval += to_string(range.front()); retval += to_string(range.front());
range.pop_front(); range.pop_front();
} }
} }
retval; retval;
} }
def filter(container, f, inserter) : call_exists(range, container) { def filter(container, f, inserter) : call_exists(range, container) {
auto r := range(container); auto r := range(container);
while (!r.empty()) { while (!r.empty()) {
if (f(r.front())) { if (f(r.front())) {
inserter(r.front()); inserter(r.front());
} }
r.pop_front(); r.pop_front();
} }
} }
# Returns a new Vector which match the second value function # Returns a new Vector which match the second value function
def filter(container, f) { def filter(container, f) {
auto retval := new(container); auto retval := new(container);
filter(container, f, back_inserter(retval)); filter(container, f, back_inserter(retval));
retval; retval;
} }
def generate_range(x, y, inserter) { def generate_range(x, y, inserter) {
auto i = x; auto i = x;
while (i <= y) { while (i <= y) {
inserter(i); inserter(i);
++i; ++i;
} }
} }
# Returns a new Vector which represents the range from the first value to the second value # Returns a new Vector which represents the range from the first value to the second value
def generate_range(x, y) { def generate_range(x, y) {
auto retval := Vector(); auto retval := Vector();
generate_range(x,y,back_inserter(retval)); generate_range(x,y,back_inserter(retval));
retval; retval;
} }
# Returns a new Vector with the first value to the second value as its elements # Returns a new Vector with the first value to the second value as its elements
def collate(x, y) { def collate(x, y) {
return [x, y]; return [x, y];
} }
def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) { def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) {
auto r_x := range(x); auto r_x := range(x);
auto r_y := range(y); auto r_y := range(y);
while (!r_x.empty() && !r_y.empty()) { while (!r_x.empty() && !r_y.empty()) {
inserter(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();
} }
} }
# Returns a new Vector which joins matching elements of the second and third value with the first value function # Returns a new Vector which joins matching elements of the second and third value with the first value function
def zip_with(f, x, y) { def zip_with(f, x, y) {
auto retval := Vector(); auto retval := Vector();
zip_with(f,x,y,back_inserter(retval)); zip_with(f,x,y,back_inserter(retval));
retval; retval;
} }
# Returns a new Vector which joins matching elements of the first and second # Returns a new Vector which joins matching elements of the first and second
def zip(x, y) { def zip(x, y) {
zip_with(collate, x, y); zip_with(collate, x, y);
} }
# Returns the position of the second value string in the first value string # Returns the position of the second value string in the first value string
def string::find(string substr) { def string::find(string substr) {
find(this, substr, size_t(0)); find(this, substr, size_t(0));
} }
# Returns the position of last match of the second value string in the first value string # Returns the position of last match of the second value string in the first value string
def string::rfind(string substr) { def string::rfind(string substr) {
rfind(this, substr, size_t(-1)); rfind(this, substr, size_t(-1));
} }
# Returns the position of the first match of elements in the second value string in the first value string # Returns the position of the first match of elements in the second value string in the first value string
def string::find_first_of(string list) { def string::find_first_of(string list) {
find_first_of(this, list, size_t(0)); find_first_of(this, list, size_t(0));
} }
# Returns the position of the last match of elements in the second value string in the first value string # Returns the position of the last match of elements in the second value string in the first value string
def string::find_last_of(string list) { def string::find_last_of(string list) {
find_last_of(this, list, size_t(-1)); find_last_of(this, list, size_t(-1));
}
# Returns the position of the first non-matching element in the second value string in the first value string
def string::find_first_not_of(string list) {
find_first_not_of(this, list, size_t(0));
}
# Returns the position of the last non-matching element in the second value string in the first value string
def string::find_last_not_of(string list) {
find_last_not_of(this, list, size_t(-1));
}
def string::ltrim() {
drop_while(this, fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'});
}
def string::rtrim() {
reverse(drop_while(reverse(this), fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'}));
}
def string::trim() {
ltrim(rtrim(this));
} }
def find(container, value, Function compare_func) : call_exists(range, container) { # Returns the position of the first non-matching element in the second value string in the first value string
auto range := range(container); def string::find_first_not_of(string list) {
while (!range.empty()) { find_first_not_of(this, list, size_t(0));
if (compare_func(range.front(), value)) { }
return range;
} else {
range.pop_front();
}
}
range;
}
def find(container, value) { # Returns the position of the last non-matching element in the second value string in the first value string
find(container, value, eq) def string::find_last_not_of(string list) {
} find_last_not_of(this, list, size_t(-1));
}
def string::ltrim() {
drop_while(this, fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'});
}
def string::rtrim() {
reverse(drop_while(reverse(this), fun(x) { x == ' ' || x == '\t' || x == '\r' || x == '\n'}));
}
def string::trim() {
ltrim(rtrim(this));
}
def find(container, value, Function compare_func) : call_exists(range, container) {
auto range := range(container);
while (!range.empty()) {
if (compare_func(range.front(), value)) {
return range;
} else {
range.pop_front();
}
}
range;
}
def find(container, value) {
find(container, value, eq)
}
)chaiscript"; )chaiscript";