forked from cosmonaut/wraith-lang
				
			Compare commits
	
		
			3 Commits 
		
	
	
		
			743828450c
			...
			32541d4794
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | 32541d4794 | |
|  | aa2449f7df | |
|  | 252dc9b87f | 
							
								
								
									
										18
									
								
								iftest.w
								
								
								
								
							
							
						
						
									
										18
									
								
								iftest.w
								
								
								
								
							|  | @ -1,4 +1,15 @@ | |||
| struct MyStruct { | ||||
|     static MyFunc(): int { | ||||
|         myStructInt: int = 595959959; | ||||
|         return myStructInt; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| struct Program { | ||||
|     static Foo(): int { | ||||
|         return 0; | ||||
|     } | ||||
| 
 | ||||
|     static Main(): int { | ||||
|         myInt: int = 54; | ||||
|         if (myInt < 0) { | ||||
|  | @ -9,7 +20,6 @@ struct Program { | |||
|             signTag: int = 1; | ||||
|         } | ||||
| 
 | ||||
|         lol: int = 420; | ||||
|         myBool: bool; | ||||
|         if (myBool) { | ||||
|             if (myBool) { | ||||
|  | @ -23,6 +33,12 @@ struct Program { | |||
|             } | ||||
|         } | ||||
| 
 | ||||
|         someInt: int = 9585858; | ||||
| 
 | ||||
|         return 0; | ||||
|     } | ||||
| 
 | ||||
|     static Bar(): int { | ||||
|         return 0; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -593,6 +593,7 @@ char* TypeTagToString(TypeTag *tag) { | |||
|             size_t innerStrLen = strlen(inner); | ||||
|             char *result = malloc(sizeof(char) * (innerStrLen + 5)); | ||||
|             sprintf(result, "Ref<%s>", inner); | ||||
|             free(inner); | ||||
|             return result; | ||||
|         } | ||||
|         case Custom: | ||||
|  |  | |||
|  | @ -1,3 +1,4 @@ | |||
| #include <stdbool.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdint.h> | ||||
|  | @ -43,7 +44,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) { | |||
| 
 | ||||
|         case IfStatement: { | ||||
|             Node *stmtSeq = astNode->children[1]; | ||||
|             IdNode *ifNode = MakeIdNode(LexicalScope, "if", parent); | ||||
|             IdNode *ifNode = MakeIdNode(OrderedScope, "if", parent); | ||||
|             for (i = 0; i < stmtSeq->childCount; i++) { | ||||
|                 AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode)); | ||||
|             } | ||||
|  | @ -53,15 +54,15 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) { | |||
|         case IfElseStatement: { | ||||
|             Node *ifNode = astNode->children[0]; | ||||
|             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 *elseBranch = MakeIdNode(LexicalScope, "else", ifElseNode); | ||||
|             IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode); | ||||
| 
 | ||||
|             AddChildToNode(ifElseNode, ifBranch); | ||||
|             AddChildToNode(ifElseNode, elseBranch); | ||||
|             for (i = 0; i < elseStmts->childCount; i++) { | ||||
|                 AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch)); | ||||
|             } | ||||
|             AddChildToNode(ifElseNode, elseBranch); | ||||
| 
 | ||||
|             return ifElseNode; | ||||
|         } | ||||
|  | @ -69,7 +70,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) { | |||
|         case ForLoop: { | ||||
|             Node *loopDecl = astNode->children[0]; | ||||
|             Node *loopBody = astNode->children[3]; | ||||
|             IdNode *loopNode = MakeIdNode(LexicalScope, "for-loop", parent); | ||||
|             IdNode *loopNode = MakeIdNode(OrderedScope, "for-loop", parent); | ||||
|             AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode)); | ||||
|             for (i = 0; i < loopBody->childCount; i++) { | ||||
|                 AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode)); | ||||
|  | @ -111,7 +112,7 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) { | |||
|         } | ||||
| 
 | ||||
