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

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

View File

@ -38,104 +38,95 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
uint32_t i; uint32_t i;
IdNode *mainNode; IdNode *mainNode;
switch (astNode->syntaxKind) { 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: { case Assignment: {
if (astNode->children[0]->syntaxKind == Declaration) { if (astNode->assignmentStatement.left->syntaxKind == Declaration) {
return MakeIdTree(astNode->children[0], parent); return MakeIdTree(astNode->assignmentStatement.left, parent);
} else { } else {
for (i = 0; i < astNode->childCount; i++) { AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.left, parent));
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent)); AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.right, parent));
}
return NULL; return NULL;
} }
} }
case IfStatement: { case BinaryExpression:
mainNode = MakeIdNode(OrderedScope, "if", parent); AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.left, parent));
Node *clause = astNode->children[0]; AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.right, parent));
Node *stmtSeq = astNode->children[1]; return NULL;
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 Declaration: { 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); mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[1]->typeTag = mainNode->typeTag; idNode->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));
}
break; break;
} }
case DeclarationSequence: { case DeclarationSequence: {
mainNode = MakeIdNode(UnorderedScope, "", parent); mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) { for (i = 0; i < astNode->declarationSequence.count; i++) {
AddChildToNode(mainNode, MakeIdTree(astNode->children[i], mainNode)); AddChildToNode(
mainNode, MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
} }
break; 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: { case Identifier: {
mainNode = MakeIdNode(Placeholder, astNode->value.string, parent); char *name = astNode->identifier.name;
IdNode *lookupNode = LookupId(mainNode, NULL, astNode->value.string); mainNode = MakeIdNode(Placeholder, name, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, name);
if (lookupNode == NULL) { 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)); TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
tag->type = Unknown; tag->type = Unknown;
astNode->typeTag = tag; astNode->typeTag = tag;
@ -145,12 +136,73 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
break; break;
} }
default: { case IfStatement: {
for (i = 0; i < astNode->childCount; i++) { Node *clause = astNode->ifStatement.expression;
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent)); 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; 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; astNode->idLink = mainNode;
@ -203,7 +255,6 @@ void PrintIdTree(IdNode *tree, uint32_t tabCount) {
} }
} }
int PrintAncestors(IdNode *node) { int PrintAncestors(IdNode *node) {
if (node == NULL) return -1; if (node == NULL) return -1;