tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
34
Task/S-Expressions/Perl/s-expressions-1.pl
Normal file
34
Task/S-Expressions/Perl/s-expressions-1.pl
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use Text::Balanced qw(extract_delimited extract_bracketed);
|
||||
|
||||
sub sexpr
|
||||
{
|
||||
my $txt = $_[0];
|
||||
$txt =~ s/^\s+//s;
|
||||
$txt =~ s/\s+$//s;
|
||||
$txt =~ /^\((.*)\)$/s or die "Not an s-expression: <<<$txt>>>";
|
||||
$txt = $1;
|
||||
|
||||
my $ret = [];
|
||||
my $w;
|
||||
while ($txt ne '') {
|
||||
my $c = substr $txt,0,1;
|
||||
if ($c eq '(') {
|
||||
($w, $txt) = extract_bracketed($txt, '()');
|
||||
$w = sexpr($w);
|
||||
} elsif ($c eq '"') {
|
||||
($w, $txt) = extract_delimited($txt, '"');
|
||||
$w =~ s/^"(.*)"/$1/;
|
||||
} else {
|
||||
$txt =~ s/^(\S+)// and $w = $1;
|
||||
}
|
||||
push @$ret, $w;
|
||||
$txt =~ s/^\s+//s;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
sub quote
|
||||
{ (local $_ = $_[0]) =~ /[\s\"\(\)]/s ? do{s/\"/\\\"/gs; qq{"$_"}} : $_; }
|
||||
|
||||
sub sexpr2txt
|
||||
{ qq{(@{[ map { ref($_) eq '' ? quote($_) : sexpr2txt($_) } @{$_[0]} ]})} }
|
||||
13
Task/S-Expressions/Perl/s-expressions-2.pl
Normal file
13
Task/S-Expressions/Perl/s-expressions-2.pl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
my $s = sexpr(q{
|
||||
|
||||
((data "quoted data" 123 4.5)
|
||||
(data (!@# (4.5) "(more" "data)")))
|
||||
|
||||
});
|
||||
|
||||
# Dump structure
|
||||
use Data::Dumper;
|
||||
print Dumper $s;
|
||||
|
||||
# Convert back
|
||||
print sexpr2txt($s)."\n";
|
||||
Loading…
Add table
Add a link
Reference in a new issue