Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Try doing this:

    $ bin/007 --backend=i13n examples/name.007

Should get this output:

        # static frame #1
        # call A from line zero: frame 2
        # static frame #3
        # call B from line zero: frame 4
     1. macro name(expr) {
            # static frame #5
            # call C from line zero: frame 6
     2.     if expr ~~ Q::Postfix::Property {
     3.         expr = expr.property;
     4.     }
            # static frame #7
            # call D from line zero: frame 8
     5.     if expr !~~ Q::Identifier {
     6.         throw new Exception {
     7.             message: "Cannot turn a " ~ type(expr) ~ " into a name"
     8.         };
     9.     }
            # static frame #9
            # call E from line zero: frame 10
            # call F from line zero: frame 11
    10.     return quasi { expr.name };
    11. }
    12.
    13. my info = {
    14.     foo: "Bond",
    15.     bar: {
    16.         baz: "James Bond"
    17.     },
    18. };
    19.
    20. say(name(info));           # info
    21. say(name(info.foo));       # foo
    22. say(name(info.bar.baz));   # baz
  • Loading branch information
Carl Masak committed Mar 4, 2017
1 parent 244f5da commit 37f778c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
16 changes: 14 additions & 2 deletions bin/007
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use v6;
use _007;
use _007::Backend::JavaScript;
use _007::Instrumentation;

class Ref {
has Str $.deref;
Expand All @@ -18,17 +19,28 @@ constant %BACKENDS = hash
"js" => ref("javascript"),
"ast" => -> $ast, $ { say ~$ast },
"unexpanded-ast" => -> $ast, $ { say ~$ast },
"i13n" => -> $ast, $ {
my $width = ceiling(log($*program.lines + 1)/log(10));
say $*program.lines.kv.map(-> $i, $line {
my $line-number = $i + 1;
my @lines = _007::Instrumentation.annotations-for-line($line-number);
my $indent = ($line ~~ /^ " "*/).chars;
@lines.=map({ " " x $width ~ " " ~ " " x $indent ~ "# " ~ $_ });
@lines.push(sprintf "%{$width}d. %s", $line-number, $line);
slip(@lines);
}).join("\n")
},
;

sub run_007($program, Str $backend is copy) {
sub run_007($*program, Str $backend is copy) {
die "Unknown backend '$backend'"
unless %BACKENDS{$backend} :exists;
$backend = %BACKENDS{$backend}.deref
while %BACKENDS{$backend} ~~ Ref;

my $runtime = _007.runtime;
my $unexpanded = $backend eq "unexpanded-ast";
my $ast = _007.parser(:$runtime).parse($program, :$unexpanded);
my $ast = _007.parser(:$runtime).parse($*program, :$unexpanded);
%BACKENDS{$backend}($ast, $runtime);
}

Expand Down
7 changes: 6 additions & 1 deletion lib/_007/Parser/Syntax.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use _007::Val;
use _007::Q;
use _007::Instrumentation;

sub check-feature-flag($feature, $word) {
my $flag = "FLAG_007_{$word}";
Expand All @@ -19,7 +20,11 @@ grammar _007::Parser::Syntax {
token newpad { <?> {
$*parser.push-opscope;
@*declstack.push(@*declstack ?? @*declstack[*-1].clone !! {});
$*runtime.enter($*runtime.current-frame, Val::Object.new, Q::StatementList.new);
my $static-lexpad = Val::Object.new;
$*runtime.enter($*runtime.current-frame, $static-lexpad, Q::StatementList.new);
my $line-number = self.orig.substr(0, self.pos).comb("\n") + 1;
_007::Instrumentation.register-static-lexpad($static-lexpad, $line-number);
_007::Instrumentation.annotate-line($line-number, "static frame #{_007::Instrumentation.frame-counter++}");
} }

token finishpad { <?> {
Expand Down
10 changes: 8 additions & 2 deletions lib/_007/Runtime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use _007::Val;
use _007::Q;
use _007::Runtime::Builtins;
use _007::OpScope;
use _007::Instrumentation;

constant NO_OUTER = Val::Object.new;
constant RETURN_TO = Q::Identifier.new(
:name(Val::Str.new(:value("--RETURN-TO--"))),
:frame(NONE));

my $call-counter = "A";

class _007::Runtime {
has $.input;
has $.output;
Expand All @@ -31,7 +34,7 @@ class _007::Runtime {
}
}

method enter($outer-frame, $static-lexpad, $statementlist, $routine?) {
method enter($outer-frame, $static-lexpad, $statementlist, $routine?, $caller-line-number = "zero") {
my $frame = Val::Object.new(:properties(:$outer-frame, :pad(Val::Object.new)));
@!frames.push($frame);
for $static-lexpad.properties.kv -> $name, $value {
Expand Down Expand Up @@ -62,6 +65,9 @@ class _007::Runtime {
my $identifier = Q::Identifier.new(:$name, :$frame);
self.declare-var($identifier, $routine);
}
if _007::Instrumentation.static-lexpads{$static-lexpad} -> $line-number {
_007::Instrumentation.annotate-line($line-number, "call {$call-counter++} from line {$caller-line-number}: frame {_007::Instrumentation.frame-counter++}");
}
}

method leave {
Expand Down Expand Up @@ -156,7 +162,7 @@ class _007::Runtime {
if $c.hook -> &hook {
return &hook(|@arguments) || NONE;
}
self.enter($c.outer-frame, $c.static-lexpad, $c.statementlist, $c);
self.enter($c.outer-frame, $c.static-lexpad, $c.statementlist, $c, 17);
for @($c.parameterlist.parameters.elements) Z @arguments -> ($param, $arg) {
self.declare-var($param.identifier, $arg);
}
Expand Down

0 comments on commit 37f778c

Please sign in to comment.