1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00
bobbylisp/blspc/src/main.rs

75 lines
2.4 KiB
Rust
Raw Normal View History

2022-01-25 22:05:42 -06:00
use std::{fs::{read_to_string, File}, path::Path, io::Write, time::Instant};
2022-01-24 14:08:41 -06:00
use structopt::StructOpt;
mod args;
use args::Args;
2022-01-21 17:43:50 -06:00
mod util;
2022-01-23 15:42:08 -06:00
use util::cover_paren;
2022-01-21 17:43:50 -06:00
mod parser;
2022-01-24 14:08:41 -06:00
use parser::{tokenize, Parser};
2022-01-21 17:43:50 -06:00
2022-01-25 19:45:59 -06:00
mod compiler;
use compiler::compile::Compiler;
2022-01-25 16:06:57 -06:00
2022-01-21 17:43:50 -06:00
fn main() {
2022-01-25 22:05:42 -06:00
let start = Instant::now();
2022-01-24 14:08:41 -06:00
let args = Args::from_args();
2022-01-23 15:42:08 -06:00
2022-01-24 14:08:41 -06:00
let src = cover_paren(read_to_string(&args.file).unwrap());
2022-01-25 16:06:57 -06:00
let file_name = Path::new(&args.file).file_stem().unwrap().to_str().unwrap();
2022-01-23 15:42:08 -06:00
2022-01-24 14:08:41 -06:00
let tokens = tokenize(&src);
let mut parser = Parser::new(tokens.clone());
2022-01-23 15:42:08 -06:00
let result = parser.parse();
2022-01-24 14:08:41 -06:00
match args.verbose {
2022-01-25 16:06:57 -06:00
0 => {
let mut file = File::create(format!("{}.bbb", file_name)).unwrap();
2022-01-25 19:31:11 -06:00
let mut compiler = Compiler::new();
2022-01-25 22:05:42 -06:00
let before = Instant::now();
2022-01-25 21:23:36 -06:00
for instr in compiler.compile(result.unwrap(), 0).unwrap() {
2022-01-25 19:31:11 -06:00
write!(file, "{}\n", instr).unwrap();
2022-01-25 16:06:57 -06:00
}
2022-01-25 22:05:42 -06:00
let spent = before.elapsed();
let total = start.elapsed();
println!("Compiled in {}.{}s, Total of {}.{}s", spent.as_secs(), spent.subsec_millis(), total.as_secs(), total.subsec_millis());
},
1 => {
println!("Parsed AST: {:#?}", result);
let mut file = File::create(format!("{}.bbb", file_name)).unwrap();
let mut compiler = Compiler::new();
let before = Instant::now();
for instr in compiler.compile(result.unwrap(), 0).unwrap() {
write!(file, "{}\n", instr).unwrap();
}
let spent = before.elapsed();
let total = start.elapsed();
println!("Compiled in {}.{}s, Total of {}.{}s", spent.as_secs(), spent.subsec_millis(), total.as_secs(), total.subsec_millis());
2022-01-25 16:06:57 -06:00
},
2022-01-24 14:08:41 -06:00
2 | _ => {
println!("Tokens: {:?}", tokens);
2022-01-25 22:05:42 -06:00
println!("Parsed AST: {:#?}", result);
let mut file = File::create(format!("{}.bbb", file_name)).unwrap();
let mut compiler = Compiler::new();
let before = Instant::now();
for instr in compiler.compile(result.unwrap(), 0).unwrap() {
write!(file, "{}\n", instr).unwrap();
}
let spent = before.elapsed();
let total = start.elapsed();
println!("Compiled in {}.{}s, Total of {}.{}s", spent.as_secs(), spent.subsec_millis(), total.as_secs(), total.subsec_millis());
2022-01-24 14:08:41 -06:00
}
}
2022-01-21 17:43:50 -06:00
}