Disambiguates scopes where ordering matters vs ones where it doesn't

identifiers
venko 2021-05-08 12:09:25 -07:00
parent 743828450c
commit 252dc9b87f
2 changed files with 15 additions and 10 deletions

View File

@ -43,7 +43,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case IfStatement: { case IfStatement: {
Node *stmtSeq = astNode->children[1]; Node *stmtSeq = astNode->children[1];
IdNode *ifNode = MakeIdNode(LexicalScope, "if", parent); IdNode *ifNode = MakeIdNode(OrderedScope, "if", parent);
for (i = 0; i < stmtSeq->childCount; i++) { for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode)); AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode));
} }
@ -53,9 +53,9 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case IfElseStatement: { case IfElseStatement: {
Node *ifNode = astNode->children[0]; Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1]; Node *elseStmts = astNode->children[1];
IdNode *ifElseNode = MakeIdNode(LexicalScope, "if-else", parent); IdNode *ifElseNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode); IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode);
IdNode *elseBranch = MakeIdNode(LexicalScope, "else", ifElseNode); IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode);
AddChildToNode(ifElseNode, ifBranch); AddChildToNode(ifElseNode, ifBranch);
AddChildToNode(ifElseNode, elseBranch); AddChildToNode(ifElseNode, elseBranch);
@ -69,7 +69,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
case ForLoop: { case ForLoop: {
Node *loopDecl = astNode->children[0]; Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3]; Node *loopBody = astNode->children[3];
IdNode *loopNode = MakeIdNode(LexicalScope, "for-loop", parent); IdNode *loopNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode)); AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode));
for (i = 0; i < loopBody->childCount; i++) { for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode)); AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode));
@ -111,7 +111,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
} }
case DeclarationSequence: { case DeclarationSequence: {
IdNode *declSeqNode = MakeIdNode(LexicalScope, "", parent); IdNode *declSeqNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) { for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode)); AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode));
} }
@ -130,8 +130,11 @@ void PrintIdNode(IdNode *node) {
} }
switch(node->type) { switch(node->type) {
case LexicalScope: case OrderedScope:
printf("Scope (%s)\n", node->name); printf("OrderedScope (%s)\n", node->name);
break;
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
break; break;
case Struct: case Struct:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag)); printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
@ -177,7 +180,7 @@ int PrintAncestors(IdNode *node) {
return indent; return indent;
} }
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) { IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
if (root == NULL) { if (root == NULL) {
fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n"); fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n");
return NULL; return NULL;

View File

@ -7,7 +7,8 @@
#include "ast.h" #include "ast.h"
typedef enum NodeType { typedef enum NodeType {
LexicalScope, UnorderedScope,
OrderedScope,
Struct, Struct,
Function, Function,
Variable Variable
@ -30,10 +31,11 @@ typedef struct IdStatus {
} IdStatus; } IdStatus;
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName);
IdNode* MakeIdTree(Node *astNode, IdNode *parent); IdNode* MakeIdTree(Node *astNode, IdNode *parent);
void PrintIdTree(IdNode *tree, uint32_t tabCount); void PrintIdTree(IdNode *tree, uint32_t tabCount);
int PrintAncestors(IdNode *node); int PrintAncestors(IdNode *node);
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName);
IdNode* LookupId(IdNode *node, IdNode *prev, char* target);
//IdStatus CheckIds(Node *root); //IdStatus CheckIds(Node *root);