And change the "macrodef" rule to:sub demacro($) { my $text = shift; while (($macro,$pkg) = each %macro ) { my $defn = $$pkg[0]; if (defined $$pkg[1]) { my @args = @{$$pkg[1]}; while ($text =~ m/($macro\((.*?)\))/g) { my $oA = length($`), $oZ = length($&); my @vals = split /\s*,\s*/, $2; (@args eq @vals) or die "Bad arg count for macro '$macro'\n"; my $_defn = $defn; for ($i=0; $i<@args; $i++) { $_defn =~ s/$args[$i]/($vals[$i])/g; } substr($text, $oA, $oZ) = $_defn; } } else { $text =~ s/$macro/$defn/; } } return $text; }
This would process #define's like in:macrodef : '#define' /[a-z]\w*/i '(' <leftop: /[a-z]+/i ',' /[a-z]+/i> ')' /.*/ { $::macro{$item[2]} = [ $item[-1], $item[4] ]; } | '#define' /[a-z]\w*/i /.*/ { $::macro{$item[2]} = [ $item[-1] ]; }
Note that this is no less "naive" than the original demo -- simple string substitution is used for argument instantiation (though this does add parens around each arg instance to preserve operator precedence). E.g., using args "a" and "ab" would fail since the "a"-instantiation would intrude on part of the "ab". A real algorithm would need to rectify this (with smart tokenization, etc.).#define add(a,b,c) (a+b+c) #define mult(d,e) (d*e) #define pi (3.1415) main( ) { add(1,2,3); mult(4,5); float p = pi; }
In reply to Re: Macros in a RecDescent grammar
by mush4brains
in thread Macros in a RecDescent grammar
by mush4brains
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |