From 457998e0ff66eb30923f10622c979ae4b9b024b4 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Fri, 19 Jun 2015 17:13:06 -0400 Subject: [PATCH] Added in the cast function and fixed type promotion (so it would work with pointer arithmatic) --- include/Type.h | 2 +- src/ASTTransformation.cpp | 14 +++++++++++++- src/CGenerator.cpp | 2 +- src/Type.cpp | 2 ++ stdlib/conversions.krak | 7 +++++++ tests/test_conversions.expected_results | 9 +++++++++ tests/test_conversions.krak | 18 +++++++++++++++++- 7 files changed, 50 insertions(+), 4 deletions(-) diff --git a/include/Type.h b/include/Type.h index c568160..6d489e7 100644 --- a/include/Type.h +++ b/include/Type.h @@ -14,7 +14,7 @@ class ASTData; #include "ASTData.h" #include "util.h" -enum ValueType {none, template_type, template_type_type, void_type, boolean, integer, floating, double_percision, character, function_type }; +enum ValueType {none, template_type, template_type_type, void_type, boolean, character, integer, floating, double_percision, function_type }; class Type { diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index e3c018e..3868a96 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -893,7 +893,19 @@ NodeTree* ASTTransformation::doFunction(NodeTree* scope, std:: // because of the . operator, etc if (newNode->getDataRef()->valueType == NULL) { std::cout << "The value type from doFunction was null! (for " << lookup << ")" << std::endl; - newNode->getDataRef()->valueType = oldTypes[oldTypes.size()-1].clone(); + Type* newType = nullptr; + if (lookup == "->") + newType = oldTypes.back().clone(); + else if (oldTypes.front().getIndirection()) + newType = oldTypes.front().clone(); + else if (oldTypes.back().getIndirection()) + newType = oldTypes.back().clone(); + else + newType = (oldTypes.front().baseType > oldTypes.back().baseType) ? oldTypes.front().clone() : oldTypes.back().clone(); + //if (!newType) + //newType = oldTypes.back().clone(); + + newNode->getDataRef()->valueType = newType; std::cout << "function call to " << lookup << " - " << newNode->getName() << " is now " << newNode->getDataRef()->valueType << std::endl; } return newNode; diff --git a/src/CGenerator.cpp b/src/CGenerator.cpp index 8d464e0..502c7e9 100644 --- a/src/CGenerator.cpp +++ b/src/CGenerator.cpp @@ -542,7 +542,7 @@ CCodeTriple CGenerator::generate(NodeTree* from, NodeTree* enc // std::cout << name << " == " << children[0]->getData().symbol.getName() << std::endl; std::string name = children[0]->getDataRef()->symbol.getName(); ASTType funcType = children[0]->getDataRef()->type; - std::cout << "Doing function: " << name << std::endl; + //std::cout << "Doing function: " << name << std::endl; //Test for special functions only if what we're testing is, indeed, the definition, not a function call that returns a callable function pointer if (funcType == function) { if (name == "++" || name == "--") diff --git a/src/Type.cpp b/src/Type.cpp index f40ea53..2341d43 100644 --- a/src/Type.cpp +++ b/src/Type.cpp @@ -137,6 +137,8 @@ std::string Type::toString(bool showTraits) { } for (int i = 0; i < indirection; i++) typeString += "*"; + if (indirection < 0) + typeString += "negative indirection: " + intToString(indirection); if (traits.size() && showTraits) { typeString += "[ "; for (auto i : traits) diff --git a/stdlib/conversions.krak b/stdlib/conversions.krak index b4d087e..571f63c 100644 --- a/stdlib/conversions.krak +++ b/stdlib/conversions.krak @@ -18,4 +18,11 @@ fun to_int(in: T) : int { } return out; } +fun cast(in: T*):U* { + var out:U* + simple_passthrough(in:out:) """ + void* out = (void*)in; + """ + return out +} diff --git a/tests/test_conversions.expected_results b/tests/test_conversions.expected_results index b8439d7..63e1ab9 100644 --- a/tests/test_conversions.expected_results +++ b/tests/test_conversions.expected_results @@ -1,2 +1,11 @@ A 66 +57 +5 +0 +0 + +57 +5 +0 +0 diff --git a/tests/test_conversions.krak b/tests/test_conversions.krak index ab110be..0b70649 100644 --- a/tests/test_conversions.krak +++ b/tests/test_conversions.krak @@ -1,9 +1,25 @@ import io:* import conversions:* - fun main():int { println(to_char(65)) println(to_int('B')) + + var a = 1337 + var b = &a + var c = cast(b) + //var d = c + 1 + //var e = 1 + c + println(to_int(*(c+0))) + println(to_int(*(c+1))) + println(to_int(*(c+2))) + println(to_int(*(c+3))) + + println() + + println(to_int(c[0])) + println(to_int(c[1])) + println(to_int(c[2])) + println(to_int(c[3])) return 0 }