Optimization of string and vector with references and less functional code, bugfix of closing over references
This commit is contained in:
@@ -289,6 +289,7 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
{
|
{
|
||||||
std::string preName = "";
|
std::string preName = "";
|
||||||
std::string postName = "";
|
std::string postName = "";
|
||||||
|
bool closed = false;
|
||||||
// check for this being a closed over variable
|
// check for this being a closed over variable
|
||||||
// first, get declaring function, if it exists
|
// first, get declaring function, if it exists
|
||||||
if (enclosingFunction) {
|
if (enclosingFunction) {
|
||||||
@@ -297,6 +298,7 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
if (enclosingFunction->getDataRef()->closedVariables.find(from) != enclosingFunction->getDataRef()->closedVariables.end()) {
|
if (enclosingFunction->getDataRef()->closedVariables.find(from) != enclosingFunction->getDataRef()->closedVariables.end()) {
|
||||||
preName += "(*closed_variables->";
|
preName += "(*closed_variables->";
|
||||||
postName += ")";
|
postName += ")";
|
||||||
|
closed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -311,8 +313,8 @@ CCodeTriple CGenerator::generate(NodeTree<ASTData>* from, NodeTree<ASTData>* enc
|
|||||||
//If we're in an object method, and our enclosing scope is that object, we're a member of the object and should use the this reference.
|
//If we're in an object method, and our enclosing scope is that object, we're a member of the object and should use the this reference.
|
||||||
if (enclosingObject && enclosingObject->getDataRef()->scope.find(data.symbol.getName()) != enclosingObject->getDataRef()->scope.end())
|
if (enclosingObject && enclosingObject->getDataRef()->scope.find(data.symbol.getName()) != enclosingObject->getDataRef()->scope.end())
|
||||||
preName += "this->";
|
preName += "this->";
|
||||||
// dereference references, but only if inside a function
|
// dereference references, but only if inside a function and not if this is a closed over variable
|
||||||
if (enclosingFunction && data.valueType->is_reference) {
|
if (enclosingFunction && data.valueType->is_reference && !closed) {
|
||||||
preName += "(*";
|
preName += "(*";
|
||||||
postName += ")";
|
postName += ")";
|
||||||
}
|
}
|
||||||
@@ -924,7 +926,7 @@ std::string CGenerator::closureStructType(std::set<NodeTree<ASTData>*> closedVar
|
|||||||
//auto tmp = var->getDataRef()->valueType->withIncreasedIndirection();
|
//auto tmp = var->getDataRef()->valueType->withIncreasedIndirection();
|
||||||
std::string varName = var->getDataRef()->symbol.getName();
|
std::string varName = var->getDataRef()->symbol.getName();
|
||||||
varName = (varName == "this") ? varName : scopePrefix(var) + varName;
|
varName = (varName == "this") ? varName : scopePrefix(var) + varName;
|
||||||
typedefString += ValueTypeToCType(var->getDataRef()->valueType, "*"+varName) + ";";
|
typedefString += ValueTypeToCType(var->getDataRef()->valueType->withoutReference(), "*"+varName) + ";";
|
||||||
}
|
}
|
||||||
std::string structName = "closureStructType" + getID();
|
std::string structName = "closureStructType" + getID();
|
||||||
typedefString += " } " + structName + ";\n";
|
typedefString += " } " + structName + ";\n";
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import util
|
|||||||
import mem
|
import mem
|
||||||
|
|
||||||
fun string(in:*char):string {
|
fun string(in:*char):string {
|
||||||
var out:string = in
|
var out.construct(in):string
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,11 +22,11 @@ obj string (Object) {
|
|||||||
// no null terminator
|
// no null terminator
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
fun construct(vec: vector::vector<char>): *string {
|
fun construct(vec: ref vector::vector<char>): *string {
|
||||||
data.copy_construct(&vec);
|
data.copy_construct(&vec);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
fun construct(str: string): *string {
|
fun construct(str: ref string): *string {
|
||||||
return construct(str.data);
|
return construct(str.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ obj string (Object) {
|
|||||||
construct(str)
|
construct(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun operator=(str: string): void {
|
fun operator=(str: ref string): void {
|
||||||
destruct();
|
destruct();
|
||||||
data.copy_construct(&str.data)
|
data.copy_construct(&str.data)
|
||||||
}
|
}
|
||||||
@@ -62,8 +62,15 @@ obj string (Object) {
|
|||||||
}
|
}
|
||||||
fun length():int { return data.size; }
|
fun length():int { return data.size; }
|
||||||
|
|
||||||
fun operator==(other: string): bool {
|
fun operator==(other: ref string): bool {
|
||||||
return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } )
|
// you were too good for this world
|
||||||
|
//return length() == other.length() && !util::range(length()).any_true(fun(i: int):bool { return data[i] != other[i]; } )
|
||||||
|
if (data.size != other.data.size)
|
||||||
|
return false
|
||||||
|
for (var i = 0; i < data.size; i++;)
|
||||||
|
if (data.data[i] != other.data.data[i])
|
||||||
|
return false
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
fun operator==(other: *char): bool {
|
fun operator==(other: *char): bool {
|
||||||
var str.construct(other) : string
|
var str.construct(other) : string
|
||||||
@@ -76,7 +83,7 @@ obj string (Object) {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
fun operator+(str: string): string {
|
fun operator+(str: ref string): string {
|
||||||
var newStr.construct(str):string
|
var newStr.construct(str):string
|
||||||
var ret.construct(data + newStr.data):string
|
var ret.construct(data + newStr.data):string
|
||||||
return ret
|
return ret
|
||||||
@@ -91,9 +98,10 @@ obj string (Object) {
|
|||||||
data += newStr.data
|
data += newStr.data
|
||||||
}
|
}
|
||||||
|
|
||||||
fun operator+=(str: string): void {
|
fun operator+=(str: ref string): void {
|
||||||
var newStr.construct(str):string
|
//var newStr.construct(str):string
|
||||||
data += newStr.data
|
//data += newStr.data
|
||||||
|
data += str.data
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toCharArray(): *char {
|
fun toCharArray(): *char {
|
||||||
|
|||||||
@@ -54,14 +54,14 @@ obj symbol (Object) {
|
|||||||
name.copy_construct(&old->name)
|
name.copy_construct(&old->name)
|
||||||
terminal = old->terminal
|
terminal = old->terminal
|
||||||
}
|
}
|
||||||
fun operator=(old: symbol) {
|
fun operator=(old: ref symbol) {
|
||||||
destruct()
|
destruct()
|
||||||
copy_construct(&old)
|
copy_construct(&old)
|
||||||
}
|
}
|
||||||
fun operator==(other: symbol): bool {
|
fun operator==(other: ref symbol): bool {
|
||||||
return data == other.data && name == other.name && terminal == other.terminal;
|
return data == other.data && name == other.name && terminal == other.terminal;
|
||||||
}
|
}
|
||||||
fun operator!=(other: symbol): bool {
|
fun operator!=(other: ref symbol): bool {
|
||||||
return !(*this == other);
|
return !(*this == other);
|
||||||
}
|
}
|
||||||
fun to_string(): string::string {
|
fun to_string(): string::string {
|
||||||
|
|||||||
@@ -24,11 +24,19 @@ obj vector<T> (Object) {
|
|||||||
data = new<T>(8);
|
data = new<T>(8);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
fun construct(ammt: int): *vector<T> {
|
||||||
|
size = 0;
|
||||||
|
available = ammt;
|
||||||
|
data = new<T>(ammt);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
fun copy_construct(old: *vector<T>): void {
|
fun copy_construct(old: *vector<T>): void {
|
||||||
construct()
|
construct(old->size)
|
||||||
|
size = old->size
|
||||||
for (var i = 0; i < old->size; i++;)
|
for (var i = 0; i < old->size; i++;)
|
||||||
addEnd(old->get(i))
|
maybe_copy_construct(&data[i], &old->data[i]);
|
||||||
|
//addEnd(old->get(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun destruct(): void {
|
fun destruct(): void {
|
||||||
|
|||||||
2
tests/test_close_over_references.expected_results
Normal file
2
tests/test_close_over_references.expected_results
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
9
|
||||||
|
9
|
||||||
17
tests/test_close_over_references.krak
Normal file
17
tests/test_close_over_references.krak
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import io:*
|
||||||
|
|
||||||
|
fun func(it: ref int) {
|
||||||
|
it++
|
||||||
|
fun() {
|
||||||
|
it++
|
||||||
|
println(it)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main(): int {
|
||||||
|
var it = 7
|
||||||
|
func(it)
|
||||||
|
println(it)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import symbol:*
|
|||||||
|
|
||||||
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(read_file(string("grammer.kgm")))*/
|
/*var a = load_grammer(read_file(string("grammer.kgm")))*/
|
||||||
var a = load_grammer(read_file(string("grammer2.kgm")))
|
var a = load_grammer(read_file(string("grammer2.kgm")))
|
||||||
println(a.to_string())
|
println(a.to_string())
|
||||||
@@ -42,9 +42,9 @@ fun main():int {
|
|||||||
/*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"))*/
|
||||||
lex.set_input(string("hibyed"))
|
//lex.set_input(string("hibyed"))
|
||||||
println("woo lexing:")
|
println("woo lexing:")
|
||||||
range(8).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()); } )*/
|
/*range(80).for_each(fun(i: int) { println(lex.next().to_string()); } )*/
|
||||||
println(a.to_string())
|
println(a.to_string())
|
||||||
a.calculate_state_automaton()
|
a.calculate_state_automaton()
|
||||||
|
|||||||
@@ -66,38 +66,26 @@ Copied: 303 to 304
|
|||||||
Destroyed: 303
|
Destroyed: 303
|
||||||
Destroyed: 302
|
Destroyed: 302
|
||||||
Copied: 104 to 105
|
Copied: 104 to 105
|
||||||
Copied: 105 to 106
|
|
||||||
Destroyed: 105
|
|
||||||
Copied: 204 to 205
|
Copied: 204 to 205
|
||||||
Copied: 205 to 206
|
|
||||||
Destroyed: 205
|
|
||||||
Copied: 304 to 305
|
Copied: 304 to 305
|
||||||
Copied: 305 to 306
|
|
||||||
Destroyed: 305
|
|
||||||
Destroyed: 104
|
Destroyed: 104
|
||||||
Destroyed: 204
|
Destroyed: 204
|
||||||
Destroyed: 304
|
Destroyed: 304
|
||||||
Destroyed: 301
|
Destroyed: 301
|
||||||
Destroyed: 201
|
Destroyed: 201
|
||||||
Destroyed: 101
|
Destroyed: 101
|
||||||
Copied: 106 to 107
|
Copied: 105 to 106
|
||||||
Copied: 107 to 108
|
Copied: 205 to 206
|
||||||
Destroyed: 107
|
Copied: 305 to 306
|
||||||
Copied: 206 to 207
|
Destroyed: 105
|
||||||
Copied: 207 to 208
|
Destroyed: 205
|
||||||
Destroyed: 207
|
Destroyed: 305
|
||||||
Copied: 306 to 307
|
|
||||||
Copied: 307 to 308
|
|
||||||
Destroyed: 307
|
|
||||||
Destroyed: 106
|
|
||||||
Destroyed: 206
|
Destroyed: 206
|
||||||
|
Copied: 306 to 307
|
||||||
Destroyed: 306
|
Destroyed: 306
|
||||||
Destroyed: 208
|
|
||||||
Copied: 308 to 309
|
|
||||||
Destroyed: 308
|
|
||||||
done
|
done
|
||||||
Destroyed: 108
|
Destroyed: 106
|
||||||
Destroyed: 309
|
Destroyed: 307
|
||||||
Destroyed: 300
|
Destroyed: 300
|
||||||
Destroyed: 200
|
Destroyed: 200
|
||||||
Destroyed: 100
|
Destroyed: 100
|
||||||
|
|||||||
Reference in New Issue
Block a user