some minor style revisions

pull/1/head
cosmonaut 2021-05-14 11:53:09 -07:00
parent f441e5bede
commit 41bf2bece8
3 changed files with 33 additions and 32 deletions

View File

@ -74,20 +74,20 @@ typedef union
typedef struct TypeTag typedef struct TypeTag
{ {
enum Type enum Type
{ {
Unknown, Unknown,
Primitive, Primitive,
Reference, Reference,
Custom Custom
} type; } type;
union union
{ {
// Valid when type = Primitive. /* Valid when type = Primitive. */
PrimitiveType primitiveType; PrimitiveType primitiveType;
// Valid when type = Reference. /* Valid when type = Reference. */
struct TypeTag *referenceType; struct TypeTag *referenceType;
// Valid when type = Custom. /* Valid when type = Custom. */
char *customType; char *customType;
} value; } value;
} TypeTag; } TypeTag;
@ -108,7 +108,7 @@ typedef struct Node
uint64_t number; uint64_t number;
} value; } value;
PrimitiveType primitiveType; PrimitiveType primitiveType;
TypeTag *typeTag; TypeTag *typeTag;
IdNode *idLink; IdNode *idLink;
} Node; } Node;

View File

@ -24,10 +24,10 @@ void AddChildToNode(IdNode *node, IdNode *child) {
if (node->children == NULL) { if (node->children == NULL) {
node->childCapacity = 2; node->childCapacity = 2;
node->children = (IdNode**)malloc(sizeof(IdNode*) * node->childCapacity); node->children = (IdNode**) malloc(sizeof(IdNode*) * node->childCapacity);
} else if (node->childCount == node->childCapacity) { } else if (node->childCount == node->childCapacity) {
node->childCapacity *= 2; node->childCapacity *= 2;
node->children = realloc(node->children, sizeof(IdNode*) * node->childCapacity); node->children = (IdNode**) realloc(node->children, sizeof(IdNode*) * node->childCapacity);
} }
node->children[node->childCount] = child; node->children[node->childCount] = child;
@ -167,20 +167,20 @@ void PrintIdNode(IdNode *node) {
case Placeholder: case Placeholder:
printf("Placeholder (%s)\n", node->name); printf("Placeholder (%s)\n", node->name);
break; break;
case OrderedScope: case OrderedScope:
printf("OrderedScope (%s)\n", node->name); printf("OrderedScope (%s)\n", node->name);
break; break;
case UnorderedScope: case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name); 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));
break; break;
case Function: case Function:
printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag)); printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag));
break; break;
case Variable: case Variable:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag)); printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
break; break;
} }
} }
@ -242,7 +242,7 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
} }
size_t newSize = frontierCount + current->childCount - 1; size_t newSize = frontierCount + current->childCount - 1;
if (frontierCount != newSize) { if (frontierCount != newSize) {
frontier = realloc(frontier, sizeof(IdNode*) * newSize); frontier = (IdNode**) realloc(frontier, sizeof(IdNode*) * newSize);
} }
for (i = 0; i < current->childCount; i++) { for (i = 0; i < current->childCount; i++) {
frontier[frontierCount + i - 1] = current->children[i]; frontier[frontierCount + i - 1] = current->children[i];
@ -258,7 +258,7 @@ bool ScopeHasOrdering(IdNode *node) {
switch (node->type) { switch (node->type) {
case OrderedScope: case OrderedScope:
case Function: case Function:
case Variable: // this is only technically true case Variable: /* this is only technically true */
return true; return true;
default: default:
return false; return false;
@ -274,20 +274,22 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
return node; return node;
} }
// If this is the start of our search, we should not attempt to look at child nodes. Only /* 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. * 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 * 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 * 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, * 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. * an identifier has no knowledge of identifiers declared "inside" of it.
*/
if (prev == NULL) { if (prev == NULL) {
return LookupId(node->parent, node, target); return LookupId(node->parent, node, target);
} }
/* 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.
*/
uint32_t idxLimit; 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)) { if (ScopeHasOrdering(node)) {
uint32_t i; uint32_t i;
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) { for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) {
@ -303,13 +305,13 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
for (i = 0; i < idxLimit; i++) { for (i = 0; i < idxLimit; i++) {
IdNode *child = node->children[i]; IdNode *child = node->children[i];
if (child == prev || child->type == Placeholder) { if (child == prev || child->type == Placeholder) {
// Do not inspect the node we just came from or placeholders. /* Do not inspect the node we just came from or placeholders. */
continue; continue;
} }
if (strcmp(child->name, target) == 0) { if (strcmp(child->name, target) == 0) {
return child; return child;
} }
if (child->type == Struct) { if (child->type == Struct) {
uint32_t j; uint32_t j;
@ -317,7 +319,7 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
IdNode *grandchild = child->children[j]; IdNode *grandchild = child->children[j];
if (strcmp(grandchild->name, target) == 0) { if (strcmp(grandchild->name, target) == 0) {
return grandchild; return grandchild;
} }
} }
} }
} }

View File

@ -1,4 +1,5 @@
// Validates identifier usage in an AST. /* Validates identifier usage in an AST. */
#ifndef WRAITH_IDENTCHECK_H #ifndef WRAITH_IDENTCHECK_H
#define WRAITH_IDENTCHECK_H #define WRAITH_IDENTCHECK_H
@ -42,6 +43,4 @@ int PrintAncestors(IdNode *node);
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName); IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName);
IdNode* LookupId(IdNode *node, IdNode *prev, char* target); IdNode* LookupId(IdNode *node, IdNode *prev, char* target);
//IdStatus CheckIds(Node *root);
#endif /* WRAITH_IDENTCHECK_H */ #endif /* WRAITH_IDENTCHECK_H */