Skip to content

Commit 233611f

Browse files
committed
added repl
1 parent 8b8211f commit 233611f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

repl.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace igorw\lambda;
4+
5+
use Symfony\Component\Debug\ErrorHandler;
6+
7+
require 'vendor/autoload.php';
8+
9+
function prompt($mode) {
10+
echo "$mode> ";
11+
12+
return true;
13+
}
14+
15+
function format($result) {
16+
if (is_bool($result)) {
17+
return json_encode($result);
18+
}
19+
20+
return $result;
21+
}
22+
23+
$mode = 'i';
24+
25+
while (prompt($mode) && false !== ($line = fgets(STDIN))) {
26+
$exp = trim($line);
27+
28+
if (in_array($exp, ['i', 'b'], true)) {
29+
$mode = $exp;
30+
continue;
31+
}
32+
33+
$factories = [
34+
'i' => 'igorw\lambda\to_int',
35+
'b' => 'igorw\lambda\to_bool',
36+
];
37+
38+
try {
39+
$factory = $factories[$mode];
40+
echo format(evaluate($factory(parse($exp))))."\n";
41+
} catch (\Exception $e) {
42+
echo $e->getMessage()."\n";
43+
}
44+
}
45+
46+
echo "\n";

0 commit comments

Comments
 (0)