labast/src/main.rs
2024-02-05 17:13:32 +02:00

48 lines
1.1 KiB
Rust

use std::{env, fs, io::{BufRead, Write}};
use errors::RunError;
use execute::execute;
use parse::parse;
use stack::Stack;
mod stack;
mod parse;
mod execute;
mod instructions;
mod errors;
fn main() {
let args: Vec<String> = env::args().collect();
if args.get(2) == Some(&String::from("-h"))
|| args.get(2) == Some(&String::from("--help")) {
eprintln!("Usage: {0} [file]", args[0]);
return;
}
if args.len() < 2 {
let mut stack = Stack::new();
let mut line = String::new();
loop {
line.clear();
stack.program.clear();
let stdin = std::io::stdin();
print!("> ");
std::io::stdout().flush().expect("Cannot flush the buffer.");
stdin.lock().read_line(&mut line).unwrap();
parse(&mut stack, &line.trim());
execute(&mut stack, None, &"inline".to_string());
}
}
let mut stack = Stack::new();
let lines = fs::read_to_string(&args[1]).expect(&format!("{}", RunError::FailToReadFile));
parse(&mut stack, &lines);
execute(&mut stack, None, &args[1]);
}