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:
@@ -692,21 +692,23 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
||||
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));
|
||||
addToScope(newIdentifierStr, newIdentifier, scope);
|
||||
addToScope("~enclosing_scope", scope, newNode);
|
||||
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);
|
||||
if (toAssign)
|
||||
newNode->addChild(toAssign);
|
||||
@@ -791,7 +793,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
||||
throw "Ambigious parse!";
|
||||
} else {
|
||||
// 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>();
|
||||
}
|
||||
|
||||
|
||||
@@ -566,7 +566,11 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
||||
output.value += toAssign.postValue;
|
||||
return output;
|
||||
} 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:
|
||||
|
||||
@@ -43,6 +43,9 @@ obj map<T,U> (Object) {
|
||||
keys.add(key)
|
||||
values.add(value)
|
||||
}
|
||||
fun contains_key(key: T): bool {
|
||||
return keys.contains(key)
|
||||
}
|
||||
fun get(key: T): U {
|
||||
return values.get(keys.find(key))
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import set
|
||||
import map
|
||||
|
||||
__if_comp__ __C__ simple_passthrough """
|
||||
#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); )
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,14 +56,21 @@ obj regex (Object) {
|
||||
}
|
||||
|
||||
fun copy_construct(old:*regex):void {
|
||||
begin = old->begin
|
||||
regexString.copy_construct(&old->regexString)
|
||||
/*construct(old->regexString)*/
|
||||
construct(old->regexString)
|
||||
/*begin = old->begin*/
|
||||
/*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 {
|
||||
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 {
|
||||
@@ -72,7 +79,7 @@ obj regex (Object) {
|
||||
|
||||
fun operator=(other: regex):void {
|
||||
destruct()
|
||||
construct(other.regexString)
|
||||
copy_construct(&other)
|
||||
}
|
||||
|
||||
fun compile(regex_string: string::string): util::pair<*regexState, vector::vector<*regexState>> {
|
||||
|
||||
@@ -25,13 +25,6 @@ obj vector<T> (Object) {
|
||||
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 {
|
||||
construct()
|
||||
|
||||
@@ -6,14 +6,15 @@ import util:*
|
||||
|
||||
fun main():int {
|
||||
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())
|
||||
var lex = lexer(a.regexs)
|
||||
lex.set_input(read_file(string("test_grammer.krak")))
|
||||
/*lex.set_input(string("ccdahas spacedhas*/
|
||||
/*returndaaaaaaaaaaaaaa"))*/
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
5
tests/test_rec_closure.expected_results
Normal file
5
tests/test_rec_closure.expected_results
Normal file
@@ -0,0 +1,5 @@
|
||||
thingy: 4
|
||||
thingy: 3
|
||||
thingy: 2
|
||||
thingy: 1
|
||||
thingy: 0
|
||||
12
tests/test_rec_closure.krak
Normal file
12
tests/test_rec_closure.krak
Normal 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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user