Skip to content

Commit

Permalink
First pass through optimizing the QAST node structures, which saves s…
Browse files Browse the repository at this point in the history
…ome time and some memory.
  • Loading branch information
jnthn committed Jul 25, 2012
1 parent 8a9b631 commit aa69d06
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/QAST/Block.nqp
@@ -1,9 +1,11 @@
class QAST::Block is QAST::Node {
has str $!name;
has str $!blocktype;
has int $!custom_args;
has str $!cuid;
has %!symbol;

method name(*@value) { $!name := @value[0] if @value; $!name || "" }
method blocktype(*@value) { $!blocktype := @value[0] if @value; $!blocktype }
method custom_args(*@value) { $!custom_args := @value[0] if @value; $!custom_args }

Expand Down
3 changes: 3 additions & 0 deletions src/QAST/BlockMemo.nqp
@@ -1,6 +1,9 @@
class QAST::BlockMemo is QAST::Node {
has str $!name;
has str $!cuid;

method name(*@value) { $!name := @value[0] if @value; $!name || "" }

my $cur_cuid := 0;
my $cuid_suffix := ~nqp::time_n();

Expand Down
49 changes: 44 additions & 5 deletions src/QAST/Node.nqp
@@ -1,8 +1,20 @@
use NQPHLL;

class QAST::Node is NQPCapture {
class QAST::Node {
# For children.
has @!array
is parrot_vtable_handler('get_pmc_keyed_int')
is parrot_vtable_handler('set_pmc_keyed_int')
is parrot_vtable_handler('exists_keyed_int')
is parrot_vtable_handler('delete_keyed_int')
is parrot_vtable_handler('unshift_pmc')
is parrot_vtable_handler('push_pmc')
;

# For annotations, lazily allocated.
has %!hash;

has $!node;
has $!name;
has str $!named;
has $!returns;
has int $!arity;
Expand All @@ -11,27 +23,54 @@ class QAST::Node is NQPCapture {

method new(*@children, *%options) {
my $new := self.CREATE();
$new.BUILD();
nqp::splice($new.list, @children, 0, 0);
nqp::bindattr($new, QAST::Node, '@!array', @children);
for %options {
nqp::findmethod($new, $_.key)($new, $_.value);
}
$new;
}

method node(*@value) { $!node := @value[0] if @value; $!node }
method name(*@value) { $!name := @value[0] if @value; $!name }
method named(*@value) { $!named := @value[0] if @value; $!named || "" }
method returns(*@value) { $!returns := @value[0] if @value; $!returns }
method arity(*@value) { $!arity := @value[0] if @value; $!arity }
method flat(*@value) { $!flat := @value[0] if @value; $!flat }
method childorder(*@value) { $!childorder := @value[0] if @value; $!childorder || "" }

method list() { @!array }
method pop() { nqp::pop(self.list) }
method push($value) { nqp::push(self.list, $value) }
method shift() { nqp::shift(self.list) }
method unshift($value) { nqp::unshift(self.list, $value) }

method hash() {
%!hash
}
method ($key) is parrot_vtable('get_pmc_keyed_str') {
%!hash{$key}
}
method ($key) is parrot_vtable('get_pmc_keyed') {
%!hash{$key}
}
method ($key, $value) is parrot_vtable('set_pmc_keyed_str') {
%!hash{$key} := $value;
}
method ($key, $value) is parrot_vtable('set_pmc_keyed') {
%!hash{$key} := $value;
}
method ($key) is parrot_vtable('exists_keyed_str') {
nqp::existskey(%!hash, $key)
}
method ($key) is parrot_vtable('exists_keyed') {
nqp::existskey(%!hash, $key)
}
method ($key) is parrot_vtable('delete_keyed_str') {
nqp::deletekey(%!hash, $key)
}
method ($key) is parrot_vtable('delete_keyed') {
nqp::deletekey(%!hash, $key)
}

my %uniques;
method unique($prefix) {
my $id := nqp::existskey(%uniques, $prefix) ??
Expand Down
4 changes: 3 additions & 1 deletion src/QAST/Op.nqp
@@ -1,5 +1,7 @@
class QAST::Op is QAST::Node {
has str $!name;
has str $!op;

method op(*@value) { $!op := @value[0] if @value; $!op }
method name(*@value) { $!name := @value[0] if @value; $!name || "" }
method op(*@value) { $!op := @value[0] if @value; $!op }
}
2 changes: 2 additions & 0 deletions src/QAST/Regex.nqp
@@ -1,11 +1,13 @@
class QAST::Regex is QAST::Node {
has $!name;
has str $!rxtype;
has str $!subtype;
has str $!backtrack;
has int $!negate;
has int $!min;
has int $!max;

method name(*@value) { $!name := @value[0] if @value; $!name }
method rxtype(*@value) { $!rxtype := @value[0] if @value; $!rxtype || "" }
method subtype(*@value) { $!subtype := @value[0] if @value; $!subtype || "" }
method backtrack(*@value) { $!backtrack := @value[0] if @value; $!backtrack || "" }
Expand Down
3 changes: 1 addition & 2 deletions src/QAST/VM.nqp
Expand Up @@ -3,8 +3,7 @@ class QAST::VM is QAST::Node {

method new(*@children, *%alternatives) {
my $obj := nqp::create(self);
nqp::bindattr($obj, NQPCapture, '@!array', @children);
nqp::bindattr($obj, NQPCapture, '%!hash', nqp::hash());
nqp::bindattr($obj, QAST::Node, '@!array', @children);
nqp::bindattr($obj, QAST::VM, '%!alternatives', %alternatives);
$obj
}
Expand Down
2 changes: 2 additions & 0 deletions src/QAST/Var.nqp
@@ -1,9 +1,11 @@
class QAST::Var is QAST::Node {
has str $!name;
has str $!scope;
has str $!decl;
has int $!slurpy;
has $!default;

method name(*@value) { $!name := @value[0] if @value; $!name || "" }
method scope(*@value) { $!scope := @value[0] if @value; $!scope }
method decl(*@value) { $!decl := @value[0] if @value; $!decl }
method slurpy(*@value) { $!slurpy := @value[0] if @value; $!slurpy }
Expand Down

0 comments on commit aa69d06

Please sign in to comment.