From c9f9ba9979a2e9587b7e8477079f66e66d1eb56a Mon Sep 17 00:00:00 2001 From: TudbuT Date: Sat, 25 Feb 2023 13:58:17 +0100 Subject: [PATCH] ready for cargo publish --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/dyn_fns.rs | 2 +- src/lib.rs | 4 +++- src/runtime.rs | 15 ++++++++++----- src/std_fns.rs | 19 ++++++++++++++++--- src/stdlib.rs | 5 +++++ 7 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/stdlib.rs diff --git a/Cargo.lock b/Cargo.lock index 2548c86..6546599 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,7 +16,7 @@ checksum = "b03f7fbd470aa8b3ad163c85cce8bccfc11cc9c44ef12da0a4eddd98bd307352" [[package]] name = "spl" -version = "0.0.1" +version = "0.0.2" dependencies = [ "once_cell", "readformat", diff --git a/Cargo.toml b/Cargo.toml index bfe3f82..0dba262 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/dyn_fns.rs b/src/dyn_fns.rs index ec23a8e..aba8be3 100644 --- a/src/dyn_fns.rs +++ b/src/dyn_fns.rs @@ -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); diff --git a/src/lib.rs b/src/lib.rs index 60a60b6..1062b93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { 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(), })?; diff --git a/src/runtime.rs b/src/runtime.rs index 66d7bad..259d951 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -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 { 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()), + } } } diff --git a/src/std_fns.rs b/src/std_fns.rs index 917f99a..0c0b60e 100644 --- a/src/std_fns.rs +++ b/src/std_fns.rs @@ -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(()) } diff --git a/src/stdlib.rs b/src/stdlib.rs new file mode 100644 index 0000000..dbb6fbb --- /dev/null +++ b/src/stdlib.rs @@ -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");