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 tree:*
import symbol:* import symbol:*
import ast_nodes:* import ast_nodes:*
import poset:*
obj c_generator (Object) { obj c_generator (Object) {
@@ -26,15 +27,14 @@ obj c_generator (Object) {
var plain_typedefs: string = "" var plain_typedefs: string = ""
var top_level_c_passthrough: string = "" var top_level_c_passthrough: string = ""
var variable_extern_declarations: 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_pre: string = ""
var function_typedef_string: string = "" var function_typedef_string: string = ""
var function_prototypes: string = "\n/**Function Prototypes**/\n" var function_prototypes: string = "\n/**Function Prototypes**/\n"
var function_definitions: string = "\n/**Function Definitions**/\n" var function_definitions: string = "\n/**Function Definitions**/\n"
var variable_declarations: string = "\n/**Variable Declarations**/\n" var variable_declarations: string = "\n/**Variable Declarations**/\n"
var type_poset = poset<*ast_node>()
// poset generation into structs string
// iterate through asts // iterate through asts
name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) { name_ast_map.for_each(fun(name: string, tree_pair: pair<*tree<symbol>,*ast_node>) {
// iterate through children for each ast // iterate through children for each ast
@@ -68,9 +68,15 @@ obj c_generator (Object) {
// emit parameter destructors? // emit parameter destructors?
function_definitions += "}\n" 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) 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);) node->code_block.children.for_each(fun(child: *ast_node) to_ret += generate(child);)
return to_ret + "}" 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 { fun generate_function(node: *ast_node): string {
var str = node->function.name var str = node->function.name
node->function.parameters.for_each(fun(param: *ast_node) str += string("_") + type_decoration(param->identifier.type);) 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>) { fun copy_construct(old: *set<T>) {
data.copy_construct(&old->data) data.copy_construct(&old->data)
} }
fun operator=(rhs: set<T>) { fun operator=(rhs: ref set<T>) {
destruct() destruct()
copy_construct(&rhs) copy_construct(&rhs)
} }
@@ -82,7 +82,7 @@ obj set<T> (Object, Serializable) {
fun remove(item: T) { fun remove(item: T) {
var idx = data.find(item) var idx = data.find(item)
if (idx == -1) { if (idx == -1) {
io::println("CANNOT FIND ITEM TO REMOVE") /*io::println("CANNOT FIND ITEM TO REMOVE")*/
return return
} }
data.remove(idx) data.remove(idx)

View File

@@ -0,0 +1,4 @@
i depends on i+1
10 9 8 7 6 5 4 3 2 1 0
i depends on i-1
-1 0 1 2 3 4 5 6 7 8 9

28
tests/test_poset.krak Normal file
View File

@@ -0,0 +1,28 @@
import io:*
import poset:*
fun main():int {
println("i depends on i+1")
var int_poset = poset<int>()
for (var i = 0; i < 10; i++;)
int_poset.add_relationship(i, i+1)
int_poset.get_sorted().for_each(fun(i: int) {
print(i)
print(" ")
})
println()
println("i depends on i-1")
int_poset = poset<int>()
for (var i = 0; i < 10; i++;)
int_poset.add_relationship(i, i-1)
int_poset.get_sorted().for_each(fun(i: int) {
print(i)
print(" ")
})
println()
return 0
}

View File

@@ -1,11 +1,11 @@
import to_import: simple_print, a, b import to_import: simple_print, a, b
/*obj Something (ObjectTrait) {*/ obj Something (ObjectTrait) {
/*var member: int*/ var member: int
/*fun method():int {*/ fun method():int {
/*return 5*/ return 5
/*}*/ }
/*}*/ }
/*fun some_function(): int return 0;*/ /*fun some_function(): int return 0;*/
/*fun some_other_function(in: bool): float {*/ /*fun some_other_function(in: bool): float {*/
@@ -26,18 +26,19 @@ fun main(): int {
/*simple_print(1337)*/ /*simple_print(1337)*/
/*if (1 + 2 && false) simple_print("its true!")*/ /*if (1 + 2 && false) simple_print("its true!")*/
/*else simple_print("its false!")*/ /*else simple_print("its false!")*/
var counter = 10 /*var counter = 10*/
counter-- /*counter--*/
--counter /*--counter*/
while (counter) simple_print(counter--) /*while (counter) simple_print(counter--)*/
simple_print("\n") /*simple_print("\n")*/
counter = 8 /*counter = 8*/
while (counter) { /*while (counter) {*/
simple_print(--counter) /*simple_print(--counter)*/
} /*}*/
simple_print("\n") /*simple_print("\n")*/
for (var j = 0; j < 10; j++;) /*for (var j = 0; j < 10; j++;)*/
simple_print(j) /*simple_print(j)*/
var an_obj: Something
return 0 return 0
} }