Refactors identcheck for new AST. Fixes newline bugs in PrintNode.

ast_refactor
venko 2021-05-15 19:00:46 -07:00
parent abc82f381e
commit 459a1dd3b7
2 changed files with 191 additions and 122 deletions

View File

@ -438,15 +438,15 @@ static void PrintBinaryOperator(BinaryOperator operator)
switch (operator)
{
case Add:
printf("+");
printf("(+)");
break;
case Subtract:
printf("-");
printf("(-)");
break;
case Multiply:
printf("*");
printf("(*)");
break;
}
}
@ -463,154 +463,173 @@ void PrintNode(Node *node, uint32_t tabCount)
switch (node->syntaxKind)
{
case AccessExpression:
printf("\n");
PrintNode(node->accessExpression.accessee, tabCount + 1);
PrintNode(node->accessExpression.accessor, tabCount + 1);
break;
return;
case AllocExpression:
printf("\n");
PrintNode(node->allocExpression.type, tabCount + 1);
break;
return;
case Assignment:
printf("\n");
PrintNode(node->assignmentStatement.left, tabCount + 1);
PrintNode(node->assignmentStatement.right, tabCount + 1);
break;
return;
case BinaryExpression:
PrintNode(node->binaryExpression.left, tabCount + 1);
PrintBinaryOperator(node->binaryExpression.operator);
printf("\n");
PrintNode(node->binaryExpression.left, tabCount + 1);
PrintNode(node->binaryExpression.right, tabCount + 1);
break;
return;
case CustomTypeNode:
printf("%s", node->customType.name);
break;
printf("%s\n", node->customType.name);
return;
case Declaration:
printf("\n");
PrintNode(node->declaration.identifier, tabCount + 1);
PrintNode(node->declaration.type, tabCount + 1);
break;
return;
case DeclarationSequence:
printf("\n");
for (i = 0; i < node->declarationSequence.count; i += 1)
{
PrintNode(node->declarationSequence.sequence[i], tabCount + 1);
}
break;
return;
case ForLoop:
printf("\n");
PrintNode(node->forLoop.declaration, tabCount + 1);
PrintNode(node->forLoop.startNumber, tabCount + 1);
PrintNode(node->forLoop.endNumber, tabCount + 1);
PrintNode(node->forLoop.statementSequence, tabCount + 1);
break;
return;
case FunctionArgumentSequence:
printf("\n");
for (i = 0; i < node->functionArgumentSequence.count; i += 1)
{
PrintNode(node->functionArgumentSequence.sequence[i], tabCount + 1);
}
break;
return;
case FunctionCallExpression:
printf("\n");
PrintNode(node->functionCallExpression.identifier, tabCount + 1);
PrintNode(node->functionCallExpression.argumentSequence, tabCount + 1);
break;
return;
case FunctionDeclaration:
printf("\n");
PrintNode(node->functionDeclaration.functionSignature, tabCount + 1);
PrintNode(node->functionDeclaration.functionBody, tabCount + 1);
break;
return;
case FunctionModifiers:
printf("\n");
for (i = 0; i < node->functionModifiers.count; i += 1)
{
PrintNode(node->functionModifiers.sequence[i], tabCount + 1);
}
break;
return;
case FunctionSignature:
printf("\n");
PrintNode(node->functionSignature.identifier, tabCount + 1);
PrintNode(node->functionSignature.arguments, tabCount + 1);
PrintNode(node->functionSignature.type, tabCount + 1);
PrintNode(node->functionSignature.modifiers, tabCount + 1);
break;
return;
case FunctionSignatureArguments:
printf("\n");
for (i = 0; i < node->functionSignatureArguments.count; i += 1)
{
PrintNode(node->functionSignatureArguments.sequence[i], tabCount + 1);
}
break;
return;
case Identifier:
if (node->typeTag == NULL) {
printf("%s", node->identifier.name);
printf("%s\n", node->identifier.name);
} else {
char *type = TypeTagToString(node->typeTag);
printf("%s<%s>", node->identifier.name, type);
printf("%s<%s>\n", node->identifier.name, type);
}
break;
return;
case IfStatement:
printf("\n");
PrintNode(node->ifStatement.expression, tabCount + 1);
PrintNode(node->ifStatement.statementSequence, tabCount + 1);
break;
return;
case IfElseStatement:
printf("\n");
PrintNode(node->ifElseStatement.ifStatement, tabCount + 1);
PrintNode(node->ifElseStatement.elseStatement, tabCount + 1);
break;
return;
case Number:
printf("%lu", node->number.value);
break;
printf("%lu\n", node->number.value);
return;
case PrimitiveTypeNode:
printf("%s", PrimitiveTypeToString(node->primitiveType.type));
break;
printf("%s\n", PrimitiveTypeToString(node->primitiveType.type));
return;
case ReferenceTypeNode:
printf("\n");
PrintNode(node->referenceType.type, tabCount + 1);
break;
return;
case Return:
printf("\n");
PrintNode(node->returnStatement.expression, tabCount + 1);
break;
return;
case ReturnVoid:
break;
return;
case StatementSequence:
printf("\n");
for (i = 0; i < node->statementSequence.count; i += 1)
{
PrintNode(node->statementSequence.sequence[i], tabCount + 1);
}
break;
return;
case StaticModifier:
break;
printf("\n");
return;
case StringLiteral:
printf("%s", node->stringLiteral.string);
break;
return;
case StructDeclaration:
printf("\n");
PrintNode(node->structDeclaration.identifier, tabCount + 1);
PrintNode(node->structDeclaration.declarationSequence, tabCount + 1);
break;
return;
case Type:
printf("\n");
PrintNode(node->type.typeNode, tabCount + 1);
break;
return;
case UnaryExpression:
PrintUnaryOperator(node->unaryExpression.operator);
PrintNode(node->unaryExpression.child, tabCount + 1);
break;
return;
}
printf("\n");
}
TypeTag* MakeTypeTag(Node *node) {
@ -647,7 +666,6 @@ TypeTag* MakeTypeTag(Node *node) {
case StructDeclaration:
tag->type = Custom;
tag->value.customType = strdup(node->structDeclaration.identifier->identifier.name);
printf("Struct tag: %s\n", TypeTagToString(tag));
break;
case FunctionDeclaration:

View File

@ -38,104 +38,95 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
uint32_t i;
IdNode *mainNode;
switch (astNode->syntaxKind) {
case AccessExpression:
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessee, parent));
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessor, parent));
return NULL;
case AllocExpression:
AddChildToNode(parent, MakeIdTree(astNode->allocExpression.type, parent));
return NULL;
case Assignment: {
if (astNode->children[0]->syntaxKind == Declaration) {
return MakeIdTree(astNode->children[0], parent);
if (astNode->assignmentStatement.left->syntaxKind == Declaration) {
return MakeIdTree(astNode->assignmentStatement.left, parent);
} else {
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent));
}
AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.left, parent));
AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.right, parent));
return NULL;
}
}
case IfStatement: {
mainNode = MakeIdNode(OrderedScope, "if", parent);
Node *clause = astNode->children[0];
Node *stmtSeq = astNode->children[1];
for (i = 0; i < clause->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(clause->children[i], mainNode));
}
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(stmtSeq->children[i], mainNode));
}
break;
}
case IfElseStatement: {
Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1];
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", mainNode);
AddChildToNode(mainNode, ifBranch);
for (i = 0; i < elseStmts->childCount; i++) {
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
}
AddChildToNode(mainNode, elseBranch);
break;
}
case ForLoop: {
Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3];
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(loopBody->children[i], mainNode));
}
break;
}
case BinaryExpression:
AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.left, parent));
AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.right, parent));
return NULL;
case Declaration: {
mainNode = MakeIdNode(Variable, astNode->children[1]->value.string, parent);
Node *idNode = astNode->declaration.identifier;
mainNode = MakeIdNode(Variable, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[1]->typeTag = mainNode->typeTag;
break;
}
case StructDeclaration: {
Node *idNode = astNode->children[0];
Node *declsNode = astNode->children[1];
mainNode = MakeIdNode(Struct, idNode->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(declsNode->children[i], mainNode));
}
break;
}
case FunctionDeclaration: {
Node *sigNode = astNode->children[0];
Node *funcNameNode = sigNode->children[0];
Node *funcArgsNode = sigNode->children[2];
Node *bodyStatementsNode = astNode->children[1];
mainNode = MakeIdNode(Function, funcNameNode->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[0]->children[0]->typeTag = mainNode->typeTag;
for (i = 0; i < funcArgsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(funcArgsNode->children[i], mainNode));
}
for (i = 0; i < bodyStatementsNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(bodyStatementsNode->children[i], mainNode));
}
idNode->typeTag = mainNode->typeTag;
break;
}
case DeclarationSequence: {
mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(astNode->children[i], mainNode));
for (i = 0; i < astNode->declarationSequence.count; i++) {
AddChildToNode(
mainNode, MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
}
break;
}
case ForLoop: {
Node *loopDecl = astNode->forLoop.declaration;
Node *loopBody = astNode->forLoop.statementSequence;
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
AddChildToNode(mainNode, MakeIdTree(loopBody, mainNode));
break;
}
case FunctionArgumentSequence:
for (i = 0; i < astNode->functionArgumentSequence.count; i++) {
AddChildToNode(
parent, MakeIdTree(astNode->functionArgumentSequence.sequence[i], parent));
}
return NULL;
case FunctionCallExpression:
AddChildToNode(parent, MakeIdTree(astNode->functionCallExpression.identifier, parent));
AddChildToNode(
parent, MakeIdTree(astNode->functionCallExpression.argumentSequence, parent));
return NULL;
case FunctionDeclaration: {
Node *sigNode = astNode->functionDeclaration.functionSignature;
Node *idNode = sigNode->functionSignature.identifier;
char *funcName = idNode->identifier.name;
mainNode = MakeIdNode(Function, funcName, parent);
mainNode->typeTag = MakeTypeTag(astNode);
idNode->typeTag = mainNode->typeTag;
MakeIdTree(sigNode->functionSignature.arguments, mainNode);
MakeIdTree(astNode->functionDeclaration.functionBody, mainNode);
break;
}
case FunctionSignatureArguments: {
for (i = 0; i < astNode->functionSignatureArguments.count; i++) {
Node *argNode = astNode->functionSignatureArguments.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
case Identifier: {
mainNode = MakeIdNode(Placeholder, astNode->value.string, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, astNode->value.string);
char *name = astNode->identifier.name;
mainNode = MakeIdNode(Placeholder, name, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, name);
if (lookupNode == NULL) {
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", astNode->value.string);
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name);
TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
tag->type = Unknown;
astNode->typeTag = tag;
@ -145,12 +136,73 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
break;
}
default: {
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent));
case IfStatement: {
Node *clause = astNode->ifStatement.expression;
Node *stmtSeq = astNode->ifStatement.statementSequence;
mainNode = MakeIdNode(OrderedScope, "if", parent);
MakeIdTree(clause, mainNode);
MakeIdTree(stmtSeq, mainNode);
break;
}
case IfElseStatement: {
Node *ifNode = astNode->ifElseStatement.ifStatement;
Node *elseStmts = astNode->ifElseStatement.elseStatement;
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
AddChildToNode(mainNode, ifBranch);
IdNode *elseScope = MakeIdNode(OrderedScope, "else", mainNode);
MakeIdTree(elseStmts, elseScope);
AddChildToNode(mainNode, elseScope);
break;
}
case ReferenceTypeNode:
AddChildToNode(parent, MakeIdTree(astNode->referenceType.type, parent));
return NULL;
case Return:
AddChildToNode(parent, MakeIdTree(astNode->returnStatement.expression, parent));
return NULL;
case StatementSequence: {
for (i = 0; i < astNode->statementSequence.count; i++) {
Node *argNode = astNode->statementSequence.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
case StructDeclaration: {
Node *idNode = astNode->structDeclaration.identifier;
Node *declsNode = astNode->structDeclaration.declarationSequence;
mainNode = MakeIdNode(Struct, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->declarationSequence.count; i++) {
Node *decl = declsNode->declarationSequence.sequence[i];
AddChildToNode(mainNode, MakeIdTree(decl, mainNode));
}
break;
}
case Type:
AddChildToNode(parent, MakeIdTree(astNode->type.typeNode, parent));
return NULL;
case UnaryExpression:
AddChildToNode(parent, MakeIdTree(astNode->unaryExpression.child, parent));
return NULL;
case Comment:
case CustomTypeNode:
case FunctionModifiers:
case FunctionSignature:
case Number:
case PrimitiveTypeNode:
case ReturnVoid:
case StaticModifier:
case StringLiteral:
return NULL;
}
astNode->idLink = mainNode;
@ -203,7 +255,6 @@ void PrintIdTree(IdNode *tree, uint32_t tabCount) {
}
}
int PrintAncestors(IdNode *node) {
if (node == NULL) return -1;