talc/talc-bin/src/main.rs

77 lines
1.5 KiB
Rust
Raw Normal View History

2024-02-27 05:18:43 +00:00
use clap::{ColorChoice, Parser};
use std::{path::PathBuf, process::ExitCode, rc::Rc};
2024-11-04 18:25:31 +00:00
use talc_lang::{compiler::compile, parser, symbol::Symbol, value::function::disasm_recursive, Vm};
2024-02-27 05:18:43 +00:00
mod helper;
2024-11-04 18:25:31 +00:00
mod repl;
2024-02-21 16:04:18 +00:00
2024-02-23 19:54:34 +00:00
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
2024-02-27 05:18:43 +00:00
/// file to run
file: Option<PathBuf>,
2024-02-23 19:54:34 +00:00
2024-02-27 05:18:43 +00:00
/// start the repl
#[arg(short, long)]
repl: bool,
2024-02-23 19:54:34 +00:00
2024-02-27 05:18:43 +00:00
/// show disassembled bytecode
#[arg(short, long)]
disasm: bool,
2024-02-23 19:54:34 +00:00
2024-03-08 00:38:57 +00:00
/// show disassembled bytecode
2024-11-04 18:25:31 +00:00
#[arg(short = 'H', long)]
2024-03-08 00:38:57 +00:00
histfile: Option<PathBuf>,
2024-02-27 05:18:43 +00:00
/// enable or disable color
2024-11-04 18:25:31 +00:00
#[arg(short, long, default_value = "auto")]
2024-02-27 05:18:43 +00:00
color: ColorChoice,
2024-02-23 19:54:34 +00:00
}
2024-11-04 17:59:05 +00:00
fn exec(name: Symbol, src: &str, args: &Args) -> ExitCode {
2024-02-21 16:04:18 +00:00
let mut vm = Vm::new(256);
2024-02-27 05:18:43 +00:00
talc_std::load_all(&mut vm);
2024-11-03 17:50:36 +00:00
let ex = match parser::parse(src) {
2024-02-27 05:18:43 +00:00
Ok(ex) => ex,
Err(e) => {
eprintln!("Error: {e}");
return ExitCode::FAILURE
2024-11-04 18:25:31 +00:00
}
2024-02-27 05:18:43 +00:00
};
2024-11-04 17:59:05 +00:00
let func = Rc::new(compile(&ex, Some(name)));
2024-02-27 05:18:43 +00:00
if args.disasm {
if let Err(e) = disasm_recursive(&func, &mut std::io::stderr()) {
eprintln!("Error: {e}");
return ExitCode::FAILURE
2024-02-21 16:04:18 +00:00
}
}
2024-02-27 05:18:43 +00:00
if let Err(e) = vm.run_function(func.clone(), vec![func.into()]) {
eprintln!("{e}");
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
2024-02-23 19:54:34 +00:00
}
fn main() -> ExitCode {
2024-02-27 05:18:43 +00:00
let args = Args::parse();
2024-02-23 19:54:34 +00:00
2024-02-27 05:18:43 +00:00
if args.repl || args.file.is_none() {
return repl::repl(&args)
}
2024-02-22 19:27:35 +00:00
2024-11-04 18:25:31 +00:00
let file = args.file.as_ref().unwrap();
2024-11-04 17:59:05 +00:00
match std::fs::read_to_string(file) {
Ok(s) => exec(Symbol::get(file.as_os_str()), &s, &args),
2024-02-27 05:18:43 +00:00
Err(e) => {
eprintln!("Error: {e}");
ExitCode::FAILURE
}
}
2024-02-21 16:04:18 +00:00
}