Created
March 6, 2011 12:13
-
-
Save tene/857239 to your computer and use it in GitHub Desktop.
p5 macro declarative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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