1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Compiler\Ast\Value; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\LeafInterface; |
13
|
|
|
use Railt\Parser\Ast\Rule; |
14
|
|
|
use Railt\SDL\Compiler\Parser; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class NumberValueNode |
18
|
|
|
*/ |
19
|
|
|
class NumberValueNode extends Rule implements ValueInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var float|int|null |
23
|
|
|
*/ |
24
|
|
|
private $value; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return float|int|null |
28
|
|
|
*/ |
29
|
|
|
public function toPrimitive() |
30
|
|
|
{ |
31
|
|
|
if ($this->value === null) { |
32
|
|
|
$this->value = $this->parse(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->value; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return int|float |
40
|
|
|
*/ |
41
|
|
|
protected function parse() |
42
|
|
|
{ |
43
|
|
|
/** @var LeafInterface $value */ |
44
|
|
|
$value = $this->getChild(0); |
45
|
|
|
|
46
|
|
|
switch (true) { |
47
|
|
|
case $this->isHex($value): |
48
|
|
|
return $this->parseHex($value->getValue(1)); |
49
|
|
|
|
50
|
|
|
case $this->isBinary($value): |
51
|
|
|
return $this->parseBin($value->getValue(1)); |
52
|
|
|
|
53
|
|
|
case $this->isExponential($value): |
54
|
|
|
return $this->parseExponential($value->getValue()); |
55
|
|
|
|
56
|
|
|
case $this->isFloat($value): |
57
|
|
|
return $this->parseFloat($value->getValue()); |
58
|
|
|
|
59
|
|
|
case $this->isInt($value): |
60
|
|
|
return $this->parseInt($value->getValue()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return (float)$value->getValue(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $value |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
private function parseHex(string $value): int |
71
|
|
|
{ |
72
|
|
|
return \hexdec($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $value |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
private function parseBin(string $value): int |
80
|
|
|
{ |
81
|
|
|
return \bindec($value); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $value |
86
|
|
|
* @return float |
87
|
|
|
*/ |
88
|
|
|
private function parseExponential(string $value): float |
89
|
|
|
{ |
90
|
|
|
return (float)$value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $value |
95
|
|
|
* @return float |
96
|
|
|
*/ |
97
|
|
|
private function parseFloat(string $value): float |
98
|
|
|
{ |
99
|
|
|
return (float)$value; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $value |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
private function parseInt(string $value): int |
107
|
|
|
{ |
108
|
|
|
return $value >> 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param LeafInterface $leaf |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
private function isHex(LeafInterface $leaf): bool |
116
|
|
|
{ |
117
|
|
|
return $leaf->getName() === Parser::T_HEX_NUMBER; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param LeafInterface $leaf |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
private function isBinary(LeafInterface $leaf): bool |
125
|
|
|
{ |
126
|
|
|
return $leaf->getName() === Parser::T_BIN_NUMBER; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param LeafInterface $leaf |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
private function isExponential(LeafInterface $leaf): bool |
134
|
|
|
{ |
135
|
|
|
return \substr_count(\mb_strtolower($leaf->getValue()), 'e') !== 0; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param LeafInterface $leaf |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
private function isFloat(LeafInterface $leaf): bool |
143
|
|
|
{ |
144
|
|
|
return \substr_count($leaf->getValue(), '.') !== 0; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param LeafInterface $leaf |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
private function isInt(LeafInterface $leaf): bool |
152
|
|
|
{ |
153
|
|
|
return $leaf->getName() === Parser::T_NUMBER && |
154
|
|
|
\substr_count($leaf->getValue(), '.') === 0; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|