Extract non-expanding instructions to `to_bf`

master
Raymon Zutekouw 3 years ago
parent d04ecaeef5
commit 82fcfea43a
No known key found for this signature in database
GPG Key ID: 1E9BCC39EDD1DD53
  1. 76
      src/main.rs

@ -66,25 +66,10 @@ fn parse_constant(text: &str) -> Result<i32, std::num::ParseIntError> {
} }
} }
fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) { fn to_bf(rule: Rule, operand: &str, state: &mut State, out: &mut Builder) {
match statement.as_rule() { match rule {
Rule::include => {
let file_path_raw = extract_operand(statement);
let file_path = &file_path_raw[1..file_path_raw.len() - 1];
println!("Including {} into this src file", file_path);
let content = std::fs::read_to_string(&file_path)
.with_context(|| format!("could not read source file {:?}", file_path))
.unwrap();
let parsed_file = MblfParser::parse(Rule::file, &content)
.expect("Parse Error")
.next()
.unwrap();
for statement in parsed_file.into_inner() {
instruct(statement, state, out);
}
}
Rule::var => { Rule::var => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Creation of variable '{}'", variable_name); println!("Creation of variable '{}'", variable_name);
if let Some(_v) = state.variables.insert( if let Some(_v) = state.variables.insert(
String::from(variable_name), String::from(variable_name),
@ -95,14 +80,14 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
state.alloc_cnt += 1; state.alloc_cnt += 1;
} }
Rule::delvar => { Rule::delvar => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Deletion of variable '{}'", variable_name); println!("Deletion of variable '{}'", variable_name);
if let None = state.variables.remove(&String::from(variable_name)) { if let None = state.variables.remove(&String::from(variable_name)) {
panic!("Variable '{}' did not exists", variable_name); panic!("Variable '{}' did not exists", variable_name);
} }
} }
Rule::point => { Rule::point => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Pointing to variable '{}'", variable_name); println!("Pointing to variable '{}'", variable_name);
let address = state let address = state
.variables .variables
@ -117,7 +102,7 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
state.mem_pointer = address; state.mem_pointer = address;
} }
Rule::pointm => { Rule::pointm => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Pointing back to marker variable {}", variable_name); println!("Pointing back to marker variable {}", variable_name);
let address = state let address = state
.variables .variables
@ -129,7 +114,7 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
state.mem_pointer = address; state.mem_pointer = address;
} }
Rule::add => { Rule::add => {
let constant = extract_operand(statement); let constant = operand;
let constant_parsed = parse_constant(constant).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Addition of '{}', decimal value is {}", "Addition of '{}', decimal value is {}",
@ -138,7 +123,7 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
out.append("+".repeat(constant_parsed as usize)); out.append("+".repeat(constant_parsed as usize));
} }
Rule::addb => { Rule::addb => {
let constant = extract_operand(statement); let constant = operand;
let constant_parsed = parse_constant(constant).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Big Addition of '{}', decimal value is {}", "Big Addition of '{}', decimal value is {}",
@ -147,12 +132,12 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
out.append("addb\n"); out.append("addb\n");
} }
Rule::addv => { Rule::addv => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Addition to variable '{}'", variable_name); println!("Addition to variable '{}'", variable_name);
out.append("addv\n"); out.append("addv\n");
} }
Rule::sub => { Rule::sub => {
let constant = extract_operand(statement); let constant = operand;
let constant_parsed = parse_constant(constant).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Subtraction of '{}', decimal value is {}", "Subtraction of '{}', decimal value is {}",
@ -161,7 +146,7 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
out.append("-".repeat(constant_parsed as usize)); out.append("-".repeat(constant_parsed as usize));
} }
Rule::subb => { Rule::subb => {
let constant = extract_operand(statement); let constant = operand;
let constant_parsed = parse_constant(constant).unwrap(); let constant_parsed = parse_constant(constant).unwrap();
println!( println!(
"Big Subtraction of '{}', decimal value is {}", "Big Subtraction of '{}', decimal value is {}",
@ -170,12 +155,12 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
out.append("subb\n"); out.append("subb\n");
} }
Rule::subv => { Rule::subv => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Subtraction from variable '{}'", variable_name); println!("Subtraction from variable '{}'", variable_name);
out.append("subv\n"); out.append("subv\n");
} }
Rule::copy => { Rule::copy => {
let variable_name = extract_operand(statement); let variable_name = operand;
println!("Copy to variable '{}'", variable_name); println!("Copy to variable '{}'", variable_name);
out.append("copy\n"); out.append("copy\n");
} }
@ -191,11 +176,6 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
println!("Printing current variable"); println!("Printing current variable");
out.append("."); out.append(".");
} }
Rule::loopBlock => {
for nested_statement in statement.into_inner() {
instruct(nested_statement, state, out);
}
}
Rule::loopBlockStart => { Rule::loopBlockStart => {
println!("Start of loopBlock"); println!("Start of loopBlock");
out.append("["); out.append("[");
@ -212,6 +192,32 @@ fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
} }
} }
fn instruct(statement: Pair<Rule>, state: &mut State, out: &mut Builder) {
match statement.as_rule() {
Rule::include => {
let file_path_raw = extract_operand(statement);
let file_path = &file_path_raw[1..file_path_raw.len() - 1];
println!("Including {} into this src file", file_path);
let content = std::fs::read_to_string(&file_path)
.with_context(|| format!("could not read source file {:?}", file_path))
.unwrap();
let parsed_file = MblfParser::parse(Rule::file, &content)
.expect("Parse Error")
.next()
.unwrap();
for statement in parsed_file.into_inner() {
instruct(statement, state, out);
}
}
Rule::loopBlock => {
for nested_statement in statement.into_inner() {
instruct(nested_statement, state, out);
}
}
_ => to_bf(statement.as_rule(), extract_operand(statement), state, out),
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Cli::from_args(); let args = Cli::from_args();
@ -230,8 +236,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
mem_pointer: 0, mem_pointer: 0,
variables: HashMap::new(), variables: HashMap::new(),
}; };
for statement in parsed_file.into_inner() { for stmt in parsed_file.into_inner() {
instruct(statement, &mut state, &mut builder); instruct(stmt, &mut state, &mut builder);
} }
let bf = builder.string().unwrap(); let bf = builder.string().unwrap();

Loading…
Cancel
Save