diff options
author | rtk0c <[email protected]> | 2022-05-29 16:14:26 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-05-29 16:14:26 -0700 |
commit | 66b63ae887f553e1cac813546a6b827a9c85d80c (patch) | |
tree | c404dd9cc6a74853f8ea409cdeeb1419b04cef44 /buildtools/codegen/CodegenDecl.cpp | |
parent | 1a6f1ea3b76c3ed4cad5aba5502af390ce50a2c0 (diff) |
Changeset: 43 Add tostring code gen for enums
Diffstat (limited to 'buildtools/codegen/CodegenDecl.cpp')
-rw-r--r-- | buildtools/codegen/CodegenDecl.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/buildtools/codegen/CodegenDecl.cpp b/buildtools/codegen/CodegenDecl.cpp new file mode 100644 index 0000000..ac6bb01 --- /dev/null +++ b/buildtools/codegen/CodegenDecl.cpp @@ -0,0 +1,48 @@ +#include "CodegenDecl.hpp" + +#include <Utils.hpp> + +static EnumValuePattern NextPattern(EnumValuePattern val) { + return (EnumValuePattern)(val + 1); +} + +EnumValuePattern DeclEnum::CalcPattern() const { + if (elements.empty()) return EVP_Continuous; + + auto pattern = EVP_Continuous; +restart: + auto lastVal = elements[0].value; + for (size_t i = 1; i < elements.size(); ++i) { + auto currVal = elements[i].value; + switch (pattern) { + case EVP_Continuous: { + bool satisfy = lastVal + 1 == currVal; + if (!satisfy) { + pattern = NextPattern(pattern); + goto restart; + } + } break; + + case EVP_Bits: { + bool satisfy = (lastVal << 1) == currVal; + if (!satisfy) { + pattern = NextPattern(pattern); + goto restart; + } + } break; + + // A random pattern can match anything + case EVP_Random: + case EVP_COUNT: break; + } + lastVal = currVal; + } + + return pattern; +} + +void DeclEnum::CalcPatternIfNecessary() { + if (pattern == EVP_COUNT) { + CalcPattern(); + } +} |