Skip to content

Commit

Permalink
Refactored and cleaned up command-line command parsing and invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Dec 9, 2024
1 parent 5644a63 commit 11ae60f
Show file tree
Hide file tree
Showing 26 changed files with 421 additions and 376 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ humansize = "2.1.3"
thousands = "0.2.0"
path-absolutize = { version = "3.1.1", features = ["once_cell_cache"] }
ordered-float = "4.5.0"
enum_dispatch = "0.3.13"

151 changes: 0 additions & 151 deletions src/cli.rs

This file was deleted.

105 changes: 105 additions & 0 deletions src/cli/build/invoke.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use super::BuildCommand;
use crate::{
c,
cli::CliInvoke,
compiler::Compiler,
diagnostics::{DiagnosticFlags, Diagnostics, WarningDiagnostic},
single_file_only::compile_single_file_only,
source_files::SourceFiles,
target::{Target, TargetArch, TargetOs},
text::{IntoText, IntoTextStream},
unerror::unerror,
workspace::compile_workspace,
};
use std::{fs::metadata, path::Path};

impl CliInvoke for BuildCommand {
fn invoke(self) -> Result<(), ()> {
let BuildCommand { filename, options } = self;
let source_files = SourceFiles::new();
let filepath = Path::new(&filename);
let diagnostics = Diagnostics::new(&source_files, DiagnosticFlags::default());
let target = options.target;

let Ok(metadata) = metadata(filepath) else {
eprintln!("error: File or folder does not exist");
return Err(());
};

ensure_supported_target(&target, &diagnostics);

let mut compiler = Compiler {
options,
source_files: &source_files,
diagnostics: &diagnostics,
version: Default::default(),
link_filenames: Default::default(),
link_frameworks: Default::default(),
};

if metadata.is_dir() {
compile_workspace(&mut compiler, filepath, None)
} else if filepath.extension().unwrap_or_default() == "h" {
compile_header(&compiler, filepath)
} else {
compile_single_file_only(&mut compiler, filepath.parent().unwrap(), filepath)
}
}
}

fn ensure_supported_target(target: &Target, diagnostics: &Diagnostics) {
if target.arch().is_none() {
diagnostics.push(WarningDiagnostic::plain(
"Target architecture is not supported, falling back to best guess",
));
}

if target.os().is_none() {
diagnostics.push(WarningDiagnostic::plain(
"Target os is not supported, falling back to best guess",
));
}

match target.os().zip(target.arch()) {
Some((TargetOs::Windows, TargetArch::X86_64)) => (),
Some((TargetOs::Windows, TargetArch::Aarch64)) => (),
Some((TargetOs::Mac, TargetArch::X86_64)) => (),
Some((TargetOs::Mac, TargetArch::Aarch64)) => (),
Some((TargetOs::Linux, TargetArch::X86_64)) => (),
Some((TargetOs::Linux, TargetArch::Aarch64)) => (),
Some((TargetOs::FreeBsd, TargetArch::X86_64)) => (),
None => (),
#[allow(unreachable_patterns)]
_ => {
diagnostics.push(WarningDiagnostic::plain(
"Host os/architecture configuration is not officially supported, taking best guess",
));
}
}
}

fn compile_header(compiler: &Compiler, filepath: &Path) -> Result<(), ()> {
let source_files = compiler.source_files;

let content = std::fs::read_to_string(filepath).map_err(|err| {
eprintln!("{}", err);
()
})?;

let header_key = source_files.add(filepath.into(), content);

let header_contents = source_files
.get(header_key)
.content()
.chars()
.into_text_stream(header_key)
.into_text();

let preprocessed = unerror(
c::preprocessor::preprocess(header_contents, &compiler.diagnostics),
&source_files,
)?;

println!("{preprocessed:?}");
return Ok(());
}
11 changes: 11 additions & 0 deletions src/cli/build/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod invoke;
mod options;
mod parse;

pub use options::BuildOptions;

#[derive(Clone, Debug)]
pub struct BuildCommand {
pub filename: String,
pub options: BuildOptions,
}
39 changes: 39 additions & 0 deletions src/cli/build/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::target::Target;
use std::path::PathBuf;

#[derive(Clone, Debug)]
pub struct BuildOptions {
pub emit_llvm_ir: bool,
pub emit_ir: bool,
pub interpret: bool,
pub coerce_main_signature: bool,
pub excute_result: bool,
pub allow_experimental_pragma_features: bool,
pub use_pic: Option<bool>,
pub target: Target,
pub infrastructure: Option<PathBuf>,
}

impl Default for BuildOptions {
fn default() -> Self {
let current_exe = std::env::current_exe()
.expect("failed to get adept executable location")
.parent()
.expect("parent folder")
.to_path_buf();

let infrastructure = current_exe.join("infrastructure");

Self {
emit_llvm_ir: false,
emit_ir: false,
interpret: false,
coerce_main_signature: true,
excute_result: false,
allow_experimental_pragma_features: false,
use_pic: None,
target: Target::HOST,
infrastructure: Some(infrastructure),
}
}
}
Loading

0 comments on commit 11ae60f

Please sign in to comment.