Skip to content

Commit f150d4c

Browse files
committed
add big decimal parsing
1 parent 332810c commit f150d4c

File tree

4 files changed

+72
-21
lines changed

4 files changed

+72
-21
lines changed

src/main/kotlin/org/lice/compiler/parse/Number.kt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import java.math.BigInteger
1616
// get() = this >= '0' && this <= '9'
1717

1818
fun String.isInt(isNegative: Boolean = false): Boolean {
19-
if (!isNegative && this[0] == '-') return substring(1).isInt(true)
19+
if (!isNegative && '-' == this[0]) return substring(1).isInt(true)
2020
return isNotEmpty() && fold(true, { res, char ->
2121
res && char.isDigit()
2222
})
@@ -32,43 +32,55 @@ fun Char.safeLower() =
3232
}
3333

3434
fun String.isHexInt(isNegative: Boolean = false): Boolean {
35-
if (!isNegative && this[0] == '-') return substring(1).isHexInt(true)
35+
if (!isNegative && '-' == this[0]) return substring(1).isHexInt(true)
3636
return when {
3737
length <= 2 -> false
38-
this[0] != '0' || this[1].safeLower() != 'x' -> false
38+
'0' != this[0] || 'x' != this[1].safeLower() -> false
3939
else -> (2..length - 1)
4040
.map { this[it].toLowerCase() }
41-
.none { !it.isDigit() && (it < 'a' || it > 'f') }
41+
.all { it.isDigit() || it in 'a'..'f' }
4242
}
4343
}
4444

4545
fun String.isBigInt(isNegative: Boolean = false): Boolean {
46-
if (!isNegative && this[0] == '-') return substring(1).isBigInt(true)
46+
if (!isNegative && '-' == this[0]) return substring(1).isBigInt(true)
4747
return when {
4848
length <= 1 -> false
49-
this[length - 1].safeLower() != 'n' -> false
49+
'n' != this[length - 1].safeLower() -> false
5050
else -> {
5151
val a = substring(0..length - 2)
5252
a.isInt() || a.isHexInt() || a.isBinInt() || a.isOctInt()
5353
}
5454
}
5555
}
5656

57+
fun String.isBigDec(isNegative: Boolean = false): Boolean {
58+
if (!isNegative && '-' == this[0]) return substring(1).isBigDec(true)
59+
return when {
60+
length <= 2 -> false
61+
'm' != this[length - 1].safeLower() -> false
62+
else -> {
63+
val a = substring(0..length - 2)
64+
1 >= a.count { '.' == it } && a.all { '.' == it || it.isDigit() }
65+
}
66+
}
67+
}
68+
5769
fun String.isBinInt(isNegative: Boolean = false): Boolean {
5870
if (!isNegative && this[0] == '-') return substring(1).isBinInt(true)
5971
return when {
6072
length <= 2 -> false
61-
this[0] != '0' || this[1].safeLower() != 'b' -> false
62-
else -> (2..length - 1).none { this[it] != '0' && this[it] != '1' }
73+
'0' != this[0] || 'b' != this[1].safeLower() -> false
74+
else -> (2..length - 1).none { '0' != this[it] && '1' != this[it] }
6375
}
6476
}
6577

6678
fun String.isOctInt(isNegative: Boolean = false): Boolean {
67-
if (!isNegative && this[0] == '-') return substring(1).isOctInt(true)
79+
if (!isNegative && '-' == this[0]) return substring(1).isOctInt(true)
6880
return when {
6981
length <= 1 -> false
70-
this[0] != '0' -> false
71-
else -> (1..length - 1).none { !this[it].isOctalInt() }
82+
'0' != this[0] -> false
83+
else -> (1..length - 1).all { this[it].isOctalInt() }
7284
}
7385
}
7486

@@ -79,19 +91,17 @@ fun String.toHexInt(): Int {
7991
ret = ret shl 4
8092
val char = this[it].safeLower()
8193
if (char.isDigit()) ret += (char - '0')
82-
else /* if (char >= 'a' && char <= 'f') */ ret += (char - 'a' + 10)
83-
// ret *= 16
94+
else ret += (char - 'a' + 10)
8495
}
8596
return ret
8697
}
8798

8899
fun String.toBinInt(): Int {
89-
if (this[0] == '-') return -substring(1).toBinInt()
100+
if ('-' == this[0]) return -substring(1).toBinInt()
90101
var ret = 0
91102
(2..length - 1).forEach {
92103
ret = ret shl 1
93-
if (this[it] == '1') ++ret
94-
// ret *= 2
104+
if ('1' == this[it]) ++ret
95105
}
96106
return ret
97107
}
@@ -113,7 +123,6 @@ fun String.toOctInt(): Int {
113123
(1..length - 1).forEach {
114124
ret = ret shl 3
115125
ret += this[it] - '0'
116-
// ret *= 8
117126
}
118127
return ret
119128
}

