address of and dereference implemented as templated compiler intrinsics

This commit is contained in:
Nathan Braswell
2018-10-16 19:12:10 -04:00
parent c0a6b614d0
commit 194842397f
3 changed files with 23 additions and 5 deletions

22
k.krak
View File

@@ -90,6 +90,7 @@ fun main(argc: int, argv: **char): int {
var multiple_binding_options = map<*tree<ast>, vec<*tree<ast>>>()
var primitive_ops.construct(): map<str, vec<*tree<ast>>>
var number_tower = vec(binding_p(type::_char()),
binding_p(type::_uchar()),
binding_p(type::_short()),
@@ -150,6 +151,21 @@ fun main(argc: int, argv: **char): int {
}
math.remove(math.size-1)
// 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
),
binding_p(type::_ptr(template_type))
), false, false))), vec<*binding<type>>()))))
// 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))
),
template_type
), false, false))), vec<*binding<type>>()))))
// resolves all binding possibilities for one top level item
passes[str("name_possibility_resolve")] = fun(item: *tree<ast>) {
println("Running name possibility resolver?")
@@ -707,7 +723,7 @@ fun main(argc: int, argv: **char): int {
C_str += ")"
}
}
ast::_compiler_intrinsic(b) { error("compiler_intrinsic gen unimplemented"); }
ast::_compiler_intrinsic(b) { /* this can happen cuz of templated primitive ops and is_top_level_item includes parentless stuff...*/ }
ast::_cast(b) {
C_str += idt + "((" + to_c_type(b) + ")"
emit_C(t->children[0], 0)
@@ -974,10 +990,10 @@ fun syntax_to_ast(file_name: str, syntax: *tree<symbol>, import_paths: ref vec<s
return parse_type(s, declared_template_types);
}))
} else if (syntax->children[0]->data.terminal) {
return _call(vec(make_ast_binding(concat(syntax->children[0])),
return _call(vec(make_ast_binding("op" + concat(syntax->children[0])),
syntax_to_ast_helper(syntax->children[1], declared_template_types)))
} else {
return _call(vec(make_ast_binding(concat(syntax->children[1])),
return _call(vec(make_ast_binding("op" + concat(syntax->children[1])),
syntax_to_ast_helper(syntax->children[0], declared_template_types)))
}
} else {

View File

@@ -232,7 +232,7 @@ fun is_compiler_intrinsic(i: *tree<ast>): bool { match(i->data) { ast::_compiler
fun is_cast(i: *tree<ast>): bool { match(i->data) { ast::_cast(b) return true; } return false; }
fun is_value(i: *tree<ast>): bool { match(i->data) { ast::_value(b) return true; } return false; }
fun is_top_level_item(i: *tree<ast>): bool { return i->parent != null<tree<ast>>() && is_translation_unit(i->parent); }
fun is_top_level_item(i: *tree<ast>): bool { return i->parent == null<tree<ast>>() || is_translation_unit(i->parent); }
fun get_ancestor_satisfying(t: *tree<ast>, p: fun(*tree<ast>): bool): *tree<ast> {
t = t->parent

View File

@@ -39,6 +39,8 @@ fun unify(t1: *binding<type>, t2: *binding<type>) {
unify(t1->bound_to->_fun.first.second, t2->bound_to->_fun.first.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])
} else if (is_ptr(t1->bound_to)) {
unify(t1->bound_to->_ptr, t2->bound_to->_ptr)
}
} else {
error("Doesn't typecheck! Attempted to unify " + to_string(t1->bound_to) + " and " + to_string(t2->bound_to))
@@ -112,7 +114,7 @@ fun shallow_equality(a: *type, b: *type):bool {
if (is_ptr(a) != is_ptr(b))
return false
if (is_ptr(a) && is_ptr(b))
return shallow_equality(a->_ptr->bound_to, b->_ptr->bound_to)
return true
match(*a) {
type::_fun(x) {
return is_fun(b) && a->_fun.third == b->_fun.third