aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Execution/Stack.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Execution/Stack.php')
-rw-r--r--src/Execution/Stack.php42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Execution/Stack.php b/src/Execution/Stack.php
new file mode 100644
index 0000000..1fa2dae
--- /dev/null
+++ b/src/Execution/Stack.php
@@ -0,0 +1,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;
+ }
+}