ready for cargo publish
This commit is contained in:
parent
26044549b9
commit
c9f9ba9979
7 changed files with 37 additions and 12 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -16,7 +16,7 @@ checksum = "b03f7fbd470aa8b3ad163c85cce8bccfc11cc9c44ef12da0a4eddd98bd307352"
|
|||
|
||||
[[package]]
|
||||
name = "spl"
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"readformat",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "spl"
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
edition = "2021"
|
||||
description = "Stack Pogramming Language: A simple, concise scripting language."
|
||||
license = "MIT"
|
||||
|
|
|
@ -234,7 +234,7 @@ pub fn dyn_readf(stack: &mut Stack) -> OError {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn wrap(f: fn(&mut Stack) -> OError) -> impl Fn(&mut Stack) -> OError {
|
||||
pub(crate) fn wrap(f: fn(&mut Stack) -> OError) -> impl Fn(&mut Stack) -> OError {
|
||||
move |stack| unsafe {
|
||||
let frame = stack.pop_frame(0);
|
||||
let r = f(stack);
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#![allow(clippy::type_complexity)]
|
||||
#![allow(clippy::len_without_is_empty)]
|
||||
|
||||
pub mod stdlib;
|
||||
pub mod dyn_fns;
|
||||
pub mod lexer;
|
||||
pub mod mutex;
|
||||
|
@ -24,8 +25,9 @@ pub fn start_file_in_runtime(path: &str) -> Result<Stack, Error> {
|
|||
function: "root".to_owned(),
|
||||
});
|
||||
// import stdlib
|
||||
let f = find_in_splpath("std.spl");
|
||||
let words =
|
||||
lex(fs::read_to_string(find_in_splpath("std.spl")).unwrap()).map_err(|x| Error {
|
||||
lex(if let Ok(f) = f { fs::read_to_string(f).unwrap() } else { f.unwrap_err() }).map_err(|x| Error {
|
||||
kind: ErrorKind::LexError(format!("{x:?}")),
|
||||
stack: Vec::new(),
|
||||
})?;
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::{
|
|||
dyn_fns,
|
||||
mutex::*,
|
||||
std_fns,
|
||||
stream::{self, *},
|
||||
stream::{self, *}, stdlib,
|
||||
};
|
||||
|
||||
use core::panic;
|
||||
|
@ -918,15 +918,20 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn find_in_splpath(path: &str) -> String {
|
||||
pub fn find_in_splpath(path: &str) -> Result<String, String> {
|
||||
if Path::new(path).exists() {
|
||||
return path.to_owned();
|
||||
return Ok(path.to_owned());
|
||||
}
|
||||
let s = var("SPL_PATH").unwrap_or("/usr/lib/spl".to_owned()) + "/" + path;
|
||||
if Path::new(&s).exists() {
|
||||
s
|
||||
Ok(s)
|
||||
} else {
|
||||
path.to_owned()
|
||||
match path {
|
||||
"std.spl" => Err(stdlib::STD.to_owned()),
|
||||
"iter.spl" => Err(stdlib::ITER.to_owned()),
|
||||
"stream.spl" => Err(stdlib::STREAM.to_owned()),
|
||||
_ => Ok(path.to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -562,8 +562,14 @@ pub fn import(stack: &mut Stack) -> OError {
|
|||
let Value::Str(mut s) = stack.pop().lock_ro().native.clone() else {
|
||||
return stack.err(ErrorKind::InvalidCall("import".to_owned()))
|
||||
};
|
||||
let fallback = match s.as_str().rsplit_once(|x| x == '/' || x == '#').map(|(.., x)| x).unwrap_or(s.as_str()) {
|
||||
"std.spl" => Some(stdlib::STD),
|
||||
"iter.spl" => Some(stdlib::ITER),
|
||||
"stream.spl" => Some(stdlib::STREAM),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(x) = s.strip_prefix('#') {
|
||||
s = find_in_splpath(x);
|
||||
s = find_in_splpath(x).unwrap_or(x.to_owned());
|
||||
} else if let Some(x) = s.strip_prefix('@') {
|
||||
s = x.to_owned();
|
||||
} else {
|
||||
|
@ -580,8 +586,15 @@ pub fn import(stack: &mut Stack) -> OError {
|
|||
}
|
||||
stack.push(Value::Str(s).spl());
|
||||
dup(stack)?;
|
||||
read_file(stack)?;
|
||||
dyn_fns::dyn_readf(stack)?;
|
||||
read_file(stack).or_else(|x| {
|
||||
if let Some(fallback) = fallback {
|
||||
stack.push(Value::Str(fallback.to_owned()).spl());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(x)
|
||||
}
|
||||
})?;
|
||||
dyn_fns::wrap(dyn_fns::dyn_readf)(stack)?;
|
||||
call(stack)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
5
src/stdlib.rs
Normal file
5
src/stdlib.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
pub const STD: &str = include_str!("../std.spl");
|
||||
pub const ITER: &str = include_str!("../iter.spl");
|
||||
pub const STREAM: &str = include_str!("../stream.spl");
|
Loading…
Add table
Reference in a new issue