rewrites to for loop

This commit is contained in:
VaclavT 2022-01-02 12:44:49 +01:00
parent 9bf18ad709
commit d3a373ce94
1 changed files with 39 additions and 39 deletions

78
ml.cpp
View File

@ -85,8 +85,8 @@ MlValue::MlValue(const std::vector<MlValue> &list) : type(LIST), list(list) {}
MlValue::MlValue(const std::vector<std::string> &slist) : type(LIST) { MlValue::MlValue(const std::vector<std::string> &slist) : type(LIST) {
list.reserve(slist.size()); list.reserve(slist.size());
for (size_t i = 0; i < slist.size(); i++) for (const auto & item : slist)
list.push_back(MlValue::string(slist[i])); list.push_back(MlValue::string(item));
} }
MlValue MlValue::quote(const MlValue &quoted) { MlValue MlValue::quote(const MlValue &quoted) {
@ -125,15 +125,15 @@ MlValue MlValue::nil() {
MlValue::MlValue(const std::vector<MlValue> &params, MlValue ret, MlEnvironment const &env) : type(LAMBDA) { MlValue::MlValue(const std::vector<MlValue> &params, MlValue ret, MlEnvironment const &env) : type(LAMBDA) {
// We store the params and the result in the list member // We store the params and the result in the list member
// instead of having dedicated members. This is to save memory. // instead of having dedicated members. This is to save memory.
list.push_back(MlValue(params)); list.emplace_back(params);
list.push_back(ret); list.push_back(ret);
// // Lambdas capture only variables that they know they will use. // Lambdas capture only variables that they know they will use.
std::vector<std::string> used_atoms = ret.get_used_atoms(); std::vector<std::string> used_atoms = ret.get_used_atoms();
for (size_t i = 0; i < used_atoms.size(); i++) { for (const auto & used_atom : used_atoms) {
// If the environment has a symbol that this lambda uses, capture it. // If the environment has a symbol that this lambda uses, capture it.
if (env.has(used_atoms[i])) if (env.has(used_atom))
lambda_scope.set(used_atoms[i], env.get(used_atoms[i])); lambda_scope.set(used_atom, env.get(used_atom));
} }
} }
@ -167,9 +167,9 @@ std::vector<std::string> MlValue::get_used_atoms() {
case LIST: case LIST:
// If this is a list, add each of the atoms used in all // If this is a list, add each of the atoms used in all
// of the elements in the list. // of the elements in the list.
for (size_t i = 0; i < list.size(); i++) { for (auto & item : list) {
// Get the atoms used in the element // Get the atoms used in the element
tmp = list[i].get_used_atoms(); tmp = item.get_used_atoms();
// Add the used atoms to the current list of used atoms // Add the used atoms to the current list of used atoms
result.insert(result.end(), tmp.begin(), tmp.end()); result.insert(result.end(), tmp.begin(), tmp.end());
} }
@ -226,7 +226,7 @@ std::vector<MlValue> MlValue::as_list() const {
} }
// Push an item to the end of this list // Push an item to the end of this list
void MlValue::push(MlValue val) { void MlValue::push(const MlValue& val) {
// If this item is not a list, you cannot push to it. // If this item is not a list, you cannot push to it.
if (type != LIST) if (type != LIST)
throw MlError(*this, MlEnvironment(), MISMATCHED_TYPES); throw MlError(*this, MlEnvironment(), MISMATCHED_TYPES);
@ -400,8 +400,8 @@ MlValue MlValue::operator+(const MlValue &other) const {
// Maintain the value that will be returned // Maintain the value that will be returned
MlValue result = *this; MlValue result = *this;
// Add each item in the other list to the end of this list // Add each item in the other list to the end of this list
for (size_t i = 0; i < other.list.size(); i++) for (const auto & item : other.list)
result.push(other.list[i]); result.push(item);
return result; return result;
} else throw MlError(*this, MlEnvironment(), INVALID_BIN_OP); } else throw MlError(*this, MlEnvironment(), INVALID_BIN_OP);
@ -584,9 +584,9 @@ std::string MlValue::debug() const {
case FLOAT: case FLOAT:
return std::to_string(stack_data.f); return std::to_string(stack_data.f);
case STRING: case STRING:
for (size_t i = 0; i < str.length(); i++) { for (char c : str) {
if (str[i] == '"') result += "\\\""; if (c == '"') result += "\\\"";
else result.push_back(str[i]); else result.push_back(c);
} }
return "\"" + result + "\""; return "\"" + result + "\"";
case LAMBDA: case LAMBDA:
@ -919,8 +919,8 @@ namespace builtin {
// their arguments. To make a regular builtin that evaluates its // their arguments. To make a regular builtin that evaluates its
// arguments, we just call this function in our builtin definition. // arguments, we just call this function in our builtin definition.
void eval_args(std::vector<MlValue> &args, MlEnvironment &env) { void eval_args(std::vector<MlValue> &args, MlEnvironment &env) {
for (size_t i = 0; i < args.size(); i++) for (auto & arg : args)
args[i] = args[i].eval(env); arg = arg.eval(env);
} }
// Create a lambda function (SPECIAL FORM) // Create a lambda function (SPECIAL FORM)
@ -1018,8 +1018,8 @@ MlValue for_loop(std::vector<MlValue> args, MlEnvironment &env) {
MlValue acc; MlValue acc;
std::vector<MlValue> list = args[1].eval(env).as_list(); std::vector<MlValue> list = args[1].eval(env).as_list();
for (size_t i = 0; i < list.size(); i++) { for (auto & item : list) {
env.set(args[0].as_atom(), list[i]); env.set(args[0].as_atom(), item);
for (size_t j = 2; j < args.size() - 1; j++) for (size_t j = 2; j < args.size() - 1; j++)
args[j].eval(env); args[j].eval(env);
@ -1059,8 +1059,8 @@ MlValue do_and(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() < 2) if (args.size() < 2)
throw MlError(MlValue("and", do_and), env, TOO_FEW_ARGS); throw MlError(MlValue("and", do_and), env, TOO_FEW_ARGS);
for (size_t i = 0; i < args.size(); i++) for (auto & arg : args)
if (!args[i].eval(env).as_bool()) return MlValue::nil(); if (!arg.eval(env).as_bool()) return MlValue::nil();
return MlValue{true}; return MlValue{true};
} }
@ -1070,8 +1070,8 @@ MlValue do_or(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() < 2) if (args.size() < 2)
throw MlError(MlValue("or", do_or), env, TOO_FEW_ARGS); throw MlError(MlValue("or", do_or), env, TOO_FEW_ARGS);
for (size_t i = 0; i < args.size(); i++) for (auto & arg : args)
if (args[i].eval(env).as_bool()) return MlValue{true}; if (arg.eval(env).as_bool()) return MlValue{true};
return MlValue::nil(); return MlValue::nil();
} }
@ -1739,8 +1739,8 @@ MlValue string_regex_list(std::vector<MlValue> args, MlEnvironment &env) {
auto found_matches = regexp_search2(args[0].as_string(), args[1].as_string(), match_mode, ignore_case); auto found_matches = regexp_search2(args[0].as_string(), args[1].as_string(), match_mode, ignore_case);
std::vector<MlValue> list; std::vector<MlValue> list;
for(auto &l : found_matches) { for(auto &item : found_matches) {
list.push_back(l); list.push_back(item);
} }
return MlValue(list); return MlValue(list);
} }
@ -1859,9 +1859,9 @@ MlValue map_list(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("map_list", map_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("map_list", map_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result, l = args[1].as_list(), tmp; std::vector<MlValue> result, list = args[1].as_list(), tmp;
for (size_t i = 0; i < l.size(); i++) { for (const auto & item : list) {
tmp.push_back(l[i]); tmp.push_back(item);
result.push_back(args[0].apply(tmp, env)); result.push_back(args[0].apply(tmp, env));
tmp.clear(); tmp.clear();
} }
@ -1876,11 +1876,11 @@ MlValue filter_list(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() != 2) if (args.size() != 2)
throw MlError(MlValue("filter_list", filter_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("filter_list", filter_list), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result, l = args[1].as_list(), tmp; std::vector<MlValue> result, list = args[1].as_list(), tmp;
for (size_t i = 0; i < l.size(); i++) { for (const auto & item : list) {
tmp.push_back(l[i]); tmp.push_back(item);
if (args[0].apply(tmp, env).as_bool()) if (args[0].apply(tmp, env).as_bool())
result.push_back(l[i]); result.push_back(item);
tmp.clear(); tmp.clear();
} }
return MlValue(result); return MlValue(result);
@ -1894,11 +1894,11 @@ MlValue reduce_list(std::vector<MlValue> args, MlEnvironment &env) {
if (args.size() != 3) if (args.size() != 3)
throw MlError(MlValue("reduce_list", reduce_list), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS); throw MlError(MlValue("reduce_list", reduce_list), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> l = args[2].as_list(), tmp; std::vector<MlValue> list = args[2].as_list(), tmp;
MlValue acc = args[1]; MlValue acc = args[1];
for (size_t i = 0; i < l.size(); i++) { for (const auto & item : list) {
tmp.push_back(acc); tmp.push_back(acc);
tmp.push_back(l[i]); tmp.push_back(item);
acc = args[0].apply(tmp, env); acc = args[0].apply(tmp, env);
tmp.clear(); tmp.clear();
} }
@ -1951,8 +1951,8 @@ MlValue benchmark(std::vector<MlValue> args, MlEnvironment &env) {
MlValue thread_create(std::vector<MlValue> args, MlEnvironment &env) { MlValue thread_create(std::vector<MlValue> args, MlEnvironment &env) {
auto functor = [](std::vector<MlValue> args, MlEnvironment &env) -> void { auto functor = [](std::vector<MlValue> args, MlEnvironment &env) -> void {
try { try {
for (size_t i = 0; i < args.size(); i++) for (auto & arg : args)
MlValue acc = args[i].eval(env); MlValue acc = arg.eval(env);
} catch (const MlError &e) { } catch (const MlError &e) {
std::cerr << "thread_create exception: " << e.description() << std::endl; std::cerr << "thread_create exception: " << e.description() << std::endl;
} catch (const std::exception &e) { } catch (const std::exception &e) {
@ -2327,8 +2327,8 @@ int main(int argc, char *argv[]) {
// passed code // passed code
if (cmdOptionExists(argv, argv + argc, "-c")) { if (cmdOptionExists(argv, argv + argc, "-c")) {
std::vector<std::string> codes = getCmdOption(argv, argc, "-c"); std::vector<std::string> codes = getCmdOption(argv, argc, "-c");
for (size_t i = 0; i < codes.size(); i++) for (auto & code : codes)
run(codes[i], env); run(code, env);
// run files // run files
} else if (cmdOptionExists(argv, argv + argc, "-f")) { } else if (cmdOptionExists(argv, argv + argc, "-f")) {
for (auto & file : getCmdOption(argv, argc, "-f")) for (auto & file : getCmdOption(argv, argc, "-f"))