fox32asm: Allow underscores in immediate values

Co-authored-by: Lua <lua@foxgirl.dev>
This commit is contained in:
Ry 2022-03-12 11:58:00 -08:00
parent 637e62ff38
commit 492e8d51a1
2 changed files with 10 additions and 6 deletions

View File

@ -122,13 +122,13 @@ instruction_two = @{
} }
immediate_bin = ${ "0b" ~ body_bin } immediate_bin = ${ "0b" ~ body_bin }
body_bin = @{ ASCII_BIN_DIGIT+ } body_bin = @{ (ASCII_BIN_DIGIT | "_")+ }
immediate_hex = ${ "0x" ~ body_hex } immediate_hex = ${ "0x" ~ body_hex }
body_hex = @{ ASCII_HEX_DIGIT+ } body_hex = @{ (ASCII_HEX_DIGIT | "_")+ }
immediate_dec = ${ body_dec } immediate_dec = ${ body_dec }
body_dec = @{ ASCII_DIGIT+ } body_dec = @{ (ASCII_DIGIT | "_")+ }
immediate_char = ${ "'" ~ body_char ~ "'" } immediate_char = ${ "'" ~ body_char ~ "'" }
body_char = @{ '\x00'..'\x7F' } body_char = @{ '\x00'..'\x7F' }

View File

@ -602,6 +602,10 @@ fn parse_instruction(pair: pest::iterators::Pair<Rule>) -> AstNode {
} }
} }
fn remove_underscores(input: &str) -> String {
String::from_iter(input.chars().filter(|c| *c != '_'))
}
fn parse_operand(mut pair: pest::iterators::Pair<Rule>, is_pointer: bool) -> AstNode { fn parse_operand(mut pair: pest::iterators::Pair<Rule>, is_pointer: bool) -> AstNode {
//println!("parse_operand: {:#?}", pair); // debug //println!("parse_operand: {:#?}", pair); // debug
let size = *CURRENT_SIZE.lock().unwrap(); let size = *CURRENT_SIZE.lock().unwrap();
@ -616,7 +620,7 @@ fn parse_operand(mut pair: pest::iterators::Pair<Rule>, is_pointer: bool) -> Ast
match operand_value_pair.as_rule() { match operand_value_pair.as_rule() {
Rule::immediate_bin => { Rule::immediate_bin => {
let body_bin_str = operand_value_pair.into_inner().next().unwrap().as_str(); 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 { if is_pointer {
AstNode::ImmediatePointer(immediate) AstNode::ImmediatePointer(immediate)
} else { } else {
@ -629,7 +633,7 @@ fn parse_operand(mut pair: pest::iterators::Pair<Rule>, is_pointer: bool) -> Ast
} }
Rule::immediate_hex => { Rule::immediate_hex => {
let body_hex_str = operand_value_pair.into_inner().next().unwrap().as_str(); 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 { if is_pointer {
AstNode::ImmediatePointer(immediate) AstNode::ImmediatePointer(immediate)
} else { } else {
@ -642,7 +646,7 @@ fn parse_operand(mut pair: pest::iterators::Pair<Rule>, is_pointer: bool) -> Ast
} }
Rule::immediate_dec => { Rule::immediate_dec => {
let body_dec_str = operand_value_pair.into_inner().next().unwrap().as_str(); let body_dec_str = operand_value_pair.into_inner().next().unwrap().as_str();
let immediate = body_dec_str.parse::<u32>().unwrap(); let immediate = remove_underscores(body_dec_str).parse::<u32>().unwrap();
if is_pointer { if is_pointer {
AstNode::ImmediatePointer(immediate) AstNode::ImmediatePointer(immediate)
} else { } else {