From 781c40ede04ae9da9ca3029d712173ba816a74f0 Mon Sep 17 00:00:00 2001 From: koniifer Date: Sun, 1 Sep 2024 18:42:04 +0100 Subject: [PATCH] support hex, octal, binary literals --- hblang/src/lexer.rs | 12 ++++++++++++ hblang/src/parser.rs | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/hblang/src/lexer.rs b/hblang/src/lexer.rs index b103860d..8f36d695 100644 --- a/hblang/src/lexer.rs +++ b/hblang/src/lexer.rs @@ -292,6 +292,18 @@ impl<'a> Lexer<'a> { let kind = match c { ..=b' ' => continue, + b'0' if self.advance_if(b'x') => { + while let Some(b'0'..=b'9') = self.peek() { + self.advance(); + } + T::Number + } + b'0' if self.advance_if(b'b') => { + while let Some(b'0'..=b'9') = self.peek() { + self.advance(); + } + T::Number + } b'0'..=b'9' => { while let Some(b'0'..=b'9') = self.peek() { self.advance(); diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index 59fe5f8e..6908f3bd 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -391,9 +391,18 @@ impl<'a, 'b> Parser<'a, 'b> { }, T::Number => E::Number { pos: token.start, - value: match self.lexer.slice(token.range()).parse() { - Ok(value) => value, - Err(e) => self.report(format_args!("invalid number: {e}")), + value: { + let slice = self.lexer.slice(token.range()); + let (slice, radix) = match &slice.get(0..2) { + Some("0x") => (slice.trim_start_matches("0x"), 16), + 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, + Err(e) => self.report(format_args!("invalid number: {e}")), + } }, }, T::LParen => {