From deda17e18baf693aa677f736d7bb69a6cec62f85 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Thu, 27 Dec 2018 15:14:28 -0500 Subject: [PATCH] groundwork for refs, moved ref indicator into fun type --- k.krak | 125 ++++++++++++++++++++++------------------------ stdlib/type2.krak | 44 +++++----------- 2 files changed, 71 insertions(+), 98 deletions(-) diff --git a/k.krak b/k.krak index 283184b..fc014f9 100644 --- a/k.krak +++ b/k.krak @@ -112,10 +112,10 @@ fun main(argc: int, argv: **char): int { for (var j = 0; j < number_tower.size; j++;) for (var k = 0; k < number_tower.size; k++;) primitive_ops["op" + comparators[i]].add(_compiler_intrinsic(comparators[i], binding_p(type::_fun(make_triple(make_pair(vec( - number_tower[j], - number_tower[k] + make_pair(false, number_tower[j]), + make_pair(false, number_tower[k]) ), - binding_p(type::_bool()) + make_pair(false, binding_p(type::_bool())) ), false, false))), vec<*binding>())) } var math = vec(str("+"), str("-"), str("*"), str("/"), str("&"), str("|"), str("^")) @@ -130,10 +130,10 @@ fun main(argc: int, argv: **char): int { return_type = number_tower[k] } primitive_ops["op" + math[i]].add(_compiler_intrinsic(math[i], binding_p(type::_fun(make_triple(make_pair(vec( - number_tower[j], - number_tower[k] + make_pair(false, number_tower[j]), + make_pair(false, number_tower[k]) ), - return_type + make_pair(false, return_type) ), false, false))), vec<*binding>())) } } @@ -146,10 +146,10 @@ fun main(argc: int, argv: **char): int { for (var k = 0; k <= j; k++;) { var return_type = null>() primitive_ops["op" + math[i] + "="].add(_compiler_intrinsic(math[i] + "=", binding_p(type::_fun(make_triple(make_pair(vec( - number_tower[j], - number_tower[k] + make_pair(false, number_tower[j]), + make_pair(false, number_tower[k]) ), - binding_p(type::_void()) + make_pair(false, binding_p(type::_void())) ), false, false))), vec<*binding>())) } } @@ -159,39 +159,39 @@ fun main(argc: int, argv: **char): int { // address of var template_type = binding_p(type::_template_placeholder()) primitive_ops[str("op&")].add(_template(str("&"), map(str("T"), template_type), vec(_compiler_intrinsic(str("&"), binding_p(type::_fun(make_triple(make_pair(vec( - template_type + make_pair(false, template_type) ), - binding_p(type::_ptr(template_type)) + make_pair(false, binding_p(type::_ptr(template_type))) ), false, false))), vec<*binding>())))) // dereference var template_type = binding_p(type::_template_placeholder()) primitive_ops[str("op*")].add(_template(str("*"), map(str("T"), template_type), vec(_compiler_intrinsic(str("*"), binding_p(type::_fun(make_triple(make_pair(vec( - binding_p(type::_ptr(template_type)) + make_pair(false, binding_p(type::_ptr(template_type))) ), - template_type + make_pair(false, template_type) ), false, false))), vec<*binding>())))) for (var i = 0; i < number_tower.size - 2; i++;) { var template_type = binding_p(type::_template_placeholder()) primitive_ops[str("op+")].add(_template(str("+"), map(str("T"), template_type), vec(_compiler_intrinsic(str("+"), binding_p(type::_fun(make_triple(make_pair(vec( - number_tower[i], - binding_p(type::_ptr(template_type)) + make_pair(false, number_tower[i]), + make_pair(false, binding_p(type::_ptr(template_type))) ), - binding_p(type::_ptr(template_type)) + make_pair(false, binding_p(type::_ptr(template_type))) ), false, false))), vec<*binding>())))) primitive_ops[str("op+")].add(_template(str("+"), map(str("T"), template_type), vec(_compiler_intrinsic(str("+"), binding_p(type::_fun(make_triple(make_pair(vec( - binding_p(type::_ptr(template_type)), - number_tower[i] + make_pair(false, binding_p(type::_ptr(template_type))), + make_pair(false, number_tower[i]) ), - binding_p(type::_ptr(template_type)) + make_pair(false, binding_p(type::_ptr(template_type))) ), false, false))), vec<*binding>())))) // note only ptr-1, not 1-ptr to match C... primitive_ops[str("op-")].add(_template(str("-"), map(str("T"), template_type), vec(_compiler_intrinsic(str("-"), binding_p(type::_fun(make_triple(make_pair(vec( - binding_p(type::_ptr(template_type)), - number_tower[i] + make_pair(false, binding_p(type::_ptr(template_type))), + make_pair(false, number_tower[i]) ), - binding_p(type::_ptr(template_type)) + make_pair(false, binding_p(type::_ptr(template_type))) ), false, false))), vec<*binding>())))) } @@ -277,9 +277,9 @@ fun main(argc: int, argv: **char): int { var type_def_binding = make_ast_binding(name) set_ast_binding(type_def_binding, child) item->add_child(_compiler_intrinsic(ident_name, binding_p(type::_fun(make_triple(make_pair(vec( - binding_p(type::_obj(type_def_binding)) + make_pair(false, binding_p(type::_obj(type_def_binding))) ), - ident_type + make_pair(false, ident_type) ), false, false))), vec<*binding>())) println("adding compiler intrinsic to do " + name + "." + ident_name) } @@ -305,9 +305,9 @@ fun main(argc: int, argv: **char): int { /*set_ast_binding(type_def_binding, child)*/ item->add_child(_template(ident_name, new_template_type_map, vec(_compiler_intrinsic(ident_name, binding_p(type::_fun(make_triple(make_pair(vec( - binding_p(type::_obj(type_def_binding)) + make_pair(false, binding_p(type::_obj(type_def_binding))) ), - new_ident_type + make_pair(false, new_ident_type) ), false, false))), vec<*binding>())))) println("adding compiler intrinsic to do " + name + "." + ident_name) } @@ -346,14 +346,14 @@ fun main(argc: int, argv: **char): int { ast::_call(add_scope) { var t = get_type(a->children[0]) if (is_fun(t->bound_to)) - return t->bound_to->_fun.first.second + return t->bound_to->_fun.first.second.second if (is_unknown(t->bound_to)) { - var return_type = binding_p(type::_unknown()) - var parameter_types = vec<*binding>() + var return_type = make_pair(false, binding_p(type::_unknown())) + var parameter_types = vec>>() for (var i = 1; i < a->children.size; i++;) - parameter_types.add(get_type(a->children[i])) + parameter_types.add(make_pair(false, get_type(a->children[i]))) t->set(type::_fun(make_triple(make_pair(parameter_types, return_type), false, false))) - return return_type + return return_type.second } error("Trying to get type of call where type of first child is not function, but " + to_string(t->bound_to)) } @@ -393,12 +393,11 @@ fun main(argc: int, argv: **char): int { match(*t->bound_to) { type::_obj(b) return quick_bind(b, n, null>(), true) type::_ptr(p) return quick_bind_type(p, n) - type::_ref(p) return quick_bind_type(p, n) type::_fun(b) { for (var i = 0; i < b.first.first.size; i++;) - if (!quick_bind_type(b.first.first[i], n)) + if (!quick_bind_type(b.first.first[i].second, n)) return false - return quick_bind_type(b.first.second, n) + return quick_bind_type(b.first.second.second, n) } } return true @@ -476,12 +475,11 @@ fun main(argc: int, argv: **char): int { match(*t->bound_to) { type::_obj(b) return try_to_find_binding_possibilities(b, n, null>(), true) type::_ptr(p) return handle_type_binding_possibilities(p, n) - type::_ref(p) return handle_type_binding_possibilities(p, n) type::_fun(b) { for (var i = 0; i < b.first.first.size; i++;) - if (!handle_type_binding_possibilities(b.first.first[i], n)) + if (!handle_type_binding_possibilities(b.first.first[i].second, n)) return false - return handle_type_binding_possibilities(b.first.second, n) + return handle_type_binding_possibilities(b.first.second.second, n) } } return true @@ -505,10 +503,10 @@ fun main(argc: int, argv: **char): int { error("trying to call function with type wrong number of params (" + to_string(fun_type->_fun.first.first.size) + " vs " + to_string(t->children.size - 1) + "): " + to_string(fun_type)) println("\titerating through children, matching their type with the param type") for (var i = 1; i < t->children.size; i++;) - unify(fun_type->_fun.first.first[i-1], get_type(t->children[i])) + unify(fun_type->_fun.first.first[i-1].second, get_type(t->children[i])) } ast::_return() if (t->children.size > 0) - unify(get_type(get_ancestor_satisfying(t, fun(t: *tree): bool return is_function(t);))->bound_to->_fun.first.second, get_type(t->children[0])) + unify(get_type(get_ancestor_satisfying(t, fun(t: *tree): bool return is_function(t);))->bound_to->_fun.first.second.second, get_type(t->children[0])) } } traverse_for_unify(item) @@ -731,7 +729,6 @@ fun main(argc: int, argv: **char): int { type::_unknown() error("unknown in to_c_type") /*type::_unknown() return str("unknown")*/ type::_ptr(p) return to_c_type(p) + "*" - type::_ref() error("ref in to_c_type") type::_void() return str("void") type::_obj(b) return get_c_name(get_ast_binding(b)) type::_fun(b) error("fun in to_c_type unimplemented") @@ -769,14 +766,13 @@ fun main(argc: int, argv: **char): int { type::_unknown() error("unknown in resolve_type") type::_template_placeholder() error("template_placeholder in resolve_type") type::_ptr(p) resolve_type(p) - type::_ref(p) resolve_type(p) type::_obj(o) { resolve(o) pass_poset.add_close_dep(make_pair(item, str("emit_C")), make_pair(get_ast_binding(o), str("emit_C"))) } type::_fun(t) { - t.first.first.for_each(resolve_type) - resolve_type(t.first.second) + t.first.first.for_each(fun(p: pair>): void { resolve_type(p.second); }) + resolve_type(t.first.second.second) } } } @@ -936,7 +932,7 @@ fun main(argc: int, argv: **char): int { var fun_name = get_c_name(t) var fun_type = b.second->bound_to var is_ext = b.third - var return_type = fun_type->_fun.first.second + var return_type = fun_type->_fun.first.second.second var parameter_types = fun_type->_fun.first.first var is_variadic = fun_type->_fun.second var is_raw = fun_type->_fun.third @@ -948,8 +944,8 @@ fun main(argc: int, argv: **char): int { C_str += ", " C_declaration_str += ", " } - C_str += to_c_type(parameter_types[i]) + " " - C_declaration_str += to_c_type(parameter_types[i]) + C_str += to_c_type(parameter_types[i].second) + " " + C_declaration_str += to_c_type(parameter_types[i].second) emit_C(t->children[i], 0) } if (is_variadic) { @@ -1079,10 +1075,10 @@ fun main(argc: int, argv: **char): int { var real_main = _function( str("main"), binding_p(type::_fun(make_triple(make_pair(vec( - binding_p(type::_int()), - binding_p(type::_ptr(binding_p(type::_ptr(binding_p(type::_char()))))) + make_pair(false, binding_p(type::_int())), + make_pair(false, binding_p(type::_ptr(binding_p(type::_ptr(binding_p(type::_char())))))) ), - binding_p(type::_int()) + make_pair(false, binding_p(type::_int())) ), false, false))), true, vec( _identifier(str("argc"), binding_p(type::_int())), @@ -1130,13 +1126,9 @@ fun main(argc: int, argv: **char): int { } fun parse_type(syntax: *tree, declared_template_types: ref map>): *binding { - var is_ref = get_node("\"ref\"", syntax) != null>() - syntax = get_node("pre_reffed", syntax) - if (is_ref) - return binding_p(type::_ref(parse_type_helper(syntax, declared_template_types))) - else - return parse_type_helper(syntax, declared_template_types) + return parse_type_helper(get_node("pre_reffed", syntax), declared_template_types) } + fun parse_type_helper(syntax: *tree, declared_template_types: ref map>): *binding { var next = get_node("pre_reffed", syntax) if (next != null>()) @@ -1149,7 +1141,7 @@ fun parse_type_helper(syntax: *tree, declared_template_types: ref map>()) { - var inst_with = get_nodes("type", template_inst).map(fun(s: *tree): *binding { return parse_type(s, declared_template_types); }) + var inst_with = get_nodes("type", template_inst).map(fun(s: *tree): *binding { return parse_type_helper(s, declared_template_types); }) return binding_p(type::_obj(make_ast_binding(ident_str, inst_with))) } else { if (declared_template_types.contains_key(ident_str)) @@ -1158,12 +1150,12 @@ fun parse_type_helper(syntax: *tree, declared_template_types: ref map>()) { - var param_types = vec<*binding>() - var return_type = binding_p(type::_void()) + error("function type parsing not implemented") + var param_types = vec>>() + var return_type = make_pair(false, binding_p(type::_void())) var variadic = false var raw = false - return binding_p(type::_fun(make_triple(make_pair(param_types, return_type), - variadic, raw))) + return binding_p(type::_fun(make_triple(make_pair(param_types, return_type), variadic, raw))) } else if (first_child_name == "\"void\"") { return binding_p(type::_void()) } else if (first_child_name == "\"bool\"") { @@ -1226,16 +1218,17 @@ fun syntax_to_ast(file_name: str, syntax: *tree, import_paths: ref vec): *tree return syntax_to_ast_helper(x, with_added_declared_template_types);) + var parameters = get_nodes("typed_parameter", syntax).map(fun(x: *tree): pair> return make_pair(get_node("\"ref\"", get_node("type", x)) != null>(), + syntax_to_ast_helper(x, with_added_declared_template_types));) var body = syntax_to_ast_helper(get_node("statement", syntax), with_added_declared_template_types) - var return_type = null>() + var return_type = make_pair(false, null>()) var return_type_node = get_node("typed_return", syntax) if (return_type_node != null>()) - return_type = parse_type(get_node("type", return_type_node), with_added_declared_template_types) + return_type = make_pair(get_node("\"ref\"", get_node("type", return_type_node)) != null>(), parse_type(get_node("type", return_type_node), with_added_declared_template_types)) else - return_type = binding_p(type::_void()) - var function_type = binding_p(type::_fun(make_triple(make_pair(parameters.map(fun(i: *tree): *binding return i->data._identifier.second;), return_type), false, false))) - var n = _function(concat(get_node("func_identifier", syntax)), function_type, false, parameters + body) + return_type = make_pair(false, binding_p(type::_void())) + var function_type = binding_p(type::_fun(make_triple(make_pair(parameters.map(fun(i: pair>): pair> return make_pair(i.first, i.second->data._identifier.second);), return_type), false, false))) + var n = _function(concat(get_node("func_identifier", syntax)), function_type, false, parameters.map(fun(i: pair>): *tree return i.second;) + body) if (new_template_type_map.size() > 0) { return _template(n->data._function.first, new_template_type_map, vec(n)) } else { diff --git a/stdlib/type2.krak b/stdlib/type2.krak index a07b202..4aa6ce5 100644 --- a/stdlib/type2.krak +++ b/stdlib/type2.krak @@ -11,10 +11,9 @@ adt type { _void, _template_placeholder, _ptr: *binding, - _ref: *binding, _obj: *tree, - // triple, is_variadic, is raw> - _fun: triple>, *binding>, bool, bool>, + // triple>, pair>, is_variadic, is raw> + _fun: triple>>, pair>>, bool, bool>, _bool, _char, _uchar, @@ -37,9 +36,9 @@ fun unify(t1: *binding, t2: *binding) { } else { if (shallow_equality(t1->bound_to, t2->bound_to)) { if (is_fun(t1->bound_to)) { - unify(t1->bound_to->_fun.first.second, t2->bound_to->_fun.first.second) + unify(t1->bound_to->_fun.first.second.second, t2->bound_to->_fun.first.second.second) for (var i = 0; i < t1->bound_to->_fun.first.first.size; i++;) - unify(t1->bound_to->_fun.first.first[i], t2->bound_to->_fun.first.first[i]) + unify(t1->bound_to->_fun.first.first[i].second, t2->bound_to->_fun.first.first[i].second) } else if (is_ptr(t1->bound_to)) { unify(t1->bound_to->_ptr, t2->bound_to->_ptr) } else if (is_obj(t1->bound_to)) { @@ -87,21 +86,14 @@ fun inst_temp_type(t: *binding, replacements: ref map<*binding, *bin else return binding_p(type::_ptr(cp)) } - type::_ref(r) { - var cr = inst_temp_type(r, replacements) - if (cr == r) - return t - else - return binding_p(type::_ref(cr)) - } type::_fun(b) { // triple, is_variadic, is raw> - var rt = inst_temp_type(b.first.second, replacements) - var pts = b.first.first.map(fun(pt: *binding): *binding return inst_temp_type(pt, replacements);) - if (rt != b.first.second) + var rt = make_pair(b.first.second.first, inst_temp_type(b.first.second.second, replacements)) + var pts = b.first.first.map(fun(pt: pair>): pair> return make_pair(pt.first, inst_temp_type(pt.second, replacements));) + if (rt.second != b.first.second.second) return binding_p(type::_fun(make_triple(make_pair(pts, rt), b.second, b.third))) for (var i = 0; i < pts.size; i++;) - if (pts[i] != b.first.first[i]) + if (pts[i].second != b.first.first[i].second) return binding_p(type::_fun(make_triple(make_pair(pts, rt), b.second, b.third))) return t } @@ -133,20 +125,15 @@ fun equality(a: *type, b: *type, count_unknown_as_equal: bool): bool { return false return equality(p->bound_to, b->_ptr->bound_to, count_unknown_as_equal) } - type::_ref(r) { - if (!is_ref(b)) - return false - return equality(r->bound_to, b->_ref->bound_to, count_unknown_as_equal) - } type::_fun(i) { if ( !(is_fun(b) && a->_fun.second == b->_fun.second && a->_fun.third == b->_fun.third) ) return false - if ( !equality(a->_fun.first.second->bound_to, b->_fun.first.second->bound_to, count_unknown_as_equal) ) + if ( !equality(a->_fun.first.second.second->bound_to, b->_fun.first.second.second->bound_to, count_unknown_as_equal) ) return false if ( !(a->_fun.first.first.size == b->_fun.first.first.size) ) return false for (var i = 0; i < a->_fun.first.first.size; i++;) - if ( !equality(a->_fun.first.first[i]->bound_to, b->_fun.first.first[i]->bound_to, count_unknown_as_equal) ) + if ( !equality(a->_fun.first.first[i].second->bound_to, b->_fun.first.first[i].second->bound_to, count_unknown_as_equal) ) return false return true } @@ -158,7 +145,6 @@ fun to_string(it: *type): str { type::_unknown() return str("_unknown") type::_void() return str("_void") type::_ptr(p) return "*" + to_string(p->bound_to) - type::_ref(r) return "ref" + to_string(r->bound_to) type::_obj(b) { return "_obj(" + to_string(b->data) + ")" } @@ -169,10 +155,10 @@ fun to_string(it: *type): str { to_ret += "_run(" else to_ret += "_fun(" - to_ret += str(", ").join(b.first.first.map(fun(pt: *binding): str return to_string(pt->bound_to);)) + to_ret += str(", ").join(b.first.first.map(fun(pt: pair>): str return to_string(pt.first) + to_string(pt.second->bound_to);)) if (b.third) to_ret += " ..." - return to_ret + "): " + to_string(b.first.second->bound_to) + return to_ret + "): " + to_string(b.first.second.first) + to_string(b.first.second.second->bound_to) } type::_template_placeholder() return str("_template_placeholder") type::_bool() return str("_bool") @@ -207,12 +193,6 @@ fun is_ptr(x: *type): bool { } return false } -fun is_ref(x: *type): bool { - match (*x) { - type::_ref(r) return true - } - return false -} fun is_obj(x: *type): bool { match (*x) { type::_obj() return true