Fixed the close over methods and member vars bug, but there's something remaining causing the safe_recursive_delete not to work. Gotta save progress and do other stuff

This commit is contained in:
Nathan Braswell
2015-06-27 18:06:02 -04:00
parent 8feb9819b8
commit c50c977a9e
20 changed files with 370 additions and 40 deletions

View File

@@ -38,6 +38,14 @@ fun print(toPrint: string) : void {
print(charArr);
}
fun print(toPrint: bool): void {
if (toPrint)
print("true")
else
print("false")
return;
}
fun print(toPrint: int): void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """

View File

@@ -1,4 +1,5 @@
import vector
import io
fun map<T,U>(): map<T,U> {
var toRet.construct(): map<T,U>
@@ -22,18 +23,19 @@ obj map<T,U> {
keys.copy_construct(&old->keys)
values.copy_construct(&old->values)
}
fun operator=(rhs: map<T,U>) {
destruct()
copy_construct(&rhs)
}
fun destruct() {
keys.destruct()
values.destruct()
}
fun find_index(key: T): int {
return keys.find_index(key)
}
fun operator[]=(key: T, value: U) {
set(key,value)
}
fun set(key: T, value: U) {
var keyIdx = find_index(key)
var keyIdx = keys.find(key)
if (keyIdx >= 0) {
values.set(keyIdx, value)
return;
@@ -42,7 +44,16 @@ obj map<T,U> {
values.add(value)
}
fun get(key: T): U {
return values.get(keys.find_index(key))
return values.get(keys.find(key))
}
fun remove(key: T) {
var idx = keys.find(key)
if (idx < 0) {
io::println("trying to remove nonexistant key-value!")
return;
}
keys.remove(idx)
values.remove(idx)
}
fun operator[](key: T): U {
return get(key)

View File

@@ -1,4 +1,4 @@
import io:*
import set
__if_comp__ __C__ simple_passthrough """
#include <stdlib.h>
@@ -93,4 +93,15 @@ fun maybe_destruct<T(Object)>(it:T*):void {
it->destruct()
}
// a function that allows the safe deletion of recursive and complicated data structures
fun safe_recursive_delete<T>(first: T*, addingFunc: fun(T*): set::set<T*>) {
var toDelete = set::set<T*>()
var next = set::set(first)
while (toDelete != next) {
toDelete = next
toDelete.for_each( fun(it: T*) next.add(addingFunc(it)); )
}
toDelete.for_each( fun(it: T*) delete(it); )
}

View File

@@ -2,6 +2,7 @@ import io
import vector
import string
import mem
import set
import util
import conversions
@@ -55,12 +56,14 @@ obj regex(Object) {
}
fun copy_construct(old:regex*):void {
begin = old->begin
regexString.copy_construct(&old->regexString)
//begin = old->begin
//regexString.copy_construct(&old->regexString)
construct(old->regexString)
}
fun destruct():void {
regexString.destruct()
//mem::safe_recursive_delete(begin, fun(it: regexState*): set::set<regexState*> { return set::set(it); } )
}
fun operator=(other: regex):void {

60
stdlib/set.krak Normal file
View File

@@ -0,0 +1,60 @@
import vector
import io
fun set<T>(): set<T> {
var toRet.construct() : set<T>
return toRet
}
fun set<T>(item: T): set<T> {
var toRet.construct() : set<T>
toRet.add(item)
return toRet
}
obj set<T> {
var data: vector::vector<T>
fun construct() {
data.construct()
}
fun copy_construct(old: set<T>*) {
data.copy_construct(&old->data)
}
fun operator=(rhs: set<T>) {
destruct()
copy_construct(&rhs)
}
fun operator==(rhs: set<T>): bool {
if (size() != rhs.size())
return false
return !data.any_true( fun(item: T): bool return !rhs.contains(item); )
}
fun destruct() {
data.destruct()
}
fun size():int {
return data.size
}
fun contains(item: T): bool {
return data.find(item) != -1
}
fun add(item: T) {
if (!contains(item))
data.add(item)
}
fun add(items: set<T>) {
items.for_each( fun(item: T) add(item); )
}
fun remove(item: T) {
var idx = data.find(item)
if (idx == -1) {
io::println("CANNOT FIND ITEM TO REMOVE")
return
}
data.remove(idx)
}
fun for_each(func: fun(T):void) {
data.do(func)
}
}

View File

@@ -34,6 +34,16 @@ obj string (Object) {
construct(*old)
}
fun operator=(str: char*): void {
destruct();
construct(str)
}
fun operator=(str: string): void {
destruct();
data.copy_construct(&str.data)
}
fun destruct():void {
data.destruct()
}
@@ -51,11 +61,6 @@ obj string (Object) {
}
fun length():int { return data.size; }
fun operator=(str: char*): void {
destruct();
construct(str)
}
fun operator+(str: char*): string {
var newStr.construct(str):string
var ret.construct(data + newStr.data):string

View File

@@ -133,7 +133,7 @@ obj vector<T> (Object) {
// on an object that we want to put in a vector. In this way we avoid the problem
// by not generating this function unless it's called - we also get the ability to
// do a find index using a different type, which could be fun.
fun find_index<U>(value: U): int {
fun find<U>(value: U): int {
for (var i = 0; i < size; i++;)
if (data[i] == value)
return i;
@@ -159,6 +159,16 @@ obj vector<T> (Object) {
resize(size*2);
maybe_copy_construct(&data[size-1], &dataIn);
}
fun remove(index: int) {
maybe_destruct(&data[index])
for (var i = index+1; i < size; i++;) {
maybe_copy_construct(&data[i-1], &data[i])
maybe_destruct(&data[i])
}
size--
}
fun do(func: fun(T):void):void {
for (var i = 0; i < size; i++;)
func(data[i])