From 69048ebc31dd881d01a6535f90643f2d00679574 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Sun, 7 Jun 2015 19:54:08 -0400 Subject: [PATCH] Allow copy_constructers to be called with other types (i.e. var a:string = "hi" calls fun copy_construct(it:char**):void --- src/CGenerator.cpp | 10 +++++++--- stdlib/string.krak | 4 ++++ tests/test_string.expected_results | 2 ++ tests/test_string.krak | 6 +++--- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index dbbd88a..359788b 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -476,13 +476,17 @@ CCodeTriple CGenerator::generate(NodeTree* from, NodeTree* enc // be sure to end value by passing oneString true return ValueTypeToCType(children[0]->getData().valueType, generate(children[0], enclosingObject, justFuncName).oneString()) + "; " + generate(children[1], enclosingObject, true).oneString(true) + "/*Init Position Call*/"; } else { - // copy constructor if of the same type - if (*children[0]->getDataRef()->valueType == *children[1]->getDataRef()->valueType && methodExists(children[1]->getDataRef()->valueType, "copy_construct", std::vector{children[1]->getDataRef()->valueType->withIncreasedIndirection()})) { + // copy constructor if exists (even for non same types) + if (methodExists(children[0]->getDataRef()->valueType, "copy_construct", std::vector{children[1]->getDataRef()->valueType->withIncreasedIndirection()})) { CCodeTriple toAssign = generate(children[1], enclosingObject, true); std::string assignedTo = generate(children[0], enclosingObject, justFuncName).oneString(); output.value = toAssign.preValue; output.value += ValueTypeToCType(children[0]->getData().valueType, assignedTo) + ";\n"; - output.value += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&" + assignedTo + ", &" + toAssign.value, std::vector{children[0]->getDataRef()->valueType->withIncreasedIndirection()}) + ";\n" + output.postValue; + // we put the thing about to be copy constructed in a variable so we can for sure take its address + std::string toAssignTemp = "copy_construct_param" + getID(); + output.value += ValueTypeToCType(children[1]->getData().valueType, toAssignTemp) + " = " + toAssign.value + ";\n"; + + output.value += generateMethodIfExists(children[0]->getDataRef()->valueType, "copy_construct", "&" + assignedTo + ", &" + toAssignTemp, std::vector{children[1]->getDataRef()->valueType->withIncreasedIndirection()}) + ";\n" + output.postValue; output.value += toAssign.postValue; return output; } else { diff --git a/stdlib/string.krak b/stdlib/string.krak index 77657c4..1fae5a1 100644 --- a/stdlib/string.krak +++ b/stdlib/string.krak @@ -25,6 +25,10 @@ obj string (Object) { data.copy_construct(&old->data) } + fun copy_construct(old: char**): void { + construct(*old) + } + fun destruct():void { data.destruct() } diff --git a/tests/test_string.expected_results b/tests/test_string.expected_results index bd9b87c..55b29ed 100644 --- a/tests/test_string.expected_results +++ b/tests/test_string.expected_results @@ -2,3 +2,5 @@ hello strings assignment overload assignment overload2 assignment overload with additional +hope +hope3 diff --git a/tests/test_string.krak b/tests/test_string.krak index 7d1d17f..a9790e4 100644 --- a/tests/test_string.krak +++ b/tests/test_string.krak @@ -10,9 +10,9 @@ fun main(): int { io::println(str + "2"); str += " with additional" io::println(str); - //var initilized:string::string = "hope" - //io::println(initilized) - //io::println(initilized+ "3") + var initilized:string::string = "hope" + io::println(initilized) + io::println(initilized+ "3") return 0; }