Skip to content

Instantly share code, notes, and snippets.

@Mouq
Created February 2, 2015 07:10
Show Gist options
  • Select an option

  • Save Mouq/bdddda34e4e0c5569b35 to your computer and use it in GitHub Desktop.

Select an option

Save Mouq/bdddda34e4e0c5569b35 to your computer and use it in GitHub Desktop.
use Inline::C;
use C::Parser::Grammar;
use C::Parser::Actions;
use Grammar::Mixin;
my role Inline::Grammar {
method INTERPOLATE(|c) {Grammar.'INTERPOLATE'(self,|c);}
token routine_declarator:sym<inline> {
:my $*InlineC := True;
:my $*LINE_NO := HLL::Compiler.lineof(self.orig(), self.from(), :cache(1));
<sym> <.end_keyword> <routine_def('sub')> # Generalize to <routine_declarator>?
}
token blockoid {
:my $*CURPAD;
:my %*HANDLERS;
<.finishpad>
[
| <InlineC> '{' ~ '}' [<C11=.FOREIGN_LANG: 'C11', 'TOP'>||<panic: "Couldn't parse C">]
| '{YOU_ARE_HERE}' <you_are_here>
| :dba('block') '{' ~ '}' <statementlist(1)> <?ENDSTMT>
| <?terminator> { $*W.throw($/, 'X::Syntax::Missing', what =>'block') }
| <?> { $*W.throw($/, 'X::Syntax::Missing', what => 'block') }
]
{ $*CURPAD := $*W.pop_lexpad() }
}
token InlineC { <?{$*InlineC}> }
}
my role Inline::Actions {
method routine_declarator:sym<inline>($/) { make $<routine_def>.ast }
method routine_def(Mu $/) {
nextsame;
if $*InlineC {
$*W.apply_trait($/, '&trait_mod:<is>', $/.made, :inline<C>);
}
}
method blockoid(Mu $/) {
if $<InlineC> {
my $past := QAST::Stmts.new(QAST::Stmt.new(
QAST::Want.new(
QAST::WVal.new( :value(Str) ),
'Ss', QAST::SVal.new( :value(~$<C11>)),
)
));
my $BLOCK := $*CURPAD;
$BLOCK.blocktype('declaration_static');
$BLOCK.push($past);
$BLOCK.node($/);
$BLOCK.annotate('handlers', %*HANDLERS) if %*HANDLERS;
make $BLOCK;
} else { nextsame }
}
}
sub slangify ($role, :$into = 'MAIN') {
nqp::bindkey(%*LANG, $into, %*LANG{$into}.HOW.mixin(%*LANG{$into}, $role));
}
sub new-slang ($into, $grammar, $actions) {
nqp::bindkey(%*LANG, $into, $grammar);
nqp::bindkey(%*LANG, $into~'-actions', $actions);
}
sub EXPORT(|) {
new-slang 'C11', C::Parser::Grammar, C::Parser::Actions;
slangify Inline::Grammar, :into<MAIN>;
slangify Inline::Actions, :into<MAIN-actions>;
{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment