Test Failed
Push — master ( ecd78b...d05c81 )
by Kirill
02:43
created

NumberValueNode::parseBin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Invocation;
11
12
use Railt\Parser\Ast\LeafInterface;
13
use Railt\Parser\Ast\Rule;
14
use Railt\SDL\Frontend\IR\Value\FloatValue;
15
use Railt\SDL\Frontend\IR\Value\IntValue;
16
use Railt\SDL\Frontend\IR\Value\ValueInterface;
17
use Railt\SDL\Frontend\Parser;
18
19
/**
20
 * Class NumberValueNode
21
 */
22
class NumberValueNode extends Rule implements AstValueInterface
23
{
24
    /**
25
     * @return ValueInterface
26
     */
27
    public function unpack(): ValueInterface
28
    {
29
        $value = $this->parse();
30
31
        if (\is_int($value)) {
32
            return new IntValue($value, $this->getOffset());
33
        }
34
35
        return new FloatValue((float)$value, $this->getOffset());
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 LeafInterface $leaf
68
     * @return bool
69
     */
70
    private function isHex(LeafInterface $leaf): bool
71
    {
72
        return $leaf->getName() === Parser::T_HEX_NUMBER;
73
    }
74
75
    /**
76
     * @param string $value
77
     * @return int
78
     */
79
    private function parseHex(string $value): int
80
    {
81
        return \hexdec($value);
82
    }
83
84
    /**
85
     * @param LeafInterface $leaf
86
     * @return bool
87
     */
88
    private function isBinary(LeafInterface $leaf): bool
89
    {
90
        return $leaf->getName() === Parser::T_BIN_NUMBER;
91
    }
92
93
    /**
94
     * @param string $value
95
     * @return int
96
     */
97
    private function parseBin(string $value): int
98
    {
99
        return \bindec($value);
100
    }
101
102
    /**
103
     * @param LeafInterface $leaf
104
     * @return bool
105
     */
106
    private function isExponential(LeafInterface $leaf): bool
107
    {
108
        return \substr_count(\mb_strtolower($leaf->getValue()), 'e') !== 0;
109
    }
110
111
    /**
112
     * @param string $value
113
     * @return float
114
     */
115
    private function parseExponential(string $value): float
116
    {
117
        return (float)$value;
118
    }
119
120
    /**
121
     * @param LeafInterface $leaf
122
     * @return bool
123
     */
124
    private function isFloat(LeafInterface $leaf): bool
125
    {
126
        return \substr_count($leaf->getValue(), '.') !== 0;
127
    }
128
129
    /**
130
     * @param string $value
131
     * @return float
132
     */
133
    private function parseFloat(string $value): float
134
    {
135
        return (float)$value;
136
    }
137
138
    /**
139
     * @param LeafInterface $leaf
140
     * @return bool
141
     */
142
    private function isInt(LeafInterface $leaf): bool
143
    {
144
        return $leaf->getName() === Parser::T_NUMBER && \substr_count($leaf->getValue(), '.') === 0;
145
    }
146
147
    /**
148
     * @param string $value
149
     * @return int
150
     */
151
    private function parseInt(string $value): int
152
    {
153
        return $value >> 0;
154
    }
155
}
156