Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
50
Task/S-expressions/Sidef/s-expressions.sidef
Normal file
50
Task/S-expressions/Sidef/s-expressions.sidef
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
func sexpr(txt) {
|
||||
txt.trim!
|
||||
|
||||
if (txt.match(/^\((.*)\)$/s)) {|m|
|
||||
txt = m[0]
|
||||
}
|
||||
else {
|
||||
die "Invalid: <<#{txt}>>"
|
||||
}
|
||||
|
||||
var w
|
||||
var ret = []
|
||||
|
||||
while (!txt.is_empty) {
|
||||
given (txt.first) {
|
||||
when('(') {
|
||||
(w, txt) = txt.extract_bracketed('()');
|
||||
w = sexpr(w)
|
||||
}
|
||||
when ('"') {
|
||||
(w, txt) = txt.extract_delimited('"')
|
||||
w.sub!(/^"(.*)"/, {|s1| s1 })
|
||||
}
|
||||
else {
|
||||
txt.sub!(/^(\S+)/, {|s1| w = s1; '' })
|
||||
}
|
||||
}
|
||||
ret << w
|
||||
txt.trim_beg!
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func sexpr2txt(String e) {
|
||||
e ~~ /[\s"\(\)]/ ? do { e.gsub!('"', '\\"'); %Q("#{e}") } : e
|
||||
}
|
||||
|
||||
func sexpr2txt(expr) {
|
||||
'(' + expr.map {|e| sexpr2txt(e) }.join(' ') + ')'
|
||||
}
|
||||
|
||||
var s = sexpr(%q{
|
||||
|
||||
((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
|
||||
})
|
||||
|
||||
say s # dump structure
|
||||
say sexpr2txt(s) # convert back
|
||||
Loading…
Add table
Add a link
Reference in a new issue