Can't finish template inst tonight.

This commit is contained in:
Nathan Braswell
2018-10-08 00:28:42 -04:00
parent 0ae2fbaae6
commit 39ecf24e69
8 changed files with 575 additions and 448 deletions

View File

@@ -5,6 +5,7 @@ import set:*
import util:*
import str:*
import mem:*
import binding:*
adt ast {
_translation_unit: str,
@@ -14,7 +15,7 @@ adt ast {
_type_def: str,
_adt_def: str,
_function: triple<str, *binding<type>, bool>,
_template: pair<str, set<str>>,
_template: pair<str, map<str, *binding<type>>>,
_declaration,
_block,
_if,
@@ -31,16 +32,26 @@ adt ast {
_cast: *binding<type>,
_value: pair<str, *binding<type>>
}
fun deref_to_string<T>(in: *T): str
if (in == mem::null<T>())
return str("null")
else
return to_string(in)
fun deref_to_string<T>(in: *T, ts: fun(*T): str): str
if (in == mem::null<T>())
return str("null")
else
return ts(in)
fun to_string(a: ref ast): str {
match(a) {
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 + "[" + str(",").join(b.second.map(fun(x:*binding<type>): str { return deref_to_string(x->bound_to); })) + "]" + "->" + to_string(b.third->bound_to) + ")"
ast::_binding(b) return str("_binding(") + b.first + "[" + str(",").join(b.second.map(fun(x:*binding<type>): str { return deref_to_string(x->bound_to); })) + "]" + "->" + deref_to_string(b.third->bound_to, fun(t: *tree<ast>): str return to_string(t->data);) + ")"
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) + ")"
ast::_template(b) return str("_template(") + b.first + "[" + str(",").join(b.second.data) + "])"
ast::_template(b) return str("_template(") + b.first + "[" + str(",").join(b.second.keys) + "])"
ast::_declaration() return str("_declaration")
ast::_block() return str("_block")
ast::_if() return str("_if")
@@ -82,7 +93,7 @@ fun _binding(p1: str, p2: vec<*binding<type>>, p3: *binding<tree<ast>>): *tree<a
fun _function(p1: str, p2: *binding<type>, p3: bool): *tree<ast> {
return new<tree<ast>>()->construct(ast::_function(make_triple(p1, p2, p3)))
}
fun _template(p1: str, p2: set<str>): *tree<ast> {
fun _template(p1: str, p2: map<str, *binding<type>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_template(make_pair(p1, p2)))
}
fun _compiler_intrinsic(p1: str, p2: *binding<type>, p3: vec<*binding<type>>): *tree<ast> {
@@ -154,7 +165,7 @@ fun _binding(p1: str, p2: vec<*binding<type>>, p3: *binding<tree<ast>>, c: ref v
fun _function(p1: str, p2: *binding<type>, p3: bool, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_function(make_triple(p1, p2, p3)), c)
}
fun _template(p1: str, p2: set<str>, c: ref vec<*tree<ast>>): *tree<ast> {
fun _template(p1: str, p2: map<str, *binding<type>>, c: ref vec<*tree<ast>>): *tree<ast> {
return new<tree<ast>>()->construct(ast::_template(make_pair(p1, p2)), c)
}
fun _compiler_intrinsic(p1: str, p2: *binding<type>, p3: vec<*binding<type>>, c: ref vec<*tree<ast>>): *tree<ast> {
@@ -229,59 +240,6 @@ fun get_ancestor_satisfying(t: *tree<ast>, p: fun(*tree<ast>): bool): *tree<ast>
return t
}
var bindings: *vec<*void>
fun binding<T>(): *binding<T> {
return binding(null<T>())
}
fun binding<T>(it: *T): *binding<T> {
var to_ret = new<binding<T>>()->construct(it)
if (bindings == null<vec<*void>>())
bindings = new<vec<*void>>()->construct()
bindings->add( (to_ret) cast *void )
return to_ret
}
obj binding<T> (Object) {
var bound_to: *T
fun construct(): *binding<T> {
bound_to = null<T>()
return this
}
fun construct(it: *T): *binding<T> {
bound_to = it
return this
}
fun copy_construct(old: *binding<T>): void {
bound_to = old->bound_to
}
fun destruct() {
bound_to = null<T>()
}
fun bound(): bool {
return bound_to != null<T>()
}
fun set(to: *T) {
// don't set null, that will set all unbound ones
if (bound_to == null<T>()) {
bound_to = to
return
}
var from = bound_to
for (var i = 0; i < bindings->size; i++;)
if ( ((bindings->get(i)) cast *binding<T>)->bound_to == from)
((bindings->get(i)) cast *binding<T>)->bound_to = to
}
fun to_string(): str {
return "binding(" + to_string(bound_to) + ")"
/*return "binding(" + deref_to_string(bound_to) + ")"*/
}
}
fun make_ast_binding(s: *char): *tree<ast> {
return make_ast_binding(str(s))
}

66
stdlib/binding.krak Normal file
View File

@@ -0,0 +1,66 @@
import vec:*
import str:*
// for decent to string
// should be fixed by UFCS or decent scoping on template types
import ast:*
import type2:*
var bindings: *vec<*void>
fun binding<T>(): *binding<T> {
return binding(null<T>())
}
fun binding_p<T>(it: T): *binding<T> {
var p = new<T>()
p->copy_construct(&it)
return binding(p)
}
fun binding<T>(it: *T): *binding<T> {
var to_ret = new<binding<T>>()->construct(it)
if (bindings == null<vec<*void>>())
bindings = new<vec<*void>>()->construct()
bindings->add( (to_ret) cast *void )
return to_ret
}
obj binding<T> (Object) {
var bound_to: *T
fun construct(): *binding<T> {
bound_to = null<T>()
return this
}
fun construct(it: *T): *binding<T> {
bound_to = it
return this
}
fun copy_construct(old: *binding<T>): void {
bound_to = old->bound_to
}
fun destruct() {
bound_to = null<T>()
}
fun bound(): bool {
return bound_to != null<T>()
}
fun set(to: T) {
var p = new<T>()
p->copy_construct(&to)
set(p)
}
fun set(to: *T) {
// don't set null, that will set all unbound ones
if (bound_to == null<T>()) {
bound_to = to
return
}
var from = bound_to
for (var i = 0; i < bindings->size; i++;)
if ( ((bindings->get(i)) cast *binding<T>)->bound_to == from)
((bindings->get(i)) cast *binding<T>)->bound_to = to
}
fun to_string(): str {
/*return "binding(" + to_string(bound_to) + ")"*/
return "binding(" + deref_to_string(bound_to) + ")"
}
}

View File

@@ -115,5 +115,13 @@ obj map<T,U> (Object, Serializable) {
for (var i = 0; i < keys.size; i++;)
func(keys[i], values[i])
}
fun associate<O,N>(func: fun(T,U): util::pair<O,N>): map<O,N> {
var to_ret = map<O,N>()
for (var i = 0; i < keys.size; i++;) {
var nkv = func(keys[i], values[i])
to_ret[nkv.first] = nkv.second
}
return to_ret
}
}

View File

@@ -1,6 +1,7 @@
import vec
import io
import serialize
import util
fun set<T>(): set<T> {
var toRet.construct() : set<T>
@@ -55,6 +56,11 @@ obj set<T> (Object, Serializable) {
fun size():int {
return data.size
}
fun single(): T {
if (size() != 1)k
util::error("trying to single with size != 1")
return data[0]
}
fun contains(items: ref set<T>): bool {
return items.size() == 0 || !items.any_true( fun(item: T): bool return !contains(item); )
}

View File

@@ -37,11 +37,12 @@ fun to_string(in: ulong): str
fun to_string<T>(in: *T): str
return str("ptr:<") + to_string_num((in) cast ulong) + ">"
fun deref_to_string<T>(in: *T): str
if (in == mem::null<T>())
return str("null")
else
return in->to_string()
/*fun deref_to_string<T>(in: *T): str*/
/*if (in == mem::null<T>())*/
/*return str("null")*/
/*else*/
/*return to_string(in)*/
/*return in->to_string()*/
fun string_to_num<T>(it: str): T {
var is_negative = false

View File

@@ -4,14 +4,17 @@ import vec:*
import util:*
import tree:*
import ast:*
import binding:*
adt base_type {
adt type {
_unknown,
_void,
_template_placeholder,
_ptr: *binding<type>,
_ref: *binding<type>,
_obj: *tree<ast>,
// triple<pair<param_types, return_type>, is_variadic, is raw>
_fun: triple<pair<vec<*binding<type>>, *binding<type>>, bool, bool>,
_template_placeholder,
_bool,
_char,
_uchar,
@@ -24,225 +27,224 @@ adt base_type {
_float,
_double
}
fun type(b: base_type, ind: int, ref: bool): *type {
return new<type>()->construct(b, ind, ref)
fun unify(t1: *binding<type>, t2: *binding<type>) {
if (is_unknown(t1->bound_to)) {
t1->set(t2->bound_to)
} else if (is_unknown(t2->bound_to)) {
t2->set(t1->bound_to)
} 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)
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 {
error("Doesn't typecheck! Attempted to unify " + to_string(t1->bound_to) + " and " + to_string(t2->bound_to))
}
}
}
obj type (Object) {
var base: base_type
var indirection: int
var is_ref: bool
fun construct(): *type {
base.copy_construct(&base_type::_unknown())
indirection = 0
is_ref = false
return this
}
fun construct(base_in: base_type, indirection_in: int, is_ref_in: bool): *type {
base.copy_construct(&base_in)
indirection = indirection_in
is_ref = is_ref_in
return this
}
fun copy_construct(old: *type) {
base.copy_construct(&old->base)
indirection = old->indirection
is_ref = old->is_ref
}
fun operator=(other: ref type) {
destruct()
copy_construct(&other)
}
fun destruct() {
base.destruct()
}
fun operator!=(other: ref type):bool return !equality(other, true, false);
fun operator==(other: ref type):bool return equality(other, true, false);
fun equality(other: *type, care_about_ref: bool, count_unknown_as_equal: bool):bool return equality(*other, care_about_ref, count_unknown_as_equal);
fun equality(other: ref type, care_about_ref: bool, count_unknown_as_equal: bool):bool {
if (count_unknown_as_equal && (is_unknown() || other.is_unknown()))
fun equality(a: *type, b: *type, count_unknown_as_equal: bool): bool {
if (count_unknown_as_equal && (is_unknown(a) || is_unknown(b)))
return true
match(*a) {
type::_ptr(p) {
if (!is_ptr(b))
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) )
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) )
return false
return true
if (care_about_ref && (is_ref != other.is_ref))
return false
if (indirection != other.indirection)
return false
match(base) {
base_type::_fun(b) {
if ( !(other.is_fun() && base._fun.second == other.base._fun.second && base._fun.third == other.base._fun.third) )
return false
if ( !(base._fun.first.second->bound_to->equality(other.base._fun.first.second->bound_to, care_about_ref, count_unknown_as_equal)) )
return false
if ( !(base._fun.first.first.size == other.base._fun.first.first.size) )
return false
for (var i = 0; i < base._fun.first.first.size; i++;)
if ( !(base._fun.first.first[i]->bound_to->equality(other.base._fun.first.first[i]->bound_to, care_about_ref, count_unknown_as_equal)) )
return false
return true
}
}
return base == other.base
}
fun shallow_equality(other: *type):bool {
return shallow_equality(*other)
}
fun shallow_equality(other: ref type):bool {
if (indirection != other.indirection)
return false
match(base) {
base_type::_fun(b) {
return other.is_fun() && base._fun.third == other.base._fun.third
}
}
return base == other.base
}
fun to_string(): str {
var indr_string = str("")
if (is_ref)
indr_string += " ref "
for (var i = 0; i < indirection; i++;) indr_string += "*"
match (base) {
base_type::_unknown() return indr_string + "_unknown"
base_type::_void() return indr_string + "_void"
base_type::_obj(b) {
return indr_string + "_obj(" + to_string(b->data) + ")"
}
base_type::_fun(b) {
// triple<pair<param_types, return_type>, is_variadic, is raw>
var to_ret = indr_string
if (b.second)
to_ret += "_run("
else
to_ret += "_fun("
to_ret += str(", ").join(b.first.first.map(fun(pt: *binding<type>): str return pt->bound_to->to_string();))
if (b.third)
to_ret += " ..."
return to_ret + "): " + b.first.second->bound_to->to_string()
}
base_type::_template_placeholder() return indr_string + "_template_placeholder"
base_type::_bool() return indr_string + "_bool"
base_type::_char() return indr_string + "_char"
base_type::_uchar() return indr_string + "_uchar"
base_type::_short() return indr_string + "_short"
base_type::_ushort() return indr_string + "_ushort"
base_type::_int() return indr_string + "_int"
base_type::_uint() return indr_string + "_uint"
base_type::_long() return indr_string + "_long"
base_type::_ulong() return indr_string + "_ulong"
base_type::_float() return indr_string + "_float"
base_type::_double() return indr_string + "_double"
}
return str("impossible type, indirection:") + indirection
}
fun is_unknown(): bool {
match (base) {
base_type::_unknown() return true
}
return *a == *b
}
fun shallow_equality(a: *type, b: *type):bool {
if (is_ptr(a) != is_ptr(b))
return false
}
fun is_void(): bool {
match (base) {
base_type::_void() return true
if (is_ptr(a) && is_ptr(b))
return shallow_equality(a->_ptr->bound_to, b->_ptr->bound_to)
match(*a) {
type::_fun(x) {
return is_fun(b) && a->_fun.third == b->_fun.third
}
return false
}
fun is_obj(): bool {
match (base) {
base_type::_obj() return true
return *a == *b
}
fun to_string(it: *type): str {
match (*it) {
type::_unknown() return str("_unknown")
type::_void() return str("_void")
type::_ptr(p) return "*" + to_string(p)
type::_ref(r) return "ref" + to_string(r)
type::_obj(b) {
return "_obj(" + to_string(b->data) + ")"
}
return false
}
fun is_fun(): bool {
match (base) {
base_type::_fun() return true
type::_fun(b) {
// triple<pair<param_types, return_type>, is_variadic, is raw>
var to_ret = str()
if (b.second)
to_ret += "_run("
else
to_ret += "_fun("
to_ret += str(", ").join(b.first.first.map(fun(pt: *binding<type>): str return to_string(pt->bound_to);))
if (b.third)
to_ret += " ..."
return to_ret + "): " + to_string(b.first.second->bound_to)
}
return false
type::_template_placeholder() return str("_template_placeholder")
type::_bool() return str("_bool")
type::_char() return str("_char")
type::_uchar() return str("_uchar")
type::_short() return str("_short")
type::_ushort() return str("_ushort")
type::_int() return str("_int")
type::_uint() return str("_uint")
type::_long() return str("_long")
type::_ulong() return str("_ulong")
type::_float() return str("_float")
type::_double() return str("_double")
}
fun is_template_placeholder(): bool {
match (base) {
base_type::_template_placeholder() return true
}
return false
return str("impossible type")
}
fun is_unknown(x: *type): bool {
match (*x) {
type::_unknown() return true
}
fun is_bool(): bool {
match (base) {
base_type::_bool() return true
}
return false
return false
}
fun is_void(x: *type): bool {
match (*x) {
type::_void() return true
}
fun is_char(): bool {
match (base) {
base_type::_char() return true
}
return false
return false
}
fun is_ptr(x: *type): bool {
match (*x) {
type::_ptr(p) return true
}
fun is_uchar(): bool {
match (base) {
base_type::_uchar() return true
}
return false
return false
}
fun is_ref(x: *type): bool {
match (*x) {
type::_ref(r) return true
}
fun is_short(): bool {
match (base) {
base_type::_short() return true
}
return false
return false
}
fun is_obj(x: *type): bool {
match (*x) {
type::_obj() return true
}
fun is_ushort(): bool {
match (base) {
base_type::_ushort() return true
}
return false
return false
}
fun is_fun(x: *type): bool {
match (*x) {
type::_fun(b) return true
}
fun is_int(): bool {
match (base) {
base_type::_int() return true
}
return false
return false
}
fun is_template_placeholder(x: *type): bool {
match (*x) {
type::_template_placeholder() return true
}
fun is_uint(): bool {
match (base) {
base_type::_uint() return true
}
return false
return false
}
fun is_bool(x: *type): bool {
match (*x) {
type::_bool() return true
}
fun is_long(): bool {
match (base) {
base_type::_long() return true
}
return false
return false
}
fun is_char(x: *type): bool {
match (*x) {
type::_char() return true
}
fun is_ulong(): bool {
match (base) {
base_type::_ulong() return true
}
return false
return false
}
fun is_uchar(x: *type): bool {
match (*x) {
type::_uchar() return true
}
fun is_float(): bool {
match (base) {
base_type::_float() return true
}
return false
return false
}
fun is_short(x: *type): bool {
match (*x) {
type::_short() return true
}
fun is_double(): bool {
match (base) {
base_type::_double() return true
}
return false
return false
}
fun is_ushort(x: *type): bool {
match (*x) {
type::_ushort() return true
}
fun is_signed(): bool {
match (base) {
base_type::_char() return true
base_type::_int() return true
base_type::_long() return true
base_type::_short() return true
base_type::_float() return true
base_type::_double() return true
return false
}
fun is_int(x: *type): bool {
match (*x) {
type::_int() return true
}
return false
}
fun is_uint(x: *type): bool {
match (*x) {
type::_uint() return true
}
return false
}
fun is_long(x: *type): bool {
match (*x) {
type::_long() return true
}
return false
}
fun is_ulong(x: *type): bool {
match (*x) {
type::_ulong() return true
}
return false
}
fun is_float(x: *type): bool {
match (*x) {
type::_float() return true
}
return false
}
fun is_double(x: *type): bool {
match (*x) {
type::_double() return true
}
return false
}
fun is_signed(x: *type): bool {
match (*x) {
type::_char() return true
type::_int() return true
type::_long() return true
type::_short() return true
type::_float() return true
type::_double() return true
base_type::_uchar() return false
base_type::_ushort() return false
base_type::_uint() return false
base_type::_ulong() return false
}
return false
type::_uchar() return false
type::_ushort() return false
type::_uint() return false
type::_ulong() return false
}
return false
}

View File

@@ -3,6 +3,7 @@ import util:*;
import io:*;
import serialize:*;
import util:*;
import map:*;
fun vec<T>():vec<T> {
var out.construct():vec<T>
@@ -169,7 +170,6 @@ obj vec<T> (Object, Serializable) {
}
fun get(index: int): ref T {
if (index < 0 || index >= size) {
println("Vector access out of bounds! Retuning 0th element as sanest option");
print("Vector tried to access element: ");
println(index);
print("Max Index of vec: ");
@@ -316,6 +316,14 @@ obj vec<T> (Object, Serializable) {
}
return newVec
}
fun associate<K,V>(func: fun(T):pair<K,V>): map<K,V> {
var to_ret = map<K,V>()
for (var i = 0; i < size; i++;) {
var kv = func(data[i])
to_ret[kv.first] = kv.second
}
return to_ret
}
fun find_first_satisfying(func: fun(T):bool): T return filter(func)[0]
fun filter(func: fun(T):bool):vec<T> {
var newVec.construct(): vec<T>