You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.6 KiB
Rust

use ledstrip_vm::registers::get_register_code_by_name;
use ledstrip_vm::tokens::{
AddToken, ClearToken, CmdToken, CopyToken, DebugToken, DivToken, ExitToken, GotoToken, JeToken,
JgToken, JlToken, LabelToken, LoadToken, LshToken, ModToken, MulToken, PauseToken, RshToken,
SendToken, SetToken, SubToken, Token, WriteToken,
5 years ago
};
use std::fs::{read_to_string, File};
use std::io;
use std::io::{BufWriter, Write};
use std::num::ParseIntError;
5 years ago
use structopt::StructOpt;
macro_rules! some_box {
($expr:expr) => {
Some(Box::new($expr))
};
}
#[derive(StructOpt, Debug)]
struct Opts {
#[structopt(short = "i", name = "input")]
input_file: String,
#[structopt(short = "o", name = "output")]
output_file: String,
}
fn main() -> io::Result<()> {
let opts: Opts = Opts::from_args();
let input_file_name = opts.input_file;
let contents = read_to_string(&input_file_name)?;
5 years ago
let f = File::create(opts.output_file)?;
let mut writer = BufWriter::new(f);
let mut line_number = 0;
5 years ago
contents
.lines()
.filter_map(|line| {
line_number += 1;
if let Some(token) = get_token(line) {
Some(token.to_bytecode())
} else if line.replace("\\s", "").len() > 0 && !line.starts_with("#") {
println!(
"Failed to parse instruction '{}' \n-> {}:{}",
line, &input_file_name, line_number
);
None
} else {
None
}
})
5 years ago
.for_each(|code| {
writer.write(&code).expect("Failed to write output.");
5 years ago
});
writer.flush()?;
5 years ago
Ok(())
}
/// Parses the line into a token
5 years ago
fn get_token(line: &str) -> Option<Box<dyn Token>> {
let mut instr_parts = line.split_whitespace();
match instr_parts.next()? {
"exit" => some_box!(ExitToken {
register: get_register_code_by_name(instr_parts.next()?)?,
}),
5 years ago
"set" => some_box!(SetToken {
value: parse_value(instr_parts.next()?).expect(&format!(
"Failed to parse the hex value into a u8: {}.",
line
)),
register: get_register_code_by_name(instr_parts.next()?)?,
5 years ago
}),
"copy" => some_box!(CopyToken {
register_1: get_register_code_by_name(instr_parts.next()?)?,
register_2: get_register_code_by_name(instr_parts.next()?)?,
5 years ago
}),
"load" => some_box!(LoadToken),
"clear" => some_box!(ClearToken {
register: get_register_code_by_name(instr_parts.next()?)?,
5 years ago
}),
"write" => some_box!(WriteToken),
"label" => some_box!(LabelToken),
"goto" => some_box!(GotoToken),
"debug" => some_box!(DebugToken),
5 years ago
"add" => some_box!(AddToken),
"sub" => some_box!(SubToken),
"mul" => some_box!(MulToken),
"div" => some_box!(DivToken),
"mod" => some_box!(ModToken),
"lsh" => some_box!(LshToken),
"rsh" => some_box!(RshToken),
"jg" => some_box!(JgToken),
"jl" => some_box!(JlToken),
"je" => some_box!(JeToken),
"pause" => some_box!(PauseToken),
"cmd" => some_box!(CmdToken),
"send" => some_box!(SendToken),
5 years ago
_ => None,
}
}
/// Parses a value depending on if it starts with 0x (as a hex value)
/// or just is a plain base-10 number
fn parse_value(value: &str) -> Result<u8, ParseIntError> {
if value.starts_with("0x") {
let value = value.trim_start_matches("0x");
Ok(i64::from_str_radix(value, 16)? as u8)
} else {
value.parse::<u8>()
}
}