From 492e8d51a1ac916be1c5731e1b83c975c095878b Mon Sep 17 00:00:00 2001 From: Ry Date: Sat, 12 Mar 2022 11:58:00 -0800 Subject: [PATCH] fox32asm: Allow underscores in immediate values Co-authored-by: Lua --- src/fox32.pest | 6 +++--- src/main.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/fox32.pest b/src/fox32.pest index bb4fcaa..5157327 100644 --- a/src/fox32.pest +++ b/src/fox32.pest @@ -122,13 +122,13 @@ instruction_two = @{ } immediate_bin = ${ "0b" ~ body_bin } -body_bin = @{ ASCII_BIN_DIGIT+ } +body_bin = @{ (ASCII_BIN_DIGIT | "_")+ } immediate_hex = ${ "0x" ~ body_hex } -body_hex = @{ ASCII_HEX_DIGIT+ } +body_hex = @{ (ASCII_HEX_DIGIT | "_")+ } immediate_dec = ${ body_dec } -body_dec = @{ ASCII_DIGIT+ } +body_dec = @{ (ASCII_DIGIT | "_")+ } immediate_char = ${ "'" ~ body_char ~ "'" } body_char = @{ '\x00'..'\x7F' } diff --git a/src/main.rs b/src/main.rs index 9c02e63..1519850 100644 --- a/src/main.rs +++ b/src/main.rs @@ -602,6 +602,10 @@ fn parse_instruction(pair: pest::iterators::Pair) -> AstNode { } } +fn remove_underscores(input: &str) -> String { + String::from_iter(input.chars().filter(|c| *c != '_')) +} + fn parse_operand(mut pair: pest::iterators::Pair, is_pointer: bool) -> AstNode { //println!("parse_operand: {:#?}", pair); // debug let size = *CURRENT_SIZE.lock().unwrap(); @@ -616,7 +620,7 @@ fn parse_operand(mut pair: pest::iterators::Pair, is_pointer: bool) -> Ast match operand_value_pair.as_rule() { Rule::immediate_bin => { let body_bin_str = operand_value_pair.into_inner().next().unwrap().as_str(); - let immediate = u32::from_str_radix(body_bin_str, 2).unwrap(); + let immediate = u32::from_str_radix(&remove_underscores(body_bin_str), 2).unwrap(); if is_pointer { AstNode::ImmediatePointer(immediate) } else { @@ -629,7 +633,7 @@ fn parse_operand(mut pair: pest::iterators::Pair, is_pointer: bool) -> Ast } Rule::immediate_hex => { let body_hex_str = operand_value_pair.into_inner().next().unwrap().as_str(); - let immediate = u32::from_str_radix(body_hex_str, 16).unwrap(); + let immediate = u32::from_str_radix(&remove_underscores(body_hex_str), 16).unwrap(); if is_pointer { AstNode::ImmediatePointer(immediate) } else { @@ -642,7 +646,7 @@ fn parse_operand(mut pair: pest::iterators::Pair, is_pointer: bool) -> Ast } Rule::immediate_dec => { let body_dec_str = operand_value_pair.into_inner().next().unwrap().as_str(); - let immediate = body_dec_str.parse::().unwrap(); + let immediate = remove_underscores(body_dec_str).parse::().unwrap(); if is_pointer { AstNode::ImmediatePointer(immediate) } else {