lily/src/type.hpp

58 lines
1.3 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <map>
#include <memory>
namespace lily {
enum reserved_types {
type_id_int = 0,
type_id_str,
type_id_last
};
struct type {
virtual ~type() = default;
virtual bool unify_with(type* other) = 0;
virtual int arity() { return 0; }
};
struct type_internal : type {
int type_id;
type_internal(int id) : type_id(id) {}
bool unify_with(type* other);
};
struct type_parameter : type {
type* actual_type = nullptr;
bool unify_with(type* other);
};
struct type_data : type {
struct constructor {
type_data* parent;
int id;
std::vector<type*> params;
};
int type_id;
std::map<std::string, std::unique_ptr<constructor>> constructors;
type_data(int id) : type_id(id) {}
constructor* create_constructor(const std::string& name, std::vector<type*>&& params);
bool unify_with(type* other);
};
struct type_func : type {
type* left;
type* right;
type_func(type* l, type* r) :
left(l), right(r) {}
bool unify_with(type* other);
int arity() { return right->arity() + 1; }
};
}