diff options
author | nea <nea@nea.moe> | 2023-08-15 20:24:01 +0200 |
---|---|---|
committer | nea <nea@nea.moe> | 2023-08-15 20:24:01 +0200 |
commit | 54b0f2ea841d2ce3895713415481271aa3034b30 (patch) | |
tree | 401775e9ce327506546fa614e79decb667ab1aa4 /src | |
parent | d1f49743a0a00a1156272d174e8b6b5c4bb9acec (diff) | |
download | nealisp-54b0f2ea841d2ce3895713415481271aa3034b30.tar.gz nealisp-54b0f2ea841d2ce3895713415481271aa3034b30.tar.bz2 nealisp-54b0f2ea841d2ce3895713415481271aa3034b30.zip |
Add support for hex literals
Diffstat (limited to 'src')
-rw-r--r-- | src/LispParser.kt | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/LispParser.kt b/src/LispParser.kt index e005c2d..3341779 100644 --- a/src/LispParser.kt +++ b/src/LispParser.kt @@ -15,6 +15,7 @@ class LispParser private constructor(filename: String, string: String) { } val digits = "1234567890" + val hexDigits = digits + "abcdefABCDEF" val alphabet = "abcdefghijklmnopqrstuvwxyz" val validStartingIdentifiers = "-.#+*'!$%&/=?_~|^" + alphabet + alphabet.uppercase() val validIdentifiers = validStartingIdentifiers + digits @@ -62,11 +63,14 @@ class LispParser private constructor(filename: String, string: String) { fun parseNumber(): LispAst.NumberLiteral { val start = racer.idx racer.pushState() - val number = racer.consumeWhile { it.last().let { it in digits || it == '.' } } - val double = number.toDoubleOrNull() + val number = racer.consumeWhile { it.last().let { it in hexDigits || it == '.' || it == 'x' } } + var double = number.toDoubleOrNull() + if (number.startsWith("0x")) { + double = number.substring(2).toLong(16).toDouble() + } if (double == null) { racer.popState() - racer.error("Could not parse number") + racer.error("Could not parse number $number") } racer.discardState() return LispAst.NumberLiteral(racer.span(start), double) |