From 30a6d0929a47f1fa5c4b643a5e60108c5edd8390 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 8 Jan 2018 05:57:36 -0800 Subject: [PATCH] Starting hex parsing --- src/schala_lang/parsing.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/schala_lang/parsing.rs b/src/schala_lang/parsing.rs index cd184f1..73f961d 100644 --- a/src/schala_lang/parsing.rs +++ b/src/schala_lang/parsing.rs @@ -1003,6 +1003,19 @@ fn parse_binary(digits: String) -> ParseResult { Ok(result) } +fn parse_hex(digits: String) -> ParseResult { + let mut result: u64 = 0; + let mut multiplier: u64 = 1; + for d in digits.chars().rev() { + match d.to_digit(16) { + Some(n) => result += n as u64 * multiplier, + None => return ParseError::new("Encountered a non-hex digit in a hex literal"), + } + multiplier *= 16; + } + Ok(result) +} + pub fn parse(input: Vec) -> (Result, Vec) { let mut parser = Parser::new(input); let ast = parser.program();