Skip to content

For TPRC 2023 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/assign/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ sub gen_code {
my $def = $elem->{def} // '';
$def &&= " // $def";

push @$code,
($elem->sigil eq '@')
? "$var $oper \@$from\[$i..\@$from-1\]$def;" :
($elem->{cast})
? "$var $oper \[\@$from\[$i..\@$from-1\]\]$def;" :
"$var $oper $from\->[$i]$def;";
if ($elem->sigil eq '@' || $elem->{cast}) {
my $begin = $i;
my $end = abs(@$elems - $i);
$i = - $end;

push @$code,
($elem->sigil eq '@')
? "$var $oper \@$from\[$begin..\@$from-$end\]$def;"
: "$var $oper \[\@$from\[$begin..\@$from-$end\]\]$def;";
}
else {
push @$code, "$var $oper $from\->[$i]$def;";
}

$i++;
}
Expand Down
48 changes: 40 additions & 8 deletions lib/assign/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use assign::Struct;
use base 'assign::Struct';

use assign::Types;
use assign::Array;

use XXX;

Expand All @@ -17,13 +18,38 @@ sub parse_elem {
my $type = ref($tok);
next if $type eq 'PPI::Token::Whitespace';

if ($type eq 'PPI::Token::Word') {
$self->parse_fat_comma;
$self->parse_whitespaces;

my $str = $tok->content;
my $v = shift(@$in);
if ($v && ref($v) eq 'PPI::Structure::Constructor') {
my $deepkey = "{$str}";
if ($v->braces eq '[]') {
my $struct = assign::Array->new(node => $v, deepkey => $deepkey)->parse;
push @$elems, $struct;
}
elsif ($v->braces eq '{}') {
my $struct = assign::Hash->new(node => $v, deepkey => $deepkey)->parse;
push @$elems, $struct;
}
else {
XXX $v, "Expecting [...] or {...}";
}
}

return 1;
}

if ($type eq 'PPI::Token::Symbol') {
my $str = $tok->content;
if ($str =~ /^\$\w+$/) {
push @$elems, assign::var->new($str);
return 1;
}
}

XXX $tok, "unexpected token";
}
return 0;
Expand All @@ -35,21 +61,27 @@ sub gen_code {
my $code = [ @$init ];
my $elems = $self->{elems};

if ($decl) {
if ($decl && (my @to_declares = grep $_->can("val"), @$elems)) {
push @$code,
"$decl(" .
join(', ',
map $_->val,
@$elems
) .
');';
map $_->val,
@to_declares
) . ');'
}

for my $elem (@$elems) {
my $type = ref $elem;
my $var = $elem->val;
(my $key = $var) =~ s/^\$//;
push @$code, "$var $oper $from\->{$key};";
if ( $type eq 'assign::Hash' || $type eq 'assign::Array' ) {
my $_from = $from . ($elem->{deepkey} ? "->" . $elem->{deepkey} : "");
push @$code, $elem->gen_code($decl, $oper, $_from, $init);
}
else {
my $var = $elem->val;
(my $key = $var) =~ s/^\$//;
my $deepkey = $elem->{deepkey};
push @$code, "$var $oper $from\->$deepkey\{$key\};";
}
}

return join "\n", @$code;
Expand Down
58 changes: 57 additions & 1 deletion lib/assign/Struct.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sub new {
my $class = shift;
bless {
elems => [],
deepkey => "",
@_,
}, $class;
}
Expand Down Expand Up @@ -37,7 +38,7 @@ sub parse {

while (1) {
$self->parse_elem or last;
$self->parse_comma or last;
$self->parse_optional_comma or last;
}

return $self;
Expand All @@ -63,6 +64,61 @@ sub parse_comma {
return 0;
}

sub parse_optional_comma {
my ($self) = @_;
my $in = $self->{in};
while (@$in) {
my $tok = shift(@$in);
my $type = ref($tok);
next if $type eq 'PPI::Token::Whitespace';

if ($type eq 'PPI::Token::Operator' and
$tok->content eq ','
) {
return 1;
}
else {
unshift(@$in, $tok);
last;
}
}
return 0;
}

sub parse_fat_comma {
my ($self) = @_;

$self->parse_whitespaces;

my $in = $self->{in};
while (@$in) {
my $tok = shift(@$in);
my $type = ref($tok);

if ($type eq 'PPI::Token::Operator' and
$tok->content eq '=>'
) {
return 1;
}
else {
XXX $tok, $in, "fat comma expected";
}
}
return 0;
}

sub parse_whitespaces {
my ($self) = @_;
my $in = $self->{in};
while (@$in) {
my $tok = shift(@$in);
next if ref($tok) eq 'PPI::Token::Whitespace';
unshift @$in, $tok;
last;
}
return 0;
}

sub get_var {
my ($self, $var) = @_;
my $def;
Expand Down
1 change: 1 addition & 0 deletions lib/assign/Types.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sub new {
bless {
var => $var,
def => $def,
deepkey => "",
}, $class;
}

Expand Down
15 changes: 15 additions & 0 deletions talk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SHELL := bash

VROOM := \
vroom \
clean \

default:
ifdef s
vroom vroom --skip=$s
else
vroom vroom
endif

$(VROOM):
vroom $@
10 changes: 10 additions & 0 deletions talk/destructure.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env clojure

(def people [
{ :name "Ingy" :favs ["coffee" "yellow"] }
{ :name "Gugod" :favs ["tea" "blue"] }])

(defn about [{:keys [name] [color drink] :favs}]
(println (str name " wears " color " and drinks " drink ".")))

(map about people)
11 changes: 11 additions & 0 deletions talk/destructure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

const people = [
{ name: 'Ingy', favs: ['coffee', 'yellow'] },
{ name: 'Gugod', favs: ['tea', 'blue'] },
];

people.forEach((person) => {
const { name, favs: [ drink, color ] } = person;
console.log(`${name} wears ${color} and drinks ${drink}.`);
});
19 changes: 19 additions & 0 deletions talk/destructure.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env perl

use v5.16;

use assign::0;

my $people = [
{ name => 'Ingy', favs => ['coffee', 'yellow'] },
{ name => 'Gugod', favs => ['tea', 'blue'] },
];

for my $person (@people) {
my {
$name,
favs => [ $drink, $color ],
} = $person;

say "$name wears $color and drinks $drink.";
}
10 changes: 10 additions & 0 deletions talk/destructure2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node

const people = [
{ name: 'Ingy', favs: ['coffee', 'yellow'] },
{ name: 'Gugod', favs: ['tea', 'blue'] },
];

people.forEach(({ name, favs: [ drink, color ] }) => {
console.log(`${name} wears ${color} and drinks ${drink}.`);
});
Loading