2019-06-05 21:16:40 -07:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-06-09 20:24:44 -07:00
|
|
|
#include <map>
|
2019-06-05 21:16:40 -07:00
|
|
|
|
|
|
|
namespace lily {
|
|
|
|
enum reserved_types {
|
|
|
|
type_id_int = 0,
|
|
|
|
type_id_str,
|
|
|
|
type_id_last
|
|
|
|
};
|
|
|
|
|
|
|
|
struct type {
|
|
|
|
virtual ~type() = default;
|
2019-06-09 19:51:53 -07:00
|
|
|
virtual bool unify_with(type* other) = 0;
|
2019-06-09 21:25:02 -07:00
|
|
|
virtual int arity() { return 0; }
|
2019-06-05 21:16:40 -07:00
|
|
|
};
|
|
|
|
|
2019-06-09 16:13:13 -07:00
|
|
|
struct type_internal : type {
|
|
|
|
int type_id;
|
|
|
|
|
|
|
|
type_internal(int id) : type_id(id) {}
|
2019-06-09 19:51:53 -07:00
|
|
|
bool unify_with(type* other);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct type_parameter : type {
|
|
|
|
type* actual_type = nullptr;
|
|
|
|
bool unify_with(type* other);
|
2019-06-09 16:13:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct type_data : type {
|
|
|
|
struct constructor {
|
|
|
|
type_data* parent;
|
|
|
|
int id;
|
|
|
|
std::vector<type*> params;
|
|
|
|
};
|
2019-06-05 21:16:40 -07:00
|
|
|
|
|
|
|
int type_id;
|
2019-06-09 20:24:44 -07:00
|
|
|
std::map<std::string, std::unique_ptr<constructor>> constructors;
|
2019-06-05 21:16:40 -07:00
|
|
|
|
2019-06-09 16:13:13 -07:00
|
|
|
type_data(int id) : type_id(id) {}
|
|
|
|
constructor* create_constructor(const std::string& name, std::vector<type*>&& params);
|
2019-06-09 19:51:53 -07:00
|
|
|
bool unify_with(type* other);
|
2019-06-05 21:16:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct type_func : type {
|
2019-06-09 16:13:13 -07:00
|
|
|
type* left;
|
|
|
|
type* right;
|
2019-06-05 21:16:40 -07:00
|
|
|
|
2019-06-09 16:13:13 -07:00
|
|
|
type_func(type* l, type* r) :
|
|
|
|
left(l), right(r) {}
|
2019-06-09 19:51:53 -07:00
|
|
|
bool unify_with(type* other);
|
2019-06-09 21:25:02 -07:00
|
|
|
int arity() { return right->arity() + 1; }
|
2019-06-05 21:16:40 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|