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 | " ")*
}
char =_{ "\"" ~ ANY ~ "\"" }
decNum =_{ ASCII_DIGIT+ }
hexNum =_{ "0x" ~ ASCII_HEX_DIGIT+ }
num = { hexNum | decNum }
num =_{ hexNum | decNum }
constant = { char | num }
var =@{ "var" ~ spacing+ ~ variable }
delvar =@{ "delvar" ~ spacing+ ~ variable }
@ -15,12 +17,12 @@ delvar =@{ "delvar" ~ spacing+ ~ variable }
point =@{ "point" ~ spacing+ ~ variable }
pointm =@{ "pointm" ~ spacing+ ~ variable }
add =@{ "add" ~ spacing+ ~ num }
addb =@{ "addb" ~ spacing+ ~ num }
add =@{ "add" ~ spacing+ ~ constant }
addb =@{ "addb" ~ spacing+ ~ constant }
addv =@{ "addv" ~ spacing+ ~ variable }
sub =@{ "sub" ~ spacing+ ~ num }
subb =@{ "subb" ~ spacing+ ~ num }
sub =@{ "sub" ~ spacing+ ~ constant }
subb =@{ "subb" ~ spacing+ ~ constant }
subv =@{ "subv" ~ spacing+ ~ variable }
copy =@{ "copy" ~ spacing+ ~ variable }

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

Loading…
Cancel
Save