| [b276be5] | 1 | #pragma once
 | 
|---|
| [0d10090] | 2 | #include <cstddef>
 | 
|---|
| [b276be5] | 3 | #include <exception>
 | 
|---|
| [0d10090] | 4 | #include <iomanip>
 | 
|---|
| [b276be5] | 5 | #include <memory>
 | 
|---|
| [0d10090] | 6 | #include <ostream>
 | 
|---|
| [b276be5] | 7 | #include <string>
 | 
|---|
 | 8 | #include <typeinfo>
 | 
|---|
 | 9 | #include <typeindex>
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | class bad_cast : public std::exception {
 | 
|---|
 | 12 |         std::string why;
 | 
|---|
 | 13 | public:
 | 
|---|
 | 14 |         bad_cast( const std::type_index& f, const std::type_index& t ) : std::exception() {
 | 
|---|
 | 15 |                 why = std::string{"bad cast of "} + f.name() + " to " + t.name();
 | 
|---|
 | 16 |         }
 | 
|---|
 | 17 |         ~bad_cast() override = default;
 | 
|---|
 | 18 |         
 | 
|---|
 | 19 |         const char* what() const noexcept override { return why.c_str(); }
 | 
|---|
 | 20 | };
 | 
|---|
 | 21 | 
 | 
|---|
| [a381b46] | 22 | template<typename T> std::type_index class_of() { return { typeid(T) }; }
 | 
|---|
| [87c5f40] | 23 | 
 | 
|---|
| [79b8dc3] | 24 | template<typename T> using ptr = std::unique_ptr<T>;
 | 
|---|
 | 25 | 
 | 
|---|
| [a381b46] | 26 | struct object {
 | 
|---|
| [0d10090] | 27 |         std::type_index get_class() const { return { this ? typeid(*this) : typeid(std::nullptr_t) }; }
 | 
|---|
| [b276be5] | 28 | 
 | 
|---|
| [a381b46] | 29 |         template<typename T> T& as() {
 | 
|---|
| [0d10090] | 30 |                 T* p = dynamic_cast<T*>(this);
 | 
|---|
 | 31 |                 if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
 | 
|---|
 | 32 |                 return *p;
 | 
|---|
| [b276be5] | 33 |         }
 | 
|---|
 | 34 | 
 | 
|---|
| [a381b46] | 35 |         template<typename T> const T& as() const {
 | 
|---|
| [0d10090] | 36 |                 const T* p = dynamic_cast<const T*>(this);
 | 
|---|
 | 37 |                 if ( !p ) throw bad_cast{ get_class(), class_of<T>() };
 | 
|---|
 | 38 |                 return *p;
 | 
|---|
| [b276be5] | 39 |         }
 | 
|---|
 | 40 | 
 | 
|---|
| [79b8dc3] | 41 |         virtual ptr<object> new_inst() const = 0;
 | 
|---|
 | 42 |         virtual ptr<object> new_copy() const = 0;
 | 
|---|
| [b276be5] | 43 |         virtual object& operator= (const object&) = 0;
 | 
|---|
 | 44 |         virtual ~object() = default;
 | 
|---|
 | 45 | };
 | 
|---|
 | 46 | 
 | 
|---|
| [a381b46] | 47 | template<typename T, typename... Args> static inline ptr<T> make(Args&&... args) {
 | 
|---|
 | 48 |         return std::make_unique<T>(std::forward<Args>(args)...);
 | 
|---|
 | 49 | }
 | 
|---|
| [79b8dc3] | 50 | 
 | 
|---|
| [a381b46] | 51 | template<typename To, typename From> 
 | 
|---|
| [79b8dc3] | 52 | ptr<To> as_ptr( ptr<From>&& p ) { return ptr<To>{ &p.release()->template as<To>() }; }
 | 
|---|
| [87c5f40] | 53 | 
 | 
|---|
| [a381b46] | 54 | struct ordered : public virtual object {
 | 
|---|
| [87c5f40] | 55 |         virtual int cmp(const ordered&) const = 0;
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 |         bool operator< (const ordered& that) const { return cmp(that) < 0; }
 | 
|---|
 | 58 |         bool operator<= ( const ordered& that ) const { return cmp(that) <= 0; }
 | 
|---|
 | 59 |         bool operator== ( const ordered& that ) const { return cmp(that) == 0; }
 | 
|---|
 | 60 |         bool operator!= ( const ordered& that ) const { return cmp(that) != 0; }
 | 
|---|
 | 61 |         bool operator> ( const ordered& that ) const { return cmp(that) > 0; }
 | 
|---|
 | 62 |         bool operator>= ( const ordered& that ) const { return cmp(that) >= 0; }
 | 
|---|
 | 63 | };
 | 
|---|
 | 64 | 
 | 
