feat: add messaging, fix: allow tabs, fix: add =<0-5> methods to arrays

=<0-5> is the counterpart to the normal <0-5> methods used for easier indexing into the first few indexes to allow arrays to be used like tuples
This commit is contained in:
Daniella / Tove 2023-04-10 02:26:21 +02:00
parent 2f087d8835
commit adabbe0418
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
6 changed files with 75 additions and 0 deletions

44
messaging.spl Normal file
View file

@ -0,0 +1,44 @@
"messaging bus, aka event bus"
construct messaging namespace {
Message
Bus
}
construct messaging:Message {
name
content
;
construct { this | with name content this ;
name this:=name
content this:=content
this
}
}
construct messaging:Bus {
subscribers
;
construct { this | with this ;
MicroMap:new this:=subscribers
this
}
subscribe { | with message callable this ;
def entry message this:subscribers:get-or-create-entry =entry
entry:1 null eq if {
List:new entry:=1
}
callable entry:1:push
}
publish { | with message this ;
message gettype messaging:Message eq not if {
message null messaging:Message:new =message
}
def entry message:name this:subscribers:get =entry
entry null eq not if {
{ | with it ;
message it call
} entry:foreach
}
}
}

View file

@ -1082,6 +1082,7 @@ pub fn find_in_splpath(path: &str) -> Result<String, String> {
"iter.spl" => Err(stdlib::ITER.to_owned()), "iter.spl" => Err(stdlib::ITER.to_owned()),
"http.spl" => Err(stdlib::HTTP.to_owned()), "http.spl" => Err(stdlib::HTTP.to_owned()),
"stream.spl" => Err(stdlib::STREAM.to_owned()), "stream.spl" => Err(stdlib::STREAM.to_owned()),
"messaging.spl" => Err(stdlib::MESSAGING.to_owned()),
_ => Ok(path.to_owned()), _ => Ok(path.to_owned()),
} }
} }

View file

@ -613,6 +613,7 @@ pub fn import(stack: &mut Stack) -> OError {
"iter.spl" => Some(stdlib::ITER), "iter.spl" => Some(stdlib::ITER),
"http.spl" => Some(stdlib::HTTP), "http.spl" => Some(stdlib::HTTP),
"stream.spl" => Some(stdlib::STREAM), "stream.spl" => Some(stdlib::STREAM),
"messaging.spl" => Some(stdlib::MESSAGING),
_ => None, _ => None,
}; };
if let Some(x) = s.strip_prefix('#') { if let Some(x) = s.strip_prefix('#') {

View file

@ -3,3 +3,4 @@ pub const NET: &str = include_str!("../net.spl");
pub const ITER: &str = include_str!("../iter.spl"); pub const ITER: &str = include_str!("../iter.spl");
pub const HTTP: &str = include_str!("../http.spl"); pub const HTTP: &str = include_str!("../http.spl");
pub const STREAM: &str = include_str!("../stream.spl"); pub const STREAM: &str = include_str!("../stream.spl");
pub const MESSAGING: &str = include_str!("../messaging.spl");

15
std.spl
View file

@ -111,6 +111,21 @@ construct _array-ext {
4 { any | with this ; 4 { any | with this ;
4 this:get 4 this:get
} }
=0 { | with this ;
0 this:set;
}
=1 { | with this ;
1 this:set;
}
=2 { | with this ;
2 this:set;
}
=3 { | with this ;
3 this:set;
}
=4 { | with this ;
4 this:set;
}
} include _array-ext in array } include _array-ext in array
"#iter.spl" import "#iter.spl" import

View file

@ -1,6 +1,7 @@
"#stream.spl" import "#stream.spl" import
"#http.spl" import "#http.spl" import
"#messaging.spl" import
"SPL tester" =program-name "SPL tester" =program-name
@ -138,7 +139,19 @@ func main { int | with args ;
} with { with e ; } with { with e ;
e:message println e:message println
} }
"" println
"testing messages" println
def bus messaging:Bus:new =bus
bus:subscribe <{ "testmsg1" { | with message ; message:name print " called1 1" println } }
bus:subscribe <{ "testmsg1" { | with message ; message:name print " called1 2" println } }
bus:subscribe <{ "testmsg2" { | with message ; message:name print " called2 1" println } }
bus:subscribe <{ "testmsg2" { | with message ; message:name print " called2 2" println } }
"testmsg1" bus:publish
"testmsg2" bus:publish
"testmsg1" bus:publish
"testmsg3" bus:publish
100 100
} }