Applies clang-format

pull/6/head
venko 2021-05-30 13:07:12 -07:00
parent 7d5f599712
commit 7f2ca56b73
2 changed files with 128 additions and 70 deletions

183
src/ast.c
View File

@ -639,7 +639,8 @@ void PrintNode(Node *node, uint32_t tabCount)
case GenericArguments: case GenericArguments:
printf("\n"); printf("\n");
for (i = 0; i < node->genericArguments.count; i += 1) { for (i = 0; i < node->genericArguments.count; i += 1)
{
PrintNode(node->genericArguments.arguments[i], tabCount + 1); PrintNode(node->genericArguments.arguments[i], tabCount + 1);
} }
return; return;
@ -727,7 +728,7 @@ void PrintNode(Node *node, uint32_t tabCount)
} }
} }
void Recurse(Node *node, void (*func)(Node*)) void Recurse(Node *node, void (*func)(Node *))
{ {
uint32_t i; uint32_t i;
switch (node->syntaxKind) switch (node->syntaxKind)
@ -763,7 +764,8 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
case DeclarationSequence: case DeclarationSequence:
for (i = 0; i < node->declarationSequence.count; i += 1) { for (i = 0; i < node->declarationSequence.count; i += 1)
{
func(node->declarationSequence.sequence[i]); func(node->declarationSequence.sequence[i]);
} }
return; return;
@ -776,7 +778,8 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
case FunctionArgumentSequence: case FunctionArgumentSequence:
for (i = 0; i < node->functionArgumentSequence.count; i += 1) { for (i = 0; i < node->functionArgumentSequence.count; i += 1)
{
func(node->functionArgumentSequence.sequence[i]); func(node->functionArgumentSequence.sequence[i]);
} }
return; return;
@ -792,7 +795,8 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
case FunctionModifiers: case FunctionModifiers:
for (i = 0; i < node->functionModifiers.count; i += 1) { for (i = 0; i < node->functionModifiers.count; i += 1)
{
func(node->functionModifiers.sequence[i]); func(node->functionModifiers.sequence[i]);
} }
return; return;
@ -806,7 +810,8 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
case FunctionSignatureArguments: case FunctionSignatureArguments:
for (i = 0; i < node->functionSignatureArguments.count; i += 1) { for (i = 0; i < node->functionSignatureArguments.count; i += 1)
{
func(node->functionSignatureArguments.sequence[i]); func(node->functionSignatureArguments.sequence[i]);
} }
return; return;
@ -817,7 +822,8 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
case GenericArguments: case GenericArguments:
for (i = 0; i < node->genericArguments.count; i += 1) { for (i = 0; i < node->genericArguments.count; i += 1)
{
func(node->genericArguments.arguments[i]); func(node->genericArguments.arguments[i]);
} }
return; return;
@ -856,7 +862,8 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
case StatementSequence: case StatementSequence:
for (i = 0; i < node->statementSequence.count; i += 1) { for (i = 0; i < node->statementSequence.count; i += 1)
{
func(node->statementSequence.sequence[i]); func(node->statementSequence.sequence[i]);
} }
return; return;
@ -880,8 +887,10 @@ void Recurse(Node *node, void (*func)(Node*))
return; return;
default: default:
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n", fprintf(
SyntaxKindString(node->syntaxKind)); stderr,
"wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
return; return;
} }
} }
@ -939,8 +948,8 @@ TypeTag *MakeTypeTag(Node *node)
case GenericArgument: case GenericArgument:
tag->type = Generic; tag->type = Generic;
tag->value.genericType = strdup(node->genericArgument.identifier tag->value.genericType =
->identifier.name); strdup(node->genericArgument.identifier->identifier.name);
break; break;
case GenericTypeNode: case GenericTypeNode:
@ -986,13 +995,15 @@ char *TypeTagToString(TypeTag *tag)
} }
case Custom: case Custom:
{ {
char *result = malloc(sizeof(char) * (strlen(tag->value.customType) + 8)); char *result =
malloc(sizeof(char) * (strlen(tag->value.customType) + 8));
sprintf(result, "Custom<%s>", tag->value.customType); sprintf(result, "Custom<%s>", tag->value.customType);
return result; return result;
} }
case Generic: case Generic:
{ {
char *result = malloc(sizeof(char) * (strlen(tag->value.customType) + 9)); char *result =
malloc(sizeof(char) * (strlen(tag->value.customType) + 9));
sprintf(result, "Generic<%s>", tag->value.customType); sprintf(result, "Generic<%s>", tag->value.customType);
return result; return result;
} }
@ -1001,7 +1012,8 @@ char *TypeTagToString(TypeTag *tag)
void LinkParentPointers(Node *node, Node *prev) void LinkParentPointers(Node *node, Node *prev)
{ {
if (node == NULL) return; if (node == NULL)
return;
node->parent = prev; node->parent = prev;
@ -1039,7 +1051,8 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
case DeclarationSequence: case DeclarationSequence:
for (i = 0; i < node->declarationSequence.count; i += 1) { for (i = 0; i < node->declarationSequence.count; i += 1)
{
LinkParentPointers(node->declarationSequence.sequence[i], node); LinkParentPointers(node->declarationSequence.sequence[i], node);
} }
return; return;
@ -1052,8 +1065,11 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
case FunctionArgumentSequence: case FunctionArgumentSequence:
for (i = 0; i < node->functionArgumentSequence.count; i += 1) { for (i = 0; i < node->functionArgumentSequence.count; i += 1)
LinkParentPointers(node->functionArgumentSequence.sequence[i], node); {
LinkParentPointers(
node->functionArgumentSequence.sequence[i],
node);
} }
return; return;
@ -1068,7 +1084,8 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
case FunctionModifiers: case FunctionModifiers:
for (i = 0; i < node->functionModifiers.count; i += 1) { for (i = 0; i < node->functionModifiers.count; i += 1)
{
LinkParentPointers(node->functionModifiers.sequence[i], node); LinkParentPointers(node->functionModifiers.sequence[i], node);
} }
return; return;
@ -1082,8 +1099,11 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
case FunctionSignatureArguments: case FunctionSignatureArguments:
for (i = 0; i < node->functionSignatureArguments.count; i += 1) { for (i = 0; i < node->functionSignatureArguments.count; i += 1)
LinkParentPointers(node->functionSignatureArguments.sequence[i], node); {
LinkParentPointers(
node->functionSignatureArguments.sequence[i],
node);
} }
return; return;
@ -1093,7 +1113,8 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
case GenericArguments: case GenericArguments:
for (i = 0; i < node->genericArguments.count; i += 1) { for (i = 0; i < node->genericArguments.count; i += 1)
{
LinkParentPointers(node->genericArguments.arguments[i], node); LinkParentPointers(node->genericArguments.arguments[i], node);
} }
return; return;
@ -1132,7 +1153,8 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
case StatementSequence: case StatementSequence:
for (i = 0; i < node->statementSequence.count; i += 1) { for (i = 0; i < node->statementSequence.count; i += 1)
{
LinkParentPointers(node->statementSequence.sequence[i], node); LinkParentPointers(node->statementSequence.sequence[i], node);
} }
return; return;
@ -1156,8 +1178,10 @@ void LinkParentPointers(Node *node, Node *prev)
return; return;
default: default:
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n", fprintf(
SyntaxKindString(node->syntaxKind)); stderr,
"wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
return; return;
} }
} }
@ -1166,8 +1190,11 @@ Node *GetIdFromStruct(Node *structDecl)
{ {
if (structDecl->syntaxKind != StructDeclaration) if (structDecl->syntaxKind != StructDeclaration)
{ {
fprintf(stderr, "wraith: Attempted to call GetIdFromStruct on node with kind: %s.\n", fprintf(
SyntaxKindString(structDecl->syntaxKind)); stderr,
"wraith: Attempted to call GetIdFromStruct on node with kind: "
"%s.\n",
SyntaxKindString(structDecl->syntaxKind));
return NULL; return NULL;
} }
@ -1178,8 +1205,11 @@ Node *GetIdFromFunction(Node *funcDecl)
{ {
if (funcDecl->syntaxKind != FunctionDeclaration) if (funcDecl->syntaxKind != FunctionDeclaration)
{ {
fprintf(stderr, "wraith: Attempted to call GetIdFromFunction on node with kind: %s.\n", fprintf(
SyntaxKindString(funcDecl->syntaxKind)); stderr,
"wraith: Attempted to call GetIdFromFunction on node with kind: "
"%s.\n",
SyntaxKindString(funcDecl->syntaxKind));
return NULL; return NULL;
} }
@ -1191,8 +1221,11 @@ Node *GetIdFromDeclaration(Node *decl)
{ {
if (decl->syntaxKind != Declaration) if (decl->syntaxKind != Declaration)
{ {
fprintf(stderr, "wraith: Attempted to call GetIdFromDeclaration on node with kind: %s.\n", fprintf(
SyntaxKindString(decl->syntaxKind)); stderr,
"wraith: Attempted to call GetIdFromDeclaration on node with kind: "
"%s.\n",
SyntaxKindString(decl->syntaxKind));
} }
return decl->declaration.identifier; return decl->declaration.identifier;
@ -1200,16 +1233,20 @@ Node *GetIdFromDeclaration(Node *decl)
bool AssignmentHasDeclaration(Node *assign) bool AssignmentHasDeclaration(Node *assign)
{ {
return (assign->syntaxKind == Assignment return (
&& assign->assignmentStatement.left->syntaxKind == Declaration); assign->syntaxKind == Assignment &&
assign->assignmentStatement.left->syntaxKind == Declaration);
} }
Node *GetIdFromAssignment(Node *assign) Node *GetIdFromAssignment(Node *assign)
{ {
if (assign->syntaxKind != Assignment) if (assign->syntaxKind != Assignment)
{ {
fprintf(stderr, "wraith: Attempted to call GetIdFromAssignment on node with kind: %s.\n", fprintf(
SyntaxKindString(assign->syntaxKind)); stderr,
"wraith: Attempted to call GetIdFromAssignment on node with kind: "
"%s.\n",
SyntaxKindString(assign->syntaxKind));
} }
if (AssignmentHasDeclaration(assign)) if (AssignmentHasDeclaration(assign))
@ -1253,8 +1290,8 @@ Node *TryGetId(Node *node)
Node *LookupFunctionArgId(Node *funcDecl, char *target) Node *LookupFunctionArgId(Node *funcDecl, char *target)
{ {
Node *args = Node *args = funcDecl->functionDeclaration.functionSignature
funcDecl->functionDeclaration.functionSignature->functionSignature.arguments; ->functionSignature.arguments;
uint32_t i; uint32_t i;
for (i = 0; i < args->functionArgumentSequence.count; i += 1) for (i = 0; i < args->functionArgumentSequence.count; i += 1)
@ -1262,9 +1299,11 @@ Node *LookupFunctionArgId(Node *funcDecl, char *target)
Node *arg = args->functionArgumentSequence.sequence[i]; Node *arg = args->functionArgumentSequence.sequence[i];
if (arg->syntaxKind != Declaration) if (arg->syntaxKind != Declaration)
{ {
fprintf(stderr, fprintf(
"wraith: Encountered %s node in function signature args list.\n", stderr,
SyntaxKindString(arg->syntaxKind)); "wraith: Encountered %s node in function signature args "
"list.\n",
SyntaxKindString(arg->syntaxKind));
continue; continue;
} }
@ -1295,32 +1334,37 @@ Node *LookupStructInternalId(Node *structDecl, char *target)
Node *InspectNode(Node *node, char *target) Node *InspectNode(Node *node, char *target)
{ {
/* If this node may have an identifier declaration inside it, attempt to look up the identifier /* If this node may have an identifier declaration inside it, attempt to
* look up the identifier
* node itself, returning it if it matches the given target name. */ * node itself, returning it if it matches the given target name. */
if (NodeMayHaveId(node)) if (NodeMayHaveId(node))
{ {
Node *candidateId = TryGetId(node); Node *candidateId = TryGetId(node);
if (candidateId != NULL && strcmp(target, candidateId->identifier.name) == 0) if (candidateId != NULL &&
strcmp(target, candidateId->identifier.name) == 0)
{ {
return candidateId; return candidateId;
} }
} }
/* If the candidate node was not the one we wanted, but the node node is a function /* If the candidate node was not the one we wanted, but the node node is a
* declaration, it's possible that the identifier we want is one of the function's * function declaration, it's possible that the identifier we want is one of
* parameters rather than the function's name itself. */ * the function's parameters rather than the function's name itself. */
if (node->syntaxKind == FunctionDeclaration) if (node->syntaxKind == FunctionDeclaration)
{ {
Node *match = LookupFunctionArgId(node, target); Node *match = LookupFunctionArgId(node, target);
if (match != NULL) return match; if (match != NULL)
return match;
} }
/* Likewise if the node node is a struct declaration, inspect the struct's internals /* Likewise if the node node is a struct declaration, inspect the struct's
* internals
* to see if a top-level definition is the one we're looking for. */ * to see if a top-level definition is the one we're looking for. */
if (node->syntaxKind == StructDeclaration) if (node->syntaxKind == StructDeclaration)
{ {
Node *match = LookupStructInternalId(node, target); Node *match = LookupStructInternalId(node, target);
if (match != NULL) return match; if (match != NULL)
return match;
} }
return NULL; return NULL;
@ -1328,12 +1372,15 @@ Node *InspectNode(Node *node, char *target)
Node *LookupIdNode(Node *current, Node *prev, char *target) Node *LookupIdNode(Node *current, Node *prev, char *target)
{ {
if (current == NULL) return NULL; if (current == NULL)
return NULL;
Node *match; Node *match;
/* First inspect the current node to see if it contains the target identifier. */ /* First inspect the current node to see if it contains the target
* identifier. */
match = InspectNode(current, target); match = InspectNode(current, target);
if (match != NULL) return match; if (match != NULL)
return match;
/* If this is the start of our search, we should not attempt to look at /* If this is the start of our search, we should not attempt to look at
* child nodes. Only looking up the AST is valid at this point. * child nodes. Only looking up the AST is valid at this point.
@ -1355,7 +1402,8 @@ Node *LookupIdNode(Node *current, Node *prev, char *target)
{ {
Node *decl = current->declarationSequence.sequence[i]; Node *decl = current->declarationSequence.sequence[i];
match = InspectNode(decl, target); match = InspectNode(decl, target);
if (match != NULL) return match; if (match != NULL)
return match;
/*Node *declId = TryGetId(decl); /*Node *declId = TryGetId(decl);
if (declId != NULL && strcmp(target, declId->identifier.name) == 0) if (declId != NULL && strcmp(target, declId->identifier.name) == 0)
return declId;*/ return declId;*/
@ -1375,19 +1423,22 @@ Node *LookupIdNode(Node *current, Node *prev, char *target)
for (i = 0; i < idxLimit; i += 1) for (i = 0; i < idxLimit; i += 1)
{ {
Node *stmt = current->statementSequence.sequence[i]; Node *stmt = current->statementSequence.sequence[i];
if (stmt == prev) continue; if (stmt == prev)
continue;
if (strcmp(target, "g") == 0) { if (strcmp(target, "g") == 0)
{
printf("info: %s\n", SyntaxKindString(stmt->syntaxKind)); printf("info: %s\n", SyntaxKindString(stmt->syntaxKind));
} }
match = InspectNode(stmt, target); match = InspectNode(stmt, target);
if (match != NULL) return match; if (match != NULL)
return match;
/*if (NodeMayHaveId(stmt)) /*if (NodeMayHaveId(stmt))
{ {
Node *candidateId = TryGetId(current); Node *candidateId = TryGetId(current);
if (candidateId != NULL && strcmp(target, candidateId->identifier.name) == 0) if (candidateId != NULL && strcmp(target,
return candidateId; candidateId->identifier.name) == 0) return candidateId;
}*/ }*/
} }
break; break;
@ -1398,7 +1449,8 @@ Node *LookupIdNode(Node *current, Node *prev, char *target)
void IdentifierPass(Node *node) void IdentifierPass(Node *node)
{ {
if (node == NULL) return; if (node == NULL)
return;
switch (node->syntaxKind) switch (node->syntaxKind)
{ {
@ -1411,9 +1463,10 @@ void IdentifierPass(Node *node)
break; break;
case FunctionDeclaration: case FunctionDeclaration:
node->functionDeclaration.functionSignature node->functionDeclaration.functionSignature->functionSignature
->functionSignature.identifier->typeTag = MakeTypeTag(node); .identifier->typeTag = MakeTypeTag(node);
break;; break;
;
case StructDeclaration: case StructDeclaration:
node->structDeclaration.identifier->typeTag = MakeTypeTag(node); node->structDeclaration.identifier->typeTag = MakeTypeTag(node);
@ -1425,14 +1478,18 @@ void IdentifierPass(Node *node)
case Identifier: case Identifier:
{ {
if (node->typeTag != NULL) return; if (node->typeTag != NULL)
return;
char *name = node->identifier.name; char *name = node->identifier.name;
Node *declaration = LookupIdNode(node, NULL, name); Node *declaration = LookupIdNode(node, NULL, name);
if (declaration == NULL) if (declaration == NULL)
{ {
/* FIXME: Express this case as an error with AST information. */ /* FIXME: Express this case as an error with AST information. */
fprintf(stderr, "wraith: Could not find definition of identifier %s.\n", name); fprintf(
stderr,
"wraith: Could not find definition of identifier %s.\n",
name);
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag)); TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
tag->type = Unknown; tag->type = Unknown;
node->typeTag = tag; node->typeTag = tag;

View File

@ -367,11 +367,12 @@ Node *MakeForLoopNode(
void PrintNode(Node *node, uint32_t tabCount); void PrintNode(Node *node, uint32_t tabCount);
const char *SyntaxKindString(SyntaxKind syntaxKind); const char *SyntaxKindString(SyntaxKind syntaxKind);
/* Helper function for applying a void function generically over the children of an AST node. /* Helper function for applying a void function generically over the children of
* Used for functions that need to traverse the entire tree but only perform operations on a subset * an AST node. Used for functions that need to traverse the entire tree but
* of node types. Such functions can match the syntaxKinds relevant to their purpose and invoke this * only perform operations on a subset of node types. Such functions can match
* function in all other cases. */ * the syntaxKinds relevant to their purpose and invoke this function in all
void Recurse(Node *node, void (*func)(Node*)); * other cases. */
void Recurse(Node *node, void (*func)(Node *));
void LinkParentPointers(Node *node, Node *prev); void LinkParentPointers(Node *node, Node *prev);