diff --git a/stdlib/c_generator.krak b/stdlib/c_generator.krak index e4498ae..84479cf 100644 --- a/stdlib/c_generator.krak +++ b/stdlib/c_generator.krak @@ -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,*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);) diff --git a/stdlib/poset.krak b/stdlib/poset.krak new file mode 100644 index 0000000..305b1f7 --- /dev/null +++ b/stdlib/poset.krak @@ -0,0 +1,68 @@ +import vector:* +import queue:* +import map:* +import set:* + +fun poset(): poset { + var to_ret.construct(): poset + return to_ret +} + +obj poset (Object) { + var adj_matrix: map> + fun construct(): *poset { + adj_matrix.construct() + return this + } + fun copy_construct(old: *poset) { + adj_matrix.copy_construct(&old->adj_matrix) + } + fun operator=(other: ref poset) { + 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()) + } + fun get_depends_on(vertex: T): set { + var depends_on = set() + adj_matrix.for_each(fun(key: T, value: set) { + if (value.contains(vertex)) + depends_on.add(key) + }) + return depends_on + } + fun get_sorted(): vector { + var sorted = vector() + var to_do = queue() + // because we're going to destructivly update + var temp_adj_matrix = adj_matrix + temp_adj_matrix.for_each(fun(key: T, value: set) + 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 + } +} + + diff --git a/stdlib/set.krak b/stdlib/set.krak index 8a99ca5..a73f74c 100644 --- a/stdlib/set.krak +++ b/stdlib/set.krak @@ -33,7 +33,7 @@ obj set (Object, Serializable) { fun copy_construct(old: *set) { data.copy_construct(&old->data) } - fun operator=(rhs: set) { + fun operator=(rhs: ref set) { destruct() copy_construct(&rhs) } @@ -82,7 +82,7 @@ obj set (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) diff --git a/tests/test_poset.expected_results b/tests/test_poset.expected_results new file mode 100644 index 0000000..499e9b3 --- /dev/null +++ b/tests/test_poset.expected_results @@ -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 diff --git a/tests/test_poset.krak b/tests/test_poset.krak new file mode 100644 index 0000000..6346c9b --- /dev/null +++ b/tests/test_poset.krak @@ -0,0 +1,28 @@ +import io:* +import poset:* + + + +fun main():int { + println("i depends on i+1") + var int_poset = poset() + 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() + 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 +} + diff --git a/tests/to_parse.krak b/tests/to_parse.krak index 624b871..7df62b7 100644 --- a/tests/to_parse.krak +++ b/tests/to_parse.krak @@ -1,11 +1,11 @@ import to_import: simple_print, a, b -/*obj Something (ObjectTrait) {*/ - /*var member: int*/ - /*fun method():int {*/ - /*return 5*/ - /*}*/ -/*}*/ +obj Something (ObjectTrait) { + var member: int + fun method():int { + return 5 + } +} /*fun some_function(): int return 0;*/ /*fun some_other_function(in: bool): float {*/ @@ -26,18 +26,19 @@ fun main(): int { /*simple_print(1337)*/ /*if (1 + 2 && false) simple_print("its true!")*/ /*else simple_print("its false!")*/ - var counter = 10 - counter-- - --counter - while (counter) simple_print(counter--) - simple_print("\n") - counter = 8 - while (counter) { - simple_print(--counter) - } - simple_print("\n") - for (var j = 0; j < 10; j++;) - simple_print(j) + /*var counter = 10*/ + /*counter--*/ + /*--counter*/ + /*while (counter) simple_print(counter--)*/ + /*simple_print("\n")*/ + /*counter = 8*/ + /*while (counter) {*/ + /*simple_print(--counter)*/ + /*}*/ + /*simple_print("\n")*/ + /*for (var j = 0; j < 10; j++;)*/ + /*simple_print(j)*/ + var an_obj: Something return 0 }