allow direct running of embedded rust
This commit is contained in:
parent
18b8b89549
commit
62fb4d0f91
4 changed files with 65 additions and 19 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -22,7 +22,7 @@ checksum = "b03f7fbd470aa8b3ad163c85cce8bccfc11cc9c44ef12da0a4eddd98bd307352"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "spl"
|
name = "spl"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"multicall",
|
"multicall",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "spl"
|
name = "spl"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Stack Pogramming Language: A simple, concise scripting language."
|
description = "Stack Pogramming Language: A simple, concise scripting language."
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
38
src/main.rs
38
src/main.rs
|
@ -7,20 +7,38 @@ fn main() {
|
||||||
let arg = &args
|
let arg = &args
|
||||||
.next()
|
.next()
|
||||||
.unwrap_or_else(|| find_in_splpath("repl.spl").expect("no file to be run"));
|
.unwrap_or_else(|| find_in_splpath("repl.spl").expect("no file to be run"));
|
||||||
if arg == "--build" {
|
if arg == "--build" || arg == "--run" {
|
||||||
let file = args.next().unwrap();
|
let file = args.next().unwrap();
|
||||||
let data = fs::read_to_string(file.clone()).expect("unable to read specified file");
|
let data = fs::read_to_string(file.clone()).expect("unable to read specified file");
|
||||||
println!("Building SPL with specified natives file...");
|
let build_only = arg == "--build";
|
||||||
let mut builder = RustAppBuilder::new();
|
if build_only {
|
||||||
if let Some(name) = args.next() {
|
println!("Building SPL with specified natives file...");
|
||||||
builder.set_name(name);
|
}
|
||||||
|
let mut builder = RustAppBuilder::new();
|
||||||
|
if build_only {
|
||||||
|
if let Some(name) = args.next() {
|
||||||
|
builder.set_name(name);
|
||||||
|
}
|
||||||
|
println!("Embedding source...");
|
||||||
|
}
|
||||||
|
builder.add_source(file.to_owned(), data.to_owned());
|
||||||
|
if build_only {
|
||||||
|
println!("Preparing rust code...");
|
||||||
}
|
}
|
||||||
println!("Embedding source...");
|
|
||||||
builder.add_source(file, data.to_owned());
|
|
||||||
println!("Preparing rust code...");
|
|
||||||
builder.prepare(lex(data.to_owned()).expect("invalid SPL in natives file."));
|
builder.prepare(lex(data.to_owned()).expect("invalid SPL in natives file."));
|
||||||
println!("Building...");
|
if build_only {
|
||||||
println!("Built! Binary is {}", builder.build().unwrap().get_binary());
|
println!("Building...");
|
||||||
|
}
|
||||||
|
let app = builder.build(build_only).unwrap();
|
||||||
|
if build_only {
|
||||||
|
println!("Built! Binary is {}", app.get_binary());
|
||||||
|
} else {
|
||||||
|
let mut args: Vec<String> = args.collect();
|
||||||
|
args.insert(0, file);
|
||||||
|
app.execute(args).unwrap();
|
||||||
|
app.delete();
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Err(x) = start_file(arg) {
|
if let Err(x) = start_file(arg) {
|
||||||
|
|
|
@ -4,10 +4,12 @@
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::{hash_map::DefaultHasher, HashMap},
|
collections::{hash_map::DefaultHasher, HashMap},
|
||||||
env, fs,
|
env,
|
||||||
|
ffi::OsStr,
|
||||||
|
fs,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
io,
|
io,
|
||||||
process::{Child, Command},
|
process::{Child, Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{FuncImplType, Keyword, Word, Words};
|
use crate::{FuncImplType, Keyword, Word, Words};
|
||||||
|
@ -18,6 +20,8 @@ mod splrs;
|
||||||
pub struct RustApp {
|
pub struct RustApp {
|
||||||
/// The path to the binary
|
/// The path to the binary
|
||||||
binary: String,
|
binary: String,
|
||||||
|
/// The path to the project dir
|
||||||
|
dir: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RustApp {
|
impl RustApp {
|
||||||
|
@ -27,9 +31,18 @@ impl RustApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Executes the binary with some args
|
/// Executes the binary with some args
|
||||||
pub fn execute(&self, args: Vec<&str>) -> Result<Child, io::Error> {
|
pub fn execute<I, S>(&self, args: I) -> Result<Child, io::Error>
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = S>,
|
||||||
|
S: AsRef<OsStr>,
|
||||||
|
{
|
||||||
Command::new(self.binary.clone()).args(args).spawn()
|
Command::new(self.binary.clone()).args(args).spawn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Deletes the binary and rust project
|
||||||
|
pub fn delete(self) {
|
||||||
|
fs::remove_dir_all(self.dir).expect("unable to delete RustApp");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A rust function which was embedded in SPL
|
/// A rust function which was embedded in SPL
|
||||||
|
@ -99,7 +112,7 @@ impl RustAppBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the desired app, including literally building it using cargo.
|
/// Builds the desired app, including literally building it using cargo.
|
||||||
pub fn build(self) -> Result<RustApp, io::Error> {
|
pub fn build(self, output: bool) -> Result<RustApp, io::Error> {
|
||||||
// we need a temp folder!
|
// we need a temp folder!
|
||||||
let tmp = "."; // TODO replace?
|
let tmp = "."; // TODO replace?
|
||||||
let name = match self.name {
|
let name = match self.name {
|
||||||
|
@ -115,16 +128,20 @@ impl RustAppBuilder {
|
||||||
.arg("new")
|
.arg("new")
|
||||||
.arg(format!("spl-{name}"))
|
.arg(format!("spl-{name}"))
|
||||||
.current_dir(tmp)
|
.current_dir(tmp)
|
||||||
|
.stdout(Stdio::null())
|
||||||
|
.stderr(Stdio::null())
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.wait_with_output();
|
.wait();
|
||||||
Command::new("cargo")
|
Command::new("cargo")
|
||||||
.arg("add")
|
.arg("add")
|
||||||
.arg(format!("spl@{}", env!("CARGO_PKG_VERSION")))
|
.arg(format!("spl@{}", env!("CARGO_PKG_VERSION")))
|
||||||
.current_dir(format!("{tmp}/spl-{name}"))
|
.current_dir(format!("{tmp}/spl-{name}"))
|
||||||
|
.stdout(Stdio::null())
|
||||||
|
.stderr(Stdio::null())
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.wait_with_output()?;
|
.wait()?;
|
||||||
let mut runtime_init = String::new();
|
let mut runtime_init = String::new();
|
||||||
let mut code = String::new();
|
let mut code = String::new();
|
||||||
for func in self.rust_functions.into_iter().enumerate() {
|
for func in self.rust_functions.into_iter().enumerate() {
|
||||||
|
@ -169,10 +186,21 @@ impl RustAppBuilder {
|
||||||
.arg("build")
|
.arg("build")
|
||||||
.arg("--release")
|
.arg("--release")
|
||||||
.current_dir(format!("{tmp}/spl-{name}"))
|
.current_dir(format!("{tmp}/spl-{name}"))
|
||||||
|
.stdout(if output {
|
||||||
|
Stdio::inherit()
|
||||||
|
} else {
|
||||||
|
Stdio::null()
|
||||||
|
})
|
||||||
|
.stderr(if output {
|
||||||
|
Stdio::inherit()
|
||||||
|
} else {
|
||||||
|
Stdio::null()
|
||||||
|
})
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.wait_with_output()?;
|
.wait()?;
|
||||||
Ok(RustApp {
|
Ok(RustApp {
|
||||||
|
dir: format!("{tmp}/spl-{name}"),
|
||||||
binary: {
|
binary: {
|
||||||
// insanity. will have to clean this up at some point.
|
// insanity. will have to clean this up at some point.
|
||||||
let dir = format!("{tmp}/spl-{name}/target/release/");
|
let dir = format!("{tmp}/spl-{name}/target/release/");
|
||||||
|
|
Loading…
Reference in a new issue