From 15ae8622e1d9c8657e42467b61cdacbb66aee23e Mon Sep 17 00:00:00 2001 From: TudbuT Date: Mon, 9 Sep 2024 19:58:59 +0200 Subject: [PATCH] fix string ops, add string find, fix server.spl not being embedded, move stdlib to spl/, fix a null identity loss --- install.spl | 2 +- assemble.spl => spl/assemble.spl | 0 http.spl => spl/http.spl | 0 isbpl.spl => spl/isbpl.spl | 0 iter.spl => spl/iter.spl | 0 messaging.spl => spl/messaging.spl | 2 +- net.spl => spl/net.spl | 0 pure.spl => spl/pure.spl | 0 repl.spl => spl/repl.spl | 0 server.spl => spl/server.spl | 2 +- std.spl => spl/std.spl | 96 ++++++++++++++++++------------ stream.spl => spl/stream.spl | 2 +- time.spl => spl/time.spl | 0 src/stdlib.rs | 24 ++++---- src/stream/rw.rs | 1 - test.spl | 41 +++++++++++-- 16 files changed, 111 insertions(+), 59 deletions(-) rename assemble.spl => spl/assemble.spl (100%) rename http.spl => spl/http.spl (100%) rename isbpl.spl => spl/isbpl.spl (100%) rename iter.spl => spl/iter.spl (100%) rename messaging.spl => spl/messaging.spl (96%) rename net.spl => spl/net.spl (100%) rename pure.spl => spl/pure.spl (100%) rename repl.spl => spl/repl.spl (100%) rename server.spl => spl/server.spl (96%) rename std.spl => spl/std.spl (86%) rename stream.spl => spl/stream.spl (99%) rename time.spl => spl/time.spl (100%) diff --git a/install.spl b/install.spl index 3a64d31..0c7fa1e 100644 --- a/install.spl +++ b/install.spl @@ -2,7 +2,7 @@ func main { mega | with args ; [ "sudo" "mkdir" "/usr/lib/spl" ] command-wait; - [ "sh" "-c" "sudo cp *.spl /usr/lib/spl" ] command-wait; + [ "sh" "-c" "sudo cp -r spl/* /usr/lib/spl" ] command-wait; [ "cargo" "build" "--release" ] command-wait; [ "sudo" "rm" "/bin/spl" ] command-wait; [ "sudo" "cp" "target/release/spl" "/bin" ] command-wait; diff --git a/assemble.spl b/spl/assemble.spl similarity index 100% rename from assemble.spl rename to spl/assemble.spl diff --git a/http.spl b/spl/http.spl similarity index 100% rename from http.spl rename to spl/http.spl diff --git a/isbpl.spl b/spl/isbpl.spl similarity index 100% rename from isbpl.spl rename to spl/isbpl.spl diff --git a/iter.spl b/spl/iter.spl similarity index 100% rename from iter.spl rename to spl/iter.spl diff --git a/messaging.spl b/spl/messaging.spl similarity index 96% rename from messaging.spl rename to spl/messaging.spl index 4bbd7ac..9764ba2 100644 --- a/messaging.spl +++ b/spl/messaging.spl @@ -1,4 +1,4 @@ -"messaging bus, aka event bus" +"messaging bus, aka event bus"; construct messaging namespace { Message diff --git a/net.spl b/spl/net.spl similarity index 100% rename from net.spl rename to spl/net.spl diff --git a/pure.spl b/spl/pure.spl similarity index 100% rename from pure.spl rename to spl/pure.spl diff --git a/repl.spl b/spl/repl.spl similarity index 100% rename from repl.spl rename to spl/repl.spl diff --git a/server.spl b/spl/server.spl similarity index 96% rename from server.spl rename to spl/server.spl index 4b0bf7f..864082e 100644 --- a/server.spl +++ b/spl/server.spl @@ -53,7 +53,7 @@ construct net:server:ServerStream { this } accept { Stream | with this ; - def stream null Stream settype =stream + def stream Stream:mkinstance =stream this:id accept-server-stream stream:=id stream } diff --git a/std.spl b/spl/std.spl similarity index 86% rename from std.spl rename to spl/std.spl index 4158568..f7ec0e6 100644 --- a/std.spl +++ b/spl/std.spl @@ -30,55 +30,77 @@ construct FrameInfo { construct _str_ext { ; - new { any | with this ; - null clone this settype:construct + new { any | :mkinstance:construct } + mkinstance { any | with this ; + null clone this settype } to-bytes { [int] | str-to-bytes } split { str | with splitter this ; - def bytes splitter:to-bytes =bytes - def iter this:to-bytes:iter =iter - def item 0 =item - [ while { item null eq not } { + splitter:to-bytes =splitter + def bytes this:to-bytes =bytes + def i 0 =i + [ [ while { i bytes:len lt } { def match 0 =match - [ - while { match bytes:len eq not } { - iter:next =item - item null eq if { - 3 stop - } - item dup (match bytes:get) eq dup if { - match ++ =match - } not if { - 0 =match - } - } - { | pop pop } match:foreach - ] _str - } ] - } - replace { str | with replacee replacement this ; - def bytes replacee:to-bytes =bytes - def iter this:to-bytes:iter =iter - def item 0 =item - [ while { item null eq not } { - def match 0 =match - while { match bytes:len eq not } { - iter:next =item - item null eq if { - 3 stop - } - item dup (match bytes:get) eq dup if { + while { match i + bytes:len lt match splitter:len lt and } { + i match + bytes:get (match splitter:get) eq dup if { match ++ =match } not if { 0 =match + 3 stop } } - { | pop pop } match:foreach - match bytes:len eq if { - replacement _array :to-stack + i bytes:get + match splitter:len eq if { + pop ] _str [ + i match + 1 - =i } + i ++ =i + } ] _str ] + } + replace { str | with replacee replacement this ; + replacee:to-bytes =replacee + def bytes this:to-bytes =bytes + def i 0 =i + [ while { i bytes:len lt } { + def match 0 =match + while { match i + bytes:len lt match replacee:len lt and } { + i match + bytes:get (match replacee:get) eq dup if { + match ++ =match + } not if { + 0 =match + 3 stop + } + } + i bytes:get + match replacee:len eq if { + pop replacement:to-bytes:to-stack + i match + 1 - =i + } + i ++ =i } ] _str } + find { idx | with search this ; + search:to-bytes =search + def bytes this:to-bytes =bytes + def i 0 =i + while { i bytes:len lt } { + def match 0 =match + while { match i + bytes:len lt match search:len lt and } { + i match + bytes:get (match search:get) eq dup if { + match ++ =match + } not if { + 0 =match + 3 stop + } + } + match search:len eq if { + i + 4 stop + } + i ++ =i + } + null + } starts-with { bool | with beginning this ; beginning:to-bytes this:to-bytes:starts-with } diff --git a/stream.spl b/spl/stream.spl similarity index 99% rename from stream.spl rename to spl/stream.spl index 05bb021..c951206 100644 --- a/stream.spl +++ b/spl/stream.spl @@ -35,7 +35,7 @@ construct Stream { def read while { buf this:id read-stream pop _mega dup =read } { full (0 read buf:sub) aadd =full - } + } pop full } write { mega | with buf this ; diff --git a/time.spl b/spl/time.spl similarity index 100% rename from time.spl rename to spl/time.spl diff --git a/src/stdlib.rs b/src/stdlib.rs index 3ceedf3..21005e3 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -1,17 +1,18 @@ use crate::Runtime; use multicall::multicall; -pub const STD: &str = include_str!("../std.spl"); -pub const NET: &str = include_str!("../net.spl"); -pub const ITER: &str = include_str!("../iter.spl"); -pub const HTTP: &str = include_str!("../http.spl"); -pub const STREAM: &str = include_str!("../stream.spl"); -pub const MESSAGING: &str = include_str!("../messaging.spl"); -pub const ASSEMBLE: &str = include_str!("../assemble.spl"); -pub const ISBPL: &str = include_str!("../isbpl.spl"); -pub const REPL: &str = include_str!("../repl.spl"); -pub const PURE: &str = include_str!("../pure.spl"); -pub const TIME: &str = include_str!("../time.spl"); +pub const STD: &str = include_str!("../spl/std.spl"); +pub const NET: &str = include_str!("../spl/net.spl"); +pub const ITER: &str = include_str!("../spl/iter.spl"); +pub const HTTP: &str = include_str!("../spl/http.spl"); +pub const STREAM: &str = include_str!("../spl/stream.spl"); +pub const MESSAGING: &str = include_str!("../spl/messaging.spl"); +pub const ASSEMBLE: &str = include_str!("../spl/assemble.spl"); +pub const ISBPL: &str = include_str!("../spl/isbpl.spl"); +pub const REPL: &str = include_str!("../spl/repl.spl"); +pub const PURE: &str = include_str!("../spl/pure.spl"); +pub const TIME: &str = include_str!("../spl/time.spl"); +pub const SERVER: &str = include_str!("../spl/server.spl"); pub const NOP: &str = ""; pub fn register(runtime: &mut Runtime) { @@ -28,6 +29,7 @@ pub fn register(runtime: &mut Runtime) { insert("repl.spl", REPL); insert("pure.spl", PURE); insert("time.spl", TIME); + insert("server.spl", SERVER); insert("nop.spl", NOP); } } diff --git a/src/stream/rw.rs b/src/stream/rw.rs index cd2514c..dc05aaa 100644 --- a/src/stream/rw.rs +++ b/src/stream/rw.rs @@ -1,5 +1,4 @@ use std::{ - borrow::BorrowMut, collections::HashMap, hint::black_box, io::{Read, Write}, diff --git a/test.spl b/test.spl index 93c2a62..9a9e60a 100644 --- a/test.spl +++ b/test.spl @@ -1,4 +1,4 @@ - +[ "#stream.spl" import "#http.spl" import "#messaging.spl" import @@ -8,6 +8,7 @@ "SPL tester" =program-name func main { int | with args ; + def thing 1 anew =thing @@ -110,7 +111,7 @@ func main { int | with args ; def file "test.txt" 1 StreamTypes:file:create =file "hi\n" :to-bytes file:write-exact; - file:close null =file + file:close; null =file "" println "testing split" println @@ -119,9 +120,9 @@ func main { int | with args ; catch { use net:http:Request - "testing http" println + "testing http" println; def req "data.tudbut.de" 80 "GET" "/spltest" Request:new =req - req:send:body _str println + req:send:body _str println; } { with e ; e:message println "it seems the internet is not available" println @@ -180,9 +181,37 @@ func main { int | with args ; def client "localhost" 4075 StreamTypes:tcp:create =client 1024 client:read-to-end:to-str println; " ^ this should say 'Hello!'" println; - "you now have a chance to connect too: localhost:4075 - continuing in 5 seconds..." println; - 5000 time:sleep; + "you now have a chance to connect too: localhost:4075 - continuing in 2 seconds..." println; + 2000 time:sleep; + "" println + "testing string replace" println; + + "aba" "!!" "ababab" :replace println; + " ^ should be !!bab." println; + "aba" "!!" "aababab" :replace println; + " ^ should be a!!bab." println; + + "" println; + "testing string split" println; + + "ba" "abaabaabaa" :split:iter:join<", "> println; + " ^ should be a, a, a, a" println; + "ba" "abbaabbaababaa" :split:iter:join<", "> println; + " ^ should be ab, ab, a, , a" println; + + "" println; + "testing string find" println; + + "abba" "ababba" :find println; + "^ should be 2" println; + + + ] dup :len 0 eq not if { + "" println + "!! something went wrong somewhere. the stack is not empty." println + dyn-__dump + } 100 }