Skip to content

Commit 209c60b

Browse files
committed
Underscores can be used in numbers now and will be ignored
1 parent e1ad9b5 commit 209c60b

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

releases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Added new syntax for foreign methods and fields: obj#?method(...) and obj#-?field
66
which will return Option types instead of raw types
77
- Added @ syntax to deref atoms
8+
- Underscores can be used in numbers now and will be ignored: 1_000_000
89

910
## Optimizations
1011

src/main/kotlin/novah/frontend/Lexer.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ class Lexer(input: Iterator<Char>) : Iterator<Spanned<Token>> {
458458
fun readE(): String {
459459
val e = iter.next().toString()
460460
val sign = accept("+-")?.toString() ?: ""
461-
val exp = acceptMany("0123456789")
461+
val exp = acceptMany("0123456789_").replace("_", "")
462462
if (exp.isEmpty()) {
463463
val span = Span.new(startPos, iter.position())
464464
lexError("Invalid number format: expected number after `e`.", span)
@@ -481,7 +481,7 @@ class Lexer(input: Iterator<Char>) : Iterator<Spanned<Token>> {
481481
// binary numbers
482482
'b', 'B' -> {
483483
iter.next()
484-
val bin = acceptMany("01")
484+
val bin = acceptMany("01_").replace("_", "")
485485
if (bin.isEmpty()) lexError("Binary number cannot be empty")
486486
val l = accept("L")
487487
if (l != null) LongT(bin.toSafeLong(2) * n, "${pref}0$c$bin")
@@ -490,7 +490,7 @@ class Lexer(input: Iterator<Char>) : Iterator<Spanned<Token>> {
490490
// hex numbers
491491
'x', 'X' -> {
492492
iter.next()
493-
val hex = acceptMany("0123456789abcdefABCDEF")
493+
val hex = acceptMany("0123456789abcdefABCDEF_").replace("_", "")
494494
if (hex.isEmpty()) lexError("Hexadecimal number cannot be empty")
495495
val l = accept("L")
496496
if (l != null) LongT(hex.toSafeLong(16) * n, "${pref}0$c$hex")
@@ -508,14 +508,14 @@ class Lexer(input: Iterator<Char>) : Iterator<Spanned<Token>> {
508508
}
509509
}
510510
} else {
511-
val num = init + acceptMany("0123456789")
511+
val num = init + acceptMany("0123456789_").replace("_", "")
512512

513513
val next = if (iter.hasNext()) iter.peek() else ' '
514514
when {
515515
next == '.' -> {
516516
// Double
517517
iter.next()
518-
val end = acceptMany("0123456789")
518+
val end = acceptMany("0123456789_").replace("_", "")
519519
if (end.isEmpty()) lexError("Invalid number format: number cannot end in `.`")
520520
val number = if (iter.hasNext() && iter.peek().lowercaseChar() == 'e') {
521521
"$num.$end${readE()}"

src/test/resources/test/test/core.novah

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ coreTests =
3333
(true && true) `shouldBe` true
3434
(true || false) `shouldBe` true
3535
(false || false) `shouldBe` false
36-
int 125L `shouldBe` 125
36+
int 125_400L `shouldBe` 125_400
3737
int "999" `shouldBe` 999
3838
int false `shouldBe` 0
39-
int64 125 `shouldBe` 125L
39+
int64 1_000_000 `shouldBe` 1000000L
4040
float32 true `shouldBe` 1F
4141
float64 "1.78" `shouldBe` 1.78
4242
(Some 3)!! `shouldBe` 3

0 commit comments

Comments
 (0)