Packages

An embedded lisp interpreter based on the gleam implementation of MAL lisp.

Current section

Files

Jump to
kirala_l4u src l4u@bif_std.erl
Raw

src/l4u@bif_std.erl

-module(l4u@bif_std).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([bif_std_def/0]).
-spec bif_unique_int(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_unique_int(Exprs, Env) ->
{with_env, {int, l4u@sys_bridge_ffi:unique_int()}, Env}.
-spec bif_int(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_int(Exprs, Env) ->
Rexpr = case Exprs of
[{int, Val} = Expr] ->
Expr;
[{float, Val@1}] ->
{int, gleam@float:truncate(Val@1)};
[X] ->
{int,
begin
_pipe = gleam@int:parse(l4u@l4u_core:pstr(X)),
gleam@result:unwrap(_pipe, 0)
end};
Unhandable ->
l4u@sys_bridge_ffi:throw_expr(Unhandable),
{int, 0}
end,
{with_env, Rexpr, Env}.
-spec bif_float(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_float(Exprs, Env) ->
Rexpr = case Exprs of
[{float, Val} = Expr] ->
Expr;
[{int, Val@1}] ->
{float, gleam@int:to_float(Val@1)};
[X] ->
{float,
begin
_pipe = gleam@float:parse(l4u@l4u_core:pstr(X)),
gleam@result:unwrap(_pipe, +0.0)
end};
Unhandable ->
l4u@sys_bridge_ffi:throw_expr(Unhandable),
{float, +0.0}
end,
{with_env, Rexpr, Env}.
-spec bif_string(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_string(Exprs, Env) ->
{Vals@2, Delimiter@1} = case Exprs of
[Vals] ->
{l4u@l4u_core:uneval(Vals), <<""/utf8>>};
[Vals@1, Delimiter] ->
{l4u@l4u_core:uneval(Vals@1), l4u@l4u_core:pstr(Delimiter)};
Unhandable ->
l4u@sys_bridge_ffi:throw_expr(Unhandable),
{{string, <<""/utf8>>}, <<""/utf8>>}
end,
case Vals@2 of
{list, Vals@3} ->
Str = begin
_pipe = gleam@list:map(
Exprs,
fun(Expr) -> l4u@l4u_core:pstr(Expr) end
),
gleam@string:join(_pipe, <<""/utf8>>)
end,
{with_env, {string, Str}, Env};
X ->
{with_env, {string, l4u@l4u_core:pstr(X)}, Env}
end.
-spec bif_assert(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_assert(Exprs, Env) ->
_assert_subject = l4u@l4u_core:eval(Exprs, undefined, Env),
{with_env, Eexpr, Renv} = case _assert_subject of
{with_env, _, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_std"/utf8>>,
function => <<"bif_assert"/utf8>>,
line => 109})
end,
case Eexpr of
true ->
true;
_ ->
l4u@sys_bridge_ffi:throw_expr(Exprs),
false
end,
{with_env, true, Env}.
-spec bif_tokenize(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_tokenize(Exprs, Env) ->
[{string, Val}] = case Exprs of
[{string, _}] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_std"/utf8>>,
function => <<"bif_tokenize"/utf8>>,
line => 122})
end,
Rexpr = gleam@list:map(
l4u@l4u_core:tokenize(Val, Env),
fun(X) -> {string, X} end
),
{with_env, {list, Rexpr}, Env}.
-spec bif_json_format(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_json_format(Exprs, Env) ->
[Rexpr] = case Exprs of
[_] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_std"/utf8>>,
function => <<"bif_json_format"/utf8>>,
line => 139})
end,
{with_env, {string, l4u@bif_std_ffi:json_format(Rexpr)}, Env}.
-spec bif_json_parse(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_json_parse(Exprs, Env) ->
[{string, Val}] = case Exprs of
[{string, _}] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_std"/utf8>>,
function => <<"bif_json_parse"/utf8>>,
line => 151})
end,
Rexpr = l4u@bif_std_ffi:json_parse(Val),
{with_env, Rexpr, Env}.
-spec bif_escape(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_escape(Exprs, Env) ->
[{string, Val}] = case Exprs of
[{string, _}] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_std"/utf8>>,
function => <<"bif_escape"/utf8>>,
line => 158})
end,
Rexpr = l4u@sys_bridge_ffi:escape_binary_string(Val),
{with_env, {string, Rexpr}, Env}.
-spec bif_unescape(list(l4u@l4u_type:expr()), l4u@l4u_type:env()) -> l4u@l4u_type:expr().
bif_unescape(Exprs, Env) ->
[{string, Val}] = case Exprs of
[{string, _}] -> Exprs;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"l4u/bif_std"/utf8>>,
function => <<"bif_unescape"/utf8>>,
line => 165})
end,
Rexpr = l4u@sys_bridge_ffi:unescape_binary_string(Val),
{with_env, {string, Rexpr}, Env}.
-spec bif_std_def() -> list({binary(), l4u@l4u_type:expr()}).
bif_std_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(<<"unique-int"/utf8>>, fun bif_unique_int/2),
Bif(<<"int"/utf8>>, fun bif_int/2),
Bif(<<"float"/utf8>>, fun bif_float/2),
Bif(<<"string"/utf8>>, fun bif_string/2),
Bsf(<<"assert"/utf8>>, fun bif_assert/2),
Bif(<<"tokenize"/utf8>>, fun bif_tokenize/2),
Bif(<<"json-format"/utf8>>, fun bif_json_format/2),
Bif(<<"json-parse"/utf8>>, fun bif_json_parse/2),
Bif(<<"escape"/utf8>>, fun bif_escape/2),
Bif(<<"unescape"/utf8>>, fun bif_unescape/2)].