Add basis for types in k, and move to new poset setup (depend on individual imports, functions, types, and global variables)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import tree:*
|
||||
import type:*
|
||||
import type2:*
|
||||
import vec:*
|
||||
import set:*
|
||||
import util:*
|
||||
@@ -8,7 +8,7 @@ import mem:*
|
||||
|
||||
adt ast {
|
||||
_translation_unit: str,
|
||||
_import: set<str>,
|
||||
_import: pair<*ast, set<str>>,
|
||||
_identifier: pair<str, *type>,
|
||||
_binding: triple<str, vec<*type>, *tree<ast>>,
|
||||
_type_def: str,
|
||||
@@ -35,9 +35,9 @@ adt ast {
|
||||
fun to_string(a: ref ast): str {
|
||||
match(a) {
|
||||
ast::_translation_unit(b) return str("_translation_unit(") + b + ")"
|
||||
ast::_import(b) return str("_import[") + str(",").join(b.data) + "]"
|
||||
ast::_import(b) return str("_import(") + to_string(*b.first) + ")[" + str(",").join(b.second.data) + "]"
|
||||
ast::_identifier(b) return str("_identifier(") + b.first + ")"
|
||||
ast::_binding(b) return str("_binding(") + b.first + ")"
|
||||
ast::_binding(b) return str("_binding(") + b.first + "->" + to_string(b.third) + ")"
|
||||
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 + ")"
|
||||
@@ -63,8 +63,8 @@ fun to_string(a: ref ast): str {
|
||||
fun _translation_unit(p: str): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_translation_unit(p))
|
||||
}
|
||||
fun _import(p: set<str>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_import(p))
|
||||
fun _import(p1: *ast, p2: set<str>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_import(make_pair(p1,p2)))
|
||||
}
|
||||
fun _type_def(p: str): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_type_def(p))
|
||||
@@ -141,8 +141,8 @@ fun _call(): *tree<ast> {
|
||||
fun _translation_unit(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_translation_unit(p), c)
|
||||
}
|
||||
fun _import(p: set<str>, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_import(p), c)
|
||||
fun _import(p1: *ast, p2: set<str>, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_import(make_pair(p1,p2)), c)
|
||||
}
|
||||
fun _type_def(p: str, c: ref vec<*tree<ast>>): *tree<ast> {
|
||||
return new<tree<ast>>()->construct(ast::_type_def(p), c)
|
||||
|
||||
@@ -838,14 +838,21 @@ obj ast_transformation (Object) {
|
||||
if (!possible_value) match (searching_for) {
|
||||
search_type::function(type_vec) possible_value = find_or_instantiate_template_function(concat_symbol_tree(node->children[0]), null<tree<symbol>>(), scope, type_vec, template_replacements, map<str, *type>());
|
||||
}
|
||||
if (!possible_value)
|
||||
if (!possible_value) {
|
||||
var function_error_str = str()
|
||||
match (searching_for) {
|
||||
search_type::function() {
|
||||
function_error_str = "(" + searching_for.function.reduce(fun(n:*type, s:str):str {
|
||||
if (n)
|
||||
return s+","+n->to_string()
|
||||
else
|
||||
return s+",null"
|
||||
}, str()) + ")"
|
||||
}
|
||||
}
|
||||
error(node, concat_symbol_tree(node) + ": HAS NO POSSIBLE FUNCTION OR FUNCTION TEMPLATE SOLUTIONS\nlooking for: " +
|
||||
concat_symbol_tree(node->children[0]) + "(" + searching_for.function.reduce(fun(n:*type, s:str):str {
|
||||
if (n)
|
||||
return s+","+n->to_string()
|
||||
else
|
||||
return s+",null"
|
||||
}, str()) + ")")
|
||||
concat_symbol_tree(node->children[0]) + function_error_str)
|
||||
}
|
||||
return possible_value
|
||||
} else if (node->children.size == 2) {
|
||||
var template_inst = get_node("template_inst", node)
|
||||
|
||||
@@ -66,7 +66,7 @@ obj map<T,U> (Object, Serializable) {
|
||||
fun contains_key(key: ref T): bool {
|
||||
return keys.contains(key)
|
||||
}
|
||||
fun contains_value(value: ref U): bool {
|
||||
fun contains_value<V>(value: ref V): bool {
|
||||
return values.contains(value)
|
||||
}
|
||||
fun get(key: ref T): ref U {
|
||||
@@ -86,7 +86,7 @@ obj map<T,U> (Object, Serializable) {
|
||||
return get(key)
|
||||
return default_val
|
||||
}
|
||||
fun reverse_get(value: ref U): ref T {
|
||||
fun reverse_get<V>(value: ref V): ref T {
|
||||
/*return values.get(keys.find(key))*/
|
||||
var value_loc = values.find(value)
|
||||
if (value_loc == -1)
|
||||
|
||||
@@ -48,6 +48,27 @@ obj poset<T> (Object) {
|
||||
})
|
||||
return depends_on
|
||||
}
|
||||
fun top(): T {
|
||||
for (var i = 0; i < adj_matrix.keys.size; i++;) {
|
||||
if (adj_matrix.values[i].size() == 0) {
|
||||
return adj_matrix.keys[i]
|
||||
}
|
||||
}
|
||||
error("Nothing to top")
|
||||
}
|
||||
fun remove(x: ref T) {
|
||||
var dependencies = adj_matrix.get_ptr_or_null(x)
|
||||
if (dependencies == null<set<T>>())
|
||||
error("Trying to remove item from poset that doesn't contain it!")
|
||||
if (dependencies->size() != 0)
|
||||
error("Trying to remove item from poset that still has dependencies on it!")
|
||||
|
||||
for (var j = 0; j < adj_matrix.keys.size; j++;) {
|
||||
// remove is ok if it doesn't exist
|
||||
adj_matrix.values[j].remove(x)
|
||||
}
|
||||
adj_matrix.remove(x)
|
||||
}
|
||||
fun pop(): T {
|
||||
for (var i = 0; i < adj_matrix.keys.size; i++;) {
|
||||
if (adj_matrix.values[i].size() == 0) {
|
||||
|
||||
206
stdlib/type2.krak
Normal file
206
stdlib/type2.krak
Normal file
@@ -0,0 +1,206 @@
|
||||
import mem:*
|
||||
import str:*
|
||||
import vec:*
|
||||
import util:*
|
||||
import ast:*
|
||||
|
||||
adt base_type {
|
||||
_unknown,
|
||||
_void,
|
||||
_object: *ast,
|
||||
// triple<pair<param_types, return_type>, is_variadic, is raw>
|
||||
_function: triple<pair<vec<*type>, *type>, bool, bool>,
|
||||
_template_placeholder,
|
||||
_bool,
|
||||
_char,
|
||||
_uchar,
|
||||
_short,
|
||||
_ushort,
|
||||
_int,
|
||||
_uint,
|
||||
_long,
|
||||
_ulong,
|
||||
_float,
|
||||
_double
|
||||
}
|
||||
|
||||
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);
|
||||
fun operator==(other: ref type):bool return equality(other, true);
|
||||
fun equality(other: *type, care_about_ref: bool):bool return equality(*other, care_about_ref);
|
||||
fun equality(other: ref type, care_about_ref: bool):bool {
|
||||
if (care_about_ref && (is_ref != other.is_ref))
|
||||
return false
|
||||
return base == other.base && indirection == other.indirection
|
||||
}
|
||||
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::_object(b) {
|
||||
return indr_string + "_object"
|
||||
}
|
||||
base_type::_function(b) {
|
||||
// triple<pair<param_types, return_type>, is_variadic, is raw>
|
||||
return indr_string + "_function()"
|
||||
}
|
||||
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 false
|
||||
}
|
||||
fun is_void(): bool {
|
||||
match (base) {
|
||||
base_type::_void() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_object(): bool {
|
||||
match (base) {
|
||||
base_type::_object() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_function(): bool {
|
||||
match (base) {
|
||||
base_type::_function() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_template_placeholder(): bool {
|
||||
match (base) {
|
||||
base_type::_template_placeholder() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_bool(): bool {
|
||||
match (base) {
|
||||
base_type::_bool() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_char(): bool {
|
||||
match (base) {
|
||||
base_type::_char() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_uchar(): bool {
|
||||
match (base) {
|
||||
base_type::_uchar() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_short(): bool {
|
||||
match (base) {
|
||||
base_type::_short() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_ushort(): bool {
|
||||
match (base) {
|
||||
base_type::_ushort() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_int(): bool {
|
||||
match (base) {
|
||||
base_type::_int() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_uint(): bool {
|
||||
match (base) {
|
||||
base_type::_uint() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_long(): bool {
|
||||
match (base) {
|
||||
base_type::_long() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_ulong(): bool {
|
||||
match (base) {
|
||||
base_type::_ulong() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_float(): bool {
|
||||
match (base) {
|
||||
base_type::_float() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
fun is_double(): bool {
|
||||
match (base) {
|
||||
base_type::_double() return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
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
|
||||
|
||||
base_type::_uchar() return false
|
||||
base_type::_ushort() return false
|
||||
base_type::_uint() return false
|
||||
base_type::_ulong() return false
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user