Some bugfixes, allow overloading of [] and add that to vector and string, work on regex. Need closures before that finishes....
This commit is contained in:
@@ -17,7 +17,16 @@ fun println<T>(toPrint: T) : void {
|
||||
fun print(toPrint: char*) : void {
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(toPrint = toPrint::) """
|
||||
printf(toPrint);
|
||||
printf("%s", toPrint);
|
||||
"""
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
fun print(toPrint: char) : void {
|
||||
__if_comp__ __C__ {
|
||||
simple_passthrough(toPrint = toPrint::) """
|
||||
printf("%c", toPrint);
|
||||
"""
|
||||
}
|
||||
return;
|
||||
|
||||
78
stdlib/regex.krak
Normal file
78
stdlib/regex.krak
Normal file
@@ -0,0 +1,78 @@
|
||||
import io
|
||||
import vector
|
||||
import string
|
||||
|
||||
fun regex(in: char*):regex {
|
||||
return regex(string::string(in))
|
||||
}
|
||||
fun regex(in: string::string):regex {
|
||||
var out.construct(in):regex
|
||||
return out
|
||||
}
|
||||
|
||||
obj regexState(Object) {
|
||||
var character: char
|
||||
var next_states: vector::vector<regexState*>
|
||||
fun construct(charIn:char): regexState* {
|
||||
character = charIn
|
||||
next_states.construct()
|
||||
return this
|
||||
}
|
||||
fun construct(): regexState* {
|
||||
return construct(0)
|
||||
}
|
||||
fun copy_construct(old:regexState*): void {
|
||||
character = regexState->character
|
||||
next_states.copy_construct(®exState->next_states)
|
||||
}
|
||||
fun destruct():void {
|
||||
next_states.destruct()
|
||||
}
|
||||
fun match(input: char): vector::vector<regexState*> {
|
||||
return next_states.filter(fun(it:regexState*):bool { return it->character == input; } )
|
||||
}
|
||||
}
|
||||
|
||||
obj regex(Object) {
|
||||
var regexString: string::string
|
||||
var begin: regexState
|
||||
fun construct(regexStringIn: string::string): regex* {
|
||||
regexState.construct()
|
||||
regexString.copy_construct(®exStringIn)
|
||||
|
||||
var traverse = &begin
|
||||
for (var i = 0; i < regexString.length(); i++;) {
|
||||
var next = new<regexState>()->construct(regexString[i])
|
||||
traverse->next_states->add(next)
|
||||
traverse = next
|
||||
}
|
||||
traverse->next_states->add(new<regexState>()->construct(1))
|
||||
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old:regex*):void {
|
||||
begin.copy_construct(&old->begin)
|
||||
regexString.copy_construct(&old->regexString)
|
||||
}
|
||||
fun destruct():void {
|
||||
begin.destruct()
|
||||
regexString.destruct()
|
||||
}
|
||||
fun long_match(to_match: char*): int { return long_match(string::string(to_match)); }
|
||||
fun long_match(to_match: string::string): int {
|
||||
var next = vector::vector(&begin)
|
||||
var longest = 0
|
||||
for (var i = 0; i < to_match.length(); i++;) {
|
||||
if (next.size == 0)
|
||||
return longest
|
||||
if (next.any_true(fun(state: regexState*):bool { return state->character == 1; }))
|
||||
longest = i
|
||||
next = next.flatten_map(fun(state: regexState*): vector::vector<regexState*> { return state->match(to_match[i]); })
|
||||
}
|
||||
if (next.any_true(fun(state: regexState*):bool { return state->character == 1; }))
|
||||
return to_match.length()
|
||||
return longest
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import vector;
|
||||
import mem;
|
||||
|
||||
fun string(in:char*):string {
|
||||
var out:string = in
|
||||
return out
|
||||
}
|
||||
|
||||
obj string (Object) {
|
||||
var data: vector::vector<char>;
|
||||
fun construct(): string* {
|
||||
@@ -33,6 +38,9 @@ obj string (Object) {
|
||||
data.destruct()
|
||||
}
|
||||
|
||||
fun operator[](index: int): char { return data[index]; }
|
||||
fun length():int { return data.size; }
|
||||
|
||||
fun operator=(str: char*): void {
|
||||
destruct();
|
||||
construct(str)
|
||||
|
||||
@@ -74,10 +74,8 @@ obj vector<T> (Object) {
|
||||
return true;
|
||||
}
|
||||
|
||||
fun at(index: int): T {
|
||||
return get(index);
|
||||
}
|
||||
|
||||
fun at(index: int): T { return get(index); }
|
||||
fun operator[](index: int): T { return get(index); }
|
||||
fun get(index: int): T {
|
||||
if (index < 0 || index >= size) {
|
||||
println("Vector access out of bounds! Retuning 0th element as sanest option");
|
||||
@@ -97,6 +95,7 @@ obj vector<T> (Object) {
|
||||
return;
|
||||
data[index] = dataIn;
|
||||
}
|
||||
fun add(dataIn: T): void { addEnd(dataIn); }
|
||||
fun addEnd(dataIn: T): void {
|
||||
size++;
|
||||
if (size >= available)
|
||||
@@ -117,5 +116,20 @@ obj vector<T> (Object) {
|
||||
newVec.addEnd(func(data[i]))
|
||||
return newVec
|
||||
}
|
||||
fun flatten_map<U>(func: fun(T):vector<U>):vector<U> {
|
||||
var newVec.construct(): vector<U>
|
||||
for (var i = 0; i < size; i++;) {
|
||||
var to_add = func(data[i])
|
||||
for (var j = 0; j < to_add.size; j++;)
|
||||
newVec.addEnd(to_add.get(j))
|
||||
}
|
||||
return newVec
|
||||
}
|
||||
fun any_true(func: fun(T):bool):bool {
|
||||
for (var i = 0; i < size; i++;)
|
||||
if (func(data[i]))
|
||||
return true
|
||||
return false
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user