Bugfixes, range(start,end,step), and beginning work on lexer and symbol
This commit is contained in:
45
stdlib/lexer.krak
Normal file
45
stdlib/lexer.krak
Normal file
@@ -0,0 +1,45 @@
|
||||
import regex
|
||||
import symbol
|
||||
import string
|
||||
import vector
|
||||
import util
|
||||
|
||||
obj lexer {
|
||||
var regs: vector::vector<regex::regex>
|
||||
var input: string::string
|
||||
var position: int
|
||||
fun construct(): lexer* {
|
||||
regs.construct()
|
||||
input.construct()
|
||||
position = 0
|
||||
return this
|
||||
}
|
||||
fun destruct() {
|
||||
regs.destruct()
|
||||
input.destruct()
|
||||
}
|
||||
fun copy_construct(old: lexer*) {
|
||||
regs.copy_construct(&old->regs)
|
||||
input.copy_construct(&old->input)
|
||||
position = old->position
|
||||
}
|
||||
fun operator=(old: lexer) {
|
||||
destruct()
|
||||
copy_construct(&old)
|
||||
}
|
||||
fun add_regex(newOne: regex::regex) {
|
||||
regs.add(newOne)
|
||||
}
|
||||
fun set_input(in: string::string) {
|
||||
input = in
|
||||
}
|
||||
fun next(): symbol::symbol {
|
||||
var max = regs.map(fun(reg: regex::regex): util::pair<int, string::string> {
|
||||
return util::make_pair(reg.long_match(input.slice(position, -1)), reg.regexString); })
|
||||
.max(fun(first: util::pair<int, string::string>, second: util::pair<int, string::string>): bool
|
||||
{ return first.first < second.first; })
|
||||
position += max.first
|
||||
return symbol::symbol(input.slice(position-max.first, position), max.second, true)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,9 +52,10 @@ fun delete<T>(toDelete: T*, itemCount: int): void {
|
||||
fun delete<T(Object)>(toDelete: T*, itemCount: int): void {
|
||||
// start at one because the actual delete will call the destructor of the first one as it
|
||||
// finishes the pointer
|
||||
for (var i: int = 1; i < itemCount; i++;)
|
||||
for (var i: int = 0; i < itemCount; i++;)
|
||||
toDelete[i].destruct();
|
||||
delete<T>(toDelete);
|
||||
free<T>(toDelete);
|
||||
//delete<T>(toDelete);
|
||||
}
|
||||
|
||||
/* We specilize on the trait Object to decide on whether or not the destructor should be called */
|
||||
|
||||
@@ -51,7 +51,7 @@ obj regex(Object) {
|
||||
// init our begin, and the end state as the next state of each end
|
||||
begin = beginningAndEnd.first
|
||||
var end = mem::new<regexState>()->construct(conversions::to_char(1))
|
||||
beginningAndEnd.second.do(fun(it: regexState*): void { it->next_states.add(end); })
|
||||
beginningAndEnd.second.for_each(fun(it: regexState*): void { it->next_states.add(end); })
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -111,11 +111,11 @@ obj regex(Object) {
|
||||
i = perenEnd-1
|
||||
|
||||
if (alternating) {
|
||||
previous_end.do(fun(it: regexState*):void { it->next_states.add_all(innerBeginEnd.first->next_states); } )
|
||||
previous_end.for_each(fun(it: regexState*):void { it->next_states.add_all(innerBeginEnd.first->next_states); } )
|
||||
current_begin.add_all(innerBeginEnd.first->next_states)
|
||||
current_end.add_all(innerBeginEnd.second)
|
||||
} else {
|
||||
current_end.do(fun(it: regexState*):void { it->next_states.add_all(innerBeginEnd.first->next_states); } )
|
||||
current_end.for_each(fun(it: regexState*):void { it->next_states.add_all(innerBeginEnd.first->next_states); } )
|
||||
previous_begin = current_begin
|
||||
previous_end = current_end
|
||||
current_begin = innerBeginEnd.first->next_states
|
||||
@@ -129,11 +129,11 @@ obj regex(Object) {
|
||||
} else {
|
||||
var next = mem::new<regexState>()->construct(regex_string[i])
|
||||
if (alternating) {
|
||||
previous_end.do(fun(it: regexState*):void { it->next_states.add(next); })
|
||||
previous_end.for_each(fun(it: regexState*):void { it->next_states.add(next); })
|
||||
current_begin.add(next)
|
||||
current_end.add(next)
|
||||
} else {
|
||||
current_end.do(fun(it: regexState*):void { it->next_states.add(next); })
|
||||
current_end.for_each(fun(it: regexState*):void { it->next_states.add(next); })
|
||||
previous_begin = current_begin
|
||||
previous_end = current_end
|
||||
current_begin = vector::vector(next)
|
||||
|
||||
@@ -14,7 +14,7 @@ fun set<T>(item: T): set<T> {
|
||||
|
||||
fun from_vector<T>(items: vector::vector<T>): set<T> {
|
||||
var toRet.construct() : set<T>
|
||||
items.do( fun(item: T) toRet.add(item); )
|
||||
items.for_each( fun(item: T) toRet.add(item); )
|
||||
return toRet
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ obj set<T> {
|
||||
data.remove(idx)
|
||||
}
|
||||
fun for_each(func: fun(T):void) {
|
||||
data.do(func)
|
||||
data.for_each(func)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import vector
|
||||
import util
|
||||
import mem
|
||||
|
||||
fun string(in:char*):string {
|
||||
@@ -25,6 +26,9 @@ obj string (Object) {
|
||||
data.copy_construct(&vec);
|
||||
return this;
|
||||
}
|
||||
fun construct(str: string): string* {
|
||||
return construct(str.data);
|
||||
}
|
||||
|
||||
fun copy_construct(old: string*): void {
|
||||
data.copy_construct(&old->data)
|
||||
@@ -61,17 +65,32 @@ obj string (Object) {
|
||||
}
|
||||
fun length():int { return data.size; }
|
||||
|
||||
fun operator==(other: string): bool {
|
||||
return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } )
|
||||
}
|
||||
|
||||
fun operator+(str: char*): string {
|
||||
var newStr.construct(str):string
|
||||
var ret.construct(data + newStr.data):string
|
||||
return ret
|
||||
}
|
||||
|
||||
fun operator+(str: string): string {
|
||||
var newStr.construct(str):string
|
||||
var ret.construct(data + newStr.data):string
|
||||
return ret
|
||||
}
|
||||
|
||||
fun operator+=(str: char*): void {
|
||||
var newStr.construct(str):string
|
||||
data += newStr.data
|
||||
}
|
||||
|
||||
fun operator+=(str: string): void {
|
||||
var newStr.construct(str):string
|
||||
data += newStr.data
|
||||
}
|
||||
|
||||
fun toCharArray(): char* {
|
||||
var out: char* = mem::new<char>(data.size+1);
|
||||
for (var i: int = 0; i < data.size; i++;)
|
||||
|
||||
54
stdlib/symbol.krak
Normal file
54
stdlib/symbol.krak
Normal file
@@ -0,0 +1,54 @@
|
||||
import string
|
||||
|
||||
fun symbol(dataIn: char*, nameIn: char*, terminalIn: bool): symbol {
|
||||
var toRet.construct(string::string(dataIn), string::string(nameIn), terminalIn): symbol
|
||||
return toRet
|
||||
}
|
||||
|
||||
fun symbol(dataIn: string::string, nameIn: string::string, terminalIn: bool): symbol {
|
||||
var toRet.construct(dataIn, nameIn, terminalIn): symbol
|
||||
return toRet
|
||||
}
|
||||
|
||||
obj symbol {
|
||||
var data: string::string
|
||||
var name: string::string
|
||||
var terminal: bool
|
||||
|
||||
fun construct(): symbol* {
|
||||
data.construct()
|
||||
name.construct()
|
||||
return this
|
||||
}
|
||||
fun construct(dataIn: string::string, nameIn: string::string, terminalIn: bool): symbol* {
|
||||
data.construct(dataIn)
|
||||
name.construct(nameIn)
|
||||
terminal = terminalIn
|
||||
return this
|
||||
}
|
||||
fun destruct() {
|
||||
data.destruct()
|
||||
name.destruct()
|
||||
}
|
||||
fun copy_construct(old: symbol*) {
|
||||
data.copy_construct(&old->data)
|
||||
name.copy_construct(&old->name)
|
||||
terminal = old->terminal
|
||||
}
|
||||
fun operator=(old: symbol) {
|
||||
destruct()
|
||||
copy_construct(&old)
|
||||
}
|
||||
fun operator==(other: symbol): bool {
|
||||
return data == other.data && name == other.name && terminal == other.terminal;
|
||||
}
|
||||
fun to_string(): string::string {
|
||||
var terminalString: char*
|
||||
if (terminal)
|
||||
terminalString = "true"
|
||||
else
|
||||
terminalString = "false"
|
||||
return name + ": " + data + " " + terminalString
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,3 +44,38 @@ obj pair<T,U>(Object) {
|
||||
}
|
||||
}
|
||||
|
||||
fun range(end:int): range {
|
||||
var toRet.construct(0, end, 1): range
|
||||
return toRet
|
||||
}
|
||||
|
||||
fun range(begin: int, end:int): range {
|
||||
var toRet.construct(begin, end, 1): range
|
||||
return toRet
|
||||
}
|
||||
fun range(begin: int, end:int, step: int): range {
|
||||
var toRet.construct(begin, end, step): range
|
||||
return toRet
|
||||
}
|
||||
|
||||
obj range {
|
||||
var begin: int
|
||||
var end: int
|
||||
var step: int
|
||||
fun construct(beginIn: int, endIn: int, stepIn: int) : range* {
|
||||
begin = beginIn
|
||||
end = endIn
|
||||
step = stepIn
|
||||
}
|
||||
fun for_each(func: fun(int):void):void {
|
||||
for (var i = begin; i < end; i+= step;)
|
||||
func(i)
|
||||
}
|
||||
fun any_true(func: fun(int):bool):bool {
|
||||
for (var i = begin; i < end; i+= step;)
|
||||
if (func(i))
|
||||
return true
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -169,14 +169,10 @@ obj vector<T> (Object) {
|
||||
size--
|
||||
}
|
||||
|
||||
fun do(func: fun(T):void):void {
|
||||
fun for_each(func: fun(T):void):void {
|
||||
for (var i = 0; i < size; i++;)
|
||||
func(data[i])
|
||||
}
|
||||
fun do<U>(func: fun(T,U):void, extraParam: U):void {
|
||||
for (var i = 0; i < size; i++;)
|
||||
func(data[i], extraParam)
|
||||
}
|
||||
fun in_place(func: fun(T):T):void {
|
||||
for (var i = 0; i < size; i++;)
|
||||
data[i] = func(data[i])
|
||||
@@ -196,15 +192,6 @@ obj vector<T> (Object) {
|
||||
}
|
||||
return newVec
|
||||
}
|
||||
fun flatten_map<U,V>(func: fun(T,V):vector<U>, extraParam:V):vector<U> {
|
||||
var newVec.construct(): vector<U>
|
||||
for (var i = 0; i < size; i++;) {
|
||||
var to_add = func(data[i], extraParam)
|
||||
for (var j = 0; j < to_add.size; j++;)
|
||||
newVec.addEnd(to_add.get(j))
|
||||
}
|
||||
return newVec
|
||||
}
|
||||
fun filter(func: fun(T):bool):vector<T> {
|
||||
var newVec.construct(): vector<T>
|
||||
for (var i = 0; i < size; i++;)
|
||||
@@ -212,18 +199,18 @@ obj vector<T> (Object) {
|
||||
newVec.addEnd(data[i])
|
||||
return newVec
|
||||
}
|
||||
fun filter<U>(func: fun(T,U):bool, extraParam: U):vector<T> {
|
||||
var newVec.construct(): vector<T>
|
||||
for (var i = 0; i < size; i++;)
|
||||
if (func(data[i], extraParam))
|
||||
newVec.addEnd(data[i])
|
||||
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
|
||||
}
|
||||
fun max(func: fun(T,T):bool): T {
|
||||
var maxIdx = 0
|
||||
for (var i = 1; i < size; i++;)
|
||||
if (func(data[maxIdx], data[i]))
|
||||
maxIdx = i
|
||||
return data[maxIdx]
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user