From 6808aa38b1845bf4bc92ed1cbf6005911e95385a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Fri, 3 Nov 2017 10:51:36 +0100 Subject: Add the built-in macro $IF() to handle conditionals Add an $IF() macro to conditionally expand macros. Use it like this: $IF(Expression, IfTrue, IfFalse) Expression is a Perl expression that can be evaulated at macro expansion time. If the expression evaluates to 0, the result will be IfFalse, otherwise IfTrue. --- erts/emulator/utils/beam_makeops | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'erts/emulator/utils') diff --git a/erts/emulator/utils/beam_makeops b/erts/emulator/utils/beam_makeops index 452e070482..dc4da0cca0 100755 --- a/erts/emulator/utils/beam_makeops +++ b/erts/emulator/utils/beam_makeops @@ -309,7 +309,8 @@ if ($wordsize == 64) { my %predef_macros = (IS_PACKED => ['Expr'], OPERAND_POSITION => ['Expr'], -); + IF => ['Expr','IfTrue','IfFalse'], + ); foreach my $name (keys %predef_macros) { my @args = @{$predef_macros{$name}}; my $body = join(':', map { '$' . $_ } @args); @@ -1659,9 +1660,17 @@ sub expand_macro { } } elsif ($name eq 'IS_PACKED') { $body = ($body =~ /^I\[\d+\]$/) ? 0 : 1; + } elsif ($name eq 'IF') { + my $expr = $new_bindings{Expr}; + my $bool = eval $expr; + if ($@ ne '') { + &error("bad expression '$expr' in \$IF()"); + } + my $part = $bool ? 'IfTrue' : 'IfFalse'; + $body = $new_bindings{$part}; } - # Wrap body if needed and return resul.t + # Wrap body if needed and return result. $body = "do {\n$body\n} while (0)" if needs_do_wrapper($body); ($body,$rest); -- cgit v1.2.3