|---|
| [a381b46] | 65 | struct printable : public virtual object {
 | 
|---|
| [0d10090] | 66 |         virtual void print(std::ostream&) const = 0;
 | 
|---|
 | 67 | };
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | class boolean : public ordered, public printable {
 | 
|---|
| [87c5f40] | 70 |         bool x;
 | 
|---|
 | 71 | public:
 | 
|---|
 | 72 |         boolean() = default;
 | 
|---|
 | 73 |         boolean(bool x) : x(x) {}
 | 
|---|
| [c87cd93] | 74 |         boolean(const boolean&) = default;
 | 
|---|
 | 75 |         boolean(boolean&&) = default;
 | 
|---|
| [79b8dc3] | 76 |         ptr<object> new_inst() const override { return make<boolean>(); }
 | 
|---|
 | 77 |         ptr<object> new_copy() const override { return make<boolean>(*this); }
 | 
|---|
| [87c5f40] | 78 |         boolean& operator= (const boolean& that) {
 | 
|---|
 | 79 |                 x = that.x;
 | 
|---|
 | 80 |                 return *this;   
 | 
|---|
 | 81 |         }
 | 
|---|
| [3fb7f5e] | 82 |         object& operator= (const object& that) override { return *this = that.as<boolean>(); } /***/
 | 
|---|
| [c87cd93] | 83 |         boolean& operator= (boolean&&) = default;
 | 
|---|
| [87c5f40] | 84 |         ~boolean() override = default;
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |         int cmp(const boolean& that) const { return x == that.x ? 0 : x == false ? -1 : 1; }
 | 
|---|
| [3fb7f5e] | 87 |         int cmp(const ordered& that) const override { return cmp( that.as<boolean>() ); } /***/
 | 
|---|
| [87c5f40] | 88 | 
 | 
|---|
| [0d10090] | 89 |         void print(std::ostream& out) const override { out << (x ? "true" : "false"); }
 | 
|---|
| [87c5f40] | 90 | };
 | 
|---|
 | 91 | 
 | 
