diff --git a/iftest.w b/iftest.w new file mode 100644 index 0000000..90d5d11 --- /dev/null +++ b/iftest.w @@ -0,0 +1,27 @@ +struct Program { // Scope () + static Main(): int { // | Program : Struct + myInt: int = 54; // | | Main : Function + if (myInt < 0) { // | | | myInt : Variable + signTag: int = 0 - 1; // | | | Scope (if-else) + } else if (myInt == 0) { // | | | | Scope (if) + signTag: int = 0; // | | | | | signTag : Variable + } else { // | | | | Scope (else) + signTag: int = 1; // | | | | | Scope (if) + } // | | | | | | signTag : Variable + // | | | myBool : Variable + myBool: bool; // | | | Scope (if) + if (myBool) { // | | | | Scope (if) + if (myBool) { // | | | | | Scope (if) + if (myBool) { // | | | | | | Scope (if) + if (myBool) { // | | | | | | | Scope (if) + if (myBool) { // | | | | | | | | lol : Variable + lol: int = 69; + } + } + } + } + } + + return 0; + } +} diff --git a/src/ast.c b/src/ast.c index d08c224..4c39ba0 100644 --- a/src/ast.c +++ b/src/ast.c @@ -16,6 +16,8 @@ const char* SyntaxKindString(SyntaxKind syntaxKind) case Comment: return "Comment"; case CustomTypeNode: return "CustomTypeNode"; case Declaration: return "Declaration"; + case Expression: return "Expression"; + case ForLoop: return "ForLoop"; case DeclarationSequence: return "DeclarationSequence"; case FunctionArgumentSequence: return "FunctionArgumentSequence"; case FunctionCallExpression: return "FunctionCallExpression"; diff --git a/src/identcheck.c b/src/identcheck.c index 66b964e..772628b 100644 --- a/src/identcheck.c +++ b/src/identcheck.c @@ -64,24 +64,56 @@ IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) { } IdNode* MakeIdTree(Node *astNode) { + uint32_t i; switch (astNode->syntaxKind) { + case Assignment: + return (astNode->children[0]->syntaxKind == Declaration) + ? MakeIdTree(astNode->children[0]) + : NULL; + + case IfStatement: { + Node *stmtSeq = astNode->children[1]; + IdNode *ifNode = MakeIdNode(LexicalScope, "if"); + for (i = 0; i < stmtSeq->childCount; i++) { + AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i])); + } + return ifNode; + } + + case IfElseStatement: { + Node *ifNode = astNode->children[0]; + Node *elseStmts = astNode->children[1]; + IdNode *ifElseNode = MakeIdNode(LexicalScope, "if-else"); + IdNode *ifBranch = MakeIdTree(ifNode); + IdNode *elseBranch = MakeIdNode(LexicalScope, "else"); + + AddChildToNode(ifElseNode, ifBranch); + AddChildToNode(ifElseNode, elseBranch); + for (i = 0; i < elseStmts->childCount; i++) { + AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i])); + } + + return ifElseNode; + } + + case ForLoop: { + Node *loopDecl = astNode->children[0]; + Node *loopBody = astNode->children[3]; + IdNode *loopNode = MakeIdNode(LexicalScope, "for-loop"); + AddChildToNode(loopNode, MakeIdTree(loopDecl)); + for (i = 0; i < loopBody->childCount; i++) { + AddChildToNode(loopNode, MakeIdTree(loopBody->children[i])); + } + return loopNode; + } + case Declaration: return MakeIdNode(Variable, astNode->children[1]->value.string); - case DeclarationSequence: { - IdNode *declSeqNode = MakeIdNode(LexicalScope, ""); - uint32_t i; - for (i = 0; i < astNode->childCount; i++) { - AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i])); - } - return declSeqNode; - } - case StructDeclaration: { Node *idNode = astNode->children[0]; Node *declsNode = astNode->children[1]; IdNode *structNode = MakeIdNode(Struct, idNode->value.string); - uint32_t i; for (i = 0; i < declsNode->childCount; i++) { AddChildToNode(structNode, MakeIdTree(declsNode->children[i])); } @@ -92,11 +124,8 @@ IdNode* MakeIdTree(Node *astNode) { Node *sigNode = astNode->children[0]; Node *funcNameNode = sigNode->children[0]; Node *funcArgsNode = sigNode->children[2]; - Node *bodyStatementsNode = astNode->children[1]; - IdNode *funcNode = MakeIdNode(Function, funcNameNode->value.string); - uint32_t i; for (i = 0; i < funcArgsNode->childCount; i++) { AddChildToNode(funcNode, MakeIdTree(funcArgsNode->children[i])); } @@ -106,6 +135,14 @@ IdNode* MakeIdTree(Node *astNode) { return funcNode; } + case DeclarationSequence: { + IdNode *declSeqNode = MakeIdNode(LexicalScope, ""); + for (i = 0; i < astNode->childCount; i++) { + AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i])); + } + return declSeqNode; + } + default: return NULL; } @@ -118,7 +155,7 @@ void PrintIdTree(IdNode *tree, uint32_t tabCount) { } switch(tree->type) { - case LexicalScope: printf("Scope\n"); break; + case LexicalScope: printf("Scope (%s)\n", tree->name); break; case Struct: printf("%s : Struct\n", tree->name); break; case Function: printf("%s : Function\n", tree->name); break; case Variable: printf("%s : Variable\n", tree->name); break; diff --git a/src/main.c b/src/main.c index 48a2301..10668c2 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include "parser.h" #include "codegen.h" +#include "identcheck.h" int main(int argc, char *argv[]) { @@ -64,6 +65,7 @@ int main(int argc, char *argv[]) } else { + PrintIdTree(MakeIdTree(rootNode), /*tabCount=*/0); exitCode = Codegen(rootNode, optimizationLevel); } } diff --git a/src/util.h b/src/util.h index 2c305ec..99ffaa7 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,8 @@ #ifndef WRAITH_UTIL_H #define WRAITH_UTIL_H +#include + char* strdup (const char* s); #endif /* WRAITH_UTIL_H */