Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Created October 25, 2014 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natefaubion/f4be4c8531ef45de87b4 to your computer and use it in GitHub Desktop.
Save natefaubion/f4be4c8531ef45de87b4 to your computer and use it in GitHub Desktop.
macro constexpr {
rule { ($e:expr) } => {
(function() {
macro cexpr {
case { _ } => {
return [makeValue($e, #{ here })];
}
}
return cexpr;
}())
}
}
// Unfortunately requires an IIFE to appease the parser. Needs
// localexpand if we want to get rid of it.
// var a = 1 + constexpr(1 + 2);
@trxcllnt
Copy link

update -- this works now

macro constexpr {
    case {_($e:expr) } => {
        return localExpand(#{
            macro cexpr {
                case { _ } => {
                    return [makeValue($e, #{ here })];
                }
            }
            cexpr
        });
    }
}

// compiles to `var a = 1 + 3;`
var a = 1 + constexpr(1 + 2); 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment