spl/src/main.rs

60 lines
2.1 KiB
Rust

use spl::{
lex, oxidizer::RustAppBuilder, sasm::sasm_write, start_file_in_runtime, Runtime, SetRuntime,
};
use std::{env::args, fs};
fn main() {
Runtime::new().set();
let mut args = args().skip(1);
let arg = &args.next().unwrap_or("#repl.spl".to_owned());
if arg == "--build" || arg == "--run" || arg == "--buildrun" || arg == "--debug" {
let file = args.next().unwrap();
let data = fs::read_to_string(file.clone()).expect("unable to read specified file");
if arg == "--debug" {
println!("words: {}", spl::parse(data.clone()).join(" "));
println!("{}", sasm_write(lex(false, data).unwrap()));
return;
}
let build_only = arg == "--build";
if build_only {
println!("Building SPL with specified natives file...");
}
let mut builder = RustAppBuilder::new();
if build_only {
if let Some(name) = args.next() {
builder.set_name(name);
}
println!("Embedding source...");
} else {
builder.set_name(file[..file.rfind('.').unwrap_or(file.len())].to_owned());
}
builder.add_source(file.to_owned(), data.to_owned());
if build_only {
println!("Preparing rust code...");
}
builder.prepare(lex(false, data.to_owned()).expect("invalid SPL in natives file."));
if build_only {
println!("Building...");
}
let app = builder.build(build_only || arg == "--buildrun").unwrap();
if build_only {
println!("Built! Binary is {}", app.get_binary());
} else {
let mut args: Vec<String> = args.collect();
args.insert(0, file);
let mut command = app.execute(args).unwrap();
if arg != "--buildrun" {
println!("spl: cleaning temporary dir (run with --buildrun to keep)");
app.delete();
}
command.wait().unwrap();
}
return;
}
if let Err(x) = start_file_in_runtime(arg) {
println!("{x:?}");
}
Runtime::reset();
}