From 209985310eab66753955ab3145918512550c84c9 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Mon, 5 May 2014 13:52:12 -0400 Subject: [PATCH] Fixed type propagation for dereference and address-of, needs work for all the other operators also. Starting ground work for array notation --- include/Type.h | 1 + krakenGrammer.kgm | 2 +- src/ASTTransformation.cpp | 22 +++++++++++++++++++--- src/Type.cpp | 4 ++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/include/Type.h b/include/Type.h index 882e143..62bd270 100644 --- a/include/Type.h +++ b/include/Type.h @@ -27,6 +27,7 @@ class Type { ~Type(); bool const operator==(const Type &other)const; bool const operator!=(const Type &other)const; + Type* clone(); std::string toString(); ValueType baseType; NodeTree* typeDefinition; diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index 9438a5a..352eb4d 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -68,7 +68,7 @@ comparator = "==" | "<=" | ">=" | "!=" | "<" | ">" ; expression = expression WS "<<" WS term | expression WS ">>" WS shiftand | shiftand ; shiftand = shiftand WS "-" WS term | shiftand WS "\+" WS term | term ; term = term WS forward_slash WS factor | term WS "\*" WS factor | term WS "%" WS factor | factor ; -factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad ; +factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad WS "[" WS expression WS "]" | unarad ; unarad = number | identifier | function_call | bool | string | character | "\(" WS boolean_expression WS "\)" | access_operation ; number = integer | float | double ; access_operation = unarad "." identifier | unarad "->" identifier ; diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp index 7f286c6..900c66f 100644 --- a/src/ASTTransformation.cpp +++ b/src/ASTTransformation.cpp @@ -346,8 +346,10 @@ std::vector*> ASTTransformation::transformChildren(std::vector std::vector ASTTransformation::mapNodesToTypes(std::vector*> nodes) { std::vector types; - for (auto i : nodes) + for (auto i : nodes) { + std::cout << i->getDataRef()->toString() << std::endl; types.push_back(*(i->getDataRef()->valueType)); + } return types; } @@ -401,7 +403,21 @@ NodeTree* ASTTransformation::doFunction(NodeTree* scope, std:: NodeTree* function = scopeLookup(scope, lookup, mapNodesToTypes(nodes)); newNode->addChild(function); newNode->addChildren(nodes); - newNode->getDataRef()->valueType = function->getDataRef()->valueType; + + //Specially handle dereference and address of to assign the correct type + //We need some significant other type corrections here, maybe to the point of being their own function. (int + float, etc.) + for (auto i : nodes) + std::cout << i->getDataRef()->toString() << " "; + std::cout< oldTypes = mapNodesToTypes(nodes); + if (lookup == "*" || lookup == "&") { + Type* newType = oldTypes[0].clone(); + lookup == "*" ? newType->indirection-- : newType->indirection++; + newNode->getDataRef()->valueType = newType, std::cout << "Operator " + lookup << std::endl; + } else { + newNode->getDataRef()->valueType = function->getDataRef()->valueType, std::cout << "Some other ||" << lookup << "||" << std::endl; + } return newNode; } @@ -436,7 +452,7 @@ NodeTree* ASTTransformation::scopeLookup(NodeTree* scope, std: for (int j = 0; j < types.size(); j++) { if (types[j] != *(children[j]->getDataRef()->valueType)) { typesMatch = false; - std::cout << "Types do not match between two " << lookup << std::endl; + std::cout << "Types do not match between two " << lookup << " " << types[j].toString() << " vs " << children[j]->getDataRef()->valueType->toString() << std::endl; break; } } diff --git a/src/Type.cpp b/src/Type.cpp index 2e40328..3f12bbf 100644 --- a/src/Type.cpp +++ b/src/Type.cpp @@ -83,3 +83,7 @@ std::string Type::toString() { typeString += "*"; return typeString; } + +Type* Type::clone() { + return new Type(baseType, typeDefinition, indirection); +}