some formating

This commit is contained in:
VaclavT 2021-09-30 20:01:33 +02:00
parent 50aae0b0e8
commit a199f10078
4 changed files with 29 additions and 57 deletions

View File

@ -72,6 +72,7 @@
"list": "cpp", "list": "cpp",
"variant": "cpp", "variant": "cpp",
"__functional_base_03": "cpp", "__functional_base_03": "cpp",
"charconv": "cpp" "charconv": "cpp",
"cinttypes": "cpp"
} }
} }

58
ml.cpp
View File

@ -363,8 +363,7 @@ MlValue MlValue::operator+(const MlValue &other) const {
throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL); throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL);
// Other type must be a float or an int // Other type must be a float or an int
if ((is_number() || other.is_number()) && if ((is_number() || other.is_number()) && !(is_number() && other.is_number()))
!(is_number() && other.is_number()))
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP); throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
switch (type) { switch (type) {
@ -411,8 +410,7 @@ MlValue MlValue::operator-(const MlValue &other) const {
switch (type) { switch (type) {
case FLOAT: case FLOAT:
// If one is a float, promote the other by default and do // If one is a float, promote the other by default and do float subtraction.
// float subtraction.
return MlValue(stack_data.f - other.cast_to_float().stack_data.f); return MlValue(stack_data.f - other.cast_to_float().stack_data.f);
case INT: case INT:
// If the other type is a float, go ahead and promote this expression // If the other type is a float, go ahead and promote this expression
@ -484,7 +482,6 @@ MlValue MlValue::operator%(const MlValue &other) const {
throw MlError(*this, MlEnvironment(), INVALID_BIN_OP); throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
switch (type) { switch (type) {
// If we support libm, we can find the remainder of floating point values.
case FLOAT: case FLOAT:
return MlValue(fmod(stack_data.f, other.cast_to_float().stack_data.f)); return MlValue(fmod(stack_data.f, other.cast_to_float().stack_data.f));
case INT: case INT:
@ -521,8 +518,6 @@ std::string MlValue::get_type_name() const {
case NIL: case NIL:
return NIL_TYPE; return NIL_TYPE;
default: default:
// We don't know the name of this type.
// This isn't the users fault, this is just unhandled.
// This should never be reached. // This should never be reached.
throw MlError(*this, MlEnvironment(), INTERNAL_ERROR); throw MlError(*this, MlEnvironment(), INTERNAL_ERROR);
} }
@ -558,8 +553,6 @@ std::string MlValue::display() const {
case NIL: case NIL:
return "nil"; return "nil";
default: default:
// We don't know how to display whatever type this is.
// This isn't the users fault, this is just unhandled.
// This should never be reached. // This should never be reached.
throw MlError(*this, MlEnvironment(), INTERNAL_ERROR); throw MlError(*this, MlEnvironment(), INTERNAL_ERROR);
} }
@ -599,8 +592,6 @@ std::string MlValue::debug() const {
case NIL: case NIL:
return "nil"; return "nil";
default: default:
// We don't know how to debug whatever type this is.
// This isn't the users fault, this is just unhandled.
// This should never be reached. // This should never be reached.
throw MlError(*this, MlEnvironment(), INTERNAL_ERROR); throw MlError(*this, MlEnvironment(), INTERNAL_ERROR);
} }
@ -626,8 +617,7 @@ MlError::~MlError() {
std::string MlError::description() const { std::string MlError::description() const {
// return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" + msg + "\""; // return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" + msg + "\"";
return MlPerfMon::instance().callstack() + return MlPerfMon::instance().callstack() + "error: the expression `" + cause->debug() + "` with message \"" + msg + "\"";
"error: the expression `" + cause->debug() + "` with message \"" + msg + "\"";
} }
void MlEnvironment::combine(MlEnvironment const &other) { void MlEnvironment::combine(MlEnvironment const &other) {
@ -677,8 +667,7 @@ MlValue MlValue::apply(std::vector<MlValue> args, MlEnvironment &env) {
// Get the list of parameter atoms // Get the list of parameter atoms
params = list[0].list; params = list[0].list;
if (params.size() != args.size()) if (params.size() != args.size())
throw MlError(MlValue(args), env, throw MlError(MlValue(args), env, args.size() > params.size() ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > params.size() ? TOO_MANY_ARGS : TOO_FEW_ARGS);
// Get the captured scope from the lambda // Get the captured scope from the lambda
e = lambda_scope; e = lambda_scope;
@ -1119,8 +1108,7 @@ MlValue read_file_lines(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("read-file-lines", read_file_lines), env, throw MlError(MlValue("read-file-lines", read_file_lines), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
// TODO check args[1].is_lambda // TODO check args[1].is_lambda
long lines_nr = 0; long lines_nr = 0;
@ -1218,8 +1206,7 @@ MlValue str_to_date(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("str-to-date", str_to_date), env, throw MlError(MlValue("str-to-date", str_to_date), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue(string_to_date(args[0].as_string(), args[1].as_string())); return MlValue(string_to_date(args[0].as_string(), args[1].as_string()));
} }
@ -1241,8 +1228,7 @@ MlValue system_cmd(std::vector<MlValue> args, MlEnvironment &env) {
// TODO add support for more params constructing options as one string // TODO add support for more params constructing options as one string
if (args.size() != 1) if (args.size() != 1)
throw MlError(MlValue("system-cmd", system_cmd), env, throw MlError(MlValue("system-cmd", system_cmd), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return exec_system_cmd(args[0].as_string()); return exec_system_cmd(args[0].as_string());
} }
@ -1329,6 +1315,7 @@ MlValue subtract(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("-", subtract), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("-", subtract), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return args[0] - args[1]; return args[0] - args[1];
} }
@ -1365,6 +1352,7 @@ MlValue remainder(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("%", remainder), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("%", remainder), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return args[0] % args[1]; return args[0] % args[1];
} }
@ -1374,6 +1362,7 @@ MlValue eq(std::vector<MlValue> args, MlEnvironment &env) {
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(args[0] == args[1]); return MlValue(args[0] == args[1]);
} }
@ -1383,6 +1372,7 @@ MlValue neq(std::vector<MlValue> args, MlEnvironment &env) {
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(args[0] != args[1]); return MlValue(args[0] != args[1]);
} }
@ -1392,6 +1382,7 @@ MlValue greater(std::vector<MlValue> args, MlEnvironment &env) {
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(args[0] > args[1]); return MlValue(args[0] > args[1]);
} }
@ -1401,6 +1392,7 @@ MlValue less(std::vector<MlValue> args, MlEnvironment &env) {
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(args[0] < args[1]); return MlValue(args[0] < args[1]);
} }
@ -1410,6 +1402,7 @@ MlValue greater_eq(std::vector<MlValue> args, MlEnvironment &env) {
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(args[0] >= args[1]); return MlValue(args[0] >= args[1]);
} }
@ -1419,6 +1412,7 @@ MlValue less_eq(std::vector<MlValue> args, MlEnvironment &env) {
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(args[0] <= args[1]); return MlValue(args[0] <= args[1]);
} }
@ -1515,9 +1509,7 @@ MlValue len(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() != 1) if (args.size() != 1)
throw MlError(MlValue("len", len), env, args.size() > 1 ? throw MlError(MlValue("len", len), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
TOO_MANY_ARGS : TOO_FEW_ARGS
);
return MlValue(long(args[0].as_list().size())); return MlValue(long(args[0].as_list().size()));
} }
@ -1586,8 +1578,7 @@ MlValue string_replace(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() != 3) if (args.size() != 3)
throw MlError(MlValue("string-replace", string_replace), env, throw MlError(MlValue("string-replace", string_replace), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::string src = args[0].as_string(); std::string src = args[0].as_string();
replace_substring(src, args[1].as_string(), args[2].as_string()); replace_substring(src, args[1].as_string(), args[2].as_string());
@ -1599,8 +1590,7 @@ MlValue string_regex(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
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, throw MlError(MlValue("string-regex?", string_regex), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
return MlValue((long) regexp_search(args[0].as_string(), args[1].as_string())); return MlValue((long) regexp_search(args[0].as_string(), args[1].as_string()));
} }
@ -1610,8 +1600,7 @@ MlValue string_split(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("string-split", string_split), env, throw MlError(MlValue("string-split", string_split), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
// TODO do it more efficient // TODO do it more efficient
std::vector<std::string> elements = regexp_strsplit(args[0].as_string(), args[1].as_string()); std::vector<std::string> elements = regexp_strsplit(args[0].as_string(), args[1].as_string());
@ -1646,8 +1635,7 @@ MlValue string_substr(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() < 1 || args.size() > 3) if (args.size() < 1 || args.size() > 3)
throw MlError(MlValue("string-substr", string_substr), env, throw MlError(MlValue("string-substr", string_substr), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
const std::string &str = args[0].as_string(); const std::string &str = args[0].as_string();
long pos = args.size() > 1 ? args[1].as_int() : 0; long pos = args.size() > 1 ? args[1].as_int() : 0;
@ -1847,8 +1835,7 @@ MlValue thread_create(std::vector<MlValue> args, MlEnvironment &env) {
MlValue thread_under_lock(std::vector<MlValue> args, MlEnvironment &env) { MlValue thread_under_lock(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("thread_under_lock", thread_under_lock), env, throw MlError(MlValue("thread_under_lock", thread_under_lock), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
if (args[0].as_string() != "ilock") if (args[0].as_string() != "ilock")
throw MlError(MlValue("thread_under_lock", thread_under_lock), env, UNKNOWN_ERROR); throw MlError(MlValue("thread_under_lock", thread_under_lock), env, UNKNOWN_ERROR);
@ -1865,8 +1852,7 @@ MlValue thread_sleep(std::vector<MlValue> args, MlEnvironment &env) {
eval_args(args, env); eval_args(args, env);
if (args.size() != 1) if (args.size() != 1)
throw MlError(MlValue("thread-sleep", thread_sleep), env, throw MlError(MlValue("thread-sleep", thread_sleep), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::this_thread::sleep_for(std::chrono::milliseconds(args[0].as_int())); std::this_thread::sleep_for(std::chrono::milliseconds(args[0].as_int()));
return args[0]; return args[0];

15
ml.h
View File

@ -181,32 +181,17 @@ public:
bool operator==(MlValue other) const; bool operator==(MlValue other) const;
bool operator!=(const MlValue &other) const; bool operator!=(const MlValue &other) const;
bool operator>=(const MlValue &other) const; bool operator>=(const MlValue &other) const;
bool operator<=(const MlValue &other) const; bool operator<=(const MlValue &other) const;
bool operator>(const MlValue &other) const; bool operator>(const MlValue &other) const;
bool operator<(const MlValue &other) const; bool operator<(const MlValue &other) const;
// This function adds two lisp values, and returns the lisp value result.
MlValue operator+(const MlValue &other) const; MlValue operator+(const MlValue &other) const;
// This function subtracts two lisp values, and returns the lisp value result.
MlValue operator-(const MlValue &other) const; MlValue operator-(const MlValue &other) const;
// This function multiplies two lisp values, and returns the lisp value result.
MlValue operator*(const MlValue &other) const; MlValue operator*(const MlValue &other) const;
// This function divides two lisp values, and returns the lisp value result.
MlValue operator/(const MlValue &other) const; MlValue operator/(const MlValue &other) const;
// This function finds the remainder of two lisp values, and returns the lisp value result.
MlValue operator%(const MlValue &other) const; MlValue operator%(const MlValue &other) const;
// Get the name of the type of this value // Get the name of the type of this value