Added mem::safe_recursive_clone, and while it works for regex, it's actually slower then remaking it. Hmmmm, maybe because some of the stdlib is inefficent

This commit is contained in:
Nathan Braswell
2015-07-07 00:46:00 -04:00
parent 92f5c63c9a
commit f3cdea068e
9 changed files with 68 additions and 26 deletions

View File

@@ -692,21 +692,23 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
return newNode; return newNode;
} }
auto boolExp = getNode("boolean_expression", children);
NodeTree<ASTData>* toAssign = boolExp ? transform(boolExp, scope, types, limitToFunction, templateTypeReplacements) : nullptr;
// for type inferencing
if (!identifierType) {
if (toAssign)
identifierType = toAssign->getDataRef()->valueType;
else
throw "have to inference but no expression";
}
NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType)); NodeTree<ASTData>* newIdentifier = new NodeTree<ASTData>("identifier", ASTData(identifier, Symbol(newIdentifierStr, true), identifierType));
addToScope(newIdentifierStr, newIdentifier, scope); addToScope(newIdentifierStr, newIdentifier, scope);
addToScope("~enclosing_scope", scope, newNode); addToScope("~enclosing_scope", scope, newNode);
addToScope("~enclosing_scope", newNode, newIdentifier); addToScope("~enclosing_scope", newNode, newIdentifier);
auto boolExp = getNode("boolean_expression", children);
NodeTree<ASTData>* toAssign = boolExp ? transform(boolExp, scope, types, limitToFunction, templateTypeReplacements) : nullptr;
// for type inferencing
if (!identifierType) {
if (toAssign) {
identifierType = toAssign->getDataRef()->valueType;
newIdentifier->getDataRef()->valueType = identifierType;
} else
throw "have to inference but no expression";
}
newNode->addChild(newIdentifier); newNode->addChild(newIdentifier);
if (toAssign) if (toAssign)
newNode->addChild(toAssign); newNode->addChild(toAssign);
@@ -791,7 +793,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
throw "Ambigious parse!"; throw "Ambigious parse!";
} else { } else {
// Should get rid of this eventually. Right now it handles cases like sign, alpha, a comma, etc // Should get rid of this eventually. Right now it handles cases like sign, alpha, a comma, etc
std::cout << "Unhandled syntax node: " << name << std::endl; //std::cout << "Unhandled syntax node: " << name << std::endl;
return new NodeTree<ASTData>(); return new NodeTree<ASTData>();
} }

View File

@@ -566,7 +566,11 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
output.value += toAssign.postValue; output.value += toAssign.postValue;
return output; return output;
} else { } else {
return ValueTypeToCType(children[0]->getData().valueType, generate(children[0], enclosingObject, justFuncName, enclosingFunction).oneString()) + " = " + generate(children[1], enclosingObject, true, enclosingFunction) + ";"; // we might use this address in the right hand side (recursive closures), so split it up
std::string assignTo = generate(children[0], enclosingObject, justFuncName, enclosingFunction).oneString();
output.preValue = ValueTypeToCType(children[0]->getData().valueType, assignTo) + ";\n";
output += assignTo + " = " + generate(children[1], enclosingObject, true, enclosingFunction) + ";";
return output;
} }
} }
case if_comp: case if_comp:

View File

@@ -43,6 +43,9 @@ obj map<T,U> (Object) {
keys.add(key) keys.add(key)
values.add(value) values.add(value)
} }
fun contains_key(key: T): bool {
return keys.contains(key)
}
fun get(key: T): U { fun get(key: T): U {
return values.get(keys.find(key)) return values.get(keys.find(key))
} }

View File

