Packages
kirala_l4u
0.1.1
An embedded lisp interpreter based on the gleam implementation of MAL lisp.
Current section
Files
Jump to
Current section
Files
src/l4u@bif_file.erl
-module(l4u@bif_file).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([scan_directory/2, bif_file_def/0]).
-spec bif_file_exists(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_file_exists(Exprs, Env) ->
[Path] = case Exprs of
[_] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_file"/utf8>>,
function => <<"bif_file_exists"/utf8>>,
line => 37})
end,
Str_path = l4u@l4u_core:pstr(l4u@l4u_core:uneval(Path)),
Exists = l4u@sys_bridge_ffi:file_exists(Str_path),
{with_env, l4u_ffi:native_to_l4u(l4u_ffi:unsafe_corece(Exists)), Env}.
-spec bif_dir_exists(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_dir_exists(Exprs, Env) ->
[Path] = case Exprs of
[_] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_file"/utf8>>,
function => <<"bif_dir_exists"/utf8>>,
line => 45})
end,
Str_path = l4u@l4u_core:pstr(l4u@l4u_core:uneval(Path)),
Exists = l4u@sys_bridge_ffi:dir_exists(Str_path),
{with_env, l4u_ffi:native_to_l4u(l4u_ffi:unsafe_corece(Exists)), Env}.
-spec bif_get_cwd(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_get_cwd(Exprs, Env) ->
[] = case Exprs of
[] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_file"/utf8>>,
function => <<"bif_get_cwd"/utf8>>,
line => 53})
end,
Cwd = l4u@sys_bridge_ffi:get_cwd(),
{with_env, l4u_ffi:native_to_l4u(l4u_ffi:unsafe_corece(Cwd)), Env}.
-spec bif_set_cwd(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_set_cwd(Exprs, Env) ->
[Path] = case Exprs of
[_] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_file"/utf8>>,
function => <<"bif_set_cwd"/utf8>>,
line => 60})
end,
Str_path = l4u@l4u_core:pstr(l4u@l4u_core:uneval(Path)),
l4u@sys_bridge_ffi:set_cwd(Str_path),
{with_env, true, Env}.
-spec bif_abspath(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_abspath(Exprs, Env) ->
[Path] = case Exprs of
[_] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_file"/utf8>>,
function => <<"bif_abspath"/utf8>>,
line => 68})
end,
Str_path = l4u@l4u_core:pstr(l4u@l4u_core:uneval(Path)),
Abs_path = l4u@sys_bridge_ffi:abspath(Str_path),
{with_env, l4u_ffi:native_to_l4u(l4u_ffi:unsafe_corece(Abs_path)), Env}.
-spec scan_directory(binary(), integer()) -> list(binary()).
scan_directory(Path, Max_depth) ->
l4u@bif_file_ffi:scan_directory(Path, Max_depth).
-spec bif_dir_scan(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_dir_scan(Exprs, Env) ->
{Str_path, Max_depth} = case Exprs of
[Path] ->
{l4u@l4u_core:pstr(l4u@l4u_core:uneval(Path)), 9999};
[Path@1, Params] ->
_assert_subject = l4u@l4u_core:uneval(Params),
{list, Params@1} = case _assert_subject of
{list, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_file"/utf8>>,
function => <<"bif_dir_scan"/utf8>>,
line => 84})
end,
{l4u@l4u_core:pstr(l4u@l4u_core:uneval(Path@1)),
l4u@plist:plist_get_int_with_default(
Params@1,
<<"max-depth"/utf8>>,
9999
)};
Unhandable ->
l4u@sys_bridge_ffi:throw_expr(<<"dir-scan: unhandable exprs"/utf8>>),
{<<"unhandable"/utf8>>, -1}
end,
Files = l4u@bif_file_ffi:scan_directory(Str_path, Max_depth),
L4u_files = gleam@list:map(
Files,
fun(F) -> l4u_ffi:native_to_l4u(l4u_ffi:unsafe_corece(F)) end
),
{with_env, {list, L4u_files}, Env}.
-spec bif_file_def() -> list({binary(), l4u@l4u_type:expr()}).
bif_file_def() ->
Bif = fun(Name, F) -> {Name, {bif, l4u@l4u_core:description(Name), F}} end,
Bsf = fun(Name@1, F@1) ->
{Name@1, {bispform, l4u@l4u_core:description(Name@1), F@1}}
end,
[Bif(<<"file-exists?"/utf8>>, fun bif_file_exists/2),
Bif(<<"dir-exists?"/utf8>>, fun bif_dir_exists/2),
Bif(<<"dir-scan"/utf8>>, fun bif_dir_scan/2),
Bif(<<"get-cwd"/utf8>>, fun bif_get_cwd/2),
Bif(<<"set-cwd"/utf8>>, fun bif_set_cwd/2),
Bif(<<"abspath"/utf8>>, fun bif_abspath/2)].