Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2024"

[dependencies]
inkwell = {version = "0.6.0", features = ["llvm18-1"]}
clap = { version = "4.5.48", features = ["derive"] }
inkwell = { version = "0.6.0", features = ["llvm18-1"] }
29 changes: 29 additions & 0 deletions src/cmd_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::sync::OnceLock;

use clap::Parser;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct CmdArgs {
/// Input file to process
#[arg(index = 1, value_name = "SOURCE_FILE")]
pub input_file: String,

/// Output file name
#[arg(short, long, value_name = "OUTPUT")]
pub output: Option<String>,
}

/// args singleton
static ARGS: OnceLock<CmdArgs> = OnceLock::new();

/// global read only access to args
pub fn get() -> &'static CmdArgs {
ARGS.get_or_init(|| CmdArgs::parse())
}

/// just in case you don't need args but you want to make sure they are valid and return if not
pub fn init() {
ARGS.set(CmdArgs::parse())
.expect("you are calling cmd_args::init() twice");
}
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod cmd_args;
mod code_generator;
mod errors;
mod lexer;
mod macros;
mod parser;

use inkwell::context::Context;

use crate::{
Expand All @@ -19,16 +21,9 @@ use std::{
str::FromStr,
};

// TODO: use clap for cli purposes

// WARNING: Fix all string allocations
fn main() -> Result<()> {
let input: Vec<String> = std::env::args().collect();

match input.len() {
2 => run_file(&input[1])?,
_ => println!("Usage: Catalyst [filepath]"),
}
run_file(&cmd_args::get().input_file)?;
Ok(())
}

Expand All @@ -52,7 +47,11 @@ fn run_file(path: &str) -> Result<()> {

let source_path = Path::new(path).parent().unwrap_or(Path::new("."));

let output_path = source_path.join(file_name);
let output_path = cmd_args::get()
.output
.clone()
.map(|x| PathBuf::from_str(&x).unwrap())
.unwrap_or(source_path.join(file_name));

run(&bytes_str, output_path.to_str().unwrap())?;
Ok(())
Expand Down