some minor style revisions

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

View File

@ -24,10 +24,10 @@ void AddChildToNode(IdNode *node, IdNode *child) {
if (node->children == NULL) {
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) {
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;
@ -167,20 +167,20 @@ void PrintIdNode(IdNode *node) {
case Placeholder:
printf("Placeholder (%s)\n", node->name);
break;
case OrderedScope:
printf("OrderedScope (%s)\n", node->name);
case OrderedScope:
printf("OrderedScope (%s)\n", node->name);
break;
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
break;
case Struct:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
break;
case Function:
printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag));
break;
case Variable:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
case Variable:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
break;
}
}
@ -242,7 +242,7 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
}
size_t newSize = frontierCount + current->childCount - 1;
if (frontierCount != newSize) {
frontier = realloc(frontier, sizeof(IdNode*) * newSize);
frontier = (IdNode**) realloc(frontier, sizeof(IdNode*) * newSize);
}
for (i = 0; i < current->childCount; i++) {
frontier[frontierCount + i - 1] = current->children[i];
@ -258,7 +258,7 @@ bool ScopeHasOrdering(IdNode *node) {
switch (node->type) {
case OrderedScope:
case Function:
case Variable: // this is only technically true
case Variable: /* this is only technically true */
return true;
default:
return false;
@ -274,20 +274,22 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
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 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);
}
/* 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;
// 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++) {
@ -303,13 +305,13 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
for (i = 0; i < idxLimit; i++) {
IdNode *child = node->children[i];
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;
}
if (strcmp(child->name, target) == 0) {
return child;
}
}
if (child->type == Struct) {
uint32_t j;
@ -317,7 +319,7 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
IdNode *grandchild = child->children[j];
if (strcmp(grandchild->name, target) == 0) {
return grandchild;
}
}
}
}
}

View File

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