-module(gwr@exec@stack). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/gwr/exec/stack.gleam"). -export([create/0, length/1, push/2, peek/1, pop/1, pop_repeat/2, pop_all/1, pop_while/2, count_on_top/2, pop_if/2, pop_as/2, is_value/1, is_label/1, is_activation_frame/1, to_value/1, to_label/1, to_frame/1, get_current_frame/1, get_current_label/1, replace_current_frame/2, get_entries/1]). -export_type([stack/0, stack_entry/0]). -type stack() :: {stack, list(stack_entry())}. -type stack_entry() :: {value_entry, gwr@spec:value()} | {label_entry, gwr@spec:label()} | {activation_entry, gwr@spec:frame()}. -file("src/gwr/exec/stack.gleam", 21). -spec create() -> stack(). create() -> {stack, []}. -file("src/gwr/exec/stack.gleam", 25). -spec length(stack()) -> integer(). length(Stack) -> erlang:length(erlang:element(2, Stack)). -file("src/gwr/exec/stack.gleam", 29). -spec push(stack(), list(stack_entry())) -> stack(). push(Stack, New_entries) -> {stack, lists:append(erlang:element(2, Stack), New_entries)}. -file("src/gwr/exec/stack.gleam", 33). -spec peek(stack()) -> gleam@option:option(stack_entry()). peek(Stack) -> gleam@option:from_result(gleam@list:last(erlang:element(2, Stack))). -file("src/gwr/exec/stack.gleam", 44). -spec pop(stack()) -> {stack(), gleam@option:option(stack_entry())}. pop(Stack) -> {{stack, gleam@list:take(erlang:element(2, Stack), length(Stack) - 1)}, peek(Stack)}. -file("src/gwr/exec/stack.gleam", 51). -spec pop_repeat(stack(), integer()) -> {stack(), list(stack_entry())}. pop_repeat(Stack, Count) -> case Count > 0 of false -> {Stack, []}; true -> gleam@yielder:fold( gleam@yielder:range(1, Count), {Stack, []}, fun(Accumulator, _) -> {Stack_after_pop, Maybe_popped_entry} = pop( erlang:element(1, Accumulator) ), case Maybe_popped_entry of {some, Entry} -> {Stack_after_pop, begin _pipe = erlang:element(2, Accumulator), lists:append(_pipe, [Entry]) end}; none -> {Stack_after_pop, erlang:element(2, Accumulator)} end end ) end. -file("src/gwr/exec/stack.gleam", 73). -spec pop_all(stack()) -> {stack(), list(stack_entry())}. pop_all(Stack) -> {create(), begin _pipe = erlang:element(2, Stack), lists:reverse(_pipe) end}. -file("src/gwr/exec/stack.gleam", 81). -spec do_pop_while( {stack(), list(stack_entry())}, fun((stack_entry()) -> boolean()) ) -> {stack(), list(stack_entry())}. do_pop_while(Accumulator, Predicate) -> case peek(erlang:element(1, Accumulator)) of {some, Entry} -> case Predicate(Entry) of true -> do_pop_while( {erlang:element(1, pop(erlang:element(1, Accumulator))), begin _pipe = erlang:element(2, Accumulator), lists:append(_pipe, [Entry]) end}, Predicate ); false -> {erlang:element(1, Accumulator), erlang:element(2, Accumulator)} end; _ -> {erlang:element(1, Accumulator), erlang:element(2, Accumulator)} end. -file("src/gwr/exec/stack.gleam", 77). -spec pop_while(stack(), fun((stack_entry()) -> boolean())) -> {stack(), list(stack_entry())}. pop_while(Stack, Predicate) -> do_pop_while({Stack, []}, Predicate). -file("src/gwr/exec/stack.gleam", 37). -spec count_on_top(stack(), fun((stack_entry()) -> boolean())) -> integer(). count_on_top(Stack, Predicate) -> _pipe = erlang:element(2, pop_while(Stack, Predicate)), erlang:length(_pipe). -file("src/gwr/exec/stack.gleam", 100). -spec pop_if(stack(), fun((stack_entry()) -> boolean())) -> {ok, {stack(), stack_entry()}} | {error, nil}. pop_if(Stack, Predicate) -> case pop(Stack) of {Stack@1, {some, Entry}} -> case Predicate(Entry) of true -> {ok, {Stack@1, Entry}}; false -> {error, nil} end; _ -> {error, nil} end. -file("src/gwr/exec/stack.gleam", 115). -spec pop_as( stack(), fun((stack_entry()) -> {ok, HWX} | {error, gwr@exec@trap:trap()}) ) -> {ok, {stack(), HWX}} | {error, gwr@exec@trap:trap()}. pop_as(Stack, Convert) -> case pop(Stack) of {Stack@1, {some, Entry}} -> gleam@result:'try'( Convert(Entry), fun(Value) -> {ok, {Stack@1, Value}} end ); _ -> _pipe = gwr@exec@trap:make(unknown), _pipe@1 = gwr@exec@trap:add_message( _pipe, <<"the stack is empty"/utf8>> ), gwr@exec@trap:to_error(_pipe@1) end. -file("src/gwr/exec/stack.gleam", 131). -spec is_value(stack_entry()) -> boolean(). is_value(Entry) -> case Entry of {value_entry, _} -> true; _ -> false end. -file("src/gwr/exec/stack.gleam", 138). -spec is_label(stack_entry()) -> boolean(). is_label(Entry) -> case Entry of {label_entry, _} -> true; _ -> false end. -file("src/gwr/exec/stack.gleam", 145). -spec is_activation_frame(stack_entry()) -> boolean(). is_activation_frame(Entry) -> case Entry of {activation_entry, _} -> true; _ -> false end. -file("src/gwr/exec/stack.gleam", 152). -spec to_value(stack_entry()) -> {ok, gwr@spec:value()} | {error, gwr@exec@trap:trap()}. to_value(Entry) -> case Entry of {value_entry, Value} -> {ok, Value}; _ -> _pipe = gwr@exec@trap:make(unknown), _pipe@1 = gwr@exec@trap:add_message( _pipe, <<"the entry at the top of the stack is not a ValueEntry"/utf8>> ), gwr@exec@trap:to_error(_pipe@1) end. -file("src/gwr/exec/stack.gleam", 164). -spec to_label(stack_entry()) -> {ok, gwr@spec:label()} | {error, gwr@exec@trap:trap()}. to_label(Entry) -> case Entry of {label_entry, Label} -> {ok, Label}; _ -> _pipe = gwr@exec@trap:make(unknown), _pipe@1 = gwr@exec@trap:add_message( _pipe, <<"the entry at the top of the stack is not a LabelEntry"/utf8>> ), gwr@exec@trap:to_error(_pipe@1) end. -file("src/gwr/exec/stack.gleam", 176). -spec to_frame(stack_entry()) -> {ok, gwr@spec:frame()} | {error, gwr@exec@trap:trap()}. to_frame(Entry) -> case Entry of {activation_entry, Frame} -> {ok, Frame}; _ -> _pipe = gwr@exec@trap:make(unknown), _pipe@1 = gwr@exec@trap:add_message( _pipe, <<"the entry at the top of the stack is not a ActivationEntry"/utf8>> ), gwr@exec@trap:to_error(_pipe@1) end. -file("src/gwr/exec/stack.gleam", 188). -spec get_current_frame(stack()) -> {ok, gwr@spec:frame()} | {error, nil}. get_current_frame(Stack) -> case begin _pipe = erlang:element( 1, pop_while(Stack, fun(Entry) -> not is_activation_frame(Entry) end) ), pop(_pipe) end of {_, {some, {activation_entry, Frame}}} -> {ok, Frame}; _ -> {error, nil} end. -file("src/gwr/exec/stack.gleam", 198). -spec get_current_label(stack()) -> {ok, gwr@spec:label()} | {error, nil}. get_current_label(Stack) -> _pipe = erlang:element( 2, pop_while(Stack, fun(Entry) -> not is_activation_frame(Entry) end) ), _pipe@1 = gleam@list:filter(_pipe, fun is_label/1), _pipe@2 = gleam@list:map(_pipe@1, fun to_label/1), _pipe@3 = gleam@result:values(_pipe@2), gleam@list:first(_pipe@3). -file("src/gwr/exec/stack.gleam", 206). -spec replace_current_frame(stack(), gwr@spec:frame()) -> {ok, stack()} | {error, nil}. replace_current_frame(Stack, New_frame) -> {Stack@1, Upper_entries} = pop_while( Stack, fun(Entry) -> not is_activation_frame(Entry) end ), {Stack@2, Frame} = pop(Stack@1), case Frame of {some, {activation_entry, _}} -> {ok, push( Stack@2, begin _pipe = [{activation_entry, New_frame}], lists:append( _pipe, begin _pipe@1 = Upper_entries, lists:reverse(_pipe@1) end ) end )}; _ -> {error, nil} end. -file("src/gwr/exec/stack.gleam", 224). -spec get_entries(stack()) -> list(stack_entry()). get_entries(Stack) -> erlang:element(2, Stack).