Skip to content

Commit 8e25cee

Browse files
committed
feat: added AstoIR build target
1 parent 932041f commit 8e25cee

4 files changed

Lines changed: 80 additions & 5 deletions

File tree

compiler/compiler_main/src/cli.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::{Path, PathBuf};
2+
13
use clap::{Parser, Subcommand, ValueEnum};
24

35
#[derive(Parser)]
@@ -34,7 +36,7 @@ pub enum CLICommand {
3436
#[command(visible_alias = "b", about = "Builds the given file(s)")]
3537
Build {
3638
#[arg(short = 'o')]
37-
out: String,
39+
out: PathBuf,
3840

3941
#[arg(long, value_enum, default_value = "llvm")]
4042
platform: Platform,
@@ -46,7 +48,7 @@ pub enum CLICommand {
4648
linker: String,
4749

4850
#[arg(required = true)]
49-
input: Vec<String>,
51+
input: Vec<PathBuf>,
5052
},
5153

5254
#[command(visible_alias = "ver", about = "Displays the version")]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::{fs, path::PathBuf};
2+
3+
use ast_parser::parse_ast_ctx;
4+
use astoir::run_astoir_mir;
5+
use lexer::lexer::lexer_parse_file;
6+
7+
use crate::quietlyquit_if_errors;
8+
9+
pub fn build_mir(path: String, out: PathBuf) {
10+
let lexer = lexer_parse_file(&path);
11+
quietlyquit_if_errors!();
12+
13+
let ast = parse_ast_ctx(&lexer.unwrap());
14+
quietlyquit_if_errors!();
15+
16+
let mir = run_astoir_mir(ast.unwrap());
17+
quietlyquit_if_errors!();
18+
19+
fs::write(out, format!("{}", mir.unwrap())).unwrap();
20+
}

compiler/compiler_main/src/cmds/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod build;
12
pub mod check;
23

34
#[macro_export]
@@ -9,3 +10,11 @@ macro_rules! quietlyquit_if_errors {
910
}
1011
};
1112
}
13+
14+
#[macro_export]
15+
macro_rules! soft_panic {
16+
($lit:literal) => {
17+
println!($lit);
18+
std::process::exit(445)
19+
};
20+
}

compiler/compiler_main/src/main.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
use std::time::Instant;
1+
use std::{
2+
fs,
3+
path::{Path, PathBuf},
4+
process::Output,
5+
time::Instant,
6+
};
27

38
use clap::Parser;
49

510
use crate::{
6-
cli::{CLICommand, Cli},
7-
cmds::check::run_check,
11+
cli::{CLICommand, Cli, OutputFormat, Platform},
12+
cmds::{build::build_mir, check::run_check},
813
version::{GIT_HASH, VERSION},
914
};
1015

@@ -35,6 +40,45 @@ fn main() {
3540
)
3641
}
3742

43+
CLICommand::Build {
44+
out,
45+
platform,
46+
format,
47+
linker,
48+
input,
49+
} => {
50+
let needs_dir = input.len() > 1 && format != OutputFormat::Executable;
51+
52+
if input.len() > 1 && out.extension().is_some() && format != OutputFormat::Executable {
53+
soft_panic!(
54+
"Output must be a directory if theres more than one input and that the target isn't an executable"
55+
);
56+
}
57+
58+
if needs_dir && !out.exists() {
59+
fs::create_dir_all(out.clone()).unwrap();
60+
}
61+
62+
if platform == Platform::AstoIR && format != OutputFormat::IR {
63+
soft_panic!("Only IR target is supported by AstoIR platform!");
64+
}
65+
66+
match platform {
67+
Platform::AstoIR => {
68+
for i in input {
69+
let mut outfile = PathBuf::from(i.file_name().unwrap());
70+
outfile.add_extension("air");
71+
72+
let output_path = out.join(outfile);
73+
74+
build_mir(i.to_str().unwrap().to_string(), output_path);
75+
}
76+
}
77+
78+
_ => todo!(),
79+
}
80+
}
81+
3882
_ => todo!(),
3983
}
4084
}

0 commit comments

Comments
 (0)