Applies clang-format
parent
7d5f599712
commit
7f2ca56b73
167
src/ast.c
167
src/ast.c
|
@ -639,7 +639,8 @@ void PrintNode(Node *node, uint32_t tabCount)
|
|||
|
||||
case GenericArguments:
|
||||
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);
|
||||
}
|
||||
return;
|
||||
|
@ -763,7 +764,8 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
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]);
|
||||
}
|
||||
return;
|
||||
|
@ -776,7 +778,8 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
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]);
|
||||
}
|
||||
return;
|
||||
|
@ -792,7 +795,8 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
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]);
|
||||
}
|
||||
return;
|
||||
|
@ -806,7 +810,8 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
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]);
|
||||
}
|
||||
return;
|
||||
|
@ -817,7 +822,8 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
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]);
|
||||
}
|
||||
return;
|
||||
|
@ -856,7 +862,8 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
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]);
|
||||
}
|
||||
return;
|
||||
|
@ -880,7 +887,9 @@ void Recurse(Node *node, void (*func)(Node*))
|
|||
return;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Unhandled SyntaxKind %s in recurse function.\n",
|
||||
SyntaxKindString(node->syntaxKind));
|
||||
return;
|
||||
}
|
||||
|
@ -939,8 +948,8 @@ TypeTag *MakeTypeTag(Node *node)
|
|||
|
||||
case GenericArgument:
|
||||
tag->type = Generic;
|
||||
tag->value.genericType = strdup(node->genericArgument.identifier
|
||||
->identifier.name);
|
||||
tag->value.genericType =
|
||||
strdup(node->genericArgument.identifier->identifier.name);
|
||||
break;
|
||||
|
||||
case GenericTypeNode:
|
||||
|
@ -986,13 +995,15 @@ char *TypeTagToString(TypeTag *tag)
|
|||
}
|
||||
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);
|
||||
return result;
|
||||
}
|
||||
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);
|
||||
return result;
|
||||
}
|
||||
|
@ -1001,7 +1012,8 @@ char *TypeTagToString(TypeTag *tag)
|
|||
|
||||
void LinkParentPointers(Node *node, Node *prev)
|
||||
{
|
||||
if (node == NULL) return;
|
||||
if (node == NULL)
|
||||
return;
|
||||
|
||||
node->parent = prev;
|
||||
|
||||
|
@ -1039,7 +1051,8 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
return;
|
||||
|
@ -1052,8 +1065,11 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
case FunctionArgumentSequence:
|
||||
for (i = 0; i < node->functionArgumentSequence.count; i += 1) {
|
||||
LinkParentPointers(node->functionArgumentSequence.sequence[i], node);
|
||||
for (i = 0; i < node->functionArgumentSequence.count; i += 1)
|
||||
{
|
||||
LinkParentPointers(
|
||||
node->functionArgumentSequence.sequence[i],
|
||||
node);
|
||||
}
|
||||
return;
|
||||
|
||||
|
@ -1068,7 +1084,8 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
return;
|
||||
|
@ -1082,8 +1099,11 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
case FunctionSignatureArguments:
|
||||
for (i = 0; i < node->functionSignatureArguments.count; i += 1) {
|
||||
LinkParentPointers(node->functionSignatureArguments.sequence[i], node);
|
||||
for (i = 0; i < node->functionSignatureArguments.count; i += 1)
|
||||
{
|
||||
LinkParentPointers(
|
||||
node->functionSignatureArguments.sequence[i],
|
||||
node);
|
||||
}
|
||||
return;
|
||||
|
||||
|
@ -1093,7 +1113,8 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
return;
|
||||
|
@ -1132,7 +1153,8 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
return;
|
||||
|
@ -1156,7 +1178,9 @@ void LinkParentPointers(Node *node, Node *prev)
|
|||
return;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Unhandled SyntaxKind %s in recurse function.\n",
|
||||
SyntaxKindString(node->syntaxKind));
|
||||
return;
|
||||
}
|
||||
|
@ -1166,7 +1190,10 @@ Node *GetIdFromStruct(Node *structDecl)
|
|||
{
|
||||
if (structDecl->syntaxKind != StructDeclaration)
|
||||
{
|
||||
fprintf(stderr, "wraith: Attempted to call GetIdFromStruct on node with kind: %s.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call GetIdFromStruct on node with kind: "
|
||||
"%s.\n",
|
||||
SyntaxKindString(structDecl->syntaxKind));
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1178,7 +1205,10 @@ Node *GetIdFromFunction(Node *funcDecl)
|
|||
{
|
||||
if (funcDecl->syntaxKind != FunctionDeclaration)
|
||||
{
|
||||
fprintf(stderr, "wraith: Attempted to call GetIdFromFunction on node with kind: %s.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call GetIdFromFunction on node with kind: "
|
||||
"%s.\n",
|
||||
SyntaxKindString(funcDecl->syntaxKind));
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1191,7 +1221,10 @@ Node *GetIdFromDeclaration(Node *decl)
|
|||
{
|
||||
if (decl->syntaxKind != Declaration)
|
||||
{
|
||||
fprintf(stderr, "wraith: Attempted to call GetIdFromDeclaration on node with kind: %s.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call GetIdFromDeclaration on node with kind: "
|
||||
"%s.\n",
|
||||
SyntaxKindString(decl->syntaxKind));
|
||||
}
|
||||
|
||||
|
@ -1200,15 +1233,19 @@ Node *GetIdFromDeclaration(Node *decl)
|
|||
|
||||
bool AssignmentHasDeclaration(Node *assign)
|
||||
{
|
||||
return (assign->syntaxKind == Assignment
|
||||
&& assign->assignmentStatement.left->syntaxKind == Declaration);
|
||||
return (
|
||||
assign->syntaxKind == Assignment &&
|
||||
assign->assignmentStatement.left->syntaxKind == Declaration);
|
||||
}
|
||||
|
||||
Node *GetIdFromAssignment(Node *assign)
|
||||
{
|
||||
if (assign->syntaxKind != Assignment)
|
||||
{
|
||||
fprintf(stderr, "wraith: Attempted to call GetIdFromAssignment on node with kind: %s.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call GetIdFromAssignment on node with kind: "
|
||||
"%s.\n",
|
||||
SyntaxKindString(assign->syntaxKind));
|
||||
}
|
||||
|
||||
|
@ -1253,8 +1290,8 @@ Node *TryGetId(Node *node)
|
|||
|
||||
Node *LookupFunctionArgId(Node *funcDecl, char *target)
|
||||
{
|
||||
Node *args =
|
||||
funcDecl->functionDeclaration.functionSignature->functionSignature.arguments;
|
||||
Node *args = funcDecl->functionDeclaration.functionSignature
|
||||
->functionSignature.arguments;
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i < args->functionArgumentSequence.count; i += 1)
|
||||
|
@ -1262,8 +1299,10 @@ Node *LookupFunctionArgId(Node *funcDecl, char *target)
|
|||
Node *arg = args->functionArgumentSequence.sequence[i];
|
||||
if (arg->syntaxKind != Declaration)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"wraith: Encountered %s node in function signature args list.\n",
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Encountered %s node in function signature args "
|
||||
"list.\n",
|
||||
SyntaxKindString(arg->syntaxKind));
|
||||
continue;
|
||||
}
|
||||
|
@ -1295,32 +1334,37 @@ Node *LookupStructInternalId(Node *structDecl, 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. */
|
||||
if (NodeMayHaveId(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;
|
||||
}
|
||||
}
|
||||
|
||||
/* If the candidate node was not the one we wanted, but the node node is a function
|
||||
* declaration, it's possible that the identifier we want is one of the function's
|
||||
* parameters rather than the function's name itself. */
|
||||
/* If the candidate node was not the one we wanted, but the node node is a
|
||||
* function declaration, it's possible that the identifier we want is one of
|
||||
* the function's parameters rather than the function's name itself. */
|
||||
if (node->syntaxKind == FunctionDeclaration)
|
||||
{
|
||||
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. */
|
||||
if (node->syntaxKind == StructDeclaration)
|
||||
{
|
||||
Node *match = LookupStructInternalId(node, target);
|
||||
if (match != NULL) return match;
|
||||
if (match != NULL)
|
||||
return match;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
@ -1328,12 +1372,15 @@ Node *InspectNode(Node *node, char *target)
|
|||
|
||||
Node *LookupIdNode(Node *current, Node *prev, char *target)
|
||||
{
|
||||
if (current == NULL) return NULL;
|
||||
if (current == NULL)
|
||||
return NULL;
|
||||
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);
|
||||
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
|
||||
* 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];
|
||||
match = InspectNode(decl, target);
|
||||
if (match != NULL) return match;
|
||||
if (match != NULL)
|
||||
return match;
|
||||
/*Node *declId = TryGetId(decl);
|
||||
if (declId != NULL && strcmp(target, declId->identifier.name) == 0)
|
||||
return declId;*/
|
||||
|
@ -1375,19 +1423,22 @@ Node *LookupIdNode(Node *current, Node *prev, char *target)
|
|||
for (i = 0; i < idxLimit; i += 1)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
match = InspectNode(stmt, target);
|
||||
if (match != NULL) return match;
|
||||
if (match != NULL)
|
||||
return match;
|
||||
/*if (NodeMayHaveId(stmt))
|
||||
{
|
||||
Node *candidateId = TryGetId(current);
|
||||
if (candidateId != NULL && strcmp(target, candidateId->identifier.name) == 0)
|
||||
return candidateId;
|
||||
if (candidateId != NULL && strcmp(target,
|
||||
candidateId->identifier.name) == 0) return candidateId;
|
||||
}*/
|
||||
}
|
||||
break;
|
||||
|
@ -1398,7 +1449,8 @@ Node *LookupIdNode(Node *current, Node *prev, char *target)
|
|||
|
||||
void IdentifierPass(Node *node)
|
||||
{
|
||||
if (node == NULL) return;
|
||||
if (node == NULL)
|
||||
return;
|
||||
|
||||
switch (node->syntaxKind)
|
||||
{
|
||||
|
@ -1411,9 +1463,10 @@ void IdentifierPass(Node *node)
|
|||
break;
|
||||
|
||||
case FunctionDeclaration:
|
||||
node->functionDeclaration.functionSignature
|
||||
->functionSignature.identifier->typeTag = MakeTypeTag(node);
|
||||
break;;
|
||||
node->functionDeclaration.functionSignature->functionSignature
|
||||
.identifier->typeTag = MakeTypeTag(node);
|
||||
break;
|
||||
;
|
||||
|
||||
case StructDeclaration:
|
||||
node->structDeclaration.identifier->typeTag = MakeTypeTag(node);
|
||||
|
@ -1425,14 +1478,18 @@ void IdentifierPass(Node *node)
|
|||
|
||||
case Identifier:
|
||||
{
|
||||
if (node->typeTag != NULL) return;
|
||||
if (node->typeTag != NULL)
|
||||
return;
|
||||
|
||||
char *name = node->identifier.name;
|
||||
Node *declaration = LookupIdNode(node, NULL, name);
|
||||
if (declaration == NULL)
|
||||
{
|
||||
/* 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));
|
||||
tag->type = Unknown;
|
||||
node->typeTag = tag;
|
||||
|
|
|
@ -367,10 +367,11 @@ Node *MakeForLoopNode(
|
|||
void PrintNode(Node *node, uint32_t tabCount);
|
||||
const char *SyntaxKindString(SyntaxKind syntaxKind);
|
||||
|
||||
/* Helper function for applying a void function generically over the children of an AST node.
|
||||
* Used for functions that need to traverse the entire tree but only perform operations on a subset
|
||||
* of node types. Such functions can match the syntaxKinds relevant to their purpose and invoke this
|
||||
* function in all other cases. */
|
||||
/* Helper function for applying a void function generically over the children of
|
||||
* an AST node. Used for functions that need to traverse the entire tree but
|
||||
* only perform operations on a subset of node types. Such functions can match
|
||||
* the syntaxKinds relevant to their purpose and invoke this function in all
|
||||
* other cases. */
|
||||
void Recurse(Node *node, void (*func)(Node *));
|
||||
|
||||
void LinkParentPointers(Node *node, Node *prev);
|
||||
|
|
Loading…
Reference in New Issue