Skip to content

Instantly share code, notes, and snippets.

@japhb
Created December 6, 2014 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japhb/92442a22962c5156e102 to your computer and use it in GitHub Desktop.
Save japhb/92442a22962c5156e102 to your computer and use it in GitHub Desktop.
Slang-modifying macro idea
(Note: please excuse silliness caused by lack of experience with Perl 6's parsing; the important bit is the idea, not the details.)
Idea: Macro declarations are internally treated as methods on the current slang, and get the sugar of being able to use as 'types' the various grammar rules in the current slang. In addition, you can AST-bind to create temporaries for use in the returned code. Examples follow:
# Accepts a variable and an identifier and results in a valid dotty:
# my $a = call_a_method($foo::bar, bark);
macro call_a_method(variable ¤obj, identifier ¤meth --> dotty) {
¤obj.¤meth
}
# Expects two terms and results in a %comma expression that doesn't re-evaluate the terms:
# ($lo, $hi) = interval(6+3i, sqrt($a + $b));
macro interval(term ¤base, term ¤range --> %comma) {
my ¤b ¤= ¤base;
my ¤r ¤= ¤range / 2;
¤b - ¤r, ¤b + ¤r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment