Fixed type propagation for dereference and address-of, needs work for all the other operators also. Starting ground work for array notation

This commit is contained in:
Nathan Braswell
2014-05-05 13:52:12 -04:00
parent 9a4507a0f5
commit 209985310e
4 changed files with 25 additions and 4 deletions

View File

@@ -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<ASTData>* typeDefinition;

View File

@@ -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 ;

View File

@@ -346,8 +346,10 @@ std::vector<NodeTree<ASTData>*> ASTTransformation::transformChildren(std::vector
std::vector<Type> ASTTransformation::mapNodesToTypes(std::vector<NodeTree<ASTData>*> nodes) {
std::vector<Type> 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<ASTData>* ASTTransformation::doFunction(NodeTree<ASTData>* scope, std::
NodeTree<ASTData>* 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<<std::endl;
std::vector<Type> 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<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* 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;
}
}

View File

@@ -83,3 +83,7 @@ std::string Type::toString() {
typeString += "*";
return typeString;
}
Type* Type::clone() {
return new Type(baseType, typeDefinition, indirection);
}