Implements identifier trees for for-loops and conditional branching
parent
aeb36f9540
commit
27587d1fb0
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,8 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
|
||||||
case Comment: return "Comment";
|
case Comment: return "Comment";
|
||||||
case CustomTypeNode: return "CustomTypeNode";
|
case CustomTypeNode: return "CustomTypeNode";
|
||||||
case Declaration: return "Declaration";
|
case Declaration: return "Declaration";
|
||||||
|
case Expression: return "Expression";
|
||||||
|
case ForLoop: return "ForLoop";
|
||||||
case DeclarationSequence: return "DeclarationSequence";
|
case DeclarationSequence: return "DeclarationSequence";
|
||||||
case FunctionArgumentSequence: return "FunctionArgumentSequence";
|
case FunctionArgumentSequence: return "FunctionArgumentSequence";
|
||||||
case FunctionCallExpression: return "FunctionCallExpression";
|
case FunctionCallExpression: return "FunctionCallExpression";
|
||||||
|
|
|
@ -64,24 +64,56 @@ IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
IdNode* MakeIdTree(Node *astNode) {
|
IdNode* MakeIdTree(Node *astNode) {
|
||||||
|
uint32_t i;
|
||||||
switch (astNode->syntaxKind) {
|
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:
|
case Declaration:
|
||||||
return MakeIdNode(Variable, astNode->children[1]->value.string);
|
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: {
|
case StructDeclaration: {
|
||||||
Node *idNode = astNode->children[0];
|
Node *idNode = astNode->children[0];
|
||||||
Node *declsNode = astNode->children[1];
|
Node *declsNode = astNode->children[1];
|
||||||
IdNode *structNode = MakeIdNode(Struct, idNode->value.string);
|
IdNode *structNode = MakeIdNode(Struct, idNode->value.string);
|
||||||
uint32_t i;
|
|
||||||
for (i = 0; i < declsNode->childCount; i++) {
|
for (i = 0; i < declsNode->childCount; i++) {
|
||||||
AddChildToNode(structNode, MakeIdTree(declsNode->children[i]));
|
AddChildToNode(structNode, MakeIdTree(declsNode->children[i]));
|
||||||
}
|
}
|
||||||
|
@ -92,11 +124,8 @@ IdNode* MakeIdTree(Node *astNode) {
|
||||||
Node *sigNode = astNode->children[0];
|
Node *sigNode = astNode->children[0];
|
||||||
Node *funcNameNode = sigNode->children[0];
|
Node *funcNameNode = sigNode->children[0];
|
||||||
Node *funcArgsNode = sigNode->children[2];
|
Node *funcArgsNode = sigNode->children[2];
|
||||||
|
|
||||||
Node *bodyStatementsNode = astNode->children[1];
|
Node *bodyStatementsNode = astNode->children[1];
|
||||||
|
|
||||||
IdNode *funcNode = MakeIdNode(Function, funcNameNode->value.string);
|
IdNode *funcNode = MakeIdNode(Function, funcNameNode->value.string);
|
||||||
uint32_t i;
|
|
||||||
for (i = 0; i < funcArgsNode->childCount; i++) {
|
for (i = 0; i < funcArgsNode->childCount; i++) {
|
||||||
AddChildToNode(funcNode, MakeIdTree(funcArgsNode->children[i]));
|
AddChildToNode(funcNode, MakeIdTree(funcArgsNode->children[i]));
|
||||||
}
|
}
|
||||||
|
@ -106,6 +135,14 @@ IdNode* MakeIdTree(Node *astNode) {
|
||||||
return funcNode;
|
return funcNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case DeclarationSequence: {
|
||||||
|
IdNode *declSeqNode = MakeIdNode(LexicalScope, "");
|
||||||
|
for (i = 0; i < astNode->childCount; i++) {
|
||||||
|
AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i]));
|
||||||
|
}
|
||||||
|
return declSeqNode;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -118,7 +155,7 @@ void PrintIdTree(IdNode *tree, uint32_t tabCount) {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(tree->type) {
|
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 Struct: printf("%s : Struct\n", tree->name); break;
|
||||||
case Function: printf("%s : Function\n", tree->name); break;
|
case Function: printf("%s : Function\n", tree->name); break;
|
||||||
case Variable: printf("%s : Variable\n", tree->name); break;
|
case Variable: printf("%s : Variable\n", tree->name); break;
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "codegen.h"
|
#include "codegen.h"
|
||||||
|
#include "identcheck.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -64,6 +65,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
PrintIdTree(MakeIdTree(rootNode), /*tabCount=*/0);
|
||||||
exitCode = Codegen(rootNode, optimizationLevel);
|
exitCode = Codegen(rootNode, optimizationLevel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef WRAITH_UTIL_H
|
#ifndef WRAITH_UTIL_H
|
||||||
#define WRAITH_UTIL_H
|
#define WRAITH_UTIL_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
char* strdup (const char* s);
|
char* strdup (const char* s);
|
||||||
|
|
||||||
#endif /* WRAITH_UTIL_H */
|
#endif /* WRAITH_UTIL_H */
|
||||||
|
|
Loading…
Reference in New Issue