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

145 lines
4.7 KiB
Rust
Raw Normal View History

2022-03-12 08:01:20 -06:00
use std::{fs, io::Write, process::Command, path::PathBuf};
use clap::Parser as ArgParser;
2022-03-07 02:15:43 -06:00
use lexer::lex;
use parser::parse;
2022-03-11 17:35:14 -06:00
use diagnostic::Diagnostics;
2022-03-06 14:45:09 -06:00
use hir::ast_to_ir;
2022-03-23 00:45:35 -05:00
use typecheck::check;
2022-03-15 19:36:39 -05:00
use codegen::ts;
pub mod args;
use args::{Args, Options};
pub mod util;
use crate::util::log;
fn main() {
let args = Args::parse();
match args.options {
Options::Compile {
input: file_name,
2022-03-12 16:00:42 -06:00
ast: print_ast,
2022-03-07 03:48:36 -06:00
log: should_log,
2022-03-15 19:36:39 -05:00
output: _output, // TODO: Custom output file
} => {
2022-03-07 03:48:36 -06:00
// Macro to only log if `should_log` is true
macro_rules! logif {
($level:expr, $msg:expr) => { if should_log { log($level, $msg); } };
}
2022-03-07 02:15:43 -06:00
// Start timer
let start = std::time::Instant::now();
// Get file contents
2022-03-07 03:48:36 -06:00
logif!(0, format!("Reading {}", &file_name.display()));
let src = fs::read_to_string(&file_name).expect("Failed to read file");
2022-03-07 02:15:43 -06:00
// Lex the file
let (tokens, lex_error) = lex(src.clone());
2022-03-06 09:50:23 -06:00
let (ast, parse_error) = parse(tokens.unwrap(), src.chars().count());
2022-03-11 17:35:14 -06:00
let mut diagnostics = Diagnostics::new();
for err in lex_error { diagnostics.add_lex_error(err); }
for err in parse_error { diagnostics.add_parse_error(err); }
2022-03-11 18:35:16 -06:00
// Report syntax errors if any
2022-03-11 17:35:14 -06:00
if diagnostics.has_error() {
diagnostics.display(src);
logif!(0, "Epic parsing fail");
std::process::exit(1);
} else {
logif!(0, format!("Parsing took {}ms", start.elapsed().as_millis()));
}
2022-03-06 09:50:23 -06:00
match ast {
Some(ast) => {
2022-03-07 02:15:43 -06:00
// Convert the AST to HIR
2022-03-11 17:35:14 -06:00
let (ir, lowering_error) = ast_to_ir(ast);
for err in lowering_error { diagnostics.add_lowering_error(err); }
2022-03-11 18:35:16 -06:00
2022-03-23 00:45:35 -05:00
if print_ast { log(0, format!("IR\n{:#?}", ir)); }
// Typecheck the HIR
match check(&ir) {
2022-03-23 19:07:24 -05:00
Ok(_) => {
logif!(0, format!("Typechecking took {}ms", start.elapsed().as_millis()));
},
Err(errs) => {
for err in errs {
diagnostics.add_typecheck_error(err);
}
2022-03-23 00:45:35 -05:00
diagnostics.display(src);
2022-03-23 19:07:24 -05:00
logif!(2, "Typechecking failed");
2022-03-23 00:45:35 -05:00
std::process::exit(1);
}
}
2022-03-12 16:00:42 -06:00
2022-03-11 18:35:16 -06:00
// Report lowering errors if any
2022-03-11 17:35:14 -06:00
if diagnostics.has_error() {
diagnostics.display(src);
logif!(0, "Epic Lowering(HIR) fail");
std::process::exit(1);
} else {
logif!(0, format!("Lowering took {}ms", start.elapsed().as_millis()));
}
2022-03-07 02:15:43 -06:00
// Generate code
2022-03-15 19:36:39 -05:00
let mut codegen = ts::Codegen::new();
2022-03-07 02:15:43 -06:00
codegen.gen(ir);
2022-03-07 03:48:36 -06:00
logif!(0, "Successfully generated code.");
2022-03-07 02:15:43 -06:00
// Write code to file
2022-03-15 19:36:39 -05:00
let output_path: PathBuf = file_name.with_extension("ts").file_name().unwrap().to_os_string().into();
2022-03-07 03:48:36 -06:00
let mut file = fs::File::create(&output_path).expect("Failed to create file");
2022-03-07 02:15:43 -06:00
file.write_all(codegen.emitted.as_bytes()).expect("Failed to write to file");
2022-03-11 18:35:16 -06:00
// End timer
let duration = start.elapsed().as_millis();
logif!(0, format!("Compilation took {}ms", duration));
logif!(0, format!("Wrote output to `{}`", output_path.display()));
2022-03-06 09:50:23 -06:00
},
2022-03-11 18:14:01 -06:00
None => { unreachable!(); }
}
}
}
}
2022-03-23 19:15:23 -05:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lexer() {
let src = "
let x: int = 1;
";
let (tokens, lex_error) = lex(src.to_string());
assert!(lex_error.is_empty());
assert_eq!(tokens.unwrap().len(), 7);
}
#[test]
fn test_parser() {
let src = "
fun main (foo: int) (bar: bool): string = do
do
let x: int = foo + 1;
end;
let y: bool = bar;
end;
";
let (tokens, lex_error) = lex(src.to_string());
assert!(lex_error.is_empty());
let (ast, parse_error) = parse(tokens.unwrap(), src.chars().count());
assert!(parse_error.is_empty());
assert!(ast.is_some());
}
}