Possibly implements full type annotation of the AST

pull/1/head
venko 2021-05-12 21:54:09 -07:00
parent 32541d4794
commit f441e5bede
6 changed files with 106 additions and 90 deletions

11
reftest.w Normal file
View File

@ -0,0 +1,11 @@
struct MyStruct {
foo: int;
}
struct Program {
static Main(): int {
myStruct: Reference<MyStruct>;
myStruct = alloc MyStruct;
return 0;
}
}

View File

@ -97,6 +97,7 @@ Node* MakeIdentifierNode(
node->syntaxKind = Identifier;
node->value.string = strdup(id);
node->childCount = 0;
node->typeTag = NULL;
return node;
}
@ -506,7 +507,12 @@ static void PrintNode(Node *node, int tabCount)
break;
case Identifier:
printf("%s", node->value.string);
if (node->typeTag == NULL) {
printf("%s", node->value.string);
} else {
char *type = TypeTagToString(node->typeTag);
printf("%s<%s>", node->value.string, type);
}
break;
case Number:
@ -561,6 +567,7 @@ TypeTag* MakeTypeTag(Node *node) {
case StructDeclaration:
tag->type = Custom;
tag->value.customType = strdup(node->children[0]->value.string);
printf("Struct tag: %s\n", TypeTagToString(tag));
break;
case FunctionDeclaration:
@ -593,15 +600,9 @@ char* TypeTagToString(TypeTag *tag) {
size_t innerStrLen = strlen(inner);
char *result = malloc(sizeof(char) * (innerStrLen + 5));
sprintf(result, "Ref<%s>", inner);
free(inner);
return result;
}
case Custom:
return tag->value.customType;
}
}
void AddTypeTags(Node *ast)
{
fprintf(stderr, "wraith: AddTypeTags not implemented yet.\n");
}

View File

@ -2,6 +2,7 @@
#define WRAITH_AST_H
#include <stdint.h>
#include "identcheck.h"
typedef enum
{
@ -108,6 +109,7 @@ typedef struct Node
} value;
PrimitiveType primitiveType;
TypeTag *typeTag;
IdNode *idLink;
} Node;
const char* SyntaxKindString(SyntaxKind syntaxKind);
@ -231,9 +233,9 @@ Node* MakeForLoopNode(
);
void PrintTree(Node *node, uint32_t tabCount);
const char* SyntaxKindString(SyntaxKind syntaxKind);
TypeTag* MakeTypeTag(Node *node);
char* TypeTagToString(TypeTag *tag);
void AddTypeTags(Node *ast);
#endif /* WRAITH_AST_H */

View File

@ -36,63 +36,74 @@ void AddChildToNode(IdNode *node, IdNode *child) {
IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
uint32_t i;
IdNode *mainNode;
switch (astNode->syntaxKind) {
case Assignment:
return (astNode->children[0]->syntaxKind == Declaration)
? MakeIdTree(astNode->children[0], parent)
: NULL;
case Assignment: {
if (astNode->children[0]->syntaxKind == Declaration) {
return MakeIdTree(astNode->children[0], parent);
} else {
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent));
}
return NULL;
}
}
case IfStatement: {
mainNode = MakeIdNode(OrderedScope, "if", parent);
Node *clause = astNode->children[0];
Node *stmtSeq = astNode->children[1];
IdNode *ifNode = MakeIdNode(OrderedScope, "if", parent);
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode));
for (i = 0; i < clause->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(clause->children[i], mainNode));
}
return ifNode;
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(mainNode, MakeIdTree(stmtSeq->children[i], mainNode));
}
break;
}
case IfElseStatement: {
Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1];
IdNode *ifElseNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode);
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", ifElseNode);
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
IdNode *elseBranch = MakeIdNode(OrderedScope, "else", mainNode);
AddChildToNode(ifElseNode, ifBranch);
AddChildToNode(mainNode, ifBranch);
for (i = 0; i < elseStmts->childCount; i++) {
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
}
AddChildToNode(ifElseNode, elseBranch);
return ifElseNode;
AddChildToNode(mainNode, elseBranch);
break;
}
case ForLoop: {
Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3];
IdNode *loopNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode));
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode));
AddChildToNode(mainNode, MakeIdTree(loopBody->children[i], mainNode));
}
return loopNode;
break;
}
case Declaration: {
IdNode *declNode = MakeIdNode(Variable, astNode->children[1]->value.string, parent);
declNode->typeTag = MakeTypeTag(astNode);
return declNode;
mainNode = MakeIdNode(Variable, astNode->children[1]->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[1]->typeTag = mainNode->typeTag;
break;
}
case StructDeclaration: {
Node *idNode = astNode->children[0];
Node *declsNode = astNode->children[1];
IdNode *structNode = MakeIdNode(Struct, idNode->value.string, parent);
structNode->typeTag = MakeTypeTag(astNode);
mainNode = MakeIdNode(Struct, idNode->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->childCount; i++) {
AddChildToNode(structNode, MakeIdTree(declsNode->children[i], structNode));
AddChildToNode(mainNode, MakeIdTree(declsNode->children[i], mainNode));
}
return structNode;
break;
}
case FunctionDeclaration: {
@ -100,28 +111,50 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
Node *funcNameNode = sigNode->children[0];
Node *funcArgsNode = sigNode->children[2];
Node *bodyStatementsNode = astNode->children[1];
IdNode *funcNode = MakeIdNode(Function, funcNameNode->value.string, parent);
funcNode->typeTag = MakeTypeTag(astNode);
mainNode = MakeIdNode(Function, funcNameNode->value.string, parent);
mainNode->typeTag = MakeTypeTag(astNode);
astNode->children[0]->children[0]->typeTag = mainNode->typeTag;
for (i = 0; i < funcArgsNode->childCount; i++) {
AddChildToNode(funcNode, MakeIdTree(funcArgsNode->children[i], funcNode));
AddChildToNode(mainNode, MakeIdTree(funcArgsNode->children[i], mainNode));
}
for (i = 0; i < bodyStatementsNode->childCount; i++) {
AddChildToNode(funcNode, MakeIdTree(bodyStatementsNode->children[i], funcNode));
AddChildToNode(mainNode, MakeIdTree(bodyStatementsNode->children[i], mainNode));
}
return funcNode;
break;
}
case DeclarationSequence: {
IdNode *declSeqNode = MakeIdNode(UnorderedScope, "", parent);
mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode));
AddChildToNode(mainNode, MakeIdTree(astNode->children[i], mainNode));
}
return declSeqNode;
break;
}
default:
case Identifier: {
mainNode = MakeIdNode(Placeholder, astNode->value.string, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, astNode->value.string);
if (lookupNode == NULL) {
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", astNode->value.string);
TypeTag *tag = (TypeTag*)malloc(sizeof(TypeTag));
tag->type = Unknown;
astNode->typeTag = tag;
} else {
astNode->typeTag = lookupNode->typeTag;
}
break;
}
default: {
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(parent, MakeIdTree(astNode->children[i], parent));
}
return NULL;
}
}
astNode->idLink = mainNode;
return mainNode;
}
void PrintIdNode(IdNode *node) {
@ -131,6 +164,9 @@ void PrintIdNode(IdNode *node) {
}
switch(node->type) {
case Placeholder:
printf("Placeholder (%s)\n", node->name);
break;
case OrderedScope:
printf("OrderedScope (%s)\n", node->name);
break;
@ -234,7 +270,7 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
return NULL;
}
if (strcmp(node->name, target) == 0) {
if (strcmp(node->name, target) == 0 && node->type != Placeholder) {
return node;
}
@ -266,8 +302,8 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
uint32_t i;
for (i = 0; i < idxLimit; i++) {
IdNode *child = node->children[i];
if (child == prev) {
// Do not inspect the node we just came from.
if (child == prev || child->type == Placeholder) {
// Do not inspect the node we just came from or placeholders.
continue;
}
@ -287,4 +323,4 @@ IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
}
return LookupId(node->parent, node, target);
}
}

View File

@ -6,7 +6,11 @@
#include "ast.h"
struct TypeTag;
struct Node;
typedef enum NodeType {
Placeholder,
UnorderedScope,
OrderedScope,
Struct,
@ -17,7 +21,7 @@ typedef enum NodeType {
typedef struct IdNode {
NodeType type;
char *name;
TypeTag *typeTag;
struct TypeTag *typeTag;
struct IdNode *parent;
struct IdNode **children;
uint32_t childCount;
@ -31,7 +35,7 @@ typedef struct IdStatus {
} IdStatus;
IdNode* MakeIdTree(Node *astNode, IdNode *parent);
IdNode* MakeIdTree(struct Node *astNode, IdNode *parent);
void PrintIdNode(IdNode *node);
void PrintIdTree(IdNode *tree, uint32_t tabCount);
int PrintAncestors(IdNode *node);

View File

@ -65,49 +65,11 @@ int main(int argc, char *argv[])
}
else
{
{ // This shit only works if you're using iftest.w
{
IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0);
printf("\nSeeking to struct 'Program'\n");
IdNode *node = LookdownId(idTree, Struct, "Program");
if (node == NULL) printf("Failed.\n");
printf("\n[attempting to look up MyStruct]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "MyStruct"));
printf("\n[attempting to look up MyFunc]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "MyFunc"));
printf("\n[attempting to look up myStructInt]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "myStructInt"));
printf("\n[attempting to look up Program]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "Program"));
printf("\n[attempting to look up Foo]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "Foo"));
printf("\n[attempting to look up Main]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "Main"));
printf("\n[attempting to look up myInt]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "myInt"));
printf("\n[attempting to look up signTag]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "signTag"));
printf("\n[attempting to look up myBool]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "myBool"));
printf("\n[attempting to look up lol]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "lol"));
printf("\n[attempting to look up someInt]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "someInt"));
printf("\n[attempting to look up Bar]: \n");
PrintIdNode(LookupId(node, /*prev=*/NULL, "Bar"));
printf("\n");
PrintTree(rootNode, /*tabCount=*/0);
}
exitCode = Codegen(rootNode, optimizationLevel);
}