src/main/kotlin/org/lice/repl/Constants.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
*/
77
package org.lice.repl
88

9-
val VERSION_CODE = "v3.0-SNAPSHOT"
9+
val VERSION_CODE = "v3.0.4-SNAPSHOT"

src/test/kotlin/org/lice/FeatureTest.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import org.jetbrains.annotations.TestOnly
44
import org.junit.Assert.assertEquals
55
import org.junit.Assert.assertNull
66
import org.junit.Test
7+
import org.lice.core.SymbolList
8+
import java.io.File
79
import java.math.BigDecimal
810
import java.math.BigInteger
911
import java.util.*
@@ -32,23 +34,41 @@ class FeatureTest {
3234
}
3335

3436
/**
35-
* test for int literals
37+
* test for primitives literals
3638
*/
3739
@Test(timeout = 1000)
3840
fun test2() {
3941
assertEquals(233666, Lice.run("""
4042
(print 233666)
4143
"""))
4244
assertEquals(233666, Lice.run("233666"))
45+
assertEquals(233666F, Lice.run("233666.0"))
46+
assertEquals(233666.0, Lice.run("233666.0000000000000000000000000000000"))
47+
assertTrue(true == Lice.run("true"))
48+
assertFalse(true == Lice.run("false"))
49+
assertNull(Lice.run("null"))
4350
}
4451

4552
/**
46-
* test for plus
53+
* test for plus minus times devide
4754
*/
4855
@Test(timeout = 1000)
4956
fun test3() {
5057
assertEquals(7, Lice.run("""
5158
(+ 1 (+ 1 2 3))
59+
"""))
60+
assertEquals(-5, Lice.run("""
61+
(- 1 (+ 1 2 3))
62+
"""))
63+
assertEquals(20, Lice.run("""
64+
(* 4 (+ 2 3))
65+
"""))
66+
assertEquals(0, Lice.run("+"))
67+
assertEquals(0, Lice.run("-"))
68+
assertEquals(0, Lice.run("(+)"))
69+
assertEquals(0, Lice.run("(-)"))
70+
assertEquals(20, Lice.run("""
71+
(* 4 (+ 2 3))
5272
"""))
5373
}
5474

@@ -60,6 +80,7 @@ class FeatureTest {
6080
assertEquals(BigInteger("10000000000000000000000000000233"), Lice.run("""
6181
(+ 10000000000000000000000000000000N 233)
6282
"""))
83+
assertEquals(BigInteger(0xDBE.toString()), Lice.run("0xDBEN"))
6384
}
6485

6586
/**

src/test/kotlin/org/lice/compiler/parse/NumberKtTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.lice.compiler.parse
22

33
import org.junit.Assert.*
44
import org.junit.Test
5-
import org.lice.compiler.parse.*
5+
import java.math.BigDecimal
66
import java.math.BigInteger
77

88
/**
@@ -95,4 +95,25 @@ class NumberKtTest {
9595
assertEquals(BigInteger("-21893219"), "-21893219n".toBigInt())
9696
}
9797

98+
@Test
99+
fun isBigDec() {
100+
assertTrue("1273983189279m".isBigDec())
101+
assertTrue("12739.83189279m".isBigDec())
102+
assertTrue("1273983189279M".isBigDec())
103+
assertTrue("127398.3189279M".isBigDec())
104+
assertTrue("-1273983189279M".isBigDec())
105+
assertTrue("-127398.3189279M".isBigDec())
106+
assertFalse("-12739831dsf9M".isBigDec())
107+
assertFalse("-127398.31dsf9M".isBigDec())
108+
}
109+
110+
@Test
111+
fun toBigDec() {
112+
assertEquals(BigDecimal("1273983189279"), "1273983189279m".toBigDec())
113+
assertEquals(BigDecimal("12739.83189279"), "12739.83189279m".toBigDec())
114+
assertEquals(BigDecimal("1273983189279"), "1273983189279M".toBigDec())
115+
assertEquals(BigDecimal("127398.3189279"), "127398.3189279M".toBigDec())
116+
assertEquals(BigDecimal("-1273983189279"), "-1273983189279M".toBigDec())
117+
assertEquals(BigDecimal("-127398.3189279"), "-127398.3189279M".toBigDec())
118+
}
98119
}

0 commit comments

Comments
 (0)