# `EEVM.Interpreter.Stack`
[🔗](https://github.com/mw2000/eevm/blob/main/lib/eevm/interpreter/stack.ex#L1)

The EVM stack — a LIFO data structure with a max depth of 1024.

# `t`

```elixir
@type t() :: %EEVM.Interpreter.Stack{
  elements: [non_neg_integer()],
  size: non_neg_integer()
}
```

# `depth`

```elixir
@spec depth(t()) :: non_neg_integer()
```

Returns the current stack depth.

# `max_depth`

```elixir
@spec max_depth() :: pos_integer()
```

Returns the maximum stack depth (1024).

# `new`

```elixir
@spec new() :: t()
```

Creates a new empty stack.

# `peek`

```elixir
@spec peek(t(), non_neg_integer()) ::
  {:ok, non_neg_integer()} | {:error, :stack_underflow}
```

Peeks at the element at the given depth (0 = top of stack).

Used by DUP and SWAP instructions.

# `pop`

```elixir
@spec pop(t()) :: {:ok, non_neg_integer(), t()} | {:error, :stack_underflow}
```

Pops the top value from the stack.

Returns `{:ok, value, new_stack}` or `{:error, :stack_underflow}`.

# `push`

```elixir
@spec push(t(), non_neg_integer()) :: {:ok, t()} | {:error, :stack_overflow}
```

Pushes a value onto the stack.

Returns `{:ok, stack}` or `{:error, :stack_overflow}` if the stack is full.

EVM values are 256-bit unsigned integers. We enforce this with a bitmask.

# `swap`

```elixir
@spec swap(t(), pos_integer()) :: {:ok, t()} | {:error, :stack_underflow}
```

Swaps the top element with the element at the given depth.

SWAP1 swaps positions 0 and 1, SWAP2 swaps 0 and 2, etc.

# `to_list`

```elixir
@spec to_list(t()) :: [non_neg_integer()]
```

Converts the stack to a list (top element first) for inspection.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
