spl/src/main.rs

61 lines
2.1 KiB
Rust
Raw Normal View History

use spl::{
lex, oxidizer::RustAppBuilder, sasm::sasm_write, start_file_in_runtime, Runtime, SetRuntime,
};
2023-02-17 22:47:45 +01:00
2023-08-04 20:38:47 +02:00
use std::{env::args, fs};
2023-02-17 20:00:39 +01:00
2023-02-25 11:37:07 +01:00
fn main() {
2024-08-28 23:54:16 +02:00
Runtime::new().set();
2023-08-04 20:38:47 +02:00
let mut args = args().skip(1);
2024-09-06 21:29:40 +02:00
let arg = &args.next().unwrap_or("#repl.spl".to_owned());
if arg == "--build" || arg == "--run" || arg == "--buildrun" || arg == "--debug" {
2023-08-04 20:38:47 +02:00
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;
}
2023-08-04 23:17:35 +02:00
let build_only = arg == "--build";
if build_only {
println!("Building SPL with specified natives file...");
}
2023-08-04 20:38:47 +02:00
let mut builder = RustAppBuilder::new();
2023-08-04 23:17:35 +02:00
if build_only {
if let Some(name) = args.next() {
builder.set_name(name);
}
println!("Embedding source...");
} else {
2024-08-30 14:01:43 +02:00
builder.set_name(file[..file.rfind('.').unwrap_or(file.len())].to_owned());
}
2023-08-04 23:17:35 +02:00
builder.add_source(file.to_owned(), data.to_owned());
if build_only {
println!("Preparing rust code...");
2023-08-04 22:39:05 +02:00
}
builder.prepare(lex(false, data.to_owned()).expect("invalid SPL in natives file."));
2023-08-04 23:17:35 +02:00
if build_only {
println!("Building...");
}
2024-08-28 23:54:16 +02:00
let app = builder.build(build_only || arg == "--buildrun").unwrap();
2023-08-04 23:17:35 +02:00
if build_only {
println!("Built! Binary is {}", app.get_binary());
} else {
let mut args: Vec<String> = args.collect();
args.insert(0, file);
2023-08-04 23:48:23 +02:00
let mut command = app.execute(args).unwrap();
2024-08-28 23:54:16 +02:00
if arg != "--buildrun" {
println!("spl: cleaning temporary dir (run with --buildrun to keep)");
app.delete();
}
2023-08-04 23:48:23 +02:00
command.wait().unwrap();
2023-08-04 23:17:35 +02:00
}
2023-08-04 20:38:47 +02:00
return;
}
2024-08-28 23:54:16 +02:00
if let Err(x) = start_file_in_runtime(arg) {
2023-02-25 11:37:07 +01:00
println!("{x:?}");
}
2024-08-28 23:54:16 +02:00
Runtime::reset();
2023-02-17 20:00:39 +01:00
}