Finished poset implementation added to the standard library, starting on getting types added and sorting, etc
This commit is contained in:
@@ -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
68
stdlib/poset.krak
Normal 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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
4
tests/test_poset.expected_results
Normal file
4
tests/test_poset.expected_results
Normal 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
28
tests/test_poset.krak
Normal 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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user