int is dead, long live long type!

This commit is contained in:
VaclavT 2021-03-17 23:56:02 +01:00
parent 7e561256ef
commit fcb3b4c5c5
6 changed files with 41 additions and 41 deletions

View File

@ -137,11 +137,11 @@ namespace json11 {
}
static MlValue ivalualize(int value) {
return MlValue(value);
return MlValue((long)value);
}
static MlValue ivalualize(bool value) {
return MlValue(value);
return MlValue((long)value);
}
static MlValue ivalualize(const string &value) {

48
ml.cpp
View File

@ -61,7 +61,7 @@ bool is_symbol(char ch) {
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; }
@ -168,11 +168,11 @@ bool MlValue::is_number() const {
// Get the "truthy" boolean value of this value.
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
int MlValue::as_int() const {
long MlValue::as_int() const {
return cast_to_int().stack_data.i;
}
@ -237,7 +237,7 @@ MlValue MlValue::cast_to_int() const {
case INT:
return *this;
case FLOAT:
return MlValue(int(stack_data.f));
return MlValue(long(stack_data.f));
// Only ints and floats can be cast to an int
default:
throw MlError(*this, MlEnvironment(), BAD_CAST);
@ -778,8 +778,8 @@ MlValue parse(std::string &s, int &ptr) {
skip_whitespace(s, ptr);
if (n.find('.') != std::string::npos)
return MlValue((negate ? -1 : 1) * atof(n.c_str()));
else return MlValue((negate ? -1 : 1) * atoi(n.c_str()));
return MlValue((negate ? -1l : 1l) * atof(n.c_str()));
else return MlValue((negate ? -1l : 1l) * atol(n.c_str()));
} else if (s[ptr] == '\"') {
// If this is a string
@ -903,7 +903,7 @@ namespace builtin {
else if (args.size() == 3)
return args[2].eval(env);
return MlValue(0);
return MlValue(0l);
}
// Define a variable with a value (SPECIAL FORM)
@ -1031,7 +1031,7 @@ namespace builtin {
if (args.size() != 2)
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);
}
@ -1064,7 +1064,7 @@ namespace builtin {
if (args.size() != 2)
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)
@ -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
std::vector<MlValue> lst;
lst.push_back(MlValue(result.first));
@ -1187,7 +1187,7 @@ namespace builtin {
if (args.size() != 1)
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
@ -1198,7 +1198,7 @@ namespace builtin {
if (args.size() != 1)
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
@ -1292,7 +1292,7 @@ namespace builtin {
if (args.size() != 2)
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?
@ -1301,7 +1301,7 @@ namespace builtin {
if (args.size() != 2)
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?
@ -1310,7 +1310,7 @@ namespace builtin {
if (args.size() != 2)
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?
@ -1319,7 +1319,7 @@ namespace builtin {
if (args.size() != 2)
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?
@ -1328,7 +1328,7 @@ namespace builtin {
if (args.size() != 2)
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?
@ -1337,7 +1337,7 @@ namespace builtin {
if (args.size() != 2)
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
@ -1388,7 +1388,7 @@ namespace builtin {
throw MlError(MlValue("index", index), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
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())
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);
std::vector<MlValue> list = args[0].as_list();
int i = args[1].as_int();
long i = args[1].as_int();
if (i > list.size())
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);
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())
throw MlError(list, env, INDEX_OUT_OF_RANGE);
@ -1436,7 +1436,7 @@ namespace builtin {
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
@ -1520,7 +1520,7 @@ namespace builtin {
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);
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) {
@ -1633,7 +1633,7 @@ namespace builtin {
while (low < high) {
result.push_back(low);
low = low + MlValue(1);
low = low + MlValue(1l);
}
return MlValue(result);
}

6
ml.h
View File

@ -98,7 +98,7 @@ public:
MlValue();
// Constructs an integer
MlValue(int i);
MlValue(long i);
// Constructs a floating point value
MlValue(double f);
@ -141,7 +141,7 @@ public:
bool as_bool() const;
// Get this item's integer value
int as_int() const;
long as_int() const;
// Get this item's floating point value
double as_float() const;
@ -224,7 +224,7 @@ private:
} type;
union {
int i;
long i;
double f;
Builtin b;
} stack_data;

View File

@ -1,15 +1,15 @@
#include "ml_date.h"
int now() {
long now() {
// get-universal-time
time_t t = std::time(0);
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"));
time_t timestamp = datetime;
@ -23,16 +23,16 @@ std::string date_to_string(const int datetime, const std::string format) {
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";
std::istringstream in{datestr.c_str()};
date::sys_seconds 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'
// 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
}
return (int) mktime(tm);
return mktime(tm);
}

View File

@ -8,12 +8,12 @@
#include <vector>
int now();
long now();
// 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);

View File

@ -93,7 +93,7 @@ MlValue exec_system_cmd(const std::string &cmd) {
// TODO add helper function for this
std::vector<MlValue> lst;
lst.push_back(MlValue(cmd_retval));
lst.push_back(MlValue((long)cmd_retval));
lst.push_back(MlValue::string(cmd_output));
return lst;
}