|         case DeclarationSequence: { | ||||
|             IdNode *declSeqNode = MakeIdNode(LexicalScope, "", parent); | ||||
|             IdNode *declSeqNode = MakeIdNode(UnorderedScope, "", parent); | ||||
|             for (i = 0; i < astNode->childCount; i++) { | ||||
|                 AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode)); | ||||
|             } | ||||
|  | @ -130,8 +131,11 @@ void PrintIdNode(IdNode *node) { | |||
|     } | ||||
| 
 | ||||
|     switch(node->type) { | ||||
|         case LexicalScope:  | ||||
|             printf("Scope (%s)\n", node->name);  | ||||
|         case OrderedScope:  | ||||
|             printf("OrderedScope (%s)\n", node->name);  | ||||
|             break; | ||||
|         case UnorderedScope:  | ||||
|             printf("UnorderedScope (%s)\n", node->name);  | ||||
|             break; | ||||
|         case Struct: | ||||
|             printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));  | ||||
|  | @ -177,9 +181,9 @@ int PrintAncestors(IdNode *node) { | |||
|     return indent; | ||||
| } | ||||
| 
 | ||||
| IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) { | ||||
| IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) { | ||||
|     if (root == NULL) { | ||||
|         fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n"); | ||||
|         fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n"); | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|  | @ -213,3 +217,74 @@ IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) { | |||
|     free(frontier); | ||||
|     return result; | ||||
| } | ||||
| 
 | ||||
| bool ScopeHasOrdering(IdNode *node) { | ||||
|     switch (node->type) { | ||||
|         case OrderedScope: | ||||
|         case Function: | ||||
|         case Variable: // this is only technically true
 | ||||
|             return true; | ||||
|         default: | ||||
|             return false; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| IdNode* LookupId(IdNode *node, IdNode *prev, char *target) { | ||||
|     if (node == NULL) { | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     if (strcmp(node->name, target) == 0) { | ||||
|         return node; | ||||
|     } | ||||
| 
 | ||||
|     // If this is the start of our search, we should not attempt to look at child nodes. Only
 | ||||
|     // looking up the scope tree is valid at this point.
 | ||||
|     //
 | ||||
|     // This has the notable side-effect that this function will return NULL if you attempt to look
 | ||||
|     // up a struct's internals starting from the node representing the struct itself. This is
 | ||||
|     // because an IdNode corresponds to the location *where an identifier is first declared.* Thus,
 | ||||
|     // an identifier has no knowledge of identifiers declared "inside" of it.
 | ||||
|     if (prev == NULL) { | ||||
|         return LookupId(node->parent, node, target); | ||||
|     } | ||||
| 
 | ||||
|     uint32_t idxLimit; | ||||
|     // If the current node forms an ordered scope then we want to prevent ourselves from looking
 | ||||
|     // up identifiers declared after the scope we have just come from.
 | ||||
|     if (ScopeHasOrdering(node)) { | ||||
|         uint32_t i; | ||||
|         for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) { | ||||
|             if (node->children[i] == prev) { | ||||
|                 break; | ||||
|             } | ||||
|         } | ||||
|     } else { | ||||
|         idxLimit = node->childCount; | ||||
|     } | ||||
| 
 | ||||
|     uint32_t i; | ||||
|     for (i = 0; i < idxLimit; i++) { | ||||
|         IdNode *child = node->children[i]; | ||||
|         if (child == prev) { | ||||
|             // Do not inspect the node we just came from.
 | ||||
|             continue; | ||||
|         } | ||||
| 
 | ||||
|         if (strcmp(child->name, target) == 0) { | ||||
|             return child; | ||||
|         }  | ||||
| 
 | ||||
|         if (child->type == Struct) { | ||||
|             uint32_t j; | ||||
|             for (j = 0; j < child->childCount; j++) { | ||||
|                 IdNode *grandchild = child->children[j]; | ||||
|                 if (strcmp(grandchild->name, target) == 0) { | ||||
|                     return grandchild; | ||||
|                 }  | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     return LookupId(node->parent, node, target); | ||||
| } | ||||
|  | @ -7,7 +7,8 @@ | |||
| #include "ast.h" | ||||
| 
 | ||||
| typedef enum NodeType { | ||||
|     LexicalScope, | ||||
|     UnorderedScope, | ||||
|     OrderedScope, | ||||
|     Struct, | ||||
|     Function, | ||||
|     Variable | ||||
|  | @ -30,10 +31,12 @@ typedef struct IdStatus { | |||
| } IdStatus; | ||||
| 
 | ||||
| 
 | ||||
| IdNode* FindId(IdNode *root, NodeType targetType, char *targetName); | ||||
| IdNode* MakeIdTree(Node *astNode, IdNode *parent); | ||||
| void PrintIdNode(IdNode *node); | ||||
| void PrintIdTree(IdNode *tree, uint32_t tabCount); | ||||
| int PrintAncestors(IdNode *node); | ||||
| IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName); | ||||
| IdNode* LookupId(IdNode *node, IdNode *prev, char* target); | ||||
| 
 | ||||
| //IdStatus CheckIds(Node *root);
 | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										42
									
								
								src/main.c
								
								
								
								
							
							
						
						
									
										42
									
								
								src/main.c
								
								
								
								
							|  | @ -65,9 +65,49 @@ int main(int argc, char *argv[]) | |||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     if (parseVerbose) { | ||||
|                     { // This shit only works if you're using iftest.w
 | ||||
|                         IdNode *idTree = MakeIdTree(rootNode, NULL); | ||||
|                         PrintIdTree(idTree, /*tabCount=*/0); | ||||
| 
 | ||||
|                         printf("\nSeeking to struct 'Program'\n"); | ||||
|                         IdNode *node = LookdownId(idTree, Struct, "Program"); | ||||
|                         if (node == NULL) printf("Failed.\n"); | ||||
| 
 | ||||
|                         printf("\n[attempting to look up MyStruct]: \n"); | ||||
|                         PrintIdNode(LookupId(node, /*prev=*/NULL, "MyStruct")); | ||||
| 
 | ||||
|                             printf("\n[attempting to look up MyFunc]: \n"); | ||||
|                             PrintIdNode(LookupId(node, /*prev=*/NULL, "MyFunc")); | ||||
| 
 | ||||
|                                 printf("\n[attempting to look up myStructInt]: \n"); | ||||
|                                 PrintIdNode(LookupId(node, /*prev=*/NULL, "myStructInt")); | ||||
| 
 | ||||
|                         printf("\n[attempting to look up Program]: \n"); | ||||
|                         PrintIdNode(LookupId(node, /*prev=*/NULL, "Program")); | ||||
| 
 | ||||
|                             printf("\n[attempting to look up Foo]: \n"); | ||||
|                             PrintIdNode(LookupId(node, /*prev=*/NULL, "Foo")); | ||||
| 
 | ||||
|                             printf("\n[attempting to look up Main]: \n"); | ||||
|                             PrintIdNode(LookupId(node, /*prev=*/NULL, "Main")); | ||||
| 
 | ||||
|                                 printf("\n[attempting to look up myInt]: \n"); | ||||
|                                 PrintIdNode(LookupId(node, /*prev=*/NULL, "myInt")); | ||||
| 
 | ||||
|                                 printf("\n[attempting to look up signTag]: \n"); | ||||
|                                 PrintIdNode(LookupId(node, /*prev=*/NULL, "signTag")); | ||||
| 
 | ||||
|                                 printf("\n[attempting to look up myBool]: \n"); | ||||
|                                 PrintIdNode(LookupId(node, /*prev=*/NULL, "myBool")); | ||||
| 
 | ||||
|                                 printf("\n[attempting to look up lol]: \n"); | ||||
|                                 PrintIdNode(LookupId(node, /*prev=*/NULL, "lol")); | ||||
| 
 | ||||
|                                 printf("\n[attempting to look up someInt]: \n"); | ||||
|                                 PrintIdNode(LookupId(node, /*prev=*/NULL, "someInt")); | ||||
| 
 | ||||
|                             printf("\n[attempting to look up Bar]: \n"); | ||||
|                             PrintIdNode(LookupId(node, /*prev=*/NULL, "Bar")); | ||||
|                     } | ||||
|                     exitCode = Codegen(rootNode, optimizationLevel); | ||||
|                 } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue