mox/src/main.rs

120 lines
3.7 KiB
Rust

use clap::{Arg, ArgAction, Command};
use mox::compiler::Compiler;
use mox::scanner::Scanner;
use mox::vm::VM;
use std::fs;
use std::process;
fn main() {
let file_arg = Arg::new("file")
.short('f')
.long("file")
.action(ArgAction::Set)
.value_name("FILE")
.help("File to run");
let command_arg = Arg::new("commands")
.value_name("COMMANDS")
.index(1)
.action(ArgAction::Set)
.help("Commands to run");
let app = Command::new("mox")
.version("1.0")
.author("publicmatt")
.about("mox interpreter!!")
.subcommand(
Command::new("run")
.about("Run commands")
.arg(&file_arg)
.arg(&command_arg),
)
.subcommand(
Command::new("scan")
.about("Scan commands")
.arg(&file_arg)
.arg(&command_arg),
);
let matches = app.get_matches();
match matches.subcommand() {
Some(("run", sub_m)) => {
if let Some(file) = sub_m.get_one::<String>("file") {
let commands = fs::read_to_string(file).unwrap_or_else(|err| {
eprintln!("Error reading '{}': {}", file, err);
process::exit(74);
});
run_content(&commands);
} else if let Some(commands) = sub_m.get_one::<String>("commands") {
run_content(commands);
} else {
println!("No file or commands provided for run.");
}
}
Some(("scan", sub_m)) => {
if let Some(file) = sub_m.get_one::<String>("file") {
let commands = fs::read_to_string(file).unwrap_or_else(|err| {
eprintln!("Error reading '{}': {}", file, err);
process::exit(74);
});
scan_content(&commands);
} else if let Some(commands) = sub_m.get_one::<String>("commands") {
scan_content(commands);
} else {
println!("No file or commands provided for scan.");
}
}
_ => {
todo!("repl not done yet")
}
}
}
// fn repl() {
// let mut input = String::new();
// let mut vm: VM = VM::new();
// let mut chunk: Chunk = Chunk::new();
// loop {
// input.clear();
// print!("> ");
// io::stdout().flush().unwrap();
//
// match io::stdin().read_line(&mut input) {
// Ok(bytes) => {
// if bytes == 0 || input.trim() == "exit" {
// println!("Bye!");
// break;
// }
// vm.interpret(&input, &mut chunk);
// }
// Err(_error) => {
// continue;
// }
// }
// }
// }
fn scan_content(source: &str) {
let input = source.to_owned();
let mut scanner = Scanner::new(&input);
scanner.compile();
}
fn run_content(source: &str) {
let mut _vm: VM = VM::new();
let owned = &source.to_owned();
let mut compiler: Compiler = Compiler::from_source(owned);
compiler.compile();
//todo!("run_content is not hooked up yet");
// let mut chunk: Chunk = compiler.compile();
// match vm.interpret(&mut chunk) {
// InterpretResult::InterpretOk => exit(0),
// InterpretResult::InterpretCompileError => exit(65),
// InterpretResult::InterpretRuntimeError => exit(70),
// }
}
fn compile_content(source: &str) {
let mut _vm: VM = VM::new();
let owned = &source.to_owned();
let mut compiler: Compiler = Compiler::from_source(owned);
compiler.compile();
}