-
-
Save japhb/92442a22962c5156e102 to your computer and use it in GitHub Desktop.
Slang-modifying macro idea
This file contains 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
(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