Skip to content

Commit 7100417

Browse files
committed
have to split filling due to chaining causing issues. fix asNumber
1 parent ab95461 commit 7100417

File tree

3 files changed

+213
-111
lines changed

3 files changed

+213
-111
lines changed

src/commonMain/kotlin/xyz/wagyourtail/unimined/mapping/jvms/ext/constant/NumberConstant.kt

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ value class NumberConstant private constructor(val value: String) : Type {
9595
append(reader.take()!!)
9696
append(DecimalConstant.read(reader))
9797
if (reader.peek()?.lowercaseChar() == 'e') {
98+
append(reader.take()!!)
9899
append(ExponentConstant.read(reader))
99100
}
100101
} else if (next == 'I') {
@@ -119,57 +120,118 @@ value class NumberConstant private constructor(val value: String) : Type {
119120

120121
}
121122

123+
fun isNegative() = value.startsWith("-")
124+
122125
fun asPositive() = value.removePrefix("-")
123126

124127
fun asNegative() = "-" + asPositive()
125128

126-
fun isWhole() = isHex() || (value.last() !in decimalSuffix && !value.contains(".") && !value.lowercase().contains('e'))
129+
fun isWhole(): Boolean {
130+
val positive = asPositive()
131+
// must be whole due to length
132+
if (positive.length == 1) {
133+
return true
134+
}
135+
// check for leading 0
136+
if (positive.first() == '0') {
137+
return false
138+
}
139+
// check for decimal suffix
140+
if (value.last().lowercaseChar() in decimalSuffix) {
141+
return false
142+
}
143+
// check for decimal/exponent
144+
if (value.contains('.', true) || value.contains('e', true)) {
145+
return false
146+
}
147+
return true
148+
}
127149

128-
fun isDecimal() = value.last().lowercaseChar() in decimalSuffix || (value.last().lowercaseChar() != 'l') && (!asPositive().startsWith("0") || asPositive().startsWith("0."))
150+
fun isDecimal(): Boolean {
151+
// has decimal suffix
152+
if (value.last().lowercaseChar() in decimalSuffix) {
153+
return true
154+
}
155+
// is not a long
156+
if (value.last().lowercaseChar() == 'l') {
157+
return false
158+
}
159+
// explicit checks
160+
return value.contains('.', true) || value.contains('e', true)
161+
}
129162

130-
fun isHex() = asPositive().substring(0, 2).lowercase().startsWith("0x")
163+
fun isHex() = asPositive().lowercase().startsWith("0x")
131164

132-
fun isBin() = asPositive().substring(0, 2).lowercase().startsWith("0b")
165+
fun isBin() = asPositive().lowercase().startsWith("0b")
133166

134-
fun isOctal() = asPositive().first() == '0' && asPositive().getOrNull(1)?.isDigit() == true
167+
fun isOctal(): Boolean {
168+
val positive = asPositive()
169+
// too short
170+
if (positive.length == 1) {
171+
return false
172+
}
173+
if (positive.first() == '0' && positive[1].isDigit()) {
174+
return true
175+
}
176+
return false
177+
}
135178

136179
fun isFloat() = value.last().lowercaseChar() == 'f' && !isHex()
137180

138181
fun isLong() = value.last().lowercaseChar() == 'l'
139182

140-
fun isDouble() = (asPositive().startsWith("I") || asPositive().startsWith("N") || isDecimal()) && !isFloat()
183+
fun isDouble(): Boolean{
184+
val positive = asPositive()
185+
// infinity or nan
186+
if (positive.startsWith("I") || positive.startsWith("N")) {
187+
return true
188+
}
189+
if (isDecimal()) {
190+
return value.last().lowercaseChar() != 'f'
191+
}
192+
return false
193+
}
141194

142195
fun isInteger() = isWhole() && !isLong()
143196

144197
fun asNumber(): Number {
145-
return if (isFloat()) {
146-
value.toFloat()
198+
if (isNegative()) {
199+
return unchecked(asPositive()).asNumber()
200+
}
201+
return if (isHex()) {
202+
if (isLong()) {
203+
value.substring(2, value.length - 1).toLong(16)
204+
} else {
205+
value.substring(2).toInt(16)
206+
}
207+
} else if (isBin()) {
208+
if (isLong()) {
209+
value.substring(2, value.length - 1).toLong(2)
210+
} else {
211+
value.substring(2).toInt(2)
212+
}
213+
} else if (isOctal()) {
214+
if (isLong()) {
215+
value.substring(0, value.length - 1).toLong(8)
216+
} else {
217+
value.toInt(8)
218+
}
219+
} else if (isFloat()) {
220+
value.substring(0, value.length - 1).toFloat()
147221
} else if (isLong()) {
148-
value.toLong()
222+
value.substring(0, value.length - 1).toLong()
149223
} else if (isDouble()) {
150-
value.toDouble()
224+
if (value.last().lowercaseChar() == 'd') {
225+
value.substring(0, value.length - 1).toDouble()
226+
} else {
227+
value.toDouble()
228+
}
151229
} else if (isInteger()) {
152230
value.toInt()
231+
} else if (isLong()) {
232+
value.substring(0, value.length - 1).toLong()
153233
} else {
154-
if (isHex()) {
155-
if (isLong()) {
156-
value.substring(2).toLong(16)
157-
} else {
158-
value.substring(2).toInt(16)
159-
}
160-
} else if (isBin()) {
161-
if (isLong()) {
162-
value.substring(2).toLong(2)
163-
} else {
164-
value.substring(2).toInt(2)
165-
}
166-
} else {
167-
if (isLong()) {
168-
value.toLong(8)
169-
} else {
170-
value.toInt(8)
171-
}
172-
}
234+
throw IllegalStateException()
173235
}
174236
}
175237

src/commonMain/kotlin/xyz/wagyourtail/unimined/mapping/resolver/MappingResolver.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ abstract class MappingResolver<T : MappingResolver<T>>(val name: String) {
259259
toFill.add(entry.requires to targets)
260260
}
261261
}
262-
resolved!!.fillMissingNames(*toFill.toTypedArray())
262+
toFill.forEach {
263+
resolved!!.fillMissingNames(it)
264+
}
263265
}.also {
264266
LOGGER.info { "Filled in missing names in $it" }
265267
}
Lines changed: 119 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,120 @@
1-
package xyz.wagyourtail.unimined.mapping.test.jvms.ext.constant
2-
3-
import xyz.wagyourtail.unimined.mapping.jvms.ext.constant.Constant
4-
import xyz.wagyourtail.unimined.mapping.test.jvms.buildStringAcceptor
5-
import kotlin.test.Test
6-
import kotlin.test.assertEquals
7-
import kotlin.test.assertFails
8-
9-
class ConstantTests {
10-
11-
@Test
12-
fun basics() {
13-
Constant.read("null")
14-
Constant.read("false")
15-
Constant.read("100")
16-
Constant.read("10e+24")
17-
Constant.read("100.23e-46")
18-
Constant.read("-100")
19-
Constant.read("-InfinityF")
20-
}
21-
22-
@Test
23-
fun illegal() {
24-
assertFails {
25-
Constant.read("09")
26-
}
27-
28-
assertFails {
29-
Constant.read("0xg")
30-
}
31-
32-
assertFails {
33-
Constant.read("0b")
34-
}
35-
36-
assertFails {
37-
Constant.read("0b2")
38-
}
39-
40-
assertFails {
41-
Constant.read("0x")
42-
}
43-
44-
assertFails {
45-
Constant.read("0x1h")
46-
}
47-
}
48-
49-
@Test
50-
fun testVisitor() {
51-
val c = Constant.read("null")
52-
assertEquals("null", buildStringAcceptor(c))
53-
54-
val c1 = Constant.read("true")
55-
assertEquals("true", buildStringAcceptor(c1))
56-
57-
val c2 = Constant.read("10e+24")
58-
assertEquals("10e+24", buildStringAcceptor(c2))
59-
60-
val c3 = Constant.read("-NaND")
61-
assertEquals("-NaND", buildStringAcceptor(c3))
62-
63-
val c4 = Constant.read("0.10e+24")
64-
assertEquals("0.10e+24", buildStringAcceptor(c4))
65-
66-
val c5 = Constant.read("0b1010")
67-
assertEquals("0b1010", buildStringAcceptor(c5))
68-
69-
val c6 = Constant.read("0x1010L")
70-
assertEquals("0x1010L", buildStringAcceptor(c6))
71-
72-
val c7 = Constant.read("0123l")
73-
assertEquals("0123l", buildStringAcceptor(c7))
74-
75-
val c8 = Constant.read("0")
76-
assertEquals("0", buildStringAcceptor(c8))
77-
78-
val c9 = Constant.read("1")
79-
assertEquals("1", buildStringAcceptor(c9))
80-
}
81-
1+
package xyz.wagyourtail.unimined.mapping.test.jvms.ext.constant
2+
3+
import xyz.wagyourtail.unimined.mapping.jvms.ext.constant.Constant
4+
import xyz.wagyourtail.unimined.mapping.test.jvms.buildStringAcceptor
5+
import kotlin.test.Test
6+
import kotlin.test.assertEquals
7+
import kotlin.test.assertFails
8+
import kotlin.test.assertTrue
9+
10+
class ConstantTests {
11+
12+
@Test
13+
fun basics() {
14+
Constant.read("null")
15+
Constant.read("false")
16+
Constant.read("100")
17+
Constant.read("10e+24")
18+
Constant.read("100.23e-46")
19+
Constant.read(".23e-46")
20+
Constant.read("0xFFFF10")
21+
Constant.read("-100")
22+
Constant.read("-InfinityF")
23+
}
24+
25+
@Test
26+
fun illegal() {
27+
assertFails {
28+
Constant.read("09")
29+
}
30+
31+
assertFails {
32+
Constant.read("0xg")
33+
}
34+
35+
assertFails {
36+
Constant.read("0b")
37+
}
38+
39+
assertFails {
40+
Constant.read("0b2")
41+
}
42+
43+
assertFails {
44+
Constant.read("0x")
45+
}
46+
47+
assertFails {
48+
Constant.read("0x1h")
49+
}
50+
}
51+
52+
@Test
53+
fun testVisitor() {
54+
val c = Constant.read("null")
55+
assertEquals("null", buildStringAcceptor(c))
56+
57+
val c1 = Constant.read("true")
58+
assertEquals("true", buildStringAcceptor(c1))
59+
60+
val c2 = Constant.read("10e+24")
61+
assertEquals("10e+24", buildStringAcceptor(c2))
62+
63+
val c3 = Constant.read("-NaND")
64+
assertEquals("-NaND", buildStringAcceptor(c3))
65+
66+
val c4 = Constant.read("0.10e+24f")
67+
assertEquals("0.10e+24f", buildStringAcceptor(c4))
68+
69+
val c5 = Constant.read("0b1010")
70+
assertEquals("0b1010", buildStringAcceptor(c5))
71+
72+
val c6 = Constant.read("0x1010L")
73+
assertEquals("0x1010L", buildStringAcceptor(c6))
74+
75+
val c7 = Constant.read("0123l")
76+
assertEquals("0123l", buildStringAcceptor(c7))
77+
78+
val c8 = Constant.read("0")
79+
assertEquals("0", buildStringAcceptor(c8))
80+
81+
val c9 = Constant.read("1")
82+
assertEquals("1", buildStringAcceptor(c9))
83+
}
84+
85+
@Test
86+
fun testValue() {
87+
val c = Constant.read("null")
88+
assertTrue(c.isNull())
89+
90+
val c1 = Constant.read("true")
91+
assertTrue(c1.getBoolean())
92+
93+
val c2 = Constant.read("10e+24")
94+
assertEquals(10e24, c2.getNumber()!!.asNumber())
95+
96+
val c3 = Constant.read("-NaND")
97+
val c3n = c3.getNumber()!!.asNumber()
98+
assertTrue(c3n is Double && c3n.isNaN())
99+
100+
val c4 = Constant.read("0.10e+24f")
101+
assertEquals(0.10e24f, c4.getNumber()!!.asNumber())
102+
103+
val c5 = Constant.read("0b1010")
104+
assertEquals(0b1010, c5.getNumber()!!.asNumber())
105+
106+
val c6 = Constant.read("0x1010L")
107+
assertEquals(0x1010L, c6.getNumber()!!.asNumber())
108+
109+
val c7 = Constant.read("0123l")
110+
assertEquals("0123".toInt(8).toLong(), c7.getNumber()!!.asNumber())
111+
112+
val c8 = Constant.read("0")
113+
assertEquals(0, c8.getNumber()!!.asNumber())
114+
115+
val c9 = Constant.read("1")
116+
assertEquals(1, c9.getNumber()!!.asNumber())
117+
118+
}
119+
82120
}

0 commit comments

Comments
 (0)