More ast_transformation work, but the big change was fixing regex so that it didn't have an exponential implmentation by using sets instead of vectors to deduplicate
This commit is contained in:
@@ -583,19 +583,23 @@ fun ast_if_comp_ptr(): *ast_node {
|
||||
return ptr
|
||||
}
|
||||
obj if_comp (Object) {
|
||||
var wanted_generator: string
|
||||
fun construct(): *if_comp {
|
||||
wanted_generator.construct()
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *if_comp) {
|
||||
wanted_generator.copy_construct(&old->wanted_generator)
|
||||
}
|
||||
fun destruct() {
|
||||
wanted_generator.destruct()
|
||||
}
|
||||
fun operator=(other: ref if_comp) {
|
||||
destruct()
|
||||
copy_construct(&other)
|
||||
}
|
||||
fun operator==(other: ref if_comp): bool {
|
||||
return true
|
||||
return wanted_generator == other.wanted_generator
|
||||
}
|
||||
}
|
||||
fun ast_simple_passthrough_ptr(): *ast_node {
|
||||
@@ -725,7 +729,7 @@ fun get_ast_name(node: *ast_node): string {
|
||||
ast_node::defer_statement(backing) return string("defer_statement")
|
||||
ast_node::assignment_statement(backing) return string("assignment_statement")
|
||||
ast_node::declaration_statement(backing) return string("declaration_statement")
|
||||
ast_node::if_comp(backing) return string("if_comp")
|
||||
ast_node::if_comp(backing) return string("if_comp: ") + backing.wanted_generator
|
||||
ast_node::simple_passthrough(backing) return string("simple_passthrough")
|
||||
ast_node::function_call(backing) return string("function_call")
|
||||
ast_node::value(backing) return string("value")
|
||||
|
||||
@@ -100,6 +100,7 @@ obj ast_transformation (Object) {
|
||||
}
|
||||
fun transform_if_comp(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
||||
var new_if_comp = ast_if_comp_ptr()
|
||||
new_if_comp->if_comp.wanted_generator = concat_symbol_tree(get_node("identifier", node))
|
||||
return new_if_comp
|
||||
}
|
||||
fun transform_simple_passthrough(node: *tree<symbol>, scope: *ast_node): *ast_node {
|
||||
|
||||
@@ -17,7 +17,7 @@ fun regex(in: string::string):regex {
|
||||
|
||||
obj regexState (Object) {
|
||||
var character: char
|
||||
var next_states: vector::vector<*regexState>
|
||||
var next_states: set::set<*regexState>
|
||||
fun construct(charIn:char): *regexState {
|
||||
character = charIn
|
||||
next_states.construct()
|
||||
@@ -33,7 +33,7 @@ obj regexState (Object) {
|
||||
fun destruct():void {
|
||||
next_states.destruct()
|
||||
}
|
||||
fun match_char(input: char): vector::vector<*regexState> {
|
||||
fun match_char(input: char): set::set<*regexState> {
|
||||
return next_states.filter(fun(it:*regexState):bool { return it->character == input; })
|
||||
}
|
||||
fun is_end():bool {
|
||||
@@ -82,7 +82,7 @@ obj regex (Object, Serializable) {
|
||||
regexString.destruct()
|
||||
*referenceCounter -= 1
|
||||
if (*referenceCounter == 0) {
|
||||
mem::safe_recursive_delete(begin, fun(it: *regexState): set::set<*regexState> { return set::from_vector(it->next_states); } )
|
||||
mem::safe_recursive_delete(begin, fun(it: *regexState): set::set<*regexState> { return it->next_states; } )
|
||||
mem::delete(referenceCounter)
|
||||
}
|
||||
}
|
||||
@@ -105,25 +105,23 @@ obj regex (Object, Serializable) {
|
||||
copy_construct(&other)
|
||||
}
|
||||
|
||||
fun compile(regex_string: string::string): util::pair<*regexState, vector::vector<*regexState>> {
|
||||
fun compile(regex_string: string::string): util::pair<*regexState, set::set<*regexState>> {
|
||||
var first = mem::new<regexState>()->construct()
|
||||
var previous_begin = vector::vector<*regexState>()
|
||||
var previous_end = vector::vector<*regexState>()
|
||||
var current_begin = vector::vector(first)
|
||||
var current_end = vector::vector(first)
|
||||
var previous_begin = set::set<*regexState>()
|
||||
var previous_end = set::set<*regexState>()
|
||||
var current_begin = set::set(first)
|
||||
var current_end = set::set(first)
|
||||
var alternating = false
|
||||
var escapeing = false
|
||||
|
||||
for (var i = 0; i < regex_string.length(); i++;) {
|
||||
if (regex_string[i] == '*' && !escapeing) {
|
||||
for (var j = 0; j < current_end.size; j++;)
|
||||
current_end[j]->next_states.add_all(current_begin)
|
||||
current_end.for_each(fun(item: *regexState) item->next_states.add_all(current_begin);)
|
||||
current_begin.add_all(previous_begin)
|
||||
current_end.add_all(previous_end)
|
||||
|
||||
} else if (regex_string[i] == '+' && !escapeing) {
|
||||
for (var j = 0; j < current_end.size; j++;)
|
||||
current_end[j]->next_states.add_all(current_begin)
|
||||
current_end.for_each(fun(item: *regexState) item->next_states.add_all(current_begin);)
|
||||
|
||||
} else if (regex_string[i] == '?' && !escapeing) {
|
||||
current_begin.add_all(previous_begin)
|
||||
@@ -170,8 +168,8 @@ obj regex (Object, Serializable) {
|
||||
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)
|
||||
current_end = vector::vector(next)
|
||||
current_begin = set::set(next)
|
||||
current_end = set::set(next)
|
||||
}
|
||||
escapeing = false
|
||||
alternating = false
|
||||
@@ -183,15 +181,15 @@ obj regex (Object, Serializable) {
|
||||
|
||||
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 next = set::set(begin)
|
||||
var longest = -1
|
||||
for (var i = 0; i < to_match.length(); i++;) {
|
||||
if (next.size == 0)
|
||||
if (next.size() == 0)
|
||||
return longest
|
||||
if (next.any_true(fun(state: *regexState):bool { return state->is_end(); }))
|
||||
longest = i
|
||||
//next = next.flatten_map<*regexState>(fun(state: *regexState): vector::vector<*regexState> { return state->match_char(to_match[i]); })
|
||||
next = next.flatten_map(fun(state: *regexState): vector::vector<*regexState> { return state->match_char(to_match[i]); })
|
||||
next = next.flatten_map(fun(state: *regexState): set::set<*regexState> { return state->match_char(to_match[i]); })
|
||||
}
|
||||
if (next.any_true(fun(state: *regexState):bool { return state->is_end(); }))
|
||||
return to_match.length()
|
||||
|
||||
@@ -26,6 +26,10 @@ obj set<T> (Object, Serializable) {
|
||||
data.construct()
|
||||
return this
|
||||
}
|
||||
fun construct(ammt: int): *set<T> {
|
||||
data.construct(ammt)
|
||||
return this
|
||||
}
|
||||
fun copy_construct(old: *set<T>) {
|
||||
data.copy_construct(&old->data)
|
||||
}
|
||||
@@ -71,6 +75,9 @@ obj set<T> (Object, Serializable) {
|
||||
if (!contains(item))
|
||||
data.add(item)
|
||||
}
|
||||
fun add_all(items: ref set<T>) {
|
||||
add(items)
|
||||
}
|
||||
fun add(items: ref set<T>) {
|
||||
items.for_each( fun(item: ref T) add(item); )
|
||||
}
|
||||
@@ -94,5 +101,16 @@ obj set<T> (Object, Serializable) {
|
||||
fun reduce<U>(func: fun(T,U): U, initial: U): U {
|
||||
return data.reduce(func, initial)
|
||||
}
|
||||
fun flatten_map<U>(func: fun(T):set<U>):set<U> {
|
||||
var newSet.construct(size()): set<U>
|
||||
for (var i = 0; i < size(); i++;)
|
||||
func(data[i]).for_each(fun(item: ref U) newSet.add(item);)
|
||||
return newSet
|
||||
}
|
||||
fun filter(func: fun(T):bool):set<T> {
|
||||
var newSet.construct(): set<T>
|
||||
newSet.data = data.filter(func)
|
||||
return newSet
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
__if_comp__ __C__ simple_passthrough """
|
||||
#include <stdio.h>
|
||||
"""
|
||||
|
||||
var a = 1
|
||||
var b = 2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user