Packages

A CEL (Common Expression Language) parser and interpreter in pure Gleam

Current section

Files

Jump to
cel src cel@interpreter@function.erl
Raw

src/cel@interpreter@function.erl

-module(cel@interpreter@function).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([filter/1, map/1, all/1, size/1, has/1, exists/1, exists_one/1]).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 14).
-spec filter_impl(
cel@interpreter@context:context(),
binary(),
list(cel@interpreter@value:value()),
list(cel@interpreter@value:value()),
cel@parser:expression()
) -> {ok, list(cel@interpreter@value:value())} |
{error, cel@interpreter@error:execution_error()}.
filter_impl(Ctx, Ident, Items, Filtered, Expr) ->
case Items of
[] ->
{ok, lists:reverse(Filtered)};
[Item | Rest] ->
Inner_ctx = begin
_pipe = cel@interpreter@context:new_inner(Ctx),
cel@interpreter@context:insert_variable(_pipe, Ident, Item)
end,
gleam@result:'try'(
cel@interpreter@evaluate:evaluate_expr(Expr, Inner_ctx),
fun(Cond) -> gleam@result:'try'(case Cond of
{bool, true} ->
{ok, [Item | Filtered]};
{bool, false} ->
{ok, Filtered};
_ ->
{error,
{unexpected_type,
[bool_t],
cel@interpreter@value:to_type(Cond),
<<"filter condition"/utf8>>}}
end, fun(Filtered@1) ->
filter_impl(Ctx, Ident, Rest, Filtered@1, Expr)
end) end
)
end.
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 43).
-spec filter(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
filter(Ftx) ->
{function_context, Name, This, Ctx, Args} = Ftx,
gleam@result:'try'(case Args of
[{ident, Ident}, Expr] ->
{ok, {Ident, Expr}};
_ ->
{error, {invalid_function_args, Name}}
end, fun(_use0) ->
{Ident@1, Expr@1} = _use0,
case This of
{some, {list, Items}} ->
_pipe = filter_impl(Ctx, Ident@1, Items, [], Expr@1),
gleam@result:map(_pipe, fun(Field@0) -> {list, Field@0} end);
{some, Other} ->
{error,
{unexpected_type,
[list_t],
cel@interpreter@value:to_type(Other),
<<"filter target"/utf8>>}};
none ->
{error, {function_expected_this, Name}}
end
end).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 72).
-spec map_impl(
cel@interpreter@context:context(),
binary(),
list(cel@interpreter@value:value()),
list(cel@interpreter@value:value()),
cel@parser:expression()
) -> {ok, list(cel@interpreter@value:value())} |
{error, cel@interpreter@error:execution_error()}.
map_impl(Ctx, Ident, Items, Mapped, Expr) ->
case Items of
[] ->
{ok, lists:reverse(Mapped)};
[Item | Rest] ->
Inner_ctx = begin
_pipe = cel@interpreter@context:new_inner(Ctx),
cel@interpreter@context:insert_variable(_pipe, Ident, Item)
end,
gleam@result:'try'(
cel@interpreter@evaluate:evaluate_expr(Expr, Inner_ctx),
fun(Value) ->
map_impl(Ctx, Ident, Rest, [Value | Mapped], Expr)
end
)
end.
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 90).
-spec map(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
map(Ftx) ->
{function_context, Name, This, Ctx, Args} = Ftx,
gleam@result:'try'(case Args of
[{ident, Ident}, Expr] ->
{ok, {Ident, Expr}};
_ ->
{error, {invalid_function_args, Name}}
end, fun(_use0) ->
{Ident@1, Expr@1} = _use0,
case This of
{some, {list, Items}} ->
_pipe = map_impl(Ctx, Ident@1, Items, [], Expr@1),
gleam@result:map(_pipe, fun(Field@0) -> {list, Field@0} end);
{some, Other} ->
{error,
{unexpected_type,
[list_t],
cel@interpreter@value:to_type(Other),
<<"map target"/utf8>>}};
none ->
{error, {function_expected_this, Name}}
end
end).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 113).
-spec all_impl(
cel@interpreter@context:context(),
binary(),
list(cel@interpreter@value:value()),
cel@parser:expression()
) -> {ok, boolean()} | {error, cel@interpreter@error:execution_error()}.
all_impl(Ctx, Ident, Items, Expr) ->
case Items of
[] ->
{ok, true};
[Item | Rest] ->
Inner_ctx = begin
_pipe = cel@interpreter@context:new_inner(Ctx),
cel@interpreter@context:insert_variable(_pipe, Ident, Item)
end,
gleam@result:'try'(
cel@interpreter@evaluate:evaluate_expr(Expr, Inner_ctx),
fun(Cond) -> case Cond of
{bool, true} ->
all_impl(Ctx, Ident, Rest, Expr);
{bool, false} ->
{ok, false};
_ ->
{error,
{unexpected_type,
[bool_t],
cel@interpreter@value:to_type(Cond),
<<"all condition"/utf8>>}}
end end
)
end.
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 139).
-spec all(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
all(Ftx) ->
{function_context, Name, This, Ctx, Args} = Ftx,
gleam@result:'try'(case Args of
[{ident, Ident}, Expr] ->
{ok, {Ident, Expr}};
_ ->
{error, {invalid_function_args, Name}}
end, fun(_use0) ->
{Ident@1, Expr@1} = _use0,
case This of
{some, {list, Items}} ->
_pipe = all_impl(Ctx, Ident@1, Items, Expr@1),
gleam@result:map(_pipe, fun(Field@0) -> {bool, Field@0} end);
{some, Other} ->
{error,
{unexpected_type,
[list_t],
cel@interpreter@value:to_type(Other),
<<"all target"/utf8>>}};
none ->
{error, {function_expected_this, Name}}
end
end).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 162).
-spec size(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
size(Ftx) ->
{function_context, Name, _, Ctx, Args} = Ftx,
gleam@result:'try'(case Args of
[Expr] ->
cel@interpreter@evaluate:evaluate_expr(Expr, Ctx);
_ ->
{error, {invalid_function_args, Name}}
end, fun(Expr@1) -> case Expr@1 of
{list, Items} ->
{ok, {int, erlang:length(Items)}};
{map, Items@1} ->
{ok, {int, maps:size(Items@1)}};
{string, Str} ->
{ok, {int, string:length(Str)}};
Other ->
{error,
{unexpected_type,
[list_t, map_t, string_t],
cel@interpreter@value:to_type(Other),
<<"size target"/utf8>>}}
end end).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 185).
-spec has(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
has(Ftx) ->
{function_context, Name, _, Ctx, Args} = Ftx,
gleam@result:map(case Args of
[{ident, _} = Expr] ->
case cel@interpreter@evaluate:evaluate_expr(Expr, Ctx) of
{ok, _} ->
{ok, true};
{error, {context_error, {no_such_key, _}}} ->
{ok, false};
{error, {context_error, {unknown_identifier, _}}} ->
{ok, false};
{error, Err} ->
{error, Err}
end;
[{member, _, {attribute, _}} = Expr] ->
case cel@interpreter@evaluate:evaluate_expr(Expr, Ctx) of
{ok, _} ->
{ok, true};
{error, {context_error, {no_such_key, _}}} ->
{ok, false};
{error, {context_error, {unknown_identifier, _}}} ->
{ok, false};
{error, Err} ->
{error, Err}
end;
_ ->
{error, {invalid_function_args, Name}}
end, fun(Exists) -> {bool, Exists} end).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 204).
-spec exists_impl(
cel@interpreter@context:context(),
binary(),
list(cel@interpreter@value:value()),
cel@parser:expression()
) -> {ok, boolean()} | {error, cel@interpreter@error:execution_error()}.
exists_impl(Ctx, Ident, Items, Expr) ->
case Items of
[] ->
{ok, false};
[Item | Rest] ->
Inner_ctx = begin
_pipe = cel@interpreter@context:new_inner(Ctx),
cel@interpreter@context:insert_variable(_pipe, Ident, Item)
end,
gleam@result:'try'(
cel@interpreter@evaluate:evaluate_expr(Expr, Inner_ctx),
fun(Cond) -> case Cond of
{bool, true} ->
{ok, true};
{bool, false} ->
exists_impl(Ctx, Ident, Rest, Expr);
_ ->
{error,
{unexpected_type,
[bool_t],
cel@interpreter@value:to_type(Cond),
<<"exists condition"/utf8>>}}
end end
)
end.
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 230).
-spec exists(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
exists(Ftx) ->
{function_context, Name, This, Ctx, Args} = Ftx,
gleam@result:'try'(case Args of
[{ident, Ident}, Expr] ->
{ok, {Ident, Expr}};
_ ->
{error, {invalid_function_args, Name}}
end, fun(_use0) ->
{Ident@1, Expr@1} = _use0,
case This of
{some, {list, Items}} ->
_pipe = exists_impl(Ctx, Ident@1, Items, Expr@1),
gleam@result:map(_pipe, fun(Field@0) -> {bool, Field@0} end);
{some, Other} ->
{error,
{unexpected_type,
[list_t],
cel@interpreter@value:to_type(Other),
<<"exists target"/utf8>>}};
none ->
{error, {function_expected_this, Name}}
end
end).
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 253).
-spec exists_one_impl(
cel@interpreter@context:context(),
binary(),
list(cel@interpreter@value:value()),
cel@parser:expression(),
boolean()
) -> {ok, boolean()} | {error, cel@interpreter@error:execution_error()}.
exists_one_impl(Ctx, Ident, Items, Expr, Found) ->
case Items of
[] ->
{ok, Found};
[Item | Rest] ->
Inner_ctx = begin
_pipe = cel@interpreter@context:new_inner(Ctx),
cel@interpreter@context:insert_variable(_pipe, Ident, Item)
end,
gleam@result:'try'(
cel@interpreter@evaluate:evaluate_expr(Expr, Inner_ctx),
fun(Cond) -> case {Cond, Found} of
{{bool, true}, true} ->
{ok, false};
{{bool, true}, false} ->
exists_one_impl(Ctx, Ident, Rest, Expr, true);
{{bool, false}, _} ->
exists_one_impl(Ctx, Ident, Rest, Expr, Found);
{_, _} ->
{error,
{unexpected_type,
[bool_t],
cel@interpreter@value:to_type(Cond),
<<"exists one condition"/utf8>>}}
end end
)
end.
-file("/Users/jonas/src/personal/cel-gleam/src/cel/interpreter/function.gleam", 281).
-spec exists_one(cel@interpreter@context:function_context()) -> {ok,
cel@interpreter@value:value()} |
{error, cel@interpreter@error:execution_error()}.
exists_one(Ftx) ->
{function_context, Name, This, Ctx, Args} = Ftx,
gleam@result:'try'(case Args of
[{ident, Ident}, Expr] ->
{ok, {Ident, Expr}};
_ ->
{error, {invalid_function_args, Name}}
end, fun(_use0) ->
{Ident@1, Expr@1} = _use0,
case This of
{some, {list, Items}} ->
_pipe = exists_one_impl(Ctx, Ident@1, Items, Expr@1, false),
gleam@result:map(_pipe, fun(Field@0) -> {bool, Field@0} end);
{some, Other} ->
{error,
{unexpected_type,
[list_t],
cel@interpreter@value:to_type(Other),
<<"exists one target"/utf8>>}};
none ->
{error, {function_expected_this, Name}}
end
end).