Compare commits

...

2 commits

Author SHA1 Message Date
fd0aed81fd
fix json arrays 2024-11-15 15:19:59 +01:00
c33b25f260
add json.spl, fix messed up =>? operator 2024-11-15 15:01:20 +01:00
5 changed files with 64 additions and 9 deletions

46
spl/json.spl Normal file
View file

@ -0,0 +1,46 @@
construct json namespace {
_StringyJSON
;
props-to-sjson { s | with spaces props this ;
def value, comma
"," spaces if { " " concat } =comma
props:last:get<1> =value
value null eq not if {
value gettype "array" eq if {
"[" value
:iter
:map<| 0 swap properties this:props-to-sjson>
:join<comma> concat
"]" concat
3 stop
}
"\""
value _str :replace<"\\" "\\\\">:replace<"\"" "\\\""> concat
"\"" concat
2 stop
}
"{"
props
:iter
:filter<{ b | :to-stack pop with key ; key ":" eq not key ";" eq not and }>
:map<{ s |
:to-stack with key value ;
"\""
key :replace<"\\" "\\\\">:replace<"\"" "\\\""> concat
"\":" concat spaces if { " " concat }
spaces value properties this:props-to-sjson concat
}>
:join<comma> concat
"}" concat
}
}
construct json:_StringyJSON {
;
sjson { s | with spaces this ;
spaces this properties json:props-to-sjson
}
}

View file

@ -717,8 +717,8 @@ func _'match-else-error { |
} }
func _'match-else-push { | func _'match-else-push { |
if { dup if {
pop swap pop
} }
} }

View file

@ -260,19 +260,17 @@ fn read_block_dyn(
"=" => { "=" => {
if str_words[i + 1] == ">" { if str_words[i + 1] == ">" {
i += 1; i += 1;
let pushing = if str_words[i + 1] == "?" { let cword = &str_words[i + 1];
if cword.contains(|c| c == '?' || c == '!') {
i += 1; i += 1;
}
let pushing = if cword.contains('?') {
words.push(Word::Call("dup".to_owned(), false, 0)); words.push(Word::Call("dup".to_owned(), false, 0));
true true
} else { } else {
false false
}; };
let throwing = if str_words[i + 1] == "!" { let throwing = cword.contains('!');
i += 1;
true
} else {
false
};
if str_words[i + 1] == "[" { if str_words[i + 1] == "[" {
i += 1; i += 1;
let mut block = let mut block =

View file

@ -17,6 +17,7 @@ pub const SERVER: &str = include_str!("../spl/server.spl");
pub const HTTP_SERVER: &str = include_str!("../spl/httpserver/base.spl"); pub const HTTP_SERVER: &str = include_str!("../spl/httpserver/base.spl");
pub const HTTP_SERVER_STATIC: &str = include_str!("../spl/httpserver/static.spl"); pub const HTTP_SERVER_STATIC: &str = include_str!("../spl/httpserver/static.spl");
pub const LINKEDLIST: &str = include_str!("../spl/linkedlist.spl"); pub const LINKEDLIST: &str = include_str!("../spl/linkedlist.spl");
pub const JSON: &str = include_str!("../spl/json.spl");
pub const NOP: &str = ""; pub const NOP: &str = "";
pub fn register(runtime: &mut Runtime) { pub fn register(runtime: &mut Runtime) {
@ -38,6 +39,7 @@ pub fn register(runtime: &mut Runtime) {
insert("httpserver/base.spl", HTTP_SERVER); insert("httpserver/base.spl", HTTP_SERVER);
insert("httpserver/static.spl", HTTP_SERVER_STATIC); insert("httpserver/static.spl", HTTP_SERVER_STATIC);
insert("linkedlist.spl", LINKEDLIST); insert("linkedlist.spl", LINKEDLIST);
insert("json.spl", JSON);
insert("nop.spl", NOP); insert("nop.spl", NOP);
} }
} }

View file

@ -6,6 +6,7 @@
"spl/time.spl" import "spl/time.spl" import
"spl/httpserver/base.spl" import "spl/httpserver/base.spl" import
"spl/linkedlist.spl" import "spl/linkedlist.spl" import
"spl/json.spl" import
"SPL tester" =program-name "SPL tester" =program-name
@ -327,6 +328,14 @@ func main { int | with args ;
} }
} }
^ok =>? ^error not if { println }
"" println
"json test" println
0 [ 0 1 2 "hi" ] properties json:props-to-sjson println