Skip to content

Commit

Permalink
Rewrite op_macro with protoregexes
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek committed Feb 23, 2011
1 parent 72728af commit 60d186d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 90 deletions.
144 changes: 71 additions & 73 deletions compilers/opsc/src/Ops/Compiler/Actions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -259,93 +259,91 @@ method op_body($/) {
make $past;
}

method op_macro:sym<expr offset>($/) {
make PAST::Op.new(
:pasttype<call>,
:name<expr_offset>,
$<arg>.ast,
);
}

method op_macro($/) {
#say('# op_macro');
# Generate set of calls to Trans:
# goto NEXT() -> goto_offset(opsize())
# goto OFFSET($addr) -> goto_offset($addr)
# goto ADDRESS($addr) -> goto_address($addr)
# expr NEXT() -> expr_offset(opsize())
# expr OFFSET($addr) -> expr_offset($addr)
# expr ADDRERR($addr) -> expr_address($addr)
# restart NEXT() -> restart_offset(opsize()); goto_address(0)
# restart OFFSET() -> restart_offset($addr); goto_offset($addr)
# XXX In trunk "restart ADDRESS" equivalent of "goto ADDRESS".
# restart ADDRESS() -> restart_address($addr); goto_address($addr)
method op_macro:sym<goto offset>($/) {
make PAST::Op.new(
:pasttype<call>,
:name<goto_offset>,
$<arg>.ast,
);
}

my $macro_type := ~$<macro_type>;
my $macro_dest := ~$<macro_destination>;
my $is_next := $macro_dest eq 'NEXT';
my $macro_name := $macro_type ~ '_' ~ lc($is_next ?? 'offset' !! $macro_dest);
method op_macro:sym<expr address>($/) {
make PAST::Op.new(
:pasttype<call>,
:name<expr_address>,
$<arg>.ast,
);
}

my $past := PAST::Stmts.new;
method op_macro:sym<goto address>($/) {
make PAST::Op.new(
:pasttype<call>,
:name<goto_address>,
$<arg>.ast,
);
}

my $macro := PAST::Op.new(
:pasttype('call'),
:name($macro_name),
method op_macro:sym<expr next>($/) {
my $past := PAST::Op.new(
:pasttype<call>,
:name<expr_offset>,
PAST::Op.new(
:pasttype<call>,
:name<OPSIZE>,
)
);
$past.push($macro);
$past<jump> := <PARROT_JUMP_RELATIVE>;

$past<jump> := list();
make $past;
}

if $macro_type ne 'expr' && $macro_dest eq 'OFFSET' {
$past<jump>.push('PARROT_JUMP_RELATIVE');
}
method op_macro:sym<goto next>($/) {
my $past := PAST::Op.new(
:pasttype<call>,
:name<goto_offset>,
PAST::Op.new(
:pasttype<call>,
:name<OPSIZE>,
)
);
$past<jump> := <PARROT_JUMP_RELATIVE>;

if $macro_type eq 'expr' || $macro_type eq 'goto' {
if $is_next {
$macro.push(PAST::Op.new(
:pasttype<call>,
:name<OPSIZE>,
));
}
else {
process_op_macro_body_word($/, $macro);
}
}
elsif $macro_type eq 'restart' {
if $is_next {
$macro.push(PAST::Op.new(
make $past;
}


method op_macro:sym<restart next> ($/) {
#say('# op_macro');
# restart NEXT() -> restart_offset(opsize()); goto_address(0)
my $past := PAST::Stmts.new(
PAST::Op.new(
:pasttype<call>,
:name<restart_offset>,
PAST::Op.new(
:pasttype<call>,
:name<OPSIZE>,
));
}
else {
process_op_macro_body_word($/, $macro);
}

$macro := PAST::Op.new(
)
),
PAST::Op.new(
:pasttype<call>,
:name<goto_address>,
);
if $is_next {
$macro.push(PAST::Op.new(
:pasttype<inline>,
:inline<0>,
));
}
else {
process_op_macro_body_word($/, $macro);
}
$past.push($macro);
}
else {
pir::die("Horribly");
}
PAST::Val.new(
:value<0>
)
),
);

make $past;
}
$past<jump> := <PARROT_JUMP_RELATIVE>;

sub process_op_macro_body_word($/, $macro) {
#_dumper($<body_word>);
if $<body_word> {
for $<body_word> {
#say(' word ' ~ $_);
my $bit := $_.ast;
$macro.push($_.ast) if defined($bit);
}
}
make $past;
}

method blockoid ($/) {
Expand Down
25 changes: 8 additions & 17 deletions compilers/opsc/src/Ops/Compiler/Grammar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,16 @@ token macro_param {
'$' $<num>=<integer> # Up to nine params.
}

rule op_macro {
<macro_type> <macro_destination> '(' <arg=.EXPR>? ')'
}

token macro_type {
[
| 'goto'
| 'expr'
| 'restart'
]
}
proto rule op_macro { <...> }
rule op_macro:sym<goto offset> { 'goto' 'OFFSET' '(' <arg=.EXPR> ')' }
rule op_macro:sym<expr offset> { 'expr' 'OFFSET' '(' <arg=.EXPR> ')' }
rule op_macro:sym<goto address> { 'goto' 'ADDRESS' '(' <arg=.EXPR> ')' }
rule op_macro:sym<expr address> { 'expr' 'ADDRESS' '(' <arg=.EXPR> ')' }
rule op_macro:sym<goto next> { 'goto' 'NEXT' '(' ')' }
rule op_macro:sym<expr next> { 'expr' 'NEXT' '(' ')' }
rule op_macro:sym<restart next> { 'restart' 'NEXT' '(' ')' }

token macro_destination {
[
| 'OFFSET'
| 'ADDRESS'
| 'NEXT'
]
}

token identifier {
<!keyword> <ident>
Expand Down

0 comments on commit 60d186d

Please sign in to comment.