Skip to content

Commit

Permalink
fix(yaksha_lisp): for loop should use = like operation to set the var…
Browse files Browse the repository at this point in the history
…iable
  • Loading branch information
JaDogg committed Dec 9, 2023
1 parent 07a691c commit c6ede2d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/.noai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no
19 changes: 18 additions & 1 deletion compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,24 @@ yaksha_lisp_builtins::for_(const std::vector<yaksha_lisp_value *> &args,
auto body = std::vector<yaksha_lisp_value *>{args.begin() + 2, args.end()};
yaksha_lisp_value *last = nullptr;
for (auto &x : list->list_) {
env->set(symbol->expr_->token_->token_, x);
bool set_completed = false;
try {
// try to set the value
env->set(symbol->expr_->token_->token_, x, false);
set_completed = true;
} catch (const parsing_error &e) {
}
if (!set_completed) {
try {
// try to define it if we cannot set it
env->set(symbol->expr_->token_->token_, x, true);
set_completed = true;
} catch (const parsing_error &e) {
}
}
if (!set_completed) {
throw parsing_error{"for failed to set symbol", "", 0, 0};
}
auto e_args = eval_args(body, env);
last = e_args[e_args.size() - 1];
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/tests/test_yaksha_lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ TEST_CASE("yaksha_lisp: map_set, map_get, map_has") {
(if (== x 2) (= success 1))
)");
}
TEST_CASE("yaksha_lisp: for loop iterate a list") {
test_snippet_execute(R"(
(def x 0)
(for i (list 1 2 3) (= x (+ x i)))
(if (== x 6) (= success 1))
)");
}
TEST_CASE("yaksha_lisp: map_keys, map_values, map_remove") {
test_snippet_execute(R"(
(def m @{a: 1, b: 2, c: 3})
Expand Down

0 comments on commit c6ede2d

Please sign in to comment.