|---|
| [0d10090] | 92 | class character : public ordered, public printable {
 | 
|---|
| [87c5f40] | 93 |         char x;
 | 
|---|
 | 94 | public:
 | 
|---|
 | 95 |         character() = default;
 | 
|---|
 | 96 |         character(char x) : x(x) {}
 | 
|---|
| [c87cd93] | 97 |         character(const character&) = default;
 | 
|---|
 | 98 |         character(character&&) = default;
 | 
|---|
| [79b8dc3] | 99 |         ptr<object> new_inst() const override { return make<character>(); }
 | 
|---|
 | 100 |         ptr<object> new_copy() const override { return make<character>(*this); }
 | 
|---|
| [87c5f40] | 101 |         character& operator= (const character& that) {
 | 
|---|
 | 102 |                 x = that.x;
 | 
|---|
 | 103 |                 return *this;   
 | 
|---|
 | 104 |         }
 | 
|---|
| [3fb7f5e] | 105 |         object& operator= (const object& that) override { return *this = that.as<character>(); } /***/
 | 
|---|
| [c87cd93] | 106 |         character& operator= (character&&) = default;
 | 
|---|
| [87c5f40] | 107 |         ~character() override = default;
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |         int cmp(const character& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
 | 
|---|
| [3fb7f5e] | 110 |         int cmp(const ordered& that) const override { return cmp( that.as<character>() ); } /***/
 | 
|---|
| [87c5f40] | 111 | 
 | 
|---|
| [0d10090] | 112 |         void print(std::ostream& out) const override {
 | 
|---|
 | 113 |                 if ( 0x20 <= x && x <= 0x7E ) { out << "'" << x << "'"; }
 | 
|---|
 | 114 |                 else { out << "'\\" << std::hex << (unsigned int)x << std::setbase(0) << "'"; }
 | 
|---|
| [87c5f40] | 115 |         }
 | 
|---|
 | 116 | };
 | 
|---|
 | 117 | 
 | 
|---|
| [0d10090] | 118 | class integer : public ordered, public printable {
 | 
|---|
| [b276be5] | 119 |         int x;
 | 
|---|
 | 120 | public:
 | 
|---|
 | 121 |         integer() = default;
 | 
|---|
 | 122 |         integer(int x) : x(x) {}
 | 
|---|
| [c87cd93] | 123 |         integer(const integer&) = default;
 | 
|---|
 | 124 |         integer(integer&&) = default;
 | 
|---|
| [79b8dc3] | 125 |         ptr<object> new_inst() const override { return make<integer>(); }
 | 
|---|
 | 126 |         ptr<object> new_copy() const override { return make<integer>(*this); }
 | 
|---|
| [b276be5] | 127 |         integer& operator= (const integer& that) {
 | 
|---|
 | 128 |                 x = that.x;
 | 
|---|
 | 129 |                 return *this;   
 | 
|---|
 | 130 |         }
 | 
|---|
| [3fb7f5e] | 131 |         object& operator= (const object& that) override { return *this = that.as<integer>(); } /***/
 | 
|---|
| [c87cd93] | 132 |         integer& operator= (integer&&) = default;
 | 
|---|
| [b276be5] | 133 |         ~integer() override = default;
 | 
|---|
 | 134 | 
 | 
|---|
| [87c5f40] | 135 |         int cmp(const integer& that) const { return x == that.x ? 0 : x < that.x ? -1 : 1; }
 | 
|---|
| [3fb7f5e] | 136 |         int cmp(const ordered& that) const override { return cmp( that.as<integer>() ); } /***/
 | 
|---|
| [87c5f40] | 137 | 
 | 
|---|
| [0d10090] | 138 |         void print(std::ostream& out) const override { out << x; }
 | 
|---|
 | 139 | };
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 | class c_string : public printable {
 | 
|---|
 | 142 |         static constexpr const char* empty = "";
 | 
|---|
 | 143 |         const char* s;
 | 
|---|
 | 144 | public:
 | 
|---|
 | 145 |         c_string() : s(empty) {}
 | 
|---|
 | 146 |         c_string(const char* s) : s(s) {}
 | 
|---|
| [c87cd93] | 147 |         c_string(const c_string&) = default;
 | 
|---|
 | 148 |         c_string(c_string&&) = default;
 | 
|---|
| [79b8dc3] | 149 |         ptr<object> new_inst() const override { return make<c_string>(); }
 | 
|---|
 | 150 |         ptr<object> new_copy() const override { return make<c_string>(s); }
 | 
|---|
| [0d10090] | 151 |         c_string& operator= (const c_string& that) {
 | 
|---|
 | 152 |                 s = that.s;
 | 
|---|
 | 153 |                 return *this;
 | 
|---|
| [87c5f40] | 154 |         }
 | 
|---|
| [3fb7f5e] | 155 |         object& operator= (const object& that) override { return *this = that.as<c_string>(); } /***/
 | 
|---|
| [c87cd93] | 156 |         c_string& operator= (c_string&&) = default;
 | 
|---|
| [0d10090] | 157 |         ~c_string() override = default;
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 |         void print(std::ostream& out) const override { out << s; }
 | 
|---|
| [87c5f40] | 160 | };
 | 
|---|
 | 161 | 
 | 
|---|
| [0d10090] | 162 | class pair : public ordered, public printable {
 | 
|---|
| [79b8dc3] | 163 |         ptr<object> x;
 | 
|---|
 | 164 |         ptr<object> y;
 | 
|---|
| [87c5f40] | 165 | public:
 | 
|---|
 | 166 |         pair() = default;
 | 
|---|
| [79b8dc3] | 167 |         pair(ptr<object>&& x, ptr<object>&& y) : x(std::move(x)), y(std::move(y)) {}
 | 
|---|
| [c87cd93] | 168 |         pair(const pair& that) : x(that.x->new_copy()), y(that.y->new_copy()) {}
 | 
|---|
 | 169 |         pair(pair&& that) : x(std::move(that.x)), y(std::move(that.y)) {}
 | 
|---|
| [79b8dc3] | 170 |         ptr<object> new_inst() const override { return make<pair>(); }
 | 
|---|
| [a381b46] | 171 |         ptr<object> new_copy() const override { return make<pair>(x->new_copy(), y->new_copy()); }
 | 
|---|
| [87c5f40] | 172 |         pair& operator= (const pair& that) {
 | 
|---|
 | 173 |                 x = that.x->new_copy();
 | 
|---|
 | 174 |                 y = that.y->new_copy();
 | 
|---|
| [b276be5] | 175 |                 return *this;
 | 
|---|
 | 176 |         }
 | 
|---|
| [3fb7f5e] | 177 |         object& operator= (const object& that) override { return *this = that.as<pair>(); } /***/
 | 
|---|
| [c87cd93] | 178 |         pair& operator= (pair&& that) {
 | 
|---|
 | 179 |                 x = std::move(that.x);
 | 
|---|
 | 180 |                 y = std::move(that.y);
 | 
|---|
 | 181 |                 return *this;
 | 
|---|
 | 182 |         }
 | 
|---|
| [87c5f40] | 183 |         ~pair() override = default;
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |         int cmp(const pair& that) const {
 | 
|---|
| [3fb7f5e] | 186 |                 int c = x->as<ordered>().cmp( that.x->as<ordered>() ); /***/
 | 
|---|
| [87c5f40] | 187 |                 if ( c != 0 ) return c;
 | 
|---|
| [3fb7f5e] | 188 |                 return y->as<ordered>().cmp( that.y->as<ordered>() ); /***/
 | 
|---|
| [87c5f40] | 189 |         }
 | 
|---|
| [0d10090] | 190 |         int cmp(const ordered& that) const override { return cmp( that.as<pair>() ); }
 | 
|---|
| [87c5f40] | 191 | 
 | 
|---|
| [0d10090] | 192 |         void print(std::ostream& out) const override {
 | 
|---|
 | 193 |                 out << "[";
 | 
|---|
| [3fb7f5e] | 194 |                 x->as<printable>().print(out); /***/
 | 
|---|
| [0d10090] | 195 |                 out << ", ";
 | 
|---|
| [3fb7f5e] | 196 |                 y->as<printable>().print(out); /***/
 | 
|---|
| [0d10090] | 197 |                 out << "]";
 | 
|---|
| [87c5f40] | 198 |         }
 | 
|---|
| [b276be5] | 199 | };
 | 
|---|