diff options
author | Vincent Ambo <mail@tazj.in> | 2021-03-15T20·44+0200 |
---|---|---|
committer | Vincent Ambo <mail@tazj.in> | 2021-03-15T20·48+0200 |
commit | b6895a5b309eca4f12062f394331fff1b9e3dff8 (patch) | |
tree | 3d916dad08acf1bad3022e63dbd082633a3550ff /users/glittershark/achilles/src/compiler.rs | |
parent | 4d193f239525954631ba9d789ca3aea9a2f4e14d (diff) | |
parent | b93268085aab14c80a400c299da5d04d2781098e (diff) |
merge(glittershark/achilles): Subtree import at 'b93268085a' r/2279
Imported from https://github.com/glittershark/achilles/ git-subtree-dir: users/glittershark/achilles git-subtree-mainline: 4d193f239525954631ba9d789ca3aea9a2f4e14d git-subtree-split: b93268085aab14c80a400c299da5d04d2781098e Change-Id: I64a583b454bbe03e20358ad7808939a4cbc212ba
Diffstat (limited to 'users/glittershark/achilles/src/compiler.rs')
-rw-r--r-- | users/glittershark/achilles/src/compiler.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/users/glittershark/achilles/src/compiler.rs b/users/glittershark/achilles/src/compiler.rs new file mode 100644 index 000000000000..f925b267df57 --- /dev/null +++ b/users/glittershark/achilles/src/compiler.rs @@ -0,0 +1,86 @@ +use std::fmt::{self, Display}; +use std::path::Path; +use std::str::FromStr; +use std::{fs, result}; + +use clap::Clap; +use test_strategy::Arbitrary; + +use crate::codegen::{self, Codegen}; +use crate::common::Result; +use crate::{parser, tc}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Arbitrary)] +pub enum OutputFormat { + LLVM, + Bitcode, +} + +impl Default for OutputFormat { + fn default() -> Self { + Self::Bitcode + } +} + +impl FromStr for OutputFormat { + type Err = String; + + fn from_str(s: &str) -> result::Result<Self, Self::Err> { + match s { + "llvm" => Ok(Self::LLVM), + "binary" => Ok(Self::Bitcode), + _ => Err(format!( + "Invalid output format {}, expected one of {{llvm, binary}}", + s + )), + } + } +} + +impl Display for OutputFormat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + OutputFormat::LLVM => f.write_str("llvm"), + OutputFormat::Bitcode => f.write_str("binary"), + } + } +} + +#[derive(Clap, Debug, PartialEq, Eq, Default)] +pub struct CompilerOptions { + #[clap(long, short = 'f', default_value)] + format: OutputFormat, +} + +pub fn compile_file(input: &Path, output: &Path, options: &CompilerOptions) -> Result<()> { + let src = fs::read_to_string(input)?; + let (_, decls) = parser::toplevel(&src)?; // TODO: statements + let decls = tc::typecheck_toplevel(decls)?; + + let context = codegen::Context::create(); + let mut codegen = Codegen::new( + &context, + &input + .file_stem() + .map_or("UNKNOWN".to_owned(), |s| s.to_string_lossy().into_owned()), + ); + for decl in &decls { + codegen.codegen_decl(decl)?; + } + match options.format { + OutputFormat::LLVM => codegen.print_to_file(output)?, + OutputFormat::Bitcode => codegen.binary_to_file(output)?, + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use test_strategy::proptest; + + #[proptest] + fn output_format_display_from_str_round_trip(of: OutputFormat) { + assert_eq!(OutputFormat::from_str(&of.to_string()), Ok(of)); + } +} |