@@ -1,4 +1,5 @@
import set import set
import map
__if_comp__ __C__ simple_passthrough """ __if_comp__ __C__ simple_passthrough """
#include <stdlib.h> #include <stdlib.h>
@@ -105,4 +106,18 @@ fun safe_recursive_delete<T>(first: *T, addingFunc: fun(*T): set::set<*T>) {
toDelete.for_each( fun(it: *T) delete(it); ) toDelete.for_each( fun(it: *T) delete(it); )
} }
// a function that allows the safe cloning of recursive and complicated data structures
// cloneing func is the func that does the cloning, it takes in a recursive clone function and
// a register clone function
fun safe_recursive_clone<T>(first: *T, cloningFunc: fun(*T, fun(*T):*T, fun(*T):void): void): *T {
var rec_map = map::map<*T,*T>()
// can't do type infrence if you need the type inside the expression...
var rec_it: fun(*T):*T = fun(it: *T): *T {
if (!rec_map.contains_key(it))
cloningFunc(it, rec_it, fun(cloned: *T) { rec_map[it] = cloned; })
return rec_map[it]
}
return rec_it(first)
}

View File

@@ -56,14 +56,21 @@ obj regex (Object) {
} }
fun copy_construct(old:*regex):void { fun copy_construct(old:*regex):void {
begin = old->begin construct(old->regexString)
regexString.copy_construct(&old->regexString) /*begin = old->begin*/
/*construct(old->regexString)*/ /*regexString.copy_construct(&old->regexString)*/
/*begin = mem::safe_recursive_clone(old->begin, fun(it: *regexState, cloner: fun(*regexState):*regexState, register: fun(*regexState):void): void {*/
/*var newOne = mem::new<regexState>()->construct(it->character)*/
/*register(newOne)*/
/*it->next_states.for_each(fun(next_state: *regexState) {*/
/*newOne->next_states.add(cloner(next_state))*/
/*})*/
/*})*/
} }
fun destruct():void { fun destruct():void {
regexString.destruct() regexString.destruct()
/*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 set::from_vector(it->next_states); } )
} }
fun operator==(other: regex):bool { fun operator==(other: regex):bool {
@@ -72,7 +79,7 @@ obj regex (Object) {
fun operator=(other: regex):void { fun operator=(other: regex):void {
destruct() destruct()
construct(other.regexString) copy_construct(&other)
} }
fun compile(regex_string: string::string): util::pair<*regexState, vector::vector<*regexState>> { fun compile(regex_string: string::string): util::pair<*regexState, vector::vector<*regexState>> {

View File

@@ -25,13 +25,6 @@ obj vector<T> (Object) {
return this; return this;
} }
fun construct(newSize: int): *vector<T>{
size = newSize;
available = newSize;
data = new<T>(newSize);
return this;
}
fun copy_construct(old: *vector<T>): void { fun copy_construct(old: *vector<T>): void {
construct() construct()

View File

@@ -6,14 +6,15 @@ import util:*
fun main():int { fun main():int {
var a = load_grammer(read_file(string("../krakenGrammer.kgm"))) var a = load_grammer(read_file(string("../krakenGrammer.kgm")))
/*var a = load_grammer(string("grammer.kgm"))*/ /*var a = load_grammer(read_file(string("grammer.kgm")))*/
println(a.to_string()) println(a.to_string())
var lex = lexer(a.regexs) var lex = lexer(a.regexs)
lex.set_input(read_file(string("test_grammer.krak"))) lex.set_input(read_file(string("test_grammer.krak")))
/*lex.set_input(string("ccdahas spacedhas*/ /*lex.set_input(string("ccdahas spacedhas*/
/*returndaaaaaaaaaaaaaa"))*/ /*returndaaaaaaaaaaaaaa"))*/
println("woo lexing:") println("woo lexing:")
range(80).for_each(fun(i: int) { println(lex.next().to_string()); } ) range(8).for_each(fun(i: int) { println(lex.next().to_string()); } )
/*range(80).for_each(fun(i: int) { println(lex.next().to_string()); } )*/
return 0 return 0
} }

View File

@@ -0,0 +1,5 @@
thingy: 4
thingy: 3
thingy: 2
thingy: 1
thingy: 0

View File

@@ -0,0 +1,12 @@
import io:*
fun main():int {
var message = "thingy: "
var func: fun(int):void = fun(it: int) {
print(message); println(it)
if (it > 0)
func(it-1)
}
func(4)
}