Skip to content

Commit

Permalink
feat(yaksha_lisp): add explode_string builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Feb 18, 2024
1 parent 22cfa67 commit 5787aaa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/libs/libs/c.yaka
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macros! {
(list (yk_create_token YK_TOKEN_NAME "inlinec")
(ykt_paren_open)
(ykt_string (+ "Const[" yk_import_ref ".Char]")) (ykt_comma)
(ykt_string (+ "'" elem::value "'") (ykt_paren_close))))
(ykt_string (+ "'" elem::value "'")) (ykt_paren_close)))
(yk_register {dsl const_str const_str})
(yk_register {dsl cstr cstr})
# c.const_str!{"Hi"} --> create a const char * const --> inlinec("Const[Ptr[Const[Char]]]", "\"Hi\"")
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/yaksha_lisp/yaksha_lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,8 @@ void yaksha_envmap::setup_builtins() {
create_builtin(this, "os_exec", yaksha_lisp_builtins::os_exec_));
set("os_shell",
create_builtin(this, "os_shell", yaksha_lisp_builtins::os_shell_));
set("explode_string",
create_builtin(this, "explode_string", yaksha_lisp_builtins::explode_string_));
// magic_dot -> map_get or access_module
set("magic_dot",
create_builtin(this, "magic_dot", yaksha_lisp_builtins::magic_dot_));
Expand Down
20 changes: 20 additions & 0 deletions compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1759,3 +1759,23 @@ yaksha_lisp_builtins::os_shell_(const std::vector<yaksha_lisp_value *> &args,
auto result = exec(cmd_args);
return env->create_number(result);
}
yaksha_lisp_value *yaksha_lisp_builtins::explode_string_(
const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env) {
if (args.size() != 1) {
throw parsing_error{"explode_string takes 1 argument", "", 0, 0};
}
auto e_args = eval_args(args, env);
auto arg = e_args[0];
if (arg->type_ != yaksha_lisp_value_type::STRING) {
throw parsing_error{"explode_string takes a string as argument", "", 0, 0};
}
auto result = env->create_val();
result->type_ = yaksha_lisp_value_type::LIST;
for (auto c : arg->str_) {
auto val = env->create_val();
val->type_ = yaksha_lisp_value_type::STRING;
val->str_ = c;
result->list_.push_back(val);
}
return result;
}
3 changes: 3 additions & 0 deletions compiler/src/yaksha_lisp/yaksha_lisp_builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ namespace yaksha {
os_exec_(const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env);
static yaksha_lisp_value *
os_shell_(const std::vector<yaksha_lisp_value *> &args, yaksha_envmap *env);
static yaksha_lisp_value *
explode_string_(const std::vector<yaksha_lisp_value *> &args,
yaksha_envmap *env);
// --
static yaksha_lisp_value *
system_lock_root_scope_(const std::vector<yaksha_lisp_value *> &args,
Expand Down
9 changes: 9 additions & 0 deletions compiler/tests/test_yaksha_lisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@ TEST_CASE("yaksha_lisp: test pop") {
(if (and (== (pop x) 3) (== x (list 1 2))) (= success 1))
)");
}
TEST_CASE("yaksha_lisp: test explode string") {
test_snippet_execute(R"(
(= x (explode_string "123"))
(= x (tail x))
(pop x)
(= s (reduce + x))
(if (== s "2") (= success 1))
)");
}
TEST_CASE("yaksha_lisp: test lambda") {
test_snippet_execute(R"(
(if (== ((lambda (x) (+ x 1)) 1) 2) (= success 1))
Expand Down

0 comments on commit 5787aaa

Please sign in to comment.