Ádd command line arguments

This commit is contained in:
Sofia 2020-06-25 02:00:53 +03:00
parent 4aa032139d
commit 70211cecc1
2 changed files with 88 additions and 13 deletions

53
src/args.rs Normal file
View File

@ -0,0 +1,53 @@
use std::path::PathBuf;
use argh::FromArgs;
#[derive(FromArgs, PartialEq, Debug)]
#[argh(description = "reid compiler and Virtual Machine")]
pub struct MainOpt {
#[argh(subcommand)]
pub subcommand: Subcommand,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum Subcommand {
Compile(Compile),
Run(Run),
CompileAndRun(CompileAndRun),
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(
subcommand,
name = "compile",
description = "compile <source> to .reidc to <output> path"
)]
pub struct Compile {
#[argh(positional, description = "source .reid path")]
pub source: String,
#[argh(positional, description = "output .reidc path")]
pub output: String,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(
subcommand,
name = "run",
description = "run compiled .reidc from <path>"
)]
pub struct Run {
#[argh(positional, description = "otus 2")]
pub path: String,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(
subcommand,
name = "c_run",
description = "compile and run given .reid file"
)]
pub struct CompileAndRun {
#[argh(positional, description = "source .reid path")]
pub source: PathBuf,
}

View File

@ -1,6 +1,7 @@
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
#![warn(clippy::all)] #![warn(clippy::all)]
mod args;
mod compiler; mod compiler;
mod errors; mod errors;
mod file_io; mod file_io;
@ -10,19 +11,15 @@ mod vm;
use file_io::open_file; use file_io::open_file;
use std::path::Path; use std::path::Path;
use compiler::Compiler; use args::*;
use compiler::{CompiledReid, Compiler};
use parser::Parser; use parser::Parser;
use vm::{BuiltinFunctionDef, BuiltinFunctions, Value, VariableType, VirtualMachine}; use vm::{
BuiltinFunctionDef, BuiltinFunctions, FunctionSignature, Value, VariableType, VirtualMachine,
};
// cargo run c_run reid_src/test.reid for previous functionality
fn main() { fn main() {
let path = Path::new("reid_src/test.reid");
let parsed = Parser::from(open_file(&path).ok().unwrap()).parse();
if let Err(error) = parsed {
eprintln!("Syntax error: {}", error);
std::process::exit(1);
}
dbg!(&parsed);
let print = BuiltinFunctionDef::new( let print = BuiltinFunctionDef::new(
"print", "print",
vec![VariableType::TypeString], vec![VariableType::TypeString],
@ -34,8 +31,29 @@ fn main() {
); );
let builtin_functions = BuiltinFunctions(vec![print]); let builtin_functions = BuiltinFunctions(vec![print]);
let opt: MainOpt = argh::from_env();
match opt.subcommand {
Subcommand::Compile(_) => {}
Subcommand::Run(_) => {}
Subcommand::CompileAndRun(c_run) => {
let path = Path::new(&c_run.source);
let compiled = compile(path, builtin_functions.signatures());
run(compiled, builtin_functions);
}
}
}
fn compile(path: &Path, builtin: Vec<FunctionSignature>) -> CompiledReid {
let parsed = Parser::from(open_file(&path).ok().unwrap()).parse();
if let Err(error) = parsed {
eprintln!("Syntax error: {}", error);
std::process::exit(1);
}
dbg!(&parsed);
let compiled = Compiler::from(parsed.unwrap()) let compiled = Compiler::from(parsed.unwrap())
.with_builtin_functions(builtin_functions.signatures()) .with_builtin_functions(builtin)
.compile(); .compile();
if let Err(error) = compiled { if let Err(error) = compiled {
eprintln!("Compilation error: {}", error); eprintln!("Compilation error: {}", error);
@ -43,8 +61,12 @@ fn main() {
} }
dbg!(&compiled); dbg!(&compiled);
let mut vm = VirtualMachine::from(compiled.unwrap()); compiled.unwrap()
vm.add_builtin_functions(builtin_functions); }
fn run(reid: CompiledReid, builtin: BuiltinFunctions) {
let mut vm = VirtualMachine::from(reid);
vm.add_builtin_functions(builtin);
if let Err(error) = vm.run() { if let Err(error) = vm.run() {
eprintln!("Runtime panic: {:#?}", error); eprintln!("Runtime panic: {:#?}", error);
std::process::exit(1); std::process::exit(1);