int is dead, long live long type!

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

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);
}