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

@ -83,11 +83,11 @@ typedef struct TypeTag
} 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;

View File

@ -27,7 +27,7 @@ void AddChildToNode(IdNode *node, IdNode *child) {
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;
@ -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,7 +305,7 @@ 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;
} }

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 */