Skip to content

Commit 2fcc668

Browse files
committed
Add dumper, pretty-print expressions in raw mode
1 parent 66b3c60 commit 2fcc668

File tree

7 files changed

+42
-26
lines changed

7 files changed

+42
-26
lines changed

README.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,7 @@ Or `r` for raw mode:
9494
$ php repl.php
9595
i> r
9696
r> λx.x
97-
array(4) {
98-
[0]=>
99-
string(2) "λ"
100-
[1]=>
101-
string(1) "x"
102-
[2]=>
103-
string(1) "x"
104-
[3]=>
105-
array(0) {
106-
}
107-
}
97+
λx.x
10898

10999
## References
110100

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"jakubledl/dissect": "dev-develop"
1414
},
1515
"autoload": {
16-
"files": ["src/eval.php", "src/parser.php", "src/krivine.php", "src/binary.php"]
16+
"files": ["src/util.php", "src/eval.php", "src/parser.php", "src/krivine.php", "src/binary.php", "src/dumper.php"]
1717
}
1818
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

repl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ function prompt($mode)
1515

1616
function format_raw($result)
1717
{
18-
ob_start();
19-
var_dump($result);
20-
return trim(ob_get_clean());
18+
return dump($result);
2119
}
2220

2321
function identity($x)

src/dumper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace igorw\lambda;
4+
5+
function dump($exp)
6+
{
7+
if (is_string($exp)) {
8+
return $exp;
9+
}
10+
11+
if ('λ' === first($exp)) {
12+
list($_, $arg, $body) = $exp;
13+
return 'λ'.dump($arg).'.'.dump($body);
14+
}
15+
16+
list($f, $arg) = $exp;
17+
return dump($f).(is_application($arg) ? '('.dump($arg).')' : dump($arg));
18+
}
19+
20+
function is_application($exp)
21+
{
22+
return is_array($exp)
23+
&& 'λ' !== first($exp)
24+
&& 2 === count($exp);
25+
}

src/eval.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919

2020
namespace igorw\lambda;
2121

22-
function first(array $list)
23-
{
24-
return $list[0];
25-
}
26-
27-
function second(array $list)
28-
{
29-
return $list[1];
30-
}
31-
3222
function evaluate($exp, array $env = [])
3323
{
3424
// actual PHP numbers and callables

src/util.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace igorw\lambda;
4+
5+
function first(array $list)
6+
{
7+
return $list[0];
8+
}
9+
10+
function second(array $list)
11+
{
12+
return $list[1];
13+
}

0 commit comments

Comments
 (0)