Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge latest changes from mater into lexical-setting.
  • Loading branch information
jnthn committed Mar 6, 2011
2 parents a32c5f9 + a007136 commit ebf7adc
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 94 deletions.
123 changes: 32 additions & 91 deletions src/HLL/Compiler.pm
Expand Up @@ -451,61 +451,22 @@ class HLL::Compiler {
}

method compile($source, *%adverbs) {
Q:PIR {
.local pmc source, adverbs, self
source = find_lex '$source'
adverbs = find_lex '%adverbs'
self = find_lex 'self'
.local pmc compiling, options
compiling = new ['Hash']
.lex '%*COMPILING', compiling
compiling['%?OPTIONS'] = adverbs
.local string target
target = adverbs['target']
target = downcase target
my %*COMPILING<%?OPTIONS> := %adverbs;

.local int stagestats
stagestats = adverbs['stagestats']
.local pmc stages, result, it
result = source
stages = self.'stages'()
it = iter stages
if stagestats goto stagestats_loop
iter_loop:
unless it goto have_result
.local string stagename
stagename = shift it
result = self.stagename(result, adverbs :flat :named)
if target == stagename goto have_result
goto iter_loop

stagestats_loop:
unless it goto have_result
stagename = shift it
$N0 = time
result = self.stagename(result, adverbs :flat :named)
$N1 = time
$N2 = $N1 - $N0
$P0 = getinterp
$P1 = $P0.'stderr_handle'()
$P1.'print'("Stage '")
$P1.'print'(stagename)
$P1.'print'("': ")
$P2 = new ['ResizablePMCArray']
push $P2, $N2
$S0 = sprintf "%.3f", $P2
$P1.'print'($S0)
$P1.'print'(" sec\n")
if target == stagename goto have_result
goto stagestats_loop

have_result:
.return (result)
};
my $target := pir::downcase(%adverbs<target>);
my $result := $source;
my $stderr := pir::getinterp().stderr_handle;
for self.stages() {
my $timestamp := pir::time__N();
$result := self."$_"($result, |%adverbs);
my $diff := pir::time__N() - $timestamp;
if %adverbs<stagestats> {
# TODO: plug in sprintf with %.3f
$stderr.print__N("Stage $_: $diff\n");
}
last if $_ eq $target;
}
return $result;
}

method parse($source, *%adverbs) {
Expand Down Expand Up @@ -677,43 +638,23 @@ class HLL::Compiler {
}

method parse_name($name) {
Q:PIR {
.local string name
$P0 = find_lex '$name'
name = $P0
# split name on ::
.local pmc ns
ns = split '::', name

# move any leading sigil to the last item
.local string sigil
$S0 = ns[0]
sigil = substr $S0, 0, 1
$I0 = index '$@%&', sigil
if $I0 < 0 goto sigil_done
$S0 = replace $S0, 0, 1, ''
ns[0] = $S0
$S0 = ns[-1]
$S0 = concat sigil, $S0
ns[-1] = $S0
sigil_done:

# remove any empty items from the list
.local pmc ns_it
ns_it = iter ns
ns = new ['ResizablePMCArray']
ns_loop:
unless ns_it goto ns_done
$S0 = shift ns_it
unless $S0 > '' goto ns_loop
push ns, $S0
goto ns_loop
ns_done:

# return the result
.return (ns)
};
my @ns := pir::split('::', $name);
my $sigil := pir::substr(@ns[0], 0, 1);

# move any leading sigil to the last item
my $idx := pir::index('$@%&', $sigil);
if $idx >= 0 {
@ns[0] := pir::substr(@ns[0], 1);
@ns[-1] := $sigil ~ @ns[-1];
}

# remove any empty items from the list
# maybe replace with a grep() once we have the setting for sure
my @actual_ns;
for @ns {
pir::push(@actual_ns, $_) unless $_ eq '';
}
@actual_ns;
}

method lineof($target, $pos, :$cache) {
Expand Down
3 changes: 0 additions & 3 deletions src/cheats/hll-grammar.pir
Expand Up @@ -10,9 +10,6 @@ src/cheats/hll-grammar.pir -- Additional HLL::Grammar methods

=cut

.include 'cclass.pasm'
.include 'src/Regex/constants.pir'

.namespace ['HLL';'Grammar']

.sub '' :load :init
Expand Down
40 changes: 40 additions & 0 deletions src/metamodel/how/NQPAttribute.pm
Expand Up @@ -26,4 +26,44 @@ knowhow NQPAttribute {
method box_target() {
$!box_target ?? 1 !! 0
}

method compose($obj) {
my $long_name := ~$!name;
if self.has_mutator {
my $method := pir::substr($long_name, 1);
unless has_method($obj, $method, 0) {
$obj.HOW.add_method($obj.WHAT, $method, method ($value?) {
pir::setattribute__vppsp(self, $obj.WHAT, $long_name, $value)
if pir::defined($value);

pir::getattribute__ppps(self, $obj.WHAT, $long_name);
}
);
}
}
else {
my $method := pir::substr($long_name, 2);
unless has_method($obj, $method, 0) {
$obj.HOW.add_method($obj, $method,
method () {
pir::getattribute__PPPs(self, $obj.WHAT, $long_name);
}
);
}
}
}

# Hack to check twigil.
method has_mutator() {
pir::substr(~$!name, 1, 1) ne '!';
}

sub has_method($target, $name, $local) {
my @methods := $target.HOW.methods($target, :local($local));
for @methods {
if $_ eq $name { return 1; }
}
return 0;
}

}
3 changes: 3 additions & 0 deletions src/metamodel/how/NQPClassHOW.pm
Expand Up @@ -146,6 +146,9 @@ knowhow NQPClassHOW {
# Incorporate any new multi candidates (needs MRO built).
self.incorporate_multi_candidates($obj);

# Compose attributes.
for self.attributes($obj, :local<0> ) { $_.compose($obj) }

# Publish type and method caches.
self.publish_type_cache($obj);
self.publish_method_cache($obj);
Expand Down

0 comments on commit ebf7adc

Please sign in to comment.