-
Notifications
You must be signed in to change notification settings - Fork 1
/
ternaryparser.h
33 lines (26 loc) · 981 Bytes
/
ternaryparser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// Created by Shae Bolt on 8/13/2017.
//
#ifndef TERNARYVM_TERNARYPARSER_H
#define TERNARYVM_TERNARYPARSER_H
#include <exception>
typedef unsigned long long ULL;
// Delcare the litparser
template<ULL Sum, ULL num_val, char... Chars> struct litparser;
// Specialize on the case where there's at least one character left:
template<ULL Sum, ULL num_val,char Head, char... Rest>
struct litparser<Sum, num_val, Head, Rest...> {
// parse a digit. recurse with new sum and ramaining digits
static_assert(Sum < 6);
static const ULL value = litparser<
(Head < '0' || Head >'2') ? throw std::exception() :
Sum+1,
Head != '2' ? num_val*4 + (Head - '0') : num_val*4 + 3,
Rest...>::value;
};
// When 'Rest' finally is empty, we reach this terminating case
template<ULL Sum, ULL num_val> struct litparser<Sum, num_val> {
static const ULL value = num_val;
//static_assert(Sum == 6);
};
#endif //TERNARYVM_TERNARYPARSER_H