Support Hex, Binary, and Octal number literals #20

Merged
mlokis merged 3 commits from :trunk into trunk 2024-09-01 14:19:14 -05:00
2 changed files with 24 additions and 3 deletions
Showing only changes of commit 781c40ede0 - Show all commits

View file

@ -292,6 +292,18 @@ impl<'a> Lexer<'a> {
let kind = match c { let kind = match c {
..=b' ' => continue, ..=b' ' => continue,
b'0' if self.advance_if(b'x') => {
while let Some(b'0'..=b'9') = self.peek() {
mlokis marked this conversation as resolved Outdated

this should account for hex digits

this should account for hex digits
self.advance();
}
T::Number
}
b'0' if self.advance_if(b'b') => {
while let Some(b'0'..=b'9') = self.peek() {
mlokis marked this conversation as resolved Outdated

should only accept [01]

should only accept `[01]`
self.advance();
}
T::Number
}
b'0'..=b'9' => { b'0'..=b'9' => {
while let Some(b'0'..=b'9') = self.peek() { while let Some(b'0'..=b'9') = self.peek() {
mlokis marked this conversation as resolved Outdated

same here but octal

same here but octal
self.advance(); self.advance();

View file

@ -391,9 +391,18 @@ impl<'a, 'b> Parser<'a, 'b> {
}, },
T::Number => E::Number { T::Number => E::Number {
pos: token.start, pos: token.start,
value: match self.lexer.slice(token.range()).parse() { value: {
let slice = self.lexer.slice(token.range());
let (slice, radix) = match &slice.get(0..2) {
Some("0x") => (slice.trim_start_matches("0x"), 16),
mlokis marked this conversation as resolved Outdated

the radixes are wrong

the radixes are wrong
Some("0b") => (slice.trim_start_matches("0b"), 16),
Some("0o") => (slice.trim_start_matches("0o"), 16),
_ => (slice, 10),
};
match u64::from_str_radix(slice, radix) {
Ok(value) => value, Ok(value) => value,
Err(e) => self.report(format_args!("invalid number: {e}")), Err(e) => self.report(format_args!("invalid number: {e}")),
}
}, },
}, },
T::LParen => { T::LParen => {