1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sphpeme\Env; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class StdEnv implements EnvInterface |
8
|
|
|
{ |
9
|
|
|
const MAPPING = [ |
10
|
|
|
'#' => '+', |
11
|
|
|
'+' => 'add', |
12
|
|
|
'-' => 'subtract', |
13
|
|
|
'*' => 'multiply', |
14
|
|
|
'/' => 'divide', |
15
|
|
|
'>' => 'gt', |
16
|
|
|
'<' => 'lt', |
17
|
|
|
'<=' => 'lte', |
18
|
|
|
'>=' => 'gte', |
19
|
|
|
'eq?' => 'isEqual', |
20
|
|
|
'eqv?' => 'isEquiv', |
21
|
|
|
'list' => '_list' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function __isset($prop): bool |
25
|
|
|
{ |
26
|
|
|
return (bool)$this->__get($prop); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function __get(string $prop) |
30
|
|
|
{ |
31
|
|
|
if (isset(static::MAPPING[$prop])) { |
32
|
|
|
return $this->__get(static::MAPPING[$prop]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return \is_callable([$this, $prop]) |
36
|
|
|
? [$this, $prop] |
37
|
|
|
: $this->$prop; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public $pi = M_PI; |
41
|
|
|
|
42
|
|
|
public function _list(...$args) |
43
|
|
|
{ |
44
|
|
|
return $args; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function car(array $list) |
48
|
|
|
{ |
49
|
|
|
return current($list); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function cdr(array $list) |
53
|
|
|
{ |
54
|
|
|
return \array_slice($list, 1); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function display($arg) |
58
|
|
|
{ |
59
|
|
|
if (is_scalar($arg) || method_exists($arg, '__toString')) { |
60
|
|
|
echo $arg; |
61
|
|
|
} else { |
62
|
|
|
var_export($arg); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function newline() |
67
|
|
|
{ |
68
|
|
|
echo PHP_EOL; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function add($first, ...$rest) |
72
|
|
|
{ |
73
|
|
|
return $first + (\count($rest) > 1 |
74
|
|
|
? $this->add(...$rest) |
|
|
|
|
75
|
|
|
: current($rest)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function subtract($first, ...$rest) |
79
|
|
|
{ |
80
|
|
|
return $first - (\count($rest) > 1 |
81
|
|
|
? $this->add(...$rest) |
|
|
|
|
82
|
|
|
: current($rest)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function multiply($first, ...$rest) |
86
|
|
|
{ |
87
|
|
|
return $first * (\count($rest) > 1 |
88
|
|
|
? $this->multiply(...$rest) |
|
|
|
|
89
|
|
|
: current($rest)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function divide($a, $b) |
93
|
|
|
{ |
94
|
|
|
return $a / $b; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function gt($a, $b) |
98
|
|
|
{ |
99
|
|
|
return $a > $b; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function lt($a, $b) |
103
|
|
|
{ |
104
|
|
|
return $a < $b; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function gte($a, $b) |
108
|
|
|
{ |
109
|
|
|
return $a >= $b; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function lte($a, $b) |
113
|
|
|
{ |
114
|
|
|
return $a <= $b; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function isEqual($a, $b) |
118
|
|
|
{ |
119
|
|
|
return $a == $b; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function isEquiv($a, $b) |
123
|
|
|
{ |
124
|
|
|
return $a === $b; |
125
|
|
|
} |
126
|
|
|
} |