Last active
November 7, 2019 15:00
-
-
Save ekpyron/64610fa3f7f73efdeee812b379ae280c to your computer and use it in GitHub Desktop.
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
#include <vector> | |
enum class Instruction { STOP, ADD }; | |
struct NotPresent {}; | |
template<template<auto> typename Operation> | |
struct EVMBuiltins | |
{ | |
static auto constexpr STOP = Operation<Instruction::STOP>{}; | |
static auto constexpr ADD = Operation<Instruction::ADD>{}; | |
static auto constexpr WEIRD = NotPresent{}; | |
}; | |
class Pattern | |
{ | |
public: | |
template<Instruction inst> struct PatternGenerator { template<typename... Args> constexpr Pattern operator() (Args&&... _args) const { return { inst, { std::forward<Args>(_args)... } }; }; }; | |
using Builtins = EVMBuiltins<PatternGenerator>; | |
Pattern(int _value) {} | |
Pattern(Instruction _inst, std::vector<Pattern> const& _arguments = {}) {} | |
}; | |
template<typename T> | |
static constexpr bool hasBuiltin(T t) { return !std::is_same_v<std::decay_t<T>, NotPresent>; } | |
template<typename Pattern> | |
void run() { | |
using Builtins = typename Pattern::Builtins; | |
if constexpr (hasBuiltin(Builtins::WEIRD)) { | |
Builtins::WEIRD(1,2,3); | |
Builtins::WEIRD("crazy invalid stuff"); | |
} | |
if constexpr (hasBuiltin(Builtins::ADD)) { | |
Builtins::ADD(1, Builtins::ADD(2, 3)); | |
} else { | |
Builtins::ADD("invalid stuff 2"); | |
} | |
} | |
int main() { | |
run<Pattern>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment