-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.php
80 lines (69 loc) · 1.37 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
require_once "vendor/autoload.php";
$parser = new \Pisp\Parser\Parser;
$code = <<<EOF
(do
(print abc)
(@def bcd 321)
(print bcd)
(@def bcde (do (@get print)))
(bcde (+ 1 2 (- (mod 120 9) 2 3 )))
(@def
repeat
["a", "b"]
(@block
(print a b a b)
)
)
(@fn
print-2-string
["str1", "str2"]
(@@@
(print (str1) (str2))
)
)
(repeat "abc" "bcd")
(print-2-string "Hello" "World")
(print Hello)
(@if (- 1 2)
(print 1)
(print 2)
)
(@def cond 10)
(@while (cond)
(print "\nCond is " cond)
(@unless (- cond 5)
(break)
)
(@def cond (- cond 1))
)
(print "\n")
(@loop
(print "Input number 5 => ")
(@unless (- input 5)
(break)
)
)
)
EOF;
$root = $parser->parse($code);
$vm = new \Pisp\VM\VM;
$vm->define("abc", "123");
$vm->define("print", function ($args, $vm) {
foreach ($args as $v) {
if (is_string($v) || method_exists($v, "__toString")) {
print($v);
} else {
@var_export($v);
}
}
});
$vm->define("input", function ($args, $vm) {
return trim(fgets(STDIN));
});
$vm->define("__defaultResolver__", function ($name, $args, $vm) {
return [true, $name];
});
\Pisp\StdLib\StandardLibrary::register($vm);
$vm->run($root);
echo PHP_EOL;