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