diff --git a/compiler/libs/libs/c.yaka b/compiler/libs/libs/c.yaka index 6b8b2639..245234c5 100644 --- a/compiler/libs/libs/c.yaka +++ b/compiler/libs/libs/c.yaka @@ -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\"") diff --git a/compiler/src/yaksha_lisp/yaksha_lisp.cpp b/compiler/src/yaksha_lisp/yaksha_lisp.cpp index 35dafea7..f03e8800 100644 --- a/compiler/src/yaksha_lisp/yaksha_lisp.cpp +++ b/compiler/src/yaksha_lisp/yaksha_lisp.cpp @@ -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_)); diff --git a/compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp b/compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp index 5fd82bec..af392388 100644 --- a/compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp +++ b/compiler/src/yaksha_lisp/yaksha_lisp_builtins.cpp @@ -1759,3 +1759,23 @@ yaksha_lisp_builtins::os_shell_(const std::vector &args, auto result = exec(cmd_args); return env->create_number(result); } +yaksha_lisp_value *yaksha_lisp_builtins::explode_string_( + const std::vector &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; +} diff --git a/compiler/src/yaksha_lisp/yaksha_lisp_builtins.h b/compiler/src/yaksha_lisp/yaksha_lisp_builtins.h index f92c7f4c..f5b9b3c6 100644 --- a/compiler/src/yaksha_lisp/yaksha_lisp_builtins.h +++ b/compiler/src/yaksha_lisp/yaksha_lisp_builtins.h @@ -267,6 +267,9 @@ namespace yaksha { os_exec_(const std::vector &args, yaksha_envmap *env); static yaksha_lisp_value * os_shell_(const std::vector &args, yaksha_envmap *env); + static yaksha_lisp_value * + explode_string_(const std::vector &args, + yaksha_envmap *env); // -- static yaksha_lisp_value * system_lock_root_scope_(const std::vector &args, diff --git a/compiler/tests/test_yaksha_lisp.cpp b/compiler/tests/test_yaksha_lisp.cpp index 7de2fdba..54c10245 100644 --- a/compiler/tests/test_yaksha_lisp.cpp +++ b/compiler/tests/test_yaksha_lisp.cpp @@ -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))