From 3ff3c4ae509420f8e432e7ee87b3534fe15c98ec Mon Sep 17 00:00:00 2001 From: Elia Perantoni Date: Fri, 26 Mar 2021 15:26:20 +0100 Subject: [PATCH] Koifile support --- .gitignore | 1 + src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 4099559..023ba4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /target .idea /prog.koi +/Koifile web/dist/ web/snippets/out web/node_modules diff --git a/src/main.rs b/src/main.rs index 68d0e52..124cf7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,16 +21,36 @@ fn main() { let matches = App::new("Koi") .version("1.0.0") .author("Elia Perantoni ") - .arg(Arg::with_name("PATH").help("Path to soure file. If omitted will read from STDIN.")) + .arg( + Arg::with_name("path") + .value_name("PATH") + .index(1) + .takes_value(true) + .help("Path to source file.") + ) + .arg( + Arg::with_name("stdin") + .short("s") + .long("stdin") + .takes_value(false) + .help("Read script from stdin.") + .conflicts_with("path") + ) + .arg( + Arg::with_name("fn") + .short("f") + .long("--fn") + .takes_value(true) + .help("Function to call.") + ) .get_matches(); - let source = match matches.value_of("PATH") { - Some(path) => fs::read_to_string(path).unwrap(), - None => { - let mut buffer = String::new(); - io::stdin().read_to_string(&mut buffer).unwrap(); - buffer - } + let source = if matches.is_present("stdin") { + let mut buffer = String::new(); + io::stdin().read_to_string(&mut buffer).unwrap(); + buffer + } else { + fs::read_to_string(matches.value_of("path").unwrap_or("Koifile")).unwrap() }; let lexer = new_lexer(source); @@ -40,4 +60,15 @@ fn main() { let mut interpreter = interp::Interpreter::new(); interpreter.run(prog); + + if let Some(f) = matches.value_of("fn") { + use ast::{Stmt, Expr}; + + interpreter.run(vec![ + Stmt::Expr(Expr::Call { + func: Box::new(Expr::Get(f.to_string())), + args: vec![] + }) + ]); + } }