aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Execution/Stack.php
blob: 1fa2daeda7126ef0e92a2df9ae022a8b1456aa16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

declare(strict_types=1);

namespace Nsfisis\Waddiwasi\Execution;

final class Stack
{
    /**
     * @param list<StackEntry> $entries
     */
    public function __construct(
        private array $entries,
    ) {
    }

    public function push(StackEntry $entry): void
    {
        $this->entries[] = $entry;
    }

    public function pop(): ?StackEntry
    {
        return array_pop($this->entries);
    }

    public function top(): ?StackEntry
    {
        $n = array_key_last($this->entries);
        return $n === null ? null : $this->entries[$n];
    }

    public function count(): int
    {
        return count($this->entries);
    }

    public function isEmpty(): bool
    {
        return $this->count() === 0;
    }
}