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\Frontend\AST\Value; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\LeafInterface; |
13
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
14
|
|
|
use Railt\SDL\Frontend\Parser; |
15
|
|
|
use Railt\SDL\IR\SymbolTable\Value; |
16
|
|
|
use Railt\SDL\IR\SymbolTable\ValueInterface; |
17
|
|
|
use Railt\SDL\IR\Type; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class NumberValue |
21
|
|
|
*/ |
22
|
|
|
class NumberValueNode extends AbstractAstValueNode |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @return int|float |
26
|
|
|
*/ |
27
|
|
|
public function parse() |
28
|
|
|
{ |
29
|
|
|
/** @var LeafInterface $value */ |
30
|
|
|
$value = $this->getChild(0); |
31
|
|
|
|
32
|
|
|
switch (true) { |
33
|
|
|
case $this->isHex($value): |
34
|
|
|
return $this->parseHex($value->getValue(1)); |
35
|
|
|
|
36
|
|
|
case $this->isBinary($value): |
37
|
|
|
return $this->parseBin($value->getValue(1)); |
38
|
|
|
|
39
|
|
|
case $this->isExponential($value): |
40
|
|
|
return $this->parseExponential($value->getValue()); |
41
|
|
|
|
42
|
|
|
case $this->isFloat($value): |
43
|
|
|
return $this->parseFloat($value->getValue()); |
44
|
|
|
|
45
|
|
|
case $this->isInt($value): |
46
|
|
|
return $this->parseInt($value->getValue()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return (float)$value->getValue(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LeafInterface $leaf |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
private function isHex(LeafInterface $leaf): bool |
57
|
|
|
{ |
58
|
|
|
return $leaf->getName() === Parser::T_HEX_NUMBER; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $value |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
private function parseHex(string $value): int |
66
|
|
|
{ |
67
|
|
|
return \hexdec($value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param LeafInterface $leaf |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
private function isBinary(LeafInterface $leaf): bool |
75
|
|
|
{ |
76
|
|
|
return $leaf->getName() === Parser::T_BIN_NUMBER; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $value |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
private function parseBin(string $value): int |
84
|
|
|
{ |
85
|
|
|
return \bindec($value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param LeafInterface $leaf |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
private function isExponential(LeafInterface $leaf): bool |
93
|
|
|
{ |
94
|
|
|
return \substr_count(\mb_strtolower($leaf->getValue()), 'e') !== 0; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $value |
99
|
|
|
* @return float |
100
|
|
|
*/ |
101
|
|
|
private function parseExponential(string $value): float |
102
|
|
|
{ |
103
|
|
|
return (float)$value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param LeafInterface $leaf |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
private function isFloat(LeafInterface $leaf): bool |
111
|
|
|
{ |
112
|
|
|
return \substr_count($leaf->getValue(), '.') !== 0; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $value |
117
|
|
|
* @return float |
118
|
|
|
*/ |
119
|
|
|
private function parseFloat(string $value): float |
120
|
|
|
{ |
121
|
|
|
return (float)$value; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param LeafInterface $leaf |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
private function isInt(LeafInterface $leaf): bool |
129
|
|
|
{ |
130
|
|
|
return $leaf->getName() === Parser::T_NUMBER && \substr_count($leaf->getValue(), '.') === 0; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $value |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
|
|
private function parseInt(string $value): int |
138
|
|
|
{ |
139
|
|
|
return $value >> 0; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|