Skip to content

Commit 3868145

Browse files
committed
it works
1 parent f1ed60c commit 3868145

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

krivine.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function de_bruijn($exp, $indices = [])
8282
function compile($exp)
8383
{
8484
if (is_int($exp) || is_float($exp) || is_bool($exp)) {
85-
return [['constant', $exp], ['stop']];
85+
return [['constant', $exp]];
8686
}
8787

8888
if (is_object($exp) && is_callable($exp)) {
@@ -96,7 +96,7 @@ function compile($exp)
9696

9797
if ('offset' === first($exp)) {
9898
list($_, $offset) = $exp;
99-
return [['access', $offset], ['continue_']];
99+
return [['access', $offset]];
100100
}
101101

102102
$f = first($exp);
@@ -124,7 +124,7 @@ function execute()
124124
echo "---\n";
125125

126126
if (!$this->code) {
127-
throw new \RuntimeException('Reached end of code stream.');
127+
return first($this->stack);
128128
}
129129

130130
$inst = first(first($this->code));
@@ -133,20 +133,15 @@ function execute()
133133
$fn = [$this, $inst];
134134

135135
$machine = $fn($inst_arg, rest($this->code), $this->env, $this->stack);
136-
137-
if (!$machine instanceof Machine) {
138-
return $machine;
139-
}
140-
141136
return $machine->execute();
142137
}
143138

144139
function access($args, $code, $env, $stack)
145140
{
146141
$i = first($args) - 1;
147-
array_unshift($stack, first($env[$i]));
142+
list($c_code, $c_env) = $env[$i];
148143

149-
return new Machine($code, $env, $stack);
144+
return new Machine($c_code, $c_env, $stack);
150145
}
151146

152147
function push($args, $code, $env, $stack)
@@ -173,23 +168,11 @@ function constant($args, $code, $env, $stack)
173168
return new Machine($code, $env, $stack);
174169
}
175170

176-
function continue_($args, $code, $env, $stack)
177-
{
178-
list($c_code, $c_env) = first($stack);
179-
180-
return new Machine([$c_code], $c_env, rest($stack));
181-
}
182-
183-
function stop($args, $code, $env, $stack)
184-
{
185-
return first($stack);
186-
}
187-
188-
function call($args, $code, $env, $stack)
189-
{
190-
$f = first($args);
191-
return $f($code, $env, $stack);
192-
}
171+
// function call($args, $code, $env, $stack)
172+
// {
173+
// $f = first($args);
174+
// return $f($code, $env, $stack);
175+
// }
193176
}
194177

195178
function evaluate(array $ops)
@@ -225,7 +208,7 @@ function evaluate(array $ops)
225208
// var_dump(compile(de_bruijn(
226209
// [[['λ', 'x', ['λ', 'y', 'y']], 5], 6]
227210
// )));
228-
var_dump(evaluate(compile(de_bruijn(
229-
[[['λ', 'x', ['λ', 'y', 'y']], 5], 6]
230-
))));
231-
// var_dump(evaluate(compile(de_bruijn($identity))));
211+
// var_dump(evaluate(compile(de_bruijn(
212+
// [[['λ', 'x', ['λ', 'y', 'y']], 5], 6]
213+
// ))));
214+
// var_dump(evaluate(compile([de_bruijn($identity), 42])));

0 commit comments

Comments
 (0)