int is dead, long live long type!
This commit is contained in:
parent
7e561256ef
commit
fcb3b4c5c5
|
|
@ -137,11 +137,11 @@ namespace json11 {
|
||||||
}
|
}
|
||||||
|
|
||||||
static MlValue ivalualize(int value) {
|
static MlValue ivalualize(int value) {
|
||||||
return MlValue(value);
|
return MlValue((long)value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MlValue ivalualize(bool value) {
|
static MlValue ivalualize(bool value) {
|
||||||
return MlValue(value);
|
return MlValue((long)value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MlValue ivalualize(const string &value) {
|
static MlValue ivalualize(const string &value) {
|
||||||
|
|
|
||||||
48
ml.cpp
48
ml.cpp
|
|
@ -61,7 +61,7 @@ bool is_symbol(char ch) {
|
||||||
|
|
||||||
MlValue::MlValue() : type(NIL) {}
|
MlValue::MlValue() : type(NIL) {}
|
||||||
|
|
||||||
MlValue::MlValue(int i) : type(INT) { stack_data.i = i; }
|
MlValue::MlValue(long i) : type(INT) { stack_data.i = i; }
|
||||||
|
|
||||||
MlValue::MlValue(double f) : type(FLOAT) { stack_data.f = f; }
|
MlValue::MlValue(double f) : type(FLOAT) { stack_data.f = f; }
|
||||||
|
|
||||||
|
|
@ -168,11 +168,11 @@ bool MlValue::is_number() const {
|
||||||
|
|
||||||
// Get the "truthy" boolean value of this value.
|
// Get the "truthy" boolean value of this value.
|
||||||
bool MlValue::as_bool() const {
|
bool MlValue::as_bool() const {
|
||||||
return type != NIL && *this != MlValue(0); // TODO remove 0 as false
|
return type != NIL && *this != MlValue(0l); // TODO remove 0 as false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get this item's integer value
|
// Get this item's integer value
|
||||||
int MlValue::as_int() const {
|
long MlValue::as_int() const {
|
||||||
return cast_to_int().stack_data.i;
|
return cast_to_int().stack_data.i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -237,7 +237,7 @@ MlValue MlValue::cast_to_int() const {
|
||||||
case INT:
|
case INT:
|
||||||
return *this;
|
return *this;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
return MlValue(int(stack_data.f));
|
return MlValue(long(stack_data.f));
|
||||||
// Only ints and floats can be cast to an int
|
// Only ints and floats can be cast to an int
|
||||||
default:
|
default:
|
||||||
throw MlError(*this, MlEnvironment(), BAD_CAST);
|
throw MlError(*this, MlEnvironment(), BAD_CAST);
|
||||||
|
|
@ -778,8 +778,8 @@ MlValue parse(std::string &s, int &ptr) {
|
||||||
skip_whitespace(s, ptr);
|
skip_whitespace(s, ptr);
|
||||||
|
|
||||||
if (n.find('.') != std::string::npos)
|
if (n.find('.') != std::string::npos)
|
||||||
return MlValue((negate ? -1 : 1) * atof(n.c_str()));
|
return MlValue((negate ? -1l : 1l) * atof(n.c_str()));
|
||||||
else return MlValue((negate ? -1 : 1) * atoi(n.c_str()));
|
else return MlValue((negate ? -1l : 1l) * atol(n.c_str()));
|
||||||
|
|
||||||
} else if (s[ptr] == '\"') {
|
} else if (s[ptr] == '\"') {
|
||||||
// If this is a string
|
// If this is a string
|
||||||
|
|
@ -903,7 +903,7 @@ namespace builtin {
|
||||||
else if (args.size() == 3)
|
else if (args.size() == 3)
|
||||||
return args[2].eval(env);
|
return args[2].eval(env);
|
||||||
|
|
||||||
return MlValue(0);
|
return MlValue(0l);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a variable with a value (SPECIAL FORM)
|
// Define a variable with a value (SPECIAL FORM)
|
||||||
|
|
@ -1031,7 +1031,7 @@ namespace builtin {
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue("random", random), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("random", random), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
int low = args[0].as_int(), high = args[1].as_int();
|
long low = args[0].as_int(), high = args[1].as_int();
|
||||||
return MlValue(rand() % (high - low + 1) + low);
|
return MlValue(rand() % (high - low + 1) + low);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1064,7 +1064,7 @@ namespace builtin {
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue("write-file", write_file), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("write-file", write_file), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
return MlValue(write_file_contents(args[0].as_string(), args[1].as_string()));
|
return MlValue((long)write_file_contents(args[0].as_string(), args[1].as_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read URL to (code content)
|
// Read URL to (code content)
|
||||||
|
|
@ -1086,7 +1086,7 @@ namespace builtin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<int, std::string> result = client.doGetRequest(args[0].as_string(), headers);
|
std::pair<long, std::string> result = client.doGetRequest(args[0].as_string(), headers);
|
||||||
// TODO add helper function for this
|
// TODO add helper function for this
|
||||||
std::vector<MlValue> lst;
|
std::vector<MlValue> lst;
|
||||||
lst.push_back(MlValue(result.first));
|
lst.push_back(MlValue(result.first));
|
||||||
|
|
@ -1187,7 +1187,7 @@ namespace builtin {
|
||||||
if (args.size() != 1)
|
if (args.size() != 1)
|
||||||
throw MlError(MlValue("is-file?", is_file), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("is-file?", is_file), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
return MlValue(is_path_file(args[0].as_string()));
|
return MlValue((long)is_path_file(args[0].as_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// is_path directory
|
// is_path directory
|
||||||
|
|
@ -1198,7 +1198,7 @@ namespace builtin {
|
||||||
if (args.size() != 1)
|
if (args.size() != 1)
|
||||||
throw MlError(MlValue("is-dir?", is_dir), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("is-dir?", is_dir), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
return MlValue(is_path_dir(args[0].as_string()));
|
return MlValue((long)is_path_dir(args[0].as_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a file and execute its code
|
// Read a file and execute its code
|
||||||
|
|
@ -1292,7 +1292,7 @@ namespace builtin {
|
||||||
|
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue("=", eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("=", eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
return MlValue(int(args[0] == args[1]));
|
return MlValue(long(args[0] == args[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Are two values not equal?
|
// Are two values not equal?
|
||||||
|
|
@ -1301,7 +1301,7 @@ namespace builtin {
|
||||||
|
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue("!=", neq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("!=", neq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
return MlValue(int(args[0] != args[1]));
|
return MlValue(long(args[0] != args[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is one number greater than another?
|
// Is one number greater than another?
|
||||||
|
|
@ -1310,7 +1310,7 @@ namespace builtin {
|
||||||
|
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue(">", greater), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue(">", greater), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
return MlValue(int(args[0] > args[1]));
|
return MlValue(long(args[0] > args[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is one number less than another?
|
// Is one number less than another?
|
||||||
|
|
@ -1319,7 +1319,7 @@ namespace builtin {
|
||||||
|
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue("<", less), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("<", less), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
return MlValue(int(args[0] < args[1]));
|
return MlValue(long(args[0] < args[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is one number greater than or equal to another?
|
// Is one number greater than or equal to another?
|
||||||
|
|
@ -1328,7 +1328,7 @@ namespace builtin {
|
||||||
|
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue(">=", greater_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue(">=", greater_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
return MlValue(int(args[0] >= args[1]));
|
return MlValue(long(args[0] >= args[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is one number less than or equal to another?
|
// Is one number less than or equal to another?
|
||||||
|
|
@ -1337,7 +1337,7 @@ namespace builtin {
|
||||||
|
|
||||||
if (args.size() != 2)
|
if (args.size() != 2)
|
||||||
throw MlError(MlValue("<=", less_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("<=", less_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
return MlValue(int(args[0] <= args[1]));
|
return MlValue(long(args[0] <= args[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the type name of a value
|
// Get the type name of a value
|
||||||
|
|
@ -1388,7 +1388,7 @@ namespace builtin {
|
||||||
throw MlError(MlValue("index", index), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("index", index), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
std::vector<MlValue> list = args[0].as_list();
|
std::vector<MlValue> list = args[0].as_list();
|
||||||
int i = args[1].as_int();
|
long i = args[1].as_int();
|
||||||
if (list.empty() || i >= list.size())
|
if (list.empty() || i >= list.size())
|
||||||
throw MlError(list, env, INDEX_OUT_OF_RANGE);
|
throw MlError(list, env, INDEX_OUT_OF_RANGE);
|
||||||
|
|
||||||
|
|
@ -1403,7 +1403,7 @@ namespace builtin {
|
||||||
throw MlError(MlValue("insert", insert), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("insert", insert), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
std::vector<MlValue> list = args[0].as_list();
|
std::vector<MlValue> list = args[0].as_list();
|
||||||
int i = args[1].as_int();
|
long i = args[1].as_int();
|
||||||
if (i > list.size())
|
if (i > list.size())
|
||||||
throw MlError(list, env, INDEX_OUT_OF_RANGE);
|
throw MlError(list, env, INDEX_OUT_OF_RANGE);
|
||||||
|
|
||||||
|
|
@ -1419,7 +1419,7 @@ namespace builtin {
|
||||||
throw MlError(MlValue("remove", remove), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("remove", remove), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
std::vector<MlValue> list = args[0].as_list();
|
std::vector<MlValue> list = args[0].as_list();
|
||||||
int i = args[1].as_int();
|
long i = args[1].as_int();
|
||||||
if (list.empty() || i >= list.size())
|
if (list.empty() || i >= list.size())
|
||||||
throw MlError(list, env, INDEX_OUT_OF_RANGE);
|
throw MlError(list, env, INDEX_OUT_OF_RANGE);
|
||||||
|
|
||||||
|
|
@ -1436,7 +1436,7 @@ namespace builtin {
|
||||||
TOO_MANY_ARGS : TOO_FEW_ARGS
|
TOO_MANY_ARGS : TOO_FEW_ARGS
|
||||||
);
|
);
|
||||||
|
|
||||||
return MlValue(int(args[0].as_list().size()));
|
return MlValue(long(args[0].as_list().size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add an item to the end of a list
|
// Add an item to the end of a list
|
||||||
|
|
@ -1520,7 +1520,7 @@ namespace builtin {
|
||||||
if (args.size() != 2) // if (args.size() < 2 || args.size() > 3)
|
if (args.size() != 2) // if (args.size() < 2 || args.size() > 3)
|
||||||
throw MlError(MlValue("string-regex?", string_regex), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
throw MlError(MlValue("string-regex?", string_regex), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||||
|
|
||||||
return MlValue(regexp_search(args[0].as_string(), args[1].as_string()));
|
return MlValue((long)regexp_search(args[0].as_string(), args[1].as_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
MlValue string_pad(std::vector<MlValue> args, MlEnvironment &env) {
|
MlValue string_pad(std::vector<MlValue> args, MlEnvironment &env) {
|
||||||
|
|
@ -1633,7 +1633,7 @@ namespace builtin {
|
||||||
|
|
||||||
while (low < high) {
|
while (low < high) {
|
||||||
result.push_back(low);
|
result.push_back(low);
|
||||||
low = low + MlValue(1);
|
low = low + MlValue(1l);
|
||||||
}
|
}
|
||||||
return MlValue(result);
|
return MlValue(result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
ml.h
6
ml.h
|
|
@ -98,7 +98,7 @@ public:
|
||||||
MlValue();
|
MlValue();
|
||||||
|
|
||||||
// Constructs an integer
|
// Constructs an integer
|
||||||
MlValue(int i);
|
MlValue(long i);
|
||||||
|
|
||||||
// Constructs a floating point value
|
// Constructs a floating point value
|
||||||
MlValue(double f);
|
MlValue(double f);
|
||||||
|
|
@ -141,7 +141,7 @@ public:
|
||||||
bool as_bool() const;
|
bool as_bool() const;
|
||||||
|
|
||||||
// Get this item's integer value
|
// Get this item's integer value
|
||||||
int as_int() const;
|
long as_int() const;
|
||||||
|
|
||||||
// Get this item's floating point value
|
// Get this item's floating point value
|
||||||
double as_float() const;
|
double as_float() const;
|
||||||
|
|
@ -224,7 +224,7 @@ private:
|
||||||
} type;
|
} type;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
int i;
|
long i;
|
||||||
double f;
|
double f;
|
||||||
Builtin b;
|
Builtin b;
|
||||||
} stack_data;
|
} stack_data;
|
||||||
|
|
|
||||||
14
ml_date.cpp
14
ml_date.cpp
|
|
@ -1,15 +1,15 @@
|
||||||
|
|
||||||
#include "ml_date.h"
|
#include "ml_date.h"
|
||||||
|
|
||||||
int now() {
|
long now() {
|
||||||
// get-universal-time
|
// get-universal-time
|
||||||
time_t t = std::time(0);
|
time_t t = std::time(0);
|
||||||
long int now = static_cast<long int>(t);
|
long int now = static_cast<long int>(t);
|
||||||
|
|
||||||
return (int) now;
|
return now;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string date_to_string(const int datetime, const std::string format) {
|
std::string date_to_string(const long datetime, const std::string format) {
|
||||||
// std::locale::global(std::locale("en-US.UTF8"));
|
// std::locale::global(std::locale("en-US.UTF8"));
|
||||||
|
|
||||||
time_t timestamp = datetime;
|
time_t timestamp = datetime;
|
||||||
|
|
@ -23,16 +23,16 @@ std::string date_to_string(const int datetime, const std::string format) {
|
||||||
return "invalid argument";
|
return "invalid argument";
|
||||||
}
|
}
|
||||||
|
|
||||||
int string_to_date(const std::string &datestr, const std::string &format) {
|
long string_to_date(const std::string &datestr, const std::string &format) {
|
||||||
// format for example "%d.%m.%Y";
|
// format for example "%d.%m.%Y";
|
||||||
|
|
||||||
std::istringstream in{datestr.c_str()};
|
std::istringstream in{datestr.c_str()};
|
||||||
date::sys_seconds tp;
|
date::sys_seconds tp;
|
||||||
in >> date::parse(format, tp);
|
in >> date::parse(format, tp);
|
||||||
return (int) tp.time_since_epoch().count();
|
return tp.time_since_epoch().count();
|
||||||
}
|
}
|
||||||
|
|
||||||
int add_to_date(const int datetime, const int quantity, const std::string &part) {
|
long add_to_date(const long datetime, const long quantity, const std::string &part) {
|
||||||
// part is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'
|
// part is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'
|
||||||
|
|
||||||
// very basic implementation, just for now - no timezones DST etc
|
// very basic implementation, just for now - no timezones DST etc
|
||||||
|
|
@ -55,5 +55,5 @@ int add_to_date(const int datetime, const int quantity, const std::string &part)
|
||||||
// TODO exception here
|
// TODO exception here
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int) mktime(tm);
|
return mktime(tm);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,12 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
int now();
|
long now();
|
||||||
|
|
||||||
// TODO temporary solution before format will be implemented
|
// TODO temporary solution before format will be implemented
|
||||||
std::string date_to_string(const int datetime, const std::string format);
|
std::string date_to_string(const long datetime, const std::string format);
|
||||||
|
|
||||||
int string_to_date(const std::string &datestr, const std::string &format);
|
long string_to_date(const std::string &datestr, const std::string &format);
|
||||||
|
|
||||||
int add_to_date(const int datetime, const int quantity, const std::string &part);
|
long add_to_date(const long datetime, const long quantity, const std::string &part);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ MlValue exec_system_cmd(const std::string &cmd) {
|
||||||
|
|
||||||
// TODO add helper function for this
|
// TODO add helper function for this
|
||||||
std::vector<MlValue> lst;
|
std::vector<MlValue> lst;
|
||||||
lst.push_back(MlValue(cmd_retval));
|
lst.push_back(MlValue((long)cmd_retval));
|
||||||
lst.push_back(MlValue::string(cmd_output));
|
lst.push_back(MlValue::string(cmd_output));
|
||||||
return lst;
|
return lst;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue