Skip to content

Commit

Permalink
Add Orb.Stack.drop/1
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Dec 2, 2023
1 parent 3ca4905 commit d610764
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/orb/ops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ defmodule Orb.Ops do
Orb.CustomType.resolve!(mod) |> to_primitive_type()
end

def type_stack_count(type) when is_primitive_type(type), do: 1
def type_stack_count(type) when is_effect(type), do: 0
def type_stack_count(type) when is_atom(type), do: 1
def type_stack_count(type) when is_tuple(type), do: tuple_size(type)

def types_compatible?(Elixir.Integer, b),
do: to_primitive_type(b) in @integer_types

Expand Down
42 changes: 42 additions & 0 deletions lib/orb/stack.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Orb.Stack do
@moduledoc false

defmodule Drop do
defstruct instruction: nil, count: 0

require alias Orb.Ops

def new(%{type: type} = instruction) do
count = Ops.typeof(instruction) |> Ops.type_stack_count()

%__MODULE__{
instruction: instruction,
count: count
}
end

def new(value) when is_number(value) do
%__MODULE__{
instruction: value,
count: 1
}
end

defimpl Orb.ToWat do
def to_wat(
%Orb.Stack.Drop{instruction: instruction, count: count},
indent
) do
[
Orb.ToWat.to_wat(instruction, indent),
"\n",
for _ <- 1..count do
[indent, "drop", "\n"]
end
]
end
end
end

def drop(instruction), do: Drop.new(instruction)
end

0 comments on commit d610764

Please sign in to comment.