Allow recursive types.

This commit is contained in:
Danila Fedorin 2019-06-05 21:36:30 -07:00
parent 1be78c3548
commit e039e44592
3 changed files with 5 additions and 5 deletions

View File

@ -7,6 +7,6 @@ namespace lily {
struct constructor {
int parent_type;
int tag;
std::vector<type_ptr> params;
std::vector<std::weak_ptr<type>> params;
};
}

View File

@ -7,7 +7,7 @@ int main() {
lily::parse(
"data Bool = { True, False }\n"
"data Color = { Red, Black }\n"
"data IntList = { Nil, Cons(Int, Int) }\n"
"data IntList = { Nil, Cons(Int, IntList) }\n"
"defn add x y = { x + y }");
} catch(lily::error& e) {
std::cout << e.message << std::endl;

View File

@ -167,10 +167,10 @@ namespace lily {
return prog.types[str];
}
static void collect_type_params(program& prog, std::vector<type_ptr>& into, pgs_tree* params, const char* source) {
static void collect_type_params(program& prog, std::vector<std::weak_ptr<type>>& into, pgs_tree* params, const char* source) {
while(true) {
pgs_tree* param = PGS_TREE_NT_CHILD(*params, 0);
into.push_back(type_tree(prog, param, source));
into.push_back(std::weak_ptr<type>(type_tree(prog, param, source)));
if(PGS_TREE_NT_COUNT(*params) == 1) break;
params = PGS_TREE_NT_CHILD(*params, 2);
@ -213,9 +213,9 @@ namespace lily {
if(prog.types.count(data_name)) throw error("cannot redefine type");
int new_type = prog.next_free_type_id++;
prog.types[data_name] = type_ptr(new type_int(new_type));
collect_constructors(prog, new_type,
PGS_TREE_NT_CHILD(*PGS_TREE_NT_CHILD(*def, PGS_TREE_NT_COUNT(*def) - 1), 1), source);
prog.types[data_name] = type_ptr(new type_int(new_type));
}
}