Merge pull request #26 from Kindelia/feat-cli

Add clap cli
This commit is contained in:
Victor Taelin 2022-07-14 20:58:27 -03:00 committed by GitHub
commit a9c2a73545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 13 deletions

1
Cargo.lock generated
View File

@ -142,6 +142,7 @@ dependencies = [
name = "kind2"
version = "0.1.0"
dependencies = [
"clap",
"hvm",
]

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
hvm = "0.1.40"
clap = { version = "3.1.8", features = ["derive"] }

20
src/cli.rs Normal file
View File

@ -0,0 +1,20 @@
pub use clap::{Parser, Subcommand};
#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
pub struct Cli {
#[clap(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
/// Run a file interpreted
#[clap(aliases = &["r"])]
Run { file: String, params: Vec<String> },
/// Check a file
#[clap(aliases = &["c"])]
Check { file: String, params: Vec<String> },
}

View File

@ -1,27 +1,42 @@
#![allow(dead_code)]
#![allow(unused_variables)]
mod cli;
mod language;
use cli::{Cli, Parser, Command};
use language::{*};
use hvm::parser as parser;
const CHECKER_HVM: &str = include_str!("checker.hvm");
fn main() -> Result<(), String> {
//gen_debug_file();
fn main() {
match run_cli() {
Ok(..) => {}
Err(err) => {
eprintln!("{}", err);
}
};
let args: Vec<String> = std::env::args().collect();
// ------------------------------------------------------------
// ------------------------------------------------------------
// ------------------------------------------------------------
if args.len() <= 2 || args[1] != "check" && args[1] != "run" {
println!("Usage:");
println!("$ kind2 check file.kind");
println!("$ kind2 run file.kind");
return Ok(());
fn run_cli() -> Result<(), String> {
let cli_matches = Cli::parse();
match cli_matches.command {
Command::Run { file: path, params } => {
kind2(&path, "Api.run_main")
}
Command::Check { file: path, params } => {
kind2(&path, "Api.check_all")
}
}
}
let path = &args[2];
fn kind2(path: &str, main_function: &str) -> Result<(), String> {
// Reads definitions from file
let file = match std::fs::read_to_string(path) {
Ok(code) => read_file(&code),
@ -47,7 +62,6 @@ fn main() -> Result<(), String> {
//println!("[{}]\n{}\n", name, show_entry(entry));
//}
//
let code = compile_file(&file);
let mut checker = (&CHECKER_HVM[0 .. CHECKER_HVM.find("////INJECT////").unwrap()]).to_string();
checker.push_str(&code);
@ -57,14 +71,14 @@ fn main() -> Result<(), String> {
// Runs with the interpreter
let mut rt = hvm::Runtime::from_code(&checker)?;
let main = rt.alloc_code(if args[1] == "check" { "Api.check_all" } else { "Api.run_main" })?;
let main = rt.alloc_code(main_function)?;
rt.normalize(main);
println!("{}", readback_string(&rt, main)); // TODO: optimize by deserializing term into text directly
// Display stats
println!("Rewrites: {}", rt.get_rewrites());
return Ok(());
Ok(())
}
// ------------------------------------------------------------