mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 04:24:43 +00:00
feat: add bin/oct/hex literal
This commit is contained in:
parent
e73e404083
commit
9025fe7e99
6 changed files with 91 additions and 10 deletions
|
@ -48,7 +48,7 @@ pub fn type_from_token_kind(kind: TokenKind) -> Type {
|
|||
use TokenKind::*;
|
||||
|
||||
match kind {
|
||||
NatLit => Type::Nat,
|
||||
NatLit | BinLit | OctLit | HexLit => Type::Nat,
|
||||
IntLit => Type::Int,
|
||||
RatioLit => Type::Ratio,
|
||||
StrLit | DocComment => Type::Str,
|
||||
|
|
|
@ -920,12 +920,29 @@ impl ValueObj {
|
|||
pub fn from_str(t: Type, mut content: Str) -> Option<Self> {
|
||||
match t {
|
||||
Type::Int => content.replace('_', "").parse::<i32>().ok().map(Self::Int),
|
||||
Type::Nat => content
|
||||
.trim_start_matches('-') // -0 -> 0
|
||||
.replace('_', "")
|
||||
.parse::<u64>()
|
||||
.ok()
|
||||
.map(Self::Nat),
|
||||
Type::Nat => {
|
||||
let content = content
|
||||
.trim_start_matches('-') // -0 -> 0
|
||||
.replace('_', "");
|
||||
if content.len() <= 1 {
|
||||
return content.parse::<u64>().ok().map(Self::Nat);
|
||||
}
|
||||
match &content[0..=1] {
|
||||
pre @ ("0b" | "0B") => {
|
||||
let content = content.trim_start_matches(pre);
|
||||
u64::from_str_radix(content, 2).ok().map(Self::Nat)
|
||||
}
|
||||
pre @ ("0o" | "0O") => {
|
||||
let content = content.trim_start_matches(pre);
|
||||
u64::from_str_radix(content, 8).ok().map(Self::Nat)
|
||||
}
|
||||
pre @ ("0x" | "0X") => {
|
||||
let content = content.trim_start_matches(pre);
|
||||
u64::from_str_radix(content, 16).ok().map(Self::Nat)
|
||||
}
|
||||
_ => content.parse::<u64>().ok().map(Self::Nat),
|
||||
}
|
||||
}
|
||||
Type::Float => content
|
||||
.replace('_', "")
|
||||
.parse::<f64>()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue