#include "type.hpp" Type::Type() {} Type::~Type() {} bool Type::intType() { return false; } bool Type::floatType() { return false; } bool Type::stringType() { return false; } bool Type::canCastTo(std::shared_ptr t) { return this == t.get(); } std::shared_ptr Type::funcType() { return std::shared_ptr(); } std::shared_ptr Type::arrayType() { return std::shared_ptr(); } std::shared_ptr Type::structType() { return std::shared_ptr(); } std::shared_ptr Type::constType() { return std::shared_ptr(); } std::shared_ptr Type::vectorType() { return std::shared_ptr(); } FuncType::FuncType(std::shared_ptr return_type, std::shared_ptr parameters, bool is_userlib, bool is_customfunction) : returnType(return_type), params(parameters), userlib(is_userlib), cfunc(is_customfunction) {} FuncType::~FuncType() {} std::shared_ptr FuncType::funcType() { return std::dynamic_pointer_cast(this->shared_from_this()); } ArrayType::ArrayType(std::shared_ptr t, int n) : type(t), size(n) {} ArrayType::~ArrayType() {} std::shared_ptr ArrayType::arrayType() { return std::dynamic_pointer_cast(this->shared_from_this()); } StructType::StructType(const std::string& i) : ident(i) {} StructType::StructType(const std::string& i, std::shared_ptr f) : ident(i), fields(f) {} StructType::~StructType() {} std::shared_ptr StructType::structType() { return std::dynamic_pointer_cast(this->shared_from_this()); } bool StructType::canCastTo(std::shared_ptr t) { return (t.get() == this) || (t == Type::null_type) || (this->shared_from_this() == Type::null_type && t->structType()); } ConstType::ConstType(int32_t n) : intValue(n), valueType(Type::int_type) {} ConstType::ConstType(float_t n) : floatValue(n), valueType(Type::float_type) {} ConstType::ConstType(std::string const& n) : stringValue(n), valueType(Type::string_type) {} ConstType::~ConstType() {} std::shared_ptr ConstType::constType() { return std::dynamic_pointer_cast(this->shared_from_this()); } VectorType::VectorType(const std::string& l, Type* t, const std::vector& szs) : label(l), elementType(t), sizes(szs) {} VectorType::~VectorType() {} std::shared_ptr VectorType::vectorType() { return std::dynamic_pointer_cast(this->shared_from_this()); } bool VectorType::canCastTo(std::shared_ptr t) { if (this == t.get()) { return true; } std::shared_ptr v = t->vectorType(); if (!v) { return false; } if (elementType != v->elementType) { return false; } if (sizes.size() != v->sizes.size()) { return false; } for (int k = 0; k < sizes.size(); ++k) { if (sizes[k] != v->sizes[k]) { return false; } } return true; } class VoidType : public Type { virtual bool canCastTo(std::shared_ptr t) override { return t == Type::void_type; } }; std::shared_ptr Type::void_type = std::make_shared(); class IntType : public Type { virtual bool intType() override { return true; } virtual bool canCastTo(std::shared_ptr t) override { return t == Type::int_type; } }; std::shared_ptr Type::int_type = std::make_shared(); class FloatType : public Type { virtual bool floatType() override { return true; } virtual bool canCastTo(std::shared_ptr t) override { return t == Type::float_type; } }; std::shared_ptr Type::float_type = std::make_shared(); class StringType : public Type { virtual bool stringType() override { return true; } virtual bool canCastTo(std::shared_ptr t) override { return t == Type::string_type; } }; std::shared_ptr Type::string_type = std::make_shared(); std::shared_ptr Type::string_type = std::make_shared("Null");