Current section
Files
Jump to
Current section
Files
src/glisp@stdlib.erl
-module(glisp@stdlib).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([standard_env/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/glisp/stdlib.gleam", 37).
-spec add(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} | {error, binary()}.
add(Args) ->
_pipe = Args,
_pipe@1 = gleam@list:try_map(_pipe, fun(Arg) -> case Arg of
{number, N} ->
{ok, N};
_ ->
{error, <<"Expected number in addition"/utf8>>}
end end),
gleam@result:map(
_pipe@1,
fun(Nums) ->
{number, gleam@list:fold(Nums, 0, fun(Acc, N@1) -> Acc + N@1 end)}
end
).
-file("src/glisp/stdlib.gleam", 48).
-spec subtract(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
subtract(Args) ->
case Args of
[] ->
{error, <<"- requires at least one argument"/utf8>>};
[{number, First} | Rest] ->
_pipe = Rest,
_pipe@1 = gleam@list:try_map(_pipe, fun(Arg) -> case Arg of
{number, N} ->
{ok, N};
_ ->
{error, <<"Expected number in subtraction"/utf8>>}
end end),
gleam@result:map(_pipe@1, fun(Nums) -> case erlang:length(Nums) of
0 ->
{number, 0 - First};
_ ->
{number,
gleam@list:fold(
Nums,
First,
fun(Acc, N@1) -> Acc - N@1 end
)}
end end);
_ ->
{error, <<"Expected number as first argument to -"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 72).
-spec multiply(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
multiply(Args) ->
_pipe = Args,
_pipe@1 = gleam@list:try_map(_pipe, fun(Arg) -> case Arg of
{number, N} ->
{ok, N};
_ ->
{error, <<"Expected number in multiplication"/utf8>>}
end end),
gleam@result:map(
_pipe@1,
fun(Nums) ->
{number, gleam@list:fold(Nums, 0, fun(Acc, N@1) -> Acc * N@1 end)}
end
).
-file("src/glisp/stdlib.gleam", 83).
-spec divide(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
divide(Args) ->
case Args of
[] ->
{error, <<"/ requires at least one argument"/utf8>>};
[{number, First} | Rest] ->
_pipe = Rest,
_pipe@1 = gleam@list:try_map(_pipe, fun(Arg) -> case Arg of
{number, N} ->
case N of
0 ->
{error, <<"Division by zero"/utf8>>};
_ ->
{ok, N}
end;
_ ->
{error, <<"Expected number in division"/utf8>>}
end end),
gleam@result:map(_pipe@1, fun(Nums) -> case erlang:length(Nums) of
0 ->
{number, case First of
0 -> 0;
Gleam@denominator -> 1 div Gleam@denominator
end};
_ ->
{number,
gleam@list:fold(
Nums,
First,
fun(Acc, N@1) -> case N@1 of
0 -> 0;
Gleam@denominator@1 -> Acc div Gleam@denominator@1
end end
)}
end end);
_ ->
{error, <<"Expected number as first argument to /"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 111).
-spec equals(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
equals(Args) ->
case Args of
[A, B] ->
case A =:= B of
true ->
{ok, {number, 1}};
false ->
{ok, {number, 0}}
end;
_ ->
{error, <<"= requires exactly 2 arguments"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 124).
-spec less_than(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
less_than(Args) ->
case Args of
[{number, A}, {number, B}] ->
case A > B of
true ->
{ok, {number, 1}};
false ->
{ok, {number, 0}}
end;
[_, _] ->
{error, <<"'<' requires numeric arguments"/utf8>>};
_ ->
{error, <<"'<' requires exactly 2 arguments"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 137).
-spec greater_than(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
greater_than(Args) ->
case Args of
[{number, A}, {number, B}] ->
case A > B of
true ->
{ok, {number, 1}};
false ->
{ok, {number, 0}}
end;
[_, _] ->
{error, <<"'>' requires numeric arguments"/utf8>>};
_ ->
{error, <<"'>' requires exactly 2 arguments"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 150).
-spec make_list(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
make_list(Args) ->
{ok, {list, Args}}.
-file("src/glisp/stdlib.gleam", 154).
-spec car(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} | {error, binary()}.
car(Args) ->
case Args of
[{list, [Head | _]}] ->
{ok, Head};
[{list, []}] ->
{error, <<"car: cannot get first element of empty list"/utf8>>};
[_] ->
{error, <<"car: argument must be a list"/utf8>>};
_ ->
{error, <<"car: expected exactly one argument"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 165).
-spec cdr(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} | {error, binary()}.
cdr(Args) ->
case Args of
[{list, [_ | Tail]}] ->
{ok, {list, Tail}};
[{list, []}] ->
{error, <<"cdr: cannot get rest of empty list"/utf8>>};
[_] ->
{error, <<"cdr: argument must be a list"/utf8>>};
_ ->
{error, <<"cdr: expected exactly one argument"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 176).
-spec cons(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} | {error, binary()}.
cons(Args) ->
case Args of
[Head, {list, Tail}] ->
{ok, {list, [Head | Tail]}};
[_, _] ->
{error, <<"cons: second argument must be a list"/utf8>>};
_ ->
{error, <<"cons: expected exactly two arguments"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 186).
-spec length(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
length(Args) ->
case Args of
[{list, Elements}] ->
{ok, {number, erlang:length(Elements)}};
[_] ->
{error, <<"length: argunment must be a list"/utf8>>};
_ ->
{error, <<"length: expected exactly one argument"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 196).
-spec null(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} | {error, binary()}.
null(Args) ->
case Args of
[{list, []}] ->
{ok, {number, 1}};
[{list, _}] ->
{ok, {number, 0}};
[_] ->
{error, <<"null?: argument must be a list"/utf8>>};
_ ->
{error, <<"null?: expected exactly one argument"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 207).
-spec 'not'(list(glisp@ast:expr())) -> {ok, glisp@ast:expr()} |
{error, binary()}.
'not'(Args) ->
case Args of
[{number, 0}] ->
{ok, {number, 1}};
[_] ->
{ok, {number, 0}};
_ ->
{error, <<"not: expected exactly one argument"/utf8>>}
end.
-file("src/glisp/stdlib.gleam", 13).
?DOC(
" Create a standard environment with built-in functions\n"
"\n"
" This includes arithmetic operators (+, -, *, /), comparison operators (=, <, >),\n"
" list manipulation functions (list, car, cdr, cons, length, null?), and logical\n"
" operations (not).\n"
"\n"
" Returns an `Env` that can be used as the basis for evaluating expressions.\n"
).
-spec standard_env() -> gleam@dict:dict(binary(), glisp@ast:expr()).
standard_env() ->
Env = begin
_pipe = glisp@environment:new(),
_pipe@1 = glisp@environment:set(
_pipe,
<<"+"/utf8>>,
{builtin, fun add/1}
),
_pipe@2 = glisp@environment:set(
_pipe@1,
<<"-"/utf8>>,
{builtin, fun subtract/1}
),
_pipe@3 = glisp@environment:set(
_pipe@2,
<<"*"/utf8>>,
{builtin, fun multiply/1}
),
_pipe@4 = glisp@environment:set(
_pipe@3,
<<"/"/utf8>>,
{builtin, fun divide/1}
),
_pipe@5 = glisp@environment:set(
_pipe@4,
<<"="/utf8>>,
{builtin, fun equals/1}
),
_pipe@6 = glisp@environment:set(
_pipe@5,
<<"<"/utf8>>,
{builtin, fun less_than/1}
),
_pipe@7 = glisp@environment:set(
_pipe@6,
<<">"/utf8>>,
{builtin, fun greater_than/1}
),
_pipe@8 = glisp@environment:set(
_pipe@7,
<<"list"/utf8>>,
{builtin, fun make_list/1}
),
_pipe@9 = glisp@environment:set(
_pipe@8,
<<"car"/utf8>>,
{builtin, fun car/1}
),
_pipe@10 = glisp@environment:set(
_pipe@9,
<<"cdr"/utf8>>,
{builtin, fun cdr/1}
),
_pipe@11 = glisp@environment:set(
_pipe@10,
<<"cons"/utf8>>,
{builtin, fun cons/1}
),
_pipe@12 = glisp@environment:set(
_pipe@11,
<<"length"/utf8>>,
{builtin, fun length/1}
),
_pipe@13 = glisp@environment:set(
_pipe@12,
<<"null?"/utf8>>,
{builtin, fun null/1}
),
glisp@environment:set(_pipe@13, <<"not"/utf8>>, {builtin, fun 'not'/1})
end,
Env.