diff options
-rw-r--r-- | src/core/Typesystem.cpp | 72 | ||||
-rw-r--r-- | src/core/Typesystem.hpp | 133 |
2 files changed, 124 insertions, 81 deletions
diff --git a/src/core/Typesystem.cpp b/src/core/Typesystem.cpp index ebb0692..a9ef61f 100644 --- a/src/core/Typesystem.cpp +++ b/src/core/Typesystem.cpp @@ -24,67 +24,33 @@ namespace ousia { -/* Class StringInstance */ +/* Class EnumType */ - -/** - * Contains a map from escape characters and their corresponding code point. - */ -static const std::unordered_map<char, char> ESCAPE_CHARACTERS_TO_CODEPOINT{ - {'n', '\n'}, - {'r', '\r'}, - {'t', '\t'}, - {'b', '\b'}, - {'f', '\f'}, - {'v', '\v'} -}; - -static const std::unordered_map<char, char> CODEPOINT_TO_ESCAPE_CHARACTER{ - {'\n', 'n'}, - {'\r', 'r'}, - {'\t', 't'}, - {'\b', 'b'}, - {'\f', 'f'}, - {'\v', 'v'} -}; - -static const char MIN_CONTROL_CHARACTER = 0x37; - -std::string StringInstance::toString() +EnumType::EnumType(Manager &mgr, const std::set<std::string> &names) : + Type(mgr, false, true) { - std::stringstream ss; - ss << '"'; - for (char c: value) { - if (c == '"') { - ss << '\\'; - } else if (c == '\\') { - ss << "\\\\"; - } else if (c <= MIN_CONTROL_CHARACTER) { - const auto it = CODEPOINT_TO_ESCAPE_CHARACTER.find(c); - if (it != CODEPOINT_TO_ESCAPE_CHARACTER.cend()) { - ss << '\\' << it->second; - } else { - ss << c; - } - } - ss << c; + int value = 0; + for (const auto &name: names) { + values.insert(std::make_pair(name, value++)) } - ss << '"'; - return ss.str(); } -/* Class StringType */ - -Rooted<TypeInstance> StringType::create() +int EnumType::valueOf(const std::string &name) { - return new StringInstance(getManager(), this, ""); + auto it = values.find(name); + if (it != values.end()) { + return it->second; + } + return -1; } -Rooted<TypeInstance> StringType::parse(const std::string &str) -{ - /*std::stringstream ss; - int state = 0;*/ - return nullptr; +std::string EnumType::toString(int value) { + for (const auto &p : values) { + if (p->second == value) { + return p->first; + } + } + return std::string{}; } /* Class Typesystem */ diff --git a/src/core/Typesystem.hpp b/src/core/Typesystem.hpp index c1476ee..8ef3072 100644 --- a/src/core/Typesystem.hpp +++ b/src/core/Typesystem.hpp @@ -22,11 +22,13 @@ #include <string> #include <vector> +#include "BufferedCharReader.hpp" #include "Managed.hpp" #include "Node.hpp" namespace ousia { +class Typesystem; class Type; class StringType; @@ -53,20 +55,6 @@ public: : Managed(mgr), type(acquire(type)) { } - - virtual std::string toString() = 0; -}; - -class StringInstance : public TypeInstance { -public: - std::string value; - - StringInstance(Manager &mgr, Handle<StringType> type, std::string value) - : TypeInstance(mgr, type), value(std::move(value)) - { - } - - std::string toString() override; }; /** @@ -75,22 +63,38 @@ public: */ class Type : public Node { public: - using Node::Node; + /** + * True, if the type cannot be extended. + */ + const bool isFinal; /** - * Returns true, if the type cannot be extended. - * - * @return true if the type is final, false otherwise. + * True, if the type represents a primitive type, such as an integer, + * doubles, enums and string. */ - virtual bool isFinal() const { return true; } + const bool isPrimitive; /** - * Returns true, if the type is a primitive type (not a composite). + * Constructor of the Type class. * - * @return true for types such as integers, doubles, enums and strings, - * false otherwise. + * @param mgr is a reference at the underlying node manager. + * @param bool isFinal specifies whether this type is final. + * @param bool isPrimitive specifies whether this type is primitive. + * @param name specifies the internal name of the type. + * @param typesystem specifies the parent type system. */ - virtual bool isPrimitive() const { return true; } + Type(Manager &mgr, bool isFinal, bool isPrimitive, std::string name, + Handle<Typesystem> typesystem = nullptr) + : Node(mgr, std::move(name), typesystem), + isFinal(isFinal), + isPrimitive(isPrimitive) + { + } + + /** + * Virtual destructor. + */ + virtual ~Type(){}; /** * Creates a new instance of this type. All values of this type are @@ -98,7 +102,7 @@ public: * * @return a new instance of this type. */ - virtual Rooted<TypeInstance> create() = 0; + // virtual Rooted<TypeInstance> create() = 0; /** * Parses the given string and produces a new instance of the given type. @@ -107,18 +111,91 @@ public: * * @param str is the string which should be parsed. */ - virtual Rooted<TypeInstance> parse(const std::string &str) = 0; + // virtual Rooted<TypeInstance> parse(BufferedCharReader &reader) = 0; }; +/** + * Type which is used to represent a string. + */ class StringType : public Type { public: - using Type::Type; + StringType(Manager &mgr, Handle<Typesystem> typesystem) + : Type(mgr, true, true, "string", typesystem){}; +}; + +/** + * Type which is used to represent an integer. + */ +class IntegerType : public Type { +public: + IntegerType(Manager &mgr, Handle<Typesystem> typesystem) + : Type(mgr, true, true, "int", typesystem){}; +}; + +/** + * Type which is used to represent a double. + */ +class DoubleType : public Type { +public: + DoubleType(Manager &mgr, Handle<Typesystem> typesystem) + : Type(mgr, true, true, "double", typesystem){}; +}; + +/** + * Type which represents a enum. + */ +class EnumType : public Type { +private: + std::map<std::string, int> values; + +public: + EnumType(Manager &mgr, std::string name, Handle<Typesystem> typesystem, + const std::set<std::string> &names); - Rooted<TypeInstance> create() override; + /** + * Returns the integer value associated to the given name. + * + * @param name is the name of the value that should be looked up. + * @return the (non-negative) value associated to the given name or -1 if + * the value does not exist. + */ + int valueOf(const std::string &name); - Rooted<TypeInstance> parse(const std::string &str) override; + /** + * Returns the name corresponding to the given enum value or an empty string + * instead. + * + * @param value is the integer representation of an enum value that should + * be converted to the corresponding string. + * @return the string corresponding to the enum value or an empty string if + * no entry with such a value exists. + */ + std::string toString(int value); +}; + +/** + * Type which represents an array. + */ +class ArrayType : public Type { +public: + const Owned<Type> innerType; + + ArrayType(Manager &mgr, std::string name, Handle<Typesystem> typesystem, + Handle<Type> innerType) + : Type(mgr, false, true, name, typesystem), + innerType(acquire(innerType)){}; }; +/** + * Type which represents a structure of other types. + */ +class StructType : public Type { +private: + std::map<std::string, Type> entries; + + StructType(Manager) +} + class Typesystem : public Node { private: NodeVector<Type> types; |