Ádd command line arguments
This commit is contained in:
parent
4aa032139d
commit
70211cecc1
53
src/args.rs
Normal file
53
src/args.rs
Normal 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,
|
||||
}
|
48
src/main.rs
48
src/main.rs
@ -1,6 +1,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(clippy::all)]
|
||||
|
||||
mod args;
|
||||
mod compiler;
|
||||
mod errors;
|
||||
mod file_io;
|
||||
@ -10,19 +11,15 @@ mod vm;
|
||||
use file_io::open_file;
|
||||
use std::path::Path;
|
||||
|
||||
use compiler::Compiler;
|
||||
use args::*;
|
||||
use compiler::{CompiledReid, Compiler};
|
||||
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() {
|
||||
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(
|
||||
"print",
|
||||
vec![VariableType::TypeString],
|
||||
@ -34,8 +31,29 @@ fn main() {
|
||||
);
|
||||
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())
|
||||
.with_builtin_functions(builtin_functions.signatures())
|
||||
.with_builtin_functions(builtin)
|
||||
.compile();
|
||||
if let Err(error) = compiled {
|
||||
eprintln!("Compilation error: {}", error);
|
||||
@ -43,8 +61,12 @@ fn main() {
|
||||
}
|
||||
dbg!(&compiled);
|
||||
|
||||
let mut vm = VirtualMachine::from(compiled.unwrap());
|
||||
vm.add_builtin_functions(builtin_functions);
|
||||
compiled.unwrap()
|
||||
}
|
||||
|
||||
fn run(reid: CompiledReid, builtin: BuiltinFunctions) {
|
||||
let mut vm = VirtualMachine::from(reid);
|
||||
vm.add_builtin_functions(builtin);
|
||||
if let Err(error) = vm.run() {
|
||||
eprintln!("Runtime panic: {:#?}", error);
|
||||
std::process::exit(1);
|
||||
|
Loading…
Reference in New Issue
Block a user