Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added clap crate for CLI #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
197 changes: 195 additions & 2 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 compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Expand Down
65 changes: 28 additions & 37 deletions compiler/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clap::{Arg, ArgMatches, Command};
use compiler::ast::{Expr, FileId};
use compiler::codegen::EmittedFile;
use compiler::global_state::Module;
Expand All @@ -6,6 +7,33 @@ use compiler::{codegen, fs, infer, lexer, parser, prelude};

use serde::Deserialize;

fn main() {
let root_cmd: Command = Command::new("borgo")
.arg_required_else_help(true)
.subcommand_required(true)
.subcommand(Command::new("build").about("Compile all .brg files into .go files"))
.subcommand(Command::new("test").arg(Arg::new("input").required(true).index(1)));

let matches = root_cmd.get_matches();
match matches.subcommand() {
Some(("build", matches)) => run_build_cmd(matches),
Some(("test", matches)) => run_test_cmd(matches),
_ => unreachable!("unmatched command"),
};
}

fn run_build_cmd(_matches: &ArgMatches) {
build_project();
println!("done");
}

fn run_test_cmd(matches: &ArgMatches) {
match matches.get_one::<String>("input") {
Some(file) => run_tests(file),
None => unreachable!("unmatched input"), // since it's a required arg we should not get here
}
}

#[derive(Deserialize, Debug)]
enum Input {
InferExpr(String),
Expand Down Expand Up @@ -95,17 +123,6 @@ fn parse_source(input: Input) {
println!("{}\n---\n{}\n---\n{}", tokens_str, err, json.unwrap());
}

fn help() {
println!(
"
Commands:

build compile all .brg files into .go files

"
);
}

fn run_tests(input: &str) {
let input: Input = serde_json::from_str(input).unwrap();

Expand Down Expand Up @@ -182,29 +199,3 @@ fn run_tests(input: &str) {
Input::ParseExpr(_) | Input::ParseFile(_) => parse_source(input),
}
}

fn main() {
// TODO use a proper parsing lib
let cmd = std::env::args().nth(1);

if cmd.is_none() {
help();
return;
}

let cmd = cmd.unwrap();

if cmd == "build" {
build_project();
println!("done");
return;
}

if cmd == "test" {
let input = std::env::args().nth(2).unwrap();
run_tests(&input);
return;
}

help();
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
profile = "default"
channel = "1.72.0"
channel = "1.74.0"