Match all instructions with variable argument

rewrite/grammar-implicit-whitespace
Raymon Zutekouw 3 years ago
parent b4d7130c8c
commit 257942e5dc
No known key found for this signature in database
GPG Key ID: 1E9BCC39EDD1DD53
  1. 29
      src/grammars/mblf.pest
  2. 50
      src/main.rs

@ -1,7 +1,34 @@
operator = @{ "operator" } // TODO
operand = @{ "operand" } // TODO
instruction = { operator ~ (spacing+ ~ operand)? }
variable =@{ ASCII_ALPHA
~ (ASCII_ALPHANUMERIC | " ")*
}
var =@{ "var" ~ spacing+ ~ variable }
delvar =@{ "delvar" ~ spacing+ ~ variable }
point =@{ "point" ~ spacing+ ~ variable }
pointm =@{ "pointm" ~ spacing+ ~ variable }
addv =@{ "addv" ~ spacing+ ~ variable }
subv =@{ "subv" ~ spacing+ ~ variable }
copy =@{ "copy" ~ spacing+ ~ variable }
instruction =_{ var
| delvar
| point
| pointm
| addv
| subv
| copy
| operator ~ (spacing+ ~ operand)?
}
loopBlockStart = { "[" ~ NEWLINE }
loopBlockEnd = { "]" }

@ -24,12 +24,56 @@ struct Cli {
output_file: std::path::PathBuf,
}
fn extract_operand(statement: Pair<Rule>) -> &str {
let mut line = statement.as_str();
while let Some(c) = line.chars().next() {
line = &line[c.len_utf8()..];
if c == ' ' {
break;
}
}
line
}
fn instruct(statement: Pair<Rule>, out: &mut Builder) {
match statement.as_rule() {
Rule::instruction => {
for nested_statement in statement.into_inner() {
instruct(nested_statement, out);
Rule::var => {
let variable_name = extract_operand(statement);
println!("Creation of variable '{}'", variable_name);
out.append("var\n");
}
Rule::delvar => {
let variable_name = extract_operand(statement);
println!("Deletion of variable '{}'", variable_name);
out.append("delvar\n");
}
Rule::point => {
let variable_name = extract_operand(statement);
println!("Pointing to variable '{}'", variable_name);
out.append("point\n");
}
Rule::pointm => {
let variable_name = extract_operand(statement);
println!("Pointing back to marker variable {}", variable_name);
out.append("pointm\n");
}
Rule::addv => {
let variable_name = extract_operand(statement);
println!("Addition to variable '{}'", variable_name);
out.append("addv\n");
}
Rule::subv => {
let variable_name = extract_operand(statement);
println!("Subtraction from variable '{}'", variable_name);
out.append("subv\n");
}
Rule::copy => {
let variable_name = extract_operand(statement);
println!("Copy to variable '{}'", variable_name);
out.append("copy\n");
}
Rule::instruction => {
out.append("\n");
}
Rule::operator => {

Loading…
Cancel
Save