Finished poset implementation added to the standard library, starting on getting types added and sorting, etc

This commit is contained in:
Nathan Braswell
2016-01-20 13:50:40 -05:00
parent 162cc98f30
commit 4ebb8bf107
6 changed files with 131 additions and 24 deletions

View File

@@ -6,6 +6,7 @@ import util:*
import tree:*
import symbol:*
import ast_nodes:*
import poset:*
obj c_generator (Object) {
@@ -26,15 +27,14 @@ obj c_generator (Object) {
var plain_typedefs: string = ""
var top_level_c_passthrough: string = ""
var variable_extern_declarations: string = ""
var structs: string = ""
var structs: string = "\n/**Type Structs**/\n"
var function_typedef_string_pre: string = ""
var function_typedef_string: string = ""
var function_prototypes: string = "\n/**Function Prototypes**/\n"
var function_definitions: string = "\n/**Function Definitions**/\n"
var variable_declarations: string = "\n/**Variable Declarations**/\n"
// poset generation into structs string
var type_poset = poset<*ast_node>()
// iterate through asts
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) {
// iterate through children for each ast
@@ -68,9 +68,15 @@ obj c_generator (Object) {
// emit parameter destructors?
function_definitions += "}\n"
}
ast_node::type_def(backing) {
type_poset.add_vertex(child)
}
}
})
})
type_poset.get_sorted().for_each(fun(vert: *ast_node) {
structs += "/* a type */\n"
})
return make_pair(prequal+plain_typedefs+top_level_c_passthrough+variable_extern_declarations+structs+function_typedef_string_pre+function_typedef_string+function_prototypes+variable_declarations+function_definitions + "\n", linker_string)
}
@@ -117,7 +123,7 @@ obj c_generator (Object) {
node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child);)
return to_ret + "}"
}
// this generates the function as a value, not the actuall function
// this generates the function as a value, not the actual function
fun generate_function(node: *ast_node): string {
var str = node->function.name
node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);)

68
stdlib/poset.krak Normal file
View File

@@ -0,0 +1,68 @@
import vector:*
import queue:*
import map:*
import set:*
fun poset<T>(): poset<T> {
var to_ret.construct(): poset<T>
return to_ret
}
obj poset<T> (Object) {
var adj_matrix: map<T, set<T>>
fun construct(): *poset<T> {
adj_matrix.construct()
return this
}
fun copy_construct(old: *poset<T>) {
adj_matrix.copy_construct(&old->adj_matrix)
}
fun operator=(other: ref poset<T>) {
destruct()
copy_construct(&other)
}
fun destruct() {
adj_matrix.destruct()
}
fun add_relationship(first: T, second: T) {
if (!adj_matrix.contains_key(first))
add_vertex(first)
if (!adj_matrix.contains_key(second))
add_vertex(second)
adj_matrix[first].add(second)
}
fun add_vertex(vertex: T) {
if (adj_matrix.contains_key(vertex))
return;
adj_matrix.set(vertex, set<T>())
}
fun get_depends_on(vertex: T): set<T> {
var depends_on = set<T>()
adj_matrix.for_each(fun(key: T, value: set<T>) {
if (value.contains(vertex))
depends_on.add(key)
})
return depends_on
}
fun get_sorted(): vector<T> {
var sorted = vector<T>()
var to_do = queue<T>()
// because we're going to destructivly update
var temp_adj_matrix = adj_matrix
temp_adj_matrix.for_each(fun(key: T, value: set<T>)
if (temp_adj_matrix[key].size() == 0) to_do.push(key);)
while (!to_do.empty()) {
var current = to_do.pop()
sorted.add(current)
get_depends_on(current).for_each(fun(vert: T) {
temp_adj_matrix[vert].remove(current)
/*print(vert); print(" now has "); print(temp_adj_matrix[vert].size()); println(" dependencies")*/
if (temp_adj_matrix[vert].size() == 0)
to_do.push(vert)
})
}
return sorted
}
}

View File

@@ -33,7 +33,7 @@ obj set<T> (Object, Serializable) {
fun copy_construct(old: *set<T>) {
data.copy_construct(&old->data)
}
fun operator=(rhs: set<T>) {
fun operator=(rhs: ref set<T>) {
destruct()
copy_construct(&rhs)
}
@@ -82,7 +82,7 @@ obj set<T> (Object, Serializable) {
fun remove(item: T) {
var idx = data.find(item)
if (idx == -1) {
io::println("CANNOT FIND ITEM TO REMOVE")
/*io::println("CANNOT FIND ITEM TO REMOVE")*/
return
}
data.remove(idx)