Extend numeric operations to constant operations

- Besides decNums and hexNums, characters are also valid
rewrite/grammar-implicit-whitespace
Raymon Zutekouw 3 years ago
parent eb61fa5cef
commit ae50ad64f4
No known key found for this signature in database
GPG Key ID: 1E9BCC39EDD1DD53
  1. 12
      src/grammars/mblf.pest
  2. 31
      src/main.rs

@ -5,9 +5,11 @@ variable =@{ ASCII_ALPHA
~ (ASCII_ALPHANUMERIC | " ")* ~ (ASCII_ALPHANUMERIC | " ")*
} }
char =_{ "\"" ~ ANY ~ "\"" }
decNum =_{ ASCII_DIGIT+ } decNum =_{ ASCII_DIGIT+ }
hexNum =_{ "0x" ~ ASCII_HEX_DIGIT+ } hexNum =_{ "0x" ~ ASCII_HEX_DIGIT+ }
num = { hexNum | decNum } num =_{ hexNum | decNum }
constant = { char | num }
var =@{ "var" ~ spacing+ ~ variable } var =@{ "var" ~ spacing+ ~ variable }
delvar =@{ "delvar" ~ spacing+ ~ variable } delvar =@{ "delvar" ~ spacing+ ~ variable }
@ -15,12 +17,12 @@ delvar =@{ "delvar" ~ spacing+ ~ variable }
point =@{ "point" ~ spacing+ ~ variable } point =@{ "point" ~ spacing+ ~ variable }
pointm =@{ "pointm" ~ spacing+ ~ variable } pointm =@{ "pointm" ~ spacing+ ~ variable }
add =@{ "add" ~ spacing+ ~ num } add =@{ "add" ~ spacing+ ~ constant }
addb =@{ "addb" ~ spacing+ ~ num } addb =@{ "addb" ~ spacing+ ~ constant }
addv =@{ "addv" ~ spacing+ ~ variable } addv =@{ "addv" ~ spacing+ ~ variable }
sub =@{ "sub" ~ spacing+ ~ num } sub =@{ "sub" ~ spacing+ ~ constant }
subb =@{ "subb" ~ spacing+ ~ num } subb =@{ "subb" ~ spacing+ ~ constant }
subv =@{ "subv" ~ spacing+ ~ variable } subv =@{ "subv" ~ spacing+ ~ variable }
copy =@{ "copy" ~ spacing+ ~ variable } copy =@{ "copy" ~ spacing+ ~ variable }

@ -36,8 +36,11 @@ fn extract_operand(statement: Pair<Rule>) -> &str {
line line
} }
fn parse_num(text: &str) -> Result<i32, std::num::ParseIntError> { fn parse_constant(text: &str) -> Result<i32, std::num::ParseIntError> {
if text.starts_with("0x") { if text.starts_with("\"") {
let c = text.chars().nth(1).unwrap();
Ok(c as i32)
} else if text.starts_with("0x") {
let without_prefix = text.trim_start_matches("0x"); let without_prefix = text.trim_start_matches("0x");
i32::from_str_radix(without_prefix, 16) i32::from_str_radix(without_prefix, 16)
} else { } else {
@ -68,20 +71,20 @@ fn instruct(statement: Pair<Rule>, out: &mut Builder) {
out.append("pointm\n"); out.append("pointm\n");
} }
Rule::add => { Rule::add => {
let number = extract_operand(statement); let constant = extract_operand(statement);
let number_parsed = parse_num(number).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Addition of '{}', decimal value is {}", "Addition of '{}', decimal value is {}",
number, number_parsed constant, constant_parsed
); );
out.append("add\n"); out.append("add\n");
} }
Rule::addb => { Rule::addb => {
let number = extract_operand(statement); let constant = extract_operand(statement);
let number_parsed = parse_num(number).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Big Addition of '{}', decimal value is {}", "Big Addition of '{}', decimal value is {}",
number, number_parsed constant, constant_parsed
); );
out.append("addb\n"); out.append("addb\n");
} }
@ -91,20 +94,20 @@ fn instruct(statement: Pair<Rule>, out: &mut Builder) {
out.append("addv\n"); out.append("addv\n");
} }
Rule::sub => { Rule::sub => {
let number = extract_operand(statement); let constant = extract_operand(statement);
let number_parsed = parse_num(number).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Subtraction of '{}', decimal value is {}", "Subtraction of '{}', decimal value is {}",
number, number_parsed constant, constant_parsed
); );
out.append("sub\n"); out.append("sub\n");
} }
Rule::subb => { Rule::subb => {
let number = extract_operand(statement); let constant = extract_operand(statement);
let number_parsed = parse_num(number).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Big Subtraction of '{}', decimal value is {}", "Big Subtraction of '{}', decimal value is {}",
number, number_parsed constant, constant_parsed
); );
out.append("subb\n"); out.append("subb\n");
} }

Loading…
Cancel
Save