diff --git a/.vscode/settings.json b/.vscode/settings.json index 3d47c94..9d0daf7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -72,6 +72,7 @@ "list": "cpp", "variant": "cpp", "__functional_base_03": "cpp", - "charconv": "cpp" + "charconv": "cpp", + "cinttypes": "cpp" } } \ No newline at end of file diff --git a/ml.cpp b/ml.cpp index aaff3aa..cb86fc7 100644 --- a/ml.cpp +++ b/ml.cpp @@ -350,7 +350,7 @@ bool MlValue::operator<(const MlValue &other) const { // If the other value is a float, promote this value to a float and compare. if (other.type == FLOAT) return cast_to_float().stack_data.f < other.stack_data.f; - // Otherwise compare the integer values + // Otherwise compare the integer values else return stack_data.i < other.stack_data.i; default: // Only allow comparisons between integers and floats @@ -363,8 +363,7 @@ MlValue MlValue::operator+(const MlValue &other) const { throw MlError(*this, MlEnvironment(), INVALID_ARGUMENT_NIL); // Other type must be a float or an int - if ((is_number() || other.is_number()) && - !(is_number() && other.is_number())) + if ((is_number() || other.is_number()) && !(is_number() && other.is_number())) throw MlError(*this, MlEnvironment(), INVALID_BIN_OP); switch (type) { @@ -411,15 +410,14 @@ MlValue MlValue::operator-(const MlValue &other) const { switch (type) { case FLOAT: - // If one is a float, promote the other by default and do - // float subtraction. + // If one is a float, promote the other by default and do float subtraction. return MlValue(stack_data.f - other.cast_to_float().stack_data.f); case INT: // If the other type is a float, go ahead and promote this expression // before continuing with the subtraction if (other.type == FLOAT) return MlValue(cast_to_float().stack_data.f - other.stack_data.f); - // Otherwise, do integer subtraction. + // Otherwise, do integer subtraction. else return MlValue(stack_data.i - other.stack_data.i); default: // This operation was done on an unsupported type @@ -443,7 +441,7 @@ MlValue MlValue::operator*(const MlValue &other) const { // before continuing with the product if (other.type == FLOAT) return MlValue(cast_to_float().stack_data.f * other.stack_data.f); - // Otherwise, do integer multiplication. + // Otherwise, do integer multiplication. else return MlValue(stack_data.i * other.stack_data.i); default: // This operation was done on an unsupported type @@ -467,7 +465,7 @@ MlValue MlValue::operator/(const MlValue &other) const { // before continuing with the product if (other.type == FLOAT) return MlValue(cast_to_float().stack_data.f / other.stack_data.f); - // Otherwise, do integer multiplication. + // Otherwise, do integer multiplication. else return MlValue(stack_data.i / other.stack_data.i); default: // This operation was done on an unsupported type @@ -484,7 +482,6 @@ MlValue MlValue::operator%(const MlValue &other) const { throw MlError(*this, MlEnvironment(), INVALID_BIN_OP); switch (type) { - // If we support libm, we can find the remainder of floating point values. case FLOAT: return MlValue(fmod(stack_data.f, other.cast_to_float().stack_data.f)); case INT: @@ -521,8 +518,6 @@ std::string MlValue::get_type_name() const { case NIL: return NIL_TYPE; 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. throw MlError(*this, MlEnvironment(), INTERNAL_ERROR); } @@ -558,8 +553,6 @@ std::string MlValue::display() const { case NIL: return "nil"; 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. throw MlError(*this, MlEnvironment(), INTERNAL_ERROR); } @@ -599,8 +592,6 @@ std::string MlValue::debug() const { case NIL: return "nil"; 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. throw MlError(*this, MlEnvironment(), INTERNAL_ERROR); } @@ -626,8 +617,7 @@ MlError::~MlError() { std::string MlError::description() const { // return "error: the expression `" + cause->debug() + "` failed in scope " + to_string(env) + " with message \"" + msg + "\""; - return MlPerfMon::instance().callstack() + - "error: the expression `" + cause->debug() + "` with message \"" + msg + "\""; + return MlPerfMon::instance().callstack() + "error: the expression `" + cause->debug() + "` with message \"" + msg + "\""; } void MlEnvironment::combine(MlEnvironment const &other) { @@ -677,8 +667,7 @@ MlValue MlValue::apply(std::vector args, MlEnvironment &env) { // Get the list of parameter atoms params = list[0].list; if (params.size() != args.size()) - throw MlError(MlValue(args), env, - args.size() > params.size() ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue(args), env, args.size() > params.size() ? TOO_MANY_ARGS : TOO_FEW_ARGS); // Get the captured scope from the lambda e = lambda_scope; @@ -1119,8 +1108,7 @@ MlValue read_file_lines(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() != 2) - throw MlError(MlValue("read-file-lines", read_file_lines), env, - args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("read-file-lines", read_file_lines), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); // TODO check args[1].is_lambda long lines_nr = 0; @@ -1218,8 +1206,7 @@ MlValue str_to_date(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() != 2) - throw MlError(MlValue("str-to-date", str_to_date), env, - args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("str-to-date", str_to_date), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); return MlValue(string_to_date(args[0].as_string(), args[1].as_string())); } @@ -1241,8 +1228,7 @@ MlValue system_cmd(std::vector args, MlEnvironment &env) { // TODO add support for more params constructing options as one string if (args.size() != 1) - throw MlError(MlValue("system-cmd", system_cmd), env, - args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("system-cmd", system_cmd), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); return exec_system_cmd(args[0].as_string()); } @@ -1329,6 +1315,7 @@ MlValue subtract(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("-", subtract), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return args[0] - args[1]; } @@ -1365,6 +1352,7 @@ MlValue remainder(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("%", remainder), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return args[0] % args[1]; } @@ -1374,6 +1362,7 @@ MlValue eq(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("=", eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return MlValue(args[0] == args[1]); } @@ -1383,6 +1372,7 @@ MlValue neq(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("!=", neq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return MlValue(args[0] != args[1]); } @@ -1392,6 +1382,7 @@ MlValue greater(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue(">", greater), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return MlValue(args[0] > args[1]); } @@ -1401,6 +1392,7 @@ MlValue less(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("<", less), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return MlValue(args[0] < args[1]); } @@ -1410,6 +1402,7 @@ MlValue greater_eq(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue(">=", greater_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return MlValue(args[0] >= args[1]); } @@ -1419,6 +1412,7 @@ MlValue less_eq(std::vector args, MlEnvironment &env) { if (args.size() != 2) throw MlError(MlValue("<=", less_eq), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + return MlValue(args[0] <= args[1]); } @@ -1515,9 +1509,7 @@ MlValue len(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() != 1) - throw MlError(MlValue("len", len), env, args.size() > 1 ? - TOO_MANY_ARGS : TOO_FEW_ARGS - ); + throw MlError(MlValue("len", len), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); return MlValue(long(args[0].as_list().size())); } @@ -1586,8 +1578,7 @@ MlValue string_replace(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() != 3) - throw MlError(MlValue("string-replace", string_replace), env, - args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("string-replace", string_replace), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); std::string src = args[0].as_string(); replace_substring(src, args[1].as_string(), args[2].as_string()); @@ -1599,8 +1590,7 @@ MlValue string_regex(std::vector args, MlEnvironment &env) { eval_args(args, env); 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((long) regexp_search(args[0].as_string(), args[1].as_string())); } @@ -1610,8 +1600,7 @@ MlValue string_split(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() != 2) - throw MlError(MlValue("string-split", string_split), env, - args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("string-split", string_split), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); // TODO do it more efficient std::vector elements = regexp_strsplit(args[0].as_string(), args[1].as_string()); @@ -1646,8 +1635,7 @@ MlValue string_substr(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() < 1 || args.size() > 3) - throw MlError(MlValue("string-substr", string_substr), env, - args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("string-substr", string_substr), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); const std::string &str = args[0].as_string(); long pos = args.size() > 1 ? args[1].as_int() : 0; @@ -1847,8 +1835,7 @@ MlValue thread_create(std::vector args, MlEnvironment &env) { MlValue thread_under_lock(std::vector args, MlEnvironment &env) { if (args.size() != 2) - throw MlError(MlValue("thread_under_lock", thread_under_lock), env, - args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("thread_under_lock", thread_under_lock), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); if (args[0].as_string() != "ilock") throw MlError(MlValue("thread_under_lock", thread_under_lock), env, UNKNOWN_ERROR); @@ -1865,8 +1852,7 @@ MlValue thread_sleep(std::vector args, MlEnvironment &env) { eval_args(args, env); if (args.size() != 1) - throw MlError(MlValue("thread-sleep", thread_sleep), env, - args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); + throw MlError(MlValue("thread-sleep", thread_sleep), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS); std::this_thread::sleep_for(std::chrono::milliseconds(args[0].as_int())); return args[0]; diff --git a/ml.h b/ml.h index b5c46f2..21d3080 100644 --- a/ml.h +++ b/ml.h @@ -181,32 +181,17 @@ public: 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; - // This function adds two lisp values, and returns the lisp value result. MlValue operator+(const MlValue &other) const; - - // This function subtracts two lisp values, and returns the lisp value result. MlValue operator-(const MlValue &other) const; - - // This function multiplies two lisp values, and returns the lisp value result. MlValue operator*(const MlValue &other) const; - - // This function divides two lisp values, and returns the lisp value result. 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; // Get the name of the type of this value diff --git a/stdlib/stdlib.lsp b/stdlib/stdlib.lsp index c167eb9..0a6bfda 100644 --- a/stdlib/stdlib.lsp +++ b/stdlib/stdlib.lsp @@ -111,7 +111,7 @@ rslt )) -(defun flatten(lst) +(defun flatten (lst) (do (define rslt '()) (for e lst