Skip to content

Commit 63e13d9

Browse files
committed
no more stack overflows
1 parent 0b59a55 commit 63e13d9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

krivine.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,29 @@ function __construct($code, $env, $stack)
111111
}
112112

113113
function execute()
114+
{
115+
$machine = $this;
116+
117+
while ($machine->code) {
118+
$machine = $machine->step();
119+
}
120+
121+
return first($this->stack);
122+
}
123+
124+
function step()
114125
{
115126
echo 'code: '.json_encode($this->code)."\n";
116127
echo 'env: '.json_encode($this->env)."\n";
117128
echo 'stack: '.json_encode($this->stack)."\n";
118129
echo "---\n";
119130

120-
if (!$this->code) {
121-
return first($this->stack);
122-
}
123-
124131
$inst = first(first($this->code));
125132
$inst_arg = rest(first($this->code));
126133

127134
$fn = [$this, $inst];
128135

129-
$machine = $fn($inst_arg, rest($this->code), $this->env, $this->stack);
130-
return $machine->execute();
136+
return $fn($inst_arg, rest($this->code), $this->env, $this->stack);
131137
}
132138

133139
function access($args, $code, $env, $stack)

0 commit comments

Comments
 (0)