Skip to content

Instantly share code, notes, and snippets.

@tene
Created March 6, 2011 12:13
Show Gist options
  • Save tene/857239 to your computer and use it in GitHub Desktop.
Save tene/857239 to your computer and use it in GitHub Desktop.
p5 macro declarative
# Macro definition, like, Perl 6 grammars, will use () for capturing subgroup
# and [] for noncapturing grouping, and <...> to generate named parsing rules
macro manip_select
{
'select' (
'*' |
[ [ <ident> '(' <ident> ')' | <ident> ] ** ',' ]
) 'from' <term>
('where' (
( <ident> <[!<>=]>+ <ident> ) ** [ 'and' | 'or' ]
))?
} -> $select, $data, $where {
Imvu::Data::manipulate( select => $cols, data => $data, where => $where);
};
my $data = [ { a => 1, b => 2}, { a => 3, b => 4} ];
select min(b) from $data;
select a, b from $data where a > 1 and b < 5;
# The macro definition generates this sub definition from the arg
# list and the macro body
sub manip_select {
my ($select, $data, $where) = @_;
manipulate( select => $cols, data => $data, where => $where);
}
my $data = [ { a => 1, b => 2}, { a => 3, b => 4} ];
manip_select(['min(b)'], $data, []);
manip_select(['a','b'], $data, [['a','>','1'],'and',['b','<',5]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment