futilehdl/src/main.rs

68 lines
1.9 KiB
Rust

mod builtin_cells;
mod frontend;
mod package;
mod parser;
mod rtlil;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use structopt::StructOpt;
use nom_greedyerror::convert_error;
use ariadne::Source;
#[derive(Debug, StructOpt)]
#[structopt(name = "example", about = "An example of StructOpt usage.")]
struct Opt {
/// Input file
#[structopt(parse(from_os_str))]
input: PathBuf,
/// Debug AST
#[structopt(short)]
debug: bool,
/// Output file, stdout if not present
#[structopt(short, parse(from_os_str))]
output: Option<PathBuf>,
}
fn main() {
let opt = Opt::from_args();
let mut infile = File::open(opt.input).expect("could not open file");
// let packages = package::PackageRegistry::new();
// let mut infile = packages
// .get("builtins")
// .expect("no package")
// .open()
// .expect("could not open file");
let mut input = String::new();
infile
.read_to_string(&mut input)
.expect("error reading file");
let input: &str = input.as_str();
let input = parser::Span::new(input);
let parsed = parser::parse(input);
match parsed {
Err(nom::Err::Error(err) | nom::Err::Failure(err)) => {
parser::error::convert_error(input, err).eprint(Source::from(input.fragment())).unwrap();
}
Err(_) => ( unreachable!() ),
Ok(res) => {
if opt.debug {
println!("{:#?}", res);
}
let lowered = crate::frontend::lower_module(res.1);
match lowered {
Ok(res) => {
let mut file = File::create(opt.output.unwrap_or_else(|| "out.rtlil".into()))
.expect("could not open file");
file.write_all(res.as_bytes())
.expect("failed to write output file");
}
Err(err) => eprintln!("{:#?}", err),
}
}
}
}