diff --git a/k.krak b/k.krak index f46a910..7552a5f 100644 --- a/k.krak +++ b/k.krak @@ -194,9 +194,9 @@ fun main(argc: int, argv: **char): int { } var try_binding = fun(binding: *tree, start_scope: *tree, type_binding: bool) { if !ast_bound(binding) { - var options = scope_lookup(start_scope, binding->data._binding.first, type_binding) + var options = scope_lookup(start_scope, ast_binding_str(binding), type_binding) if (options.size == 0) - error("Could not find any options for scope lookup of " + binding->data._binding.first) + error("Could not find any options for scope lookup of " + ast_binding_str(binding)) else if (options.size == 1) set_ast_binding(binding, options[0]) else @@ -245,8 +245,8 @@ fun main(argc: int, argv: **char): int { var get_type: fun(*tree): *binding = fun(a: *tree): *binding { match(a->data) { ast::_identifier(b) return b.second - ast::_binding(b) if (b.second->bound()) { - return get_type(b.second->bound_to) + ast::_binding(b) if (ast_bound(a)) { + return get_type(get_ast_binding(a)) } else if (unbound_types.contains_key(a)) { return unbound_types[a] } else { @@ -322,7 +322,7 @@ fun main(argc: int, argv: **char): int { var work_done = false var traverse_for_select: fun(*tree): void = fun(t: *tree) { match (t->data) { - ast::_binding(b) if (!t->data._binding.second->bound()) { + ast::_binding(b) if (!ast_bound(t)) { println(to_string(t->data) + " - not bound!") var filtered_options = multiple_binding_options[t].filter(fun(p: *tree): bool return unbound_types[t]->bound_to->equality(get_type(p)->bound_to, false, true);) if (filtered_options.size == 0) { @@ -345,7 +345,7 @@ fun main(argc: int, argv: **char): int { if (!work_done) { var traverse_for_error: fun(*tree): void = fun(t: *tree) { match (t->data) { - ast::_binding(b) if (!t->data._binding.second->bound()) { + ast::_binding(b) if (!ast_bound(t)) { var filtered_options = multiple_binding_options[t].filter(fun(p: *tree): bool return unbound_types[t]->bound_to->equality(get_type(p)->bound_to, false, true);) if (filtered_options.size > 1) { println("Attempting to use our inferenced type " + unbound_types[t]->bound_to->to_string() + " to decide what to bind " + to_string(t->data) + " to form options:") @@ -499,7 +499,7 @@ fun main(argc: int, argv: **char): int { ast::_import(b) { } ast::_identifier(b) { C_str += idt + b.first; } ast::_binding(b) { - var bound_to = b.second->bound_to + var bound_to = get_ast_binding(t) if (is_top_level_item(bound_to)) pass_poset.add_close_dep(make_pair(item, str("emit_C")), make_pair(bound_to, str("emit_C"))) else if (is_identifier(bound_to) && is_declaration(bound_to->parent) && is_top_level_item(bound_to->parent)) @@ -869,7 +869,9 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vec>()) { if (syntax->children[0]->data.name != "scoped_identifier") error(syntax, "Unexpected template instantiation (not on an identifier)") - return make_ast_binding(concat(syntax->children[0]) + "") + return make_ast_binding(concat(syntax->children[0]), get_nodes("type", template_inst).map(fun(s: *tree): *binding { + return parse_type(s); + })) } else if (syntax->children[0]->data.terminal) { return _call(vec(make_ast_binding(concat(syntax->children[0])), syntax_to_ast_helper(syntax->children[1]))) diff --git a/stdlib/ast.krak b/stdlib/ast.krak index 1d3f1ff..f2d09e2 100644 --- a/stdlib/ast.krak +++ b/stdlib/ast.krak @@ -10,7 +10,7 @@ adt ast { _translation_unit: str, _import: pair<*tree, set>, _identifier: pair>, - _binding: pair>>, + _binding: triple>, *binding>>, _type_def: str, _adt_def: str, _function: triple, bool>, @@ -36,7 +36,7 @@ fun to_string(a: ref ast): str { ast::_translation_unit(b) return str("_translation_unit(") + b + ")" ast::_import(b) return str("_import(") + to_string(b.first->data) + ")[" + str(",").join(b.second.data) + "]" ast::_identifier(b) return str("_identifier(") + b.first + ": " + deref_to_string(b.second->bound_to) + ")" - ast::_binding(b) return str("_binding(") + b.first + "->" + to_string(b.second->bound_to) + ")" + ast::_binding(b) return str("_binding(") + b.first + "[" + str(",").join(b.second.map(fun(x:*binding): str { return deref_to_string(x->bound_to); })) + "]" + "->" + to_string(b.third->bound_to) + ")" ast::_type_def(b) return str("_type_def(") + b + ")" ast::_adt_def(b) return str("_adt_def(") + b + ")" ast::_function(b) return str("_function(") + b.first + ": " + deref_to_string(b.second->bound_to) + ", ext?:" + to_string(b.third) + ")" @@ -76,8 +76,8 @@ fun _cast(p: *binding): *tree { fun _identifier(p1: str, p2: *binding): *tree { return new>()->construct(ast::_identifier(make_pair(p1, p2))) } -fun _binding(p1: str, p2: *binding>): *tree { - return new>()->construct(ast::_binding(make_pair(p1, p2))) +fun _binding(p1: str, p2: vec<*binding>, p3: *binding>): *tree { + return new>()->construct(ast::_binding(make_triple(p1, p2, p3))) } fun _function(p1: str, p2: *binding, p3: bool): *tree { return new>()->construct(ast::_function(make_triple(p1, p2, p3))) @@ -148,8 +148,8 @@ fun _cast(p: *binding, c: ref vec<*tree>): *tree { fun _identifier(p1: str, p2: *binding, c: ref vec<*tree>): *tree { return new>()->construct(ast::_identifier(make_pair(p1, p2)), c) } -fun _binding(p1: str, p2: *binding>, c: ref vec<*tree>): *tree { - return new>()->construct(ast::_binding(make_pair(p1, p2)), c) +fun _binding(p1: str, p2: vec<*binding>, p3: *binding>, c: ref vec<*tree>): *tree { + return new>()->construct(ast::_binding(make_triple(p1, p2, p3)), c) } fun _function(p1: str, p2: *binding, p3: bool, c: ref vec<*tree>): *tree { return new>()->construct(ast::_function(make_triple(p1, p2, p3)), c) @@ -286,12 +286,23 @@ fun make_ast_binding(s: *char): *tree { return make_ast_binding(str(s)) } fun make_ast_binding(s: str): *tree { - return _binding(s, binding>()) + return make_ast_binding(s, vec<*binding>()) +} +fun make_ast_binding(s: str, v: vec<*binding>): *tree { + return _binding(s, v, binding>()) +} +fun get_ast_binding_inst_types(binding: *tree): ref vec<*binding> { + match(binding->data) { + ast::_binding(b) { + return b.second + } + } + error("trying to get binding on not a binding") } fun get_ast_binding(binding: *tree): *tree { match(binding->data) { ast::_binding(b) { - return b.second->bound_to + return b.third->bound_to } } error("trying to get binding on not a binding") @@ -299,7 +310,7 @@ fun get_ast_binding(binding: *tree): *tree { fun set_ast_binding(binding: *tree, to: *tree) { match(binding->data) { ast::_binding(b) { - b.second->set(to) + b.third->set(to) return } } @@ -307,7 +318,7 @@ fun set_ast_binding(binding: *tree, to: *tree) { } fun ast_bound(binding: *tree): bool { match(binding->data) { - ast::_binding(b) return b.second->bound() + ast::_binding(b) return b.third->bound() } error("Trying to check bound for not a binding") }