Compare commits

..

9 Commits

18 changed files with 1137 additions and 2459 deletions

View File

@ -41,13 +41,15 @@ add_executable(
# Source
src/ast.h
src/codegen.h
src/identcheck.h
src/parser.h
src/validation.h
src/typeutils.h
src/util.h
src/ast.c
src/codegen.c
src/identcheck.c
src/parser.c
src/validation.c
src/typeutils.c
src/util.c
src/main.c
# Generated code

View File

@ -1,13 +0,0 @@
struct G {
Foo(t: bool): bool {
return t;
}
}
struct Program {
static main(): int {
g: G = alloc G;
g.Foo(true);
return 0;
}
}

View File

@ -12,7 +12,6 @@
"double" return DOUBLE;
"string" return STRING;
"bool" return BOOL;
"MemoryAddress" return MEMORYADDRESS;
"struct" return STRUCT;
"return" return RETURN;
"static" return STATIC;
@ -41,7 +40,6 @@
";" return SEMICOLON;
":" return COLON;
"?" return QUESTION;
"@" return AT;
"(" return LEFT_PAREN;
")" return RIGHT_PAREN;
"[" return LEFT_BRACKET;

View File

@ -25,7 +25,6 @@ extern FILE *yyin;
%token DOUBLE
%token STRING
%token BOOL
%token MEMORYADDRESS
%token STRUCT
%token RETURN
%token STATIC
@ -55,7 +54,6 @@ extern FILE *yyin;
%token SEMICOLON
%token COLON
%token QUESTION
%token AT
%token LEFT_PAREN
%token RIGHT_PAREN
%token LEFT_BRACE
@ -109,17 +107,9 @@ BaseType : VOID
{
$$ = MakePrimitiveTypeNode(Bool);
}
| MEMORYADDRESS
{
$$ = MakePrimitiveTypeNode(MemoryAddress);
}
| Identifier GenericArgumentClauseNonEmpty
{
$$ = MakeConcreteGenericTypeNode($1, $2);
}
| Identifier
{
$$ = MakeCustomTypeNode($1);
$$ = MakeCustomTypeNode(yytext);
}
| REFERENCE LESS_THAN Type GREATER_THAN
{
@ -151,40 +141,11 @@ AccessExpression : Identifier POINT AccessExpression
$$ = $1;
}
SystemCallExpression : AT Identifier
{
$$ = $2;
}
Number : NUMBER
{
$$ = MakeNumberNode(yytext);
}
FieldInit : Identifier COLON Expression
{
$$ = MakeFieldInitNode($1, $3);
}
StructInitFields : FieldInit
{
$$ = StartStructInitFieldsNode($1);
}
| StructInitFields COMMA FieldInit
{
$$ = AddFieldInitNode($1, $3);
}
|
{
$$ = MakeEmptyFieldInitNode();
}
;
StructInitExpression : Type LEFT_BRACE StructInitFields RIGHT_BRACE
{
$$ = MakeStructInitExpressionNode($1, $3);
}
PrimaryExpression : Number
| STRING_LITERAL
{
@ -196,7 +157,6 @@ PrimaryExpression : Number
}
| FunctionCallExpression
| AccessExpression
| StructInitExpression
;
UnaryExpression : BANG Expression
@ -270,13 +230,9 @@ ReturnStatement : RETURN Expression
$$ = MakeReturnVoidStatementNode();
}
FunctionCallExpression : AccessExpression GenericArgumentClause LEFT_PAREN Arguments RIGHT_PAREN
FunctionCallExpression : AccessExpression LEFT_PAREN Arguments RIGHT_PAREN
{
$$ = MakeFunctionCallExpressionNode($1, $4, $2);
}
| SystemCallExpression GenericArgumentClause LEFT_PAREN Arguments RIGHT_PAREN
{
$$ = MakeSystemCallExpressionNode($1, $4, $2);
$$ = MakeFunctionCallExpressionNode($1, $3);
}
PartialStatement : FunctionCallExpression
@ -319,11 +275,11 @@ Statements : Statement
$$ = AddStatement($1, $2);
}
Arguments : Expression
Arguments : PrimaryExpression
{
$$ = StartFunctionArgumentSequenceNode($1);
}
| Arguments COMMA Expression
| Arguments COMMA PrimaryExpression
{
$$ = AddFunctionArgumentNode($1, $3);
}
@ -351,32 +307,9 @@ Body : LEFT_BRACE Statements RIGHT_BRACE
$$ = $2;
}
GenericDeclaration : Identifier
GenericArgument : Identifier
{
$$ = MakeGenericDeclarationNode($1, NULL);
}
GenericDeclarations : GenericDeclaration
{
$$ = StartGenericDeclarationsNode($1);
}
| GenericDeclarations COMMA GenericDeclaration
{
$$ = AddGenericDeclaration($1, $3);
}
GenericDeclarationClause : LESS_THAN GenericDeclarations GREATER_THAN
{
$$ = $2;
}
|
{
$$ = MakeEmptyGenericDeclarationsNode();
}
GenericArgument : Type
{
$$ = MakeGenericArgumentNode($1);
$$ = MakeGenericArgumentNode($1, NULL);
}
GenericArguments : GenericArgument
@ -388,23 +321,21 @@ GenericArguments : GenericArgument
$$ = AddGenericArgument($1, $3);
}
GenericArgumentClauseNonEmpty : LESS_THAN GenericArguments GREATER_THAN
{
$$ = $2;
}
;
GenericArgumentClause : GenericArgumentClauseNonEmpty
GenericArgumentsClause : LESS_THAN GenericArguments GREATER_THAN
{
$$ = $2;
}
|
{
$$ = MakeEmptyGenericArgumentsNode();
}
FunctionSignature : Identifier GenericDeclarationClause LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
FunctionSignature : Identifier GenericArgumentsClause LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
{
$$ = MakeFunctionSignatureNode($1, $7, $4, MakeFunctionModifiersNode(NULL, 0), $2);
}
| STATIC Identifier GenericDeclarationClause LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
| STATIC Identifier GenericArgumentsClause LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
{
Node *modifier = MakeStaticNode();
$$ = MakeFunctionSignatureNode($2, $8, $5, MakeFunctionModifiersNode(&modifier, 1), $3);
@ -415,9 +346,9 @@ FunctionDeclaration : FunctionSignature Body
$$ = MakeFunctionDeclarationNode($1, $2);
}
StructDeclaration : STRUCT Identifier GenericDeclarationClause LEFT_BRACE Declarations RIGHT_BRACE
StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE
{
$$ = MakeStructDeclarationNode($2, $5, $3);
$$ = MakeStructDeclarationNode($2, $4);
}
Declaration : FunctionDeclaration

View File

@ -5,55 +5,14 @@ struct Foo {
static Func<T>(t: T): T {
foo: T = t;
return Foo.Func2(foo);
}
}
struct MemoryBlock<T>
{
start: MemoryAddress;
capacity: uint;
AddressOf(index: uint): MemoryAddress
{
return start + (index * @sizeof<T>());
}
Get(index: uint): T
{
return @dereference<T>(AddressOf(index));
}
Set(index: uint, value: T): void
{
@memcpy(AddressOf(index), @addr(value), @sizeof<T>());
}
Free(): void
{
@free(start);
return Func2(foo);
}
}
struct Program {
static Main(): int {
static main(): int {
x: int = 4;
y: int = Foo.Func(x);
block: MemoryBlock<int> = MemoryBlock<int>
{
capacity: y,
start: @malloc(y * @sizeof<int>())
};
block.Set(0, 5);
block.Set(1, 3);
block.Set(2, 9);
block.Set(3, 100);
Console.PrintLine("%p", block.start);
Console.PrintLine("%i", block.Get(0));
Console.PrintLine("%i", block.Get(1));
Console.PrintLine("%i", block.Get(2));
Console.PrintLine("%i", block.Get(3));
block.Free();
return 0;
return x;
}
}
}

View File

@ -1,15 +0,0 @@
struct Foo {
static Func(): void {
Func2();
}
static Func2(): void {
Func();
}
}
struct Program {
static main(): int {
return 0;
}
}

603
src/ast.c
View File

@ -19,8 +19,6 @@ const char *SyntaxKindString(SyntaxKind syntaxKind)
return "BinaryExpression";
case Comment:
return "Comment";
case ConcreteGenericTypeNode:
return "ConcreteGenericTypeNode";
case CustomTypeNode:
return "CustomTypeNode";
case Declaration:
@ -29,8 +27,6 @@ const char *SyntaxKindString(SyntaxKind syntaxKind)
return "ForLoop";
case DeclarationSequence:
return "DeclarationSequence";
case FieldInit:
return "FieldInit";
case FunctionArgumentSequence:
return "FunctionArgumentSequence";
case FunctionCallExpression:
@ -47,10 +43,6 @@ const char *SyntaxKindString(SyntaxKind syntaxKind)
return "GenericArgument";
case GenericArguments:
return "GenericArguments";
case GenericDeclaration:
return "GenericDeclaration";
case GenericDeclarations:
return "GenericDeclarations";
case GenericTypeNode:
return "GenericTypeNode";
case Identifier:
@ -75,12 +67,6 @@ const char *SyntaxKindString(SyntaxKind syntaxKind)
return "StringLiteral";
case StructDeclaration:
return "StructDeclaration";
case StructInit:
return "StructInit";
case StructInitFields:
return "StructInitFields";
case SystemCall:
return "SystemCall";
case Type:
return "Type";
case UnaryExpression:
@ -103,12 +89,11 @@ Node *MakePrimitiveTypeNode(PrimitiveType type)
return node;
}
Node *MakeCustomTypeNode(Node *identifierNode)
Node *MakeCustomTypeNode(char *name)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = CustomTypeNode;
node->customType.name = strdup(identifierNode->identifier.name);
free(identifierNode);
node->customType.name = strdup(name);
return node;
}
@ -120,18 +105,6 @@ Node *MakeReferenceTypeNode(Node *typeNode)
return node;
}
Node *MakeConcreteGenericTypeNode(
Node *identifierNode,
Node *genericArgumentsNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = ConcreteGenericTypeNode;
node->concreteGenericType.name = strdup(identifierNode->identifier.name);
node->concreteGenericType.genericArguments = genericArgumentsNode;
free(identifierNode);
return node;
}
Node *MakeTypeNode(Node *typeNode)
{
Node *node = (Node *)malloc(sizeof(Node));
@ -313,7 +286,7 @@ Node *MakeFunctionSignatureNode(
node->functionSignature.type = typeNode;
node->functionSignature.arguments = arguments;
node->functionSignature.modifiers = modifiersNode;
node->functionSignature.genericDeclarations = genericArgumentsNode;
node->functionSignature.genericArguments = genericArgumentsNode;
return node;
}
@ -330,14 +303,12 @@ Node *MakeFunctionDeclarationNode(
Node *MakeStructDeclarationNode(
Node *identifierNode,
Node *declarationSequenceNode,
Node *genericArgumentsNode)
Node *declarationSequenceNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StructDeclaration;
node->structDeclaration.identifier = identifierNode;
node->structDeclaration.declarationSequence = declarationSequenceNode;
node->structDeclaration.genericDeclarations = genericArgumentsNode;
return node;
}
@ -396,55 +367,12 @@ Node *MakeEmptyFunctionArgumentSequenceNode()
return node;
}
Node *MakeGenericDeclarationNode(Node *identifierNode, Node *constraintNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericDeclaration;
node->genericDeclaration.identifier = identifierNode;
node->genericDeclaration.constraint = constraintNode;
return node;
}
Node *StartGenericDeclarationsNode(Node *genericArgumentNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericDeclarations;
node->genericDeclarations.declarations = (Node **)malloc(sizeof(Node *));
node->genericDeclarations.declarations[0] = genericArgumentNode;
node->genericDeclarations.count = 1;
return node;
}
Node *AddGenericDeclaration(
Node *genericDeclarationsNode,
Node *genericDeclarationNode)
{
genericDeclarationsNode->genericDeclarations.declarations =
(Node **)realloc(
genericDeclarationsNode->genericDeclarations.declarations,
sizeof(Node *) *
(genericDeclarationsNode->genericDeclarations.count + 1));
genericDeclarationsNode->genericDeclarations
.declarations[genericDeclarationsNode->genericDeclarations.count] =
genericDeclarationNode;
genericDeclarationsNode->genericDeclarations.count += 1;
return genericDeclarationsNode;
}
Node *MakeEmptyGenericDeclarationsNode()
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericDeclarations;
node->genericDeclarations.declarations = NULL;
node->genericDeclarations.count = 0;
return node;
}
Node *MakeGenericArgumentNode(Node *typeNode)
Node *MakeGenericArgumentNode(Node *identifierNode, Node *constraintNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = GenericArgument;
node->genericArgument.type = typeNode;
node->genericArgument.identifier = identifierNode;
node->genericArgument.constraint = constraintNode;
return node;
}
@ -460,13 +388,13 @@ Node *StartGenericArgumentsNode(Node *genericArgumentNode)
Node *AddGenericArgument(Node *genericArgumentsNode, Node *genericArgumentNode)
{
genericArgumentsNode->genericArguments.arguments = realloc(
genericArgumentsNode->genericArguments.arguments = (Node **)realloc(
genericArgumentsNode->genericArguments.arguments,
sizeof(Node *) * (genericArgumentsNode->genericArguments.count + 1));
genericArgumentsNode->genericArguments
.arguments[genericArgumentsNode->genericArguments.count] =
genericArgumentNode;
genericArgumentNode->genericArguments.count += 1;
genericArgumentsNode->genericArguments.count += 1;
return genericArgumentsNode;
}
@ -489,27 +417,12 @@ Node *MakeGenericTypeNode(char *name)
Node *MakeFunctionCallExpressionNode(
Node *identifierNode,
Node *argumentSequenceNode,
Node *genericArgumentsNode)
Node *argumentSequenceNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionCallExpression;
node->functionCallExpression.identifier = identifierNode;
node->functionCallExpression.argumentSequence = argumentSequenceNode;
node->functionCallExpression.genericArguments = genericArgumentsNode;
return node;
}
Node *MakeSystemCallExpressionNode(
Node *identifierNode,
Node *argumentSequenceNode,
Node *genericArgumentsNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = SystemCall;
node->systemCall.identifier = identifierNode;
node->systemCall.argumentSequence = argumentSequenceNode;
node->systemCall.genericArguments = genericArgumentsNode;
return node;
}
@ -563,55 +476,6 @@ Node *MakeForLoopNode(
return node;
}
Node *MakeFieldInitNode(Node *identifierNode, Node *expressionNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FieldInit;
node->fieldInit.identifier = identifierNode;
node->fieldInit.expression = expressionNode;
return node;
}
Node *StartStructInitFieldsNode(Node *fieldInitNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StructInitFields;
node->structInitFields.fieldInits = (Node **)malloc(sizeof(Node *));
node->structInitFields.fieldInits[0] = fieldInitNode;
node->structInitFields.count = 1;
return node;
}
Node *AddFieldInitNode(Node *structInitFieldsNode, Node *fieldInitNode)
{
structInitFieldsNode->structInitFields.fieldInits = realloc(
structInitFieldsNode->structInitFields.fieldInits,
sizeof(Node *) * (structInitFieldsNode->structInitFields.count + 1));
structInitFieldsNode->structInitFields
.fieldInits[structInitFieldsNode->structInitFields.count] =
fieldInitNode;
structInitFieldsNode->structInitFields.count += 1;
return structInitFieldsNode;
}
Node *MakeEmptyFieldInitNode()
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StructInitFields;
node->structInitFields.fieldInits = NULL;
node->structInitFields.count = 0;
return node;
}
Node *MakeStructInitExpressionNode(Node *typeNode, Node *structInitFieldsNode)
{
Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StructInit;
node->structInit.type = typeNode;
node->structInit.initFields = structInitFieldsNode;
return node;
}
static const char *PrimitiveTypeToString(PrimitiveType type)
{
switch (type)
@ -622,8 +486,6 @@ static const char *PrimitiveTypeToString(PrimitiveType type)
return "UInt";
case Bool:
return "Bool";
case MemoryAddress:
return "MemoryAddress";
case Void:
return "Void";
}
@ -694,11 +556,6 @@ void PrintNode(Node *node, uint32_t tabCount)
PrintNode(node->binaryExpression.right, tabCount + 1);
return;
case ConcreteGenericTypeNode:
printf("%s\n", node->concreteGenericType.name);
PrintNode(node->concreteGenericType.genericArguments, tabCount + 1);
return;
case CustomTypeNode:
printf("%s\n", node->customType.name);
return;
@ -717,12 +574,6 @@ void PrintNode(Node *node, uint32_t tabCount)
}
return;
case FieldInit:
printf("\n");
PrintNode(node->fieldInit.identifier, tabCount + 1);
PrintNode(node->fieldInit.expression, tabCount + 1);
return;
case ForLoop:
printf("\n");
PrintNode(node->forLoop.declaration, tabCount + 1);
@ -743,7 +594,6 @@ void PrintNode(Node *node, uint32_t tabCount)
printf("\n");
PrintNode(node->functionCallExpression.identifier, tabCount + 1);
PrintNode(node->functionCallExpression.argumentSequence, tabCount + 1);
PrintNode(node->functionCallExpression.genericArguments, tabCount + 1);
return;
case FunctionDeclaration:
@ -763,7 +613,7 @@ void PrintNode(Node *node, uint32_t tabCount)
case FunctionSignature:
printf("\n");
PrintNode(node->functionSignature.identifier, tabCount + 1);
PrintNode(node->functionSignature.genericDeclarations, tabCount + 1);
PrintNode(node->functionSignature.genericArguments, tabCount + 1);
PrintNode(node->functionSignature.arguments, tabCount + 1);
PrintNode(node->functionSignature.type, tabCount + 1);
PrintNode(node->functionSignature.modifiers, tabCount + 1);
@ -781,32 +631,18 @@ void PrintNode(Node *node, uint32_t tabCount)
case GenericArgument:
printf("\n");
PrintNode(node->genericArgument.type, tabCount + 1);
PrintNode(node->genericArgument.identifier, tabCount + 1);
/* Constraint nodes are not implemented. */
/* PrintNode(node->genericArgument.constraint, tabCount + 1); */
return;
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;
case GenericDeclaration:
printf("\n");
PrintNode(node->genericDeclaration.identifier, tabCount + 1);
/* Constraint nodes are not implemented. */
/* PrintNode(node->genericDeclaration.constraint, tabCount + 1); */
return;
case GenericDeclarations:
printf("\n");
for (i = 0; i < node->genericDeclarations.count; i += 1)
{
PrintNode(node->genericDeclarations.declarations[i], tabCount + 1);
}
return;
case GenericTypeNode:
printf("%s\n", node->genericType.name);
return;
@ -869,7 +705,7 @@ void PrintNode(Node *node, uint32_t tabCount)
return;
case StringLiteral:
printf("%s\n", node->stringLiteral.string);
printf("%s", node->stringLiteral.string);
return;
case StructDeclaration:
@ -878,27 +714,6 @@ void PrintNode(Node *node, uint32_t tabCount)
PrintNode(node->structDeclaration.declarationSequence, tabCount + 1);
return;
case StructInit:
printf("\n");
PrintNode(node->structInit.type, tabCount + 1);
PrintNode(node->structInit.initFields, tabCount + 1);
return;
case StructInitFields:
printf("\n");
for (i = 0; i < node->structInitFields.count; i += 1)
{
PrintNode(node->structInitFields.fieldInits[i], tabCount + 1);
}
return;
case SystemCall:
printf("\n");
PrintNode(node->systemCall.identifier, tabCount + 1);
PrintNode(node->systemCall.argumentSequence, tabCount + 1);
PrintNode(node->systemCall.genericArguments, tabCount + 1);
return;
case Type:
printf("\n");
PrintNode(node->type.typeNode, tabCount + 1);
@ -911,7 +726,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;
switch (node->syntaxKind)
@ -938,10 +753,6 @@ void Recurse(Node *node, void (*func)(Node *))
case Comment:
return;
case ConcreteGenericTypeNode:
func(node->concreteGenericType.genericArguments);
return;
case CustomTypeNode:
return;
@ -951,17 +762,11 @@ 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;
case FieldInit:
func(node->fieldInit.identifier);
func(node->fieldInit.expression);
return;
case ForLoop:
func(node->forLoop.declaration);
func(node->forLoop.startNumber);
@ -970,8 +775,7 @@ 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;
@ -979,7 +783,6 @@ void Recurse(Node *node, void (*func)(Node *))
case FunctionCallExpression:
func(node->functionCallExpression.identifier);
func(node->functionCallExpression.argumentSequence);
func(node->functionCallExpression.genericArguments);
return;
case FunctionDeclaration:
@ -988,8 +791,7 @@ 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;
@ -999,39 +801,26 @@ void Recurse(Node *node, void (*func)(Node *))
func(node->functionSignature.type);
func(node->functionSignature.arguments);
func(node->functionSignature.modifiers);
func(node->functionSignature.genericDeclarations);
func(node->functionSignature.genericArguments);
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;
case GenericArgument:
func(node->genericArgument.type);
break;
func(node->genericArgument.identifier);
func(node->genericArgument.constraint);
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;
case GenericDeclaration:
func(node->genericDeclaration.identifier);
func(node->genericDeclaration.constraint);
return;
case GenericDeclarations:
for (i = 0; i < node->genericDeclarations.count; i += 1)
{
func(node->genericDeclarations.declarations[i]);
}
return;
case GenericTypeNode:
return;
@ -1066,8 +855,7 @@ 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;
@ -1083,26 +871,7 @@ void Recurse(Node *node, void (*func)(Node *))
func(node->structDeclaration.declarationSequence);
return;
case StructInit:
func(node->structInit.type);
func(node->structInit.initFields);
return;
case StructInitFields:
for (i = 0; i < node->structInitFields.count; i += 1)
{
func(node->structInitFields.fieldInits[i]);
}
return;
case SystemCall:
func(node->systemCall.identifier);
func(node->systemCall.argumentSequence);
func(node->systemCall.genericArguments);
return;
case Type:
func(node->type.typeNode);
return;
case UnaryExpression:
@ -1110,18 +879,14 @@ void Recurse(Node *node, void (*func)(Node *))
return;
default:
fprintf(
stderr,
"wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
fprintf(stderr, "wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
return;
}
}
TypeTag *MakeTypeTag(Node *node)
{
uint32_t i;
if (node == NULL)
{
fprintf(
@ -1152,28 +917,6 @@ TypeTag *MakeTypeTag(Node *node)
tag->value.customType = strdup(node->customType.name);
break;
case ConcreteGenericTypeNode:
tag->type = ConcreteGeneric;
tag->value.concreteGenericType.name =
strdup(node->concreteGenericType.name);
tag->value.concreteGenericType.genericArgumentCount =
node->concreteGenericType.genericArguments->genericArguments.count;
tag->value.concreteGenericType.genericArguments = malloc(
sizeof(TypeTag *) *
tag->value.concreteGenericType.genericArgumentCount);
for (i = 0;
i <
node->concreteGenericType.genericArguments->genericArguments.count;
i += 1)
{
tag->value.concreteGenericType.genericArguments[i] = MakeTypeTag(
node->concreteGenericType.genericArguments->genericArguments
.arguments[i]
->genericArgument.type);
}
break;
case Declaration:
tag = MakeTypeTag(node->declaration.type);
break;
@ -1193,16 +936,9 @@ TypeTag *MakeTypeTag(Node *node)
tag = MakeTypeTag(node->allocExpression.type);
break;
case GenericDeclaration:
tag->type = Generic;
tag->value.genericType =
strdup(node->genericDeclaration.identifier->identifier.name);
break;
case GenericTypeNode:
tag->type = Generic;
tag->value.genericType = strdup(node->genericType.name);
break;
default:
fprintf(
@ -1212,14 +948,11 @@ TypeTag *MakeTypeTag(Node *node)
SyntaxKindString(node->syntaxKind));
return NULL;
}
return tag;
}
char *TypeTagToString(TypeTag *tag)
{
uint32_t i;
if (tag == NULL)
{
fprintf(
@ -1238,291 +971,21 @@ char *TypeTagToString(TypeTag *tag)
{
char *inner = TypeTagToString(tag->value.referenceType);
size_t innerStrLen = strlen(inner);
char *result = malloc(sizeof(char) * (innerStrLen + 6));
char *result = malloc(sizeof(char) * (innerStrLen + 5));
sprintf(result, "Ref<%s>", inner);
return result;
}
case Custom:
{
char *result =
malloc(sizeof(char) * (strlen(tag->value.customType) + 9));
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.genericType) + 10));
sprintf(result, "Generic<%s>", tag->value.genericType);
return result;
}
case ConcreteGeneric:
{
char *result = strdup(tag->value.concreteGenericType.name);
uint32_t len = strlen(result);
len += 2;
result = realloc(result, sizeof(char) * len);
strcat(result, "<");
for (i = 0; i < tag->value.concreteGenericType.genericArgumentCount;
i += 1)
{
char *inner = TypeTagToString(
tag->value.concreteGenericType.genericArguments[i]);
len += strlen(inner);
result = realloc(result, sizeof(char) * (len + 3));
if (i != tag->value.concreteGenericType.genericArgumentCount - 1)
{
strcat(result, ", ");
}
strcat(result, inner);
}
result = realloc(result, sizeof(char) * (len + 1));
strcat(result, ">");
char *result = malloc(sizeof(char) * (strlen(tag->value.customType) + 9));
sprintf(result, "Generic<%s>", tag->value.customType);
return result;
}
}
}
uint8_t TypeTagEqual(TypeTag *typeTagA, TypeTag *typeTagB)
{
if (typeTagA->type != typeTagB->type)
{
return 0;
}
switch (typeTagA->type)
{
case Primitive:
return typeTagA->value.primitiveType == typeTagB->value.primitiveType;
case Reference:
return TypeTagEqual(
typeTagA->value.referenceType,
typeTagB->value.referenceType);
case Custom:
return strcmp(typeTagA->value.customType, typeTagB->value.customType) ==
0;
case Generic:
return strcmp(
typeTagA->value.genericType,
typeTagB->value.genericType) == 0;
default:
fprintf(stderr, "Invalid type comparison!");
return 0;
}
}
void LinkParentPointers(Node *node, Node *prev)
{
if (node == NULL)
return;
node->parent = prev;
uint32_t i;
switch (node->syntaxKind)
{
case AccessExpression:
LinkParentPointers(node->accessExpression.accessee, node);
LinkParentPointers(node->accessExpression.accessor, node);
return;
case AllocExpression:
LinkParentPointers(node->allocExpression.type, node);
return;
case Assignment:
LinkParentPointers(node->assignmentStatement.left, node);
LinkParentPointers(node->assignmentStatement.right, node);
return;
case BinaryExpression:
LinkParentPointers(node->binaryExpression.left, node);
LinkParentPointers(node->binaryExpression.right, node);
return;
case Comment:
return;
case CustomTypeNode:
return;
case Declaration:
LinkParentPointers(node->declaration.type, node);
LinkParentPointers(node->declaration.identifier, node);
return;
case DeclarationSequence:
for (i = 0; i < node->declarationSequence.count; i += 1)
{
LinkParentPointers(node->declarationSequence.sequence[i], node);
}
return;
case FieldInit:
LinkParentPointers(node->fieldInit.identifier, node);
LinkParentPointers(node->fieldInit.expression, node);
return;
case ForLoop:
LinkParentPointers(node->forLoop.declaration, node);
LinkParentPointers(node->forLoop.startNumber, node);
LinkParentPointers(node->forLoop.endNumber, node);
LinkParentPointers(node->forLoop.statementSequence, node);
return;
case FunctionArgumentSequence:
for (i = 0; i < node->functionArgumentSequence.count; i += 1)
{
LinkParentPointers(
node->functionArgumentSequence.sequence[i],
node);
}
return;
case FunctionCallExpression:
LinkParentPointers(node->functionCallExpression.identifier, node);
LinkParentPointers(node->functionCallExpression.argumentSequence, node);
return;
case FunctionDeclaration:
LinkParentPointers(node->functionDeclaration.functionSignature, node);
LinkParentPointers(node->functionDeclaration.functionBody, node);
return;
case FunctionModifiers:
for (i = 0; i < node->functionModifiers.count; i += 1)
{
LinkParentPointers(node->functionModifiers.sequence[i], node);
}
return;
case FunctionSignature:
LinkParentPointers(node->functionSignature.identifier, node);
LinkParentPointers(node->functionSignature.type, node);
LinkParentPointers(node->functionSignature.arguments, node);
LinkParentPointers(node->functionSignature.modifiers, node);
LinkParentPointers(node->functionSignature.genericDeclarations, node);
return;
case FunctionSignatureArguments:
for (i = 0; i < node->functionSignatureArguments.count; i += 1)
{
LinkParentPointers(
node->functionSignatureArguments.sequence[i],
node);
}
return;
case GenericArgument:
LinkParentPointers(node->genericArgument.type, node);
return;
case GenericArguments:
for (i = 0; i < node->genericArguments.count; i += 1)
{
LinkParentPointers(node->genericArguments.arguments[i], node);
}
return;
case GenericDeclaration:
LinkParentPointers(node->genericDeclaration.identifier, node);
LinkParentPointers(node->genericDeclaration.constraint, node);
return;
case GenericDeclarations:
for (i = 0; i < node->genericDeclarations.count; i += 1)
{
LinkParentPointers(node->genericDeclarations.declarations[i], node);
}
return;
case GenericTypeNode:
return;
case Identifier:
return;
case IfStatement:
LinkParentPointers(node->ifStatement.expression, node);
LinkParentPointers(node->ifStatement.statementSequence, node);
return;
case IfElseStatement:
LinkParentPointers(node->ifElseStatement.ifStatement, node);
LinkParentPointers(node->ifElseStatement.elseStatement, node);
return;
case Number:
return;
case PrimitiveTypeNode:
return;
case ReferenceTypeNode:
LinkParentPointers(node->referenceType.type, node);
return;
case Return:
LinkParentPointers(node->returnStatement.expression, node);
return;
case ReturnVoid:
return;
case StatementSequence:
for (i = 0; i < node->statementSequence.count; i += 1)
{
LinkParentPointers(node->statementSequence.sequence[i], node);
}
return;
case StaticModifier:
return;
case StringLiteral:
return;
case StructDeclaration:
LinkParentPointers(node->structDeclaration.identifier, node);
LinkParentPointers(node->structDeclaration.declarationSequence, node);
return;
case StructInit:
LinkParentPointers(node->structInit.type, node);
LinkParentPointers(node->structInit.initFields, node);
return;
case StructInitFields:
for (i = 0; i < node->structInitFields.count; i += 1)
{
LinkParentPointers(node->structInitFields.fieldInits[i], node);
}
return;
case SystemCall:
LinkParentPointers(node->systemCall.identifier, node);
LinkParentPointers(node->systemCall.argumentSequence, node);
LinkParentPointers(node->systemCall.genericArguments, node);
return;
case Type:
return;
case UnaryExpression:
LinkParentPointers(node->unaryExpression.child, node);
return;
default:
fprintf(
stderr,
"wraith: Unhandled SyntaxKind %s in recurse function.\n",
SyntaxKindString(node->syntaxKind));
return;
}
}

124
src/ast.h
View File

@ -1,6 +1,7 @@
#ifndef WRAITH_AST_H
#define WRAITH_AST_H
#include "identcheck.h"
#include <stdint.h>
/* -Wpedantic nameless union/struct silencing */
@ -19,11 +20,9 @@ typedef enum
Assignment,
BinaryExpression,
Comment,
ConcreteGenericTypeNode,
CustomTypeNode,
Declaration,
DeclarationSequence,
FieldInit,
ForLoop,
FunctionArgumentSequence,
FunctionCallExpression,
@ -33,8 +32,6 @@ typedef enum
FunctionSignatureArguments,
GenericArgument,
GenericArguments,
GenericDeclaration,
GenericDeclarations,
GenericTypeNode,
Identifier,
IfStatement,
@ -48,9 +45,6 @@ typedef enum
StaticModifier,
StringLiteral,
StructDeclaration,
StructInit,
StructInitFields,
SystemCall,
Type,
UnaryExpression
} SyntaxKind;
@ -80,8 +74,7 @@ typedef enum
UInt,
Float,
Double,
String,
MemoryAddress
String
} PrimitiveType;
typedef union
@ -90,16 +83,7 @@ typedef union
BinaryOperator binaryOperator;
} Operator;
typedef struct TypeTag TypeTag;
typedef struct ConcreteGenericTypeTag
{
char *name;
TypeTag **genericArguments;
uint32_t genericArgumentCount;
} ConcreteGenericTypeTag;
struct TypeTag
typedef struct TypeTag
{
enum Type
{
@ -107,8 +91,7 @@ struct TypeTag
Primitive,
Reference,
Custom,
Generic,
ConcreteGeneric
Generic
} type;
union
{
@ -120,10 +103,8 @@ struct TypeTag
char *customType;
/* Valid when type = Generic. */
char *genericType;
/* Valid when type = ConcreteGeneric */
ConcreteGenericTypeTag concreteGenericType;
} value;
};
} TypeTag;
typedef struct Node Node;
@ -162,12 +143,6 @@ struct Node
} comment;
struct
{
char *name;
Node *genericArguments;
} concreteGenericType;
struct
{
char *name;
@ -185,12 +160,6 @@ struct Node
uint32_t count;
} declarationSequence;
struct
{
Node *identifier;
Node *expression;
} fieldInit;
struct
{
Node *declaration;
@ -209,7 +178,6 @@ struct Node
{
Node *identifier; /* FIXME: need better name */
Node *argumentSequence;
Node *genericArguments;
} functionCallExpression;
struct
@ -230,7 +198,7 @@ struct Node
Node *type;
Node *arguments;
Node *modifiers;
Node *genericDeclarations;
Node *genericArguments;
} functionSignature;
struct
@ -241,7 +209,8 @@ struct Node
struct
{
Node *type;
Node *identifier;
Node *constraint;
} genericArgument;
struct
@ -250,18 +219,6 @@ struct Node
uint32_t count;
} genericArguments;
struct
{
Node *identifier;
Node *constraint;
} genericDeclaration;
struct
{
Node **declarations;
uint32_t count;
} genericDeclarations;
struct
{
char *name;
@ -329,28 +286,8 @@ struct Node
{
Node *identifier;
Node *declarationSequence;
Node *genericDeclarations;
} structDeclaration;
struct
{
Node *type;
Node *initFields;
} structInit;
struct
{
Node **fieldInits;
uint32_t count;
} structInitFields;
struct
{
Node *identifier;
Node *argumentSequence;
Node *genericArguments;
} systemCall;
struct
{
Node *typeNode;
@ -363,17 +300,15 @@ struct Node
} unaryExpression;
};
TypeTag *typeTag;
IdNode *idLink;
};
const char *SyntaxKindString(SyntaxKind syntaxKind);
uint8_t IsPrimitiveType(Node *typeNode);
Node *MakePrimitiveTypeNode(PrimitiveType type);
Node *MakeCustomTypeNode(Node *identifierNode);
Node *MakeCustomTypeNode(char *string);
Node *MakeReferenceTypeNode(Node *typeNode);
Node *MakeConcreteGenericTypeNode(
Node *identifierNode,
Node *genericArgumentsNode);
Node *MakeTypeNode(Node *typeNode);
Node *MakeIdentifierNode(const char *id);
Node *MakeNumberNode(const char *numberString);
@ -400,21 +335,14 @@ Node *MakeFunctionSignatureNode(
Node *MakeFunctionDeclarationNode(
Node *functionSignatureNode,
Node *functionBodyNode);
Node *MakeGenericDeclarationNode(Node *identifierNode, Node *constraintNode);
Node *MakeEmptyGenericDeclarationsNode();
Node *StartGenericDeclarationsNode(Node *genericDeclarationNode);
Node *AddGenericDeclaration(
Node *genericDeclarationsNode,
Node *genericDeclarationNode);
Node *MakeGenericArgumentNode(Node *typeNode);
Node *MakeGenericArgumentNode(Node *identifierNode, Node *constraintNode);
Node *MakeEmptyGenericArgumentsNode();
Node *StartGenericArgumentsNode(Node *genericArgumentNode);
Node *AddGenericArgument(Node *genericArgumentsNode, Node *genericArgumentNode);
Node *MakeGenericTypeNode(char *name);
Node *MakeStructDeclarationNode(
Node *identifierNode,
Node *declarationSequenceNode,
Node *genericArgumentsNode);
Node *declarationSequenceNode);
Node *StartDeclarationSequenceNode(Node *declarationNode);
Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode);
Node *StartFunctionArgumentSequenceNode(Node *argumentNode);
@ -422,12 +350,7 @@ Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode);
Node *MakeEmptyFunctionArgumentSequenceNode();
Node *MakeFunctionCallExpressionNode(
Node *identifierNode,
Node *argumentSequenceNode,
Node *genericArgumentsNode);
Node *MakeSystemCallExpressionNode(
Node *identifierNode,
Node *argumentSequenceNode,
Node *genericArgumentsNode);
Node *argumentSequenceNode);
Node *MakeAccessExpressionNode(Node *accessee, Node *accessor);
Node *MakeAllocNode(Node *typeNode);
Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode);
@ -440,28 +363,17 @@ Node *MakeForLoopNode(
Node *startNumberNode,
Node *endNumberNode,
Node *statementSequenceNode);
Node *MakeFieldInitNode(Node *identifierNode, Node *expressionNode);
Node *StartStructInitFieldsNode(Node *fieldInitNode);
Node *AddFieldInitNode(Node *structInitFieldsNode, Node *fieldInitNode);
Node *MakeEmptyFieldInitNode();
Node *MakeStructInitExpressionNode(Node *typeNode, Node *structInitFieldsNode);
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. */
void Recurse(Node *node, void (*func)(Node *));
void LinkParentPointers(Node *node, Node *prev);
/* 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*));
TypeTag *MakeTypeTag(Node *node);
char *TypeTagToString(TypeTag *tag);
uint8_t TypeTagEqual(TypeTag *typeTagA, TypeTag *typeTagB);
Node *LookupIdNode(Node *current, Node *prev, char *target);
#endif /* WRAITH_AST_H */

File diff suppressed because it is too large Load Diff

507
src/identcheck.c Normal file
View File

@ -0,0 +1,507 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "identcheck.h"
IdNode *MakeIdNode(NodeType type, char *name, IdNode *parent)
{
IdNode *node = (IdNode *)malloc(sizeof(IdNode));
node->type = type;
node->name = strdup(name);
node->parent = parent;
node->childCount = 0;
node->childCapacity = 0;
node->children = NULL;
node->typeTag = NULL;
return node;
}
void AddChildToNode(IdNode *node, IdNode *child)
{
if (child == NULL)
return;
if (node->children == NULL)
{
node->childCapacity = 2;
node->children =
(IdNode **)malloc(sizeof(IdNode *) * node->childCapacity);
}
else if (node->childCount == node->childCapacity)
{
node->childCapacity *= 2;
node->children = (IdNode **)realloc(
node->children,
sizeof(IdNode *) * node->childCapacity);
}
node->children[node->childCount] = child;
node->childCount += 1;
}
IdNode *MakeIdTree(Node *astNode, IdNode *parent)
{
uint32_t i;
IdNode *mainNode;
switch (astNode->syntaxKind)
{
case AccessExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->accessExpression.accessee, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->accessExpression.accessor, parent));
return NULL;
case AllocExpression:
astNode->typeTag = MakeTypeTag(astNode);
return NULL;
case Assignment:
{
if (astNode->assignmentStatement.left->syntaxKind == Declaration)
{
return MakeIdTree(astNode->assignmentStatement.left, parent);
}
else
{
AddChildToNode(
parent,
MakeIdTree(astNode->assignmentStatement.left, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->assignmentStatement.right, parent));
return NULL;
}
}
case BinaryExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->binaryExpression.left, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->binaryExpression.right, parent));
return NULL;
case Declaration:
{
Node *idNode = astNode->declaration.identifier;
mainNode = MakeIdNode(Variable, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
idNode->typeTag = mainNode->typeTag;
break;
}
case DeclarationSequence:
{
mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->declarationSequence.count; i++)
{
AddChildToNode(
mainNode,
MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
}
break;
}
case ForLoop:
{
Node *loopDecl = astNode->forLoop.declaration;
Node *loopBody = astNode->forLoop.statementSequence;
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
AddChildToNode(mainNode, MakeIdTree(loopDecl, mainNode));
AddChildToNode(mainNode, MakeIdTree(loopBody, mainNode));
break;
}
case FunctionArgumentSequence:
for (i = 0; i < astNode->functionArgumentSequence.count; i++)
{
AddChildToNode(
parent,
MakeIdTree(
astNode->functionArgumentSequence.sequence[i],
parent));
}
return NULL;
case FunctionCallExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->functionCallExpression.identifier, parent));
AddChildToNode(
parent,
MakeIdTree(
astNode->functionCallExpression.argumentSequence,
parent));
return NULL;
case FunctionDeclaration:
{
Node *sigNode = astNode->functionDeclaration.functionSignature;
Node *idNode = sigNode->functionSignature.identifier;
char *funcName = idNode->identifier.name;
mainNode = MakeIdNode(Function, funcName, parent);
mainNode->typeTag = MakeTypeTag(astNode);
idNode->typeTag = mainNode->typeTag;
MakeIdTree(sigNode->functionSignature.genericArguments, mainNode);
MakeIdTree(sigNode->functionSignature.arguments, mainNode);
MakeIdTree(astNode->functionDeclaration.functionBody, mainNode);
break;
}
case FunctionSignatureArguments:
{
for (i = 0; i < astNode->functionSignatureArguments.count; i++)
{
Node *argNode = astNode->functionSignatureArguments.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
case GenericArgument:
{
char *name = astNode->genericArgument.identifier->identifier.name;
mainNode = MakeIdNode(GenericType, name, parent);
break;
}
case GenericArguments:
{
for (i = 0; i < astNode->genericArguments.count; i += 1)
{
Node *argNode = astNode->genericArguments.arguments[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
case Identifier:
{
char *name = astNode->identifier.name;
mainNode = MakeIdNode(Placeholder, name, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, name);
if (lookupNode == NULL)
{
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name);
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
tag->type = Unknown;
astNode->typeTag = tag;
}
else
{
astNode->typeTag = lookupNode->typeTag;
}
break;
}
case IfStatement:
{
Node *clause = astNode->ifStatement.expression;
Node *stmtSeq = astNode->ifStatement.statementSequence;
mainNode = MakeIdNode(OrderedScope, "if", parent);
MakeIdTree(clause, mainNode);
MakeIdTree(stmtSeq, mainNode);
break;
}
case IfElseStatement:
{
Node *ifNode = astNode->ifElseStatement.ifStatement;
Node *elseStmts = astNode->ifElseStatement.elseStatement;
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, mainNode);
AddChildToNode(mainNode, ifBranch);
IdNode *elseScope = MakeIdNode(OrderedScope, "else", mainNode);
MakeIdTree(elseStmts, elseScope);
AddChildToNode(mainNode, elseScope);
break;
}
case ReferenceTypeNode:
AddChildToNode(parent, MakeIdTree(astNode->referenceType.type, parent));
return NULL;
case Return:
AddChildToNode(
parent,
MakeIdTree(astNode->returnStatement.expression, parent));
return NULL;
case StatementSequence:
{
for (i = 0; i < astNode->statementSequence.count; i++)
{
Node *argNode = astNode->statementSequence.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent));
}
return NULL;
}
case StructDeclaration:
{
Node *idNode = astNode->structDeclaration.identifier;
Node *declsNode = astNode->structDeclaration.declarationSequence;
mainNode = MakeIdNode(Struct, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->declarationSequence.count; i++)
{
Node *decl = declsNode->declarationSequence.sequence[i];
AddChildToNode(mainNode, MakeIdTree(decl, mainNode));
}
break;
}
case Type:
AddChildToNode(parent, MakeIdTree(astNode->type.typeNode, parent));
return NULL;
case UnaryExpression:
AddChildToNode(
parent,
MakeIdTree(astNode->unaryExpression.child, parent));
return NULL;
case Comment:
case CustomTypeNode:
case FunctionModifiers:
case FunctionSignature:
case Number:
case PrimitiveTypeNode:
case ReturnVoid:
case StaticModifier:
case StringLiteral:
return NULL;
}
astNode->idLink = mainNode;
return mainNode;
}
void PrintIdNode(IdNode *node)
{
if (node == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call PrintIdNode with null value.\n");
return;
}
switch (node->type)
{
case Placeholder:
printf("Placeholder (%s)\n", node->name);
break;
case OrderedScope:
printf("OrderedScope (%s)\n", node->name);
break;
case UnorderedScope:
printf("UnorderedScope (%s)\n", node->name);
break;
case Struct:
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));
break;
case GenericType:
printf("Generic type: %s\n", node->name);
break;
case Alloc:
printf("Alloc: %s\n", TypeTagToString(node->typeTag));
break;
}
}
void PrintIdTree(IdNode *tree, uint32_t tabCount)
{
if (tree == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call PrintIdTree on a null value.\n");
return;
}
uint32_t i;
for (i = 0; i < tabCount; i++)
{
printf("| ");
}
PrintIdNode(tree);
for (i = 0; i < tree->childCount; i++)
{
PrintIdTree(tree->children[i], tabCount + 1);
}
}
int PrintAncestors(IdNode *node)
{
if (node == NULL)
return -1;
int i;
int indent = 1;
indent += PrintAncestors(node->parent);
for (i = 0; i < indent; i++)
{
printf(" ");
}
PrintIdNode(node);
return indent;
}
IdNode *LookdownId(IdNode *root, NodeType targetType, char *targetName)
{
if (root == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call LookdownId on a null value.\n");
return NULL;
}
IdNode *result = NULL;
IdNode **frontier = (IdNode **)malloc(sizeof(IdNode *));
frontier[0] = root;
uint32_t frontierCount = 1;
while (frontierCount > 0)
{
IdNode *current = frontier[0];
if (current->type == targetType &&
strcmp(current->name, targetName) == 0)
{
result = current;
break;
}
uint32_t i;
for (i = 1; i < frontierCount; i++)
{
frontier[i - 1] = frontier[i];
}
size_t newSize = frontierCount + current->childCount - 1;
if (frontierCount != newSize)
{
frontier = (IdNode **)realloc(frontier, sizeof(IdNode *) * newSize);
}
for (i = 0; i < current->childCount; i++)
{
frontier[frontierCount + i - 1] = current->children[i];
}
frontierCount = newSize;
}
free(frontier);
return result;
}
bool ScopeHasOrdering(IdNode *node)
{
switch (node->type)
{
case OrderedScope:
case Function:
case Variable: /* this is only technically true */
return true;
default:
return false;
}
}
IdNode *LookupId(IdNode *node, IdNode *prev, char *target)
{
if (node == NULL)
{
return NULL;
}
if (strcmp(node->name, target) == 0 && node->type != Placeholder)
{
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 (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 (ScopeHasOrdering(node))
{
uint32_t i;
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++)
{
if (node->children[i] == prev)
{
break;
}
}
}
else
{
idxLimit = node->childCount;
}
uint32_t i;
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. */
continue;
}
if (strcmp(child->name, target) == 0)
{
return child;
}
if (child->type == Struct)
{
uint32_t j;
for (j = 0; j < child->childCount; j++)
{
IdNode *grandchild = child->children[j];
if (strcmp(grandchild->name, target) == 0)
{
return grandchild;
}
}
}
}
return LookupId(node->parent, node, target);
}

51
src/identcheck.h Normal file
View File

@ -0,0 +1,51 @@
/* Validates identifier usage in an AST. */
#ifndef WRAITH_IDENTCHECK_H
#define WRAITH_IDENTCHECK_H
#include <stdint.h>
#include "ast.h"
struct TypeTag;
struct Node;
typedef enum NodeType
{
Placeholder,
UnorderedScope,
OrderedScope,
Struct,
Function,
Variable,
GenericType,
Alloc
} NodeType;
typedef struct IdNode
{
NodeType type;
char *name;
struct TypeTag *typeTag;
struct IdNode *parent;
struct IdNode **children;
uint32_t childCount;
uint32_t childCapacity;
} IdNode;
typedef struct IdStatus
{
enum StatusCode
{
Valid,
} StatusCode;
} IdStatus;
IdNode *MakeIdTree(struct Node *astNode, IdNode *parent);
void PrintIdNode(IdNode *node);
void PrintIdTree(IdNode *tree, uint32_t tabCount);
int PrintAncestors(IdNode *node);
IdNode *LookdownId(IdNode *root, NodeType targetType, char *targetName);
IdNode *LookupId(IdNode *node, IdNode *prev, char *target);
#endif /* WRAITH_IDENTCHECK_H */

View File

@ -2,8 +2,9 @@
#include <stdlib.h>
#include "codegen.h"
#include "identcheck.h"
#include "parser.h"
#include "validation.h"
#include "typeutils.h"
int main(int argc, char *argv[])
{
@ -85,15 +86,22 @@ int main(int argc, char *argv[])
}
else
{
LinkParentPointers(rootNode, NULL);
/* FIXME: ValidateIdentifiers should return some sort of
error status object. */
ValidateIdentifiers(rootNode);
TagIdentifierTypes(rootNode);
ConvertCustomsToGenerics(rootNode);
PrintNode(rootNode, 0);
{
IdNode *idTree = MakeIdTree(rootNode, NULL);
printf("\n");
PrintIdTree(idTree, /*tabCount=*/0);
printf("Beginning codegen.\n");
printf("\nConverting custom types in the ID-tree.\n");
ConvertIdCustomsToGenerics(idTree);
printf("\n");
PrintIdTree(idTree, /*tabCount=*/0);
printf("\nConverting custom type nodes in the AST.\n");
ConvertASTCustomsToGenerics(rootNode);
printf("\n");
PrintNode(rootNode, /*tabCount=*/0);
}
exitCode = Codegen(rootNode, optimizationLevel);
}
}

83
src/typeutils.c Normal file
View File

@ -0,0 +1,83 @@
#include "typeutils.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void ConvertIdCustomsToGenerics(IdNode *node) {
uint32_t i;
switch(node->type)
{
case UnorderedScope:
case OrderedScope:
case Struct:
/* FIXME: This case will need to be modified to handle type parameters over structs. */
for (i = 0; i < node->childCount; i += 1) {
ConvertIdCustomsToGenerics(node->children[i]);
}
return;
case Variable: {
TypeTag *varType = node->typeTag;
if (varType->type == Custom) {
IdNode *x = LookupId(node->parent, node, varType->value.customType);
if (x != NULL && x->type == GenericType) {
varType->type = Generic;
}
}
return;
}
case Function: {
TypeTag *funcType = node->typeTag;
if (funcType->type == Custom) {
/* For functions we have to handle the type lookup manually since the generic type
* identifiers are declared as children of the function's IdNode. */
for (i = 0; i < node->childCount; i += 1) {
IdNode *child = node->children[i];
if (child->type == GenericType && strcmp(child->name, funcType->value.customType) == 0) {
funcType->type = Generic;
}
}
}
for (i = 0; i < node->childCount; i += 1) {
ConvertIdCustomsToGenerics(node->children[i]);
}
return;
}
}
}
/* FIXME: This function will need to be modified to handle type parameters over structs. */
void ConvertASTCustomsToGenerics(Node *node) {
uint32_t i;
switch (node->syntaxKind)
{
case Declaration:
{
Node *type = node->declaration.type->type.typeNode;
Node *id = node->declaration.identifier;
if (id->typeTag->type == Generic && type->syntaxKind == CustomTypeNode) {
free(node->declaration.type);
node->declaration.type = MakeGenericTypeNode(id->typeTag->value.genericType);
}
return;
}
case FunctionSignature:
{
Node *id = node->functionSignature.identifier;
Node *type = node->functionSignature.type;
if (id->typeTag->type == Generic && type->syntaxKind == CustomTypeNode) {
free(node->functionSignature.type);
node->functionSignature.type = MakeGenericTypeNode(id->typeTag->value.genericType);
}
ConvertASTCustomsToGenerics(node->functionSignature.arguments);
return;
}
default:
recurse(node, *ConvertASTCustomsToGenerics);
}
}

13
src/typeutils.h Normal file
View File

@ -0,0 +1,13 @@
/* Helper functions for working with types in the AST and ID-tree. */
#ifndef WRAITH_TYPEUTILS_H
#define WRAITH_TYPEUTILS_H
#include "ast.h"
#include "identcheck.h"
/* FIXME: These two functions will need to be modified to handle type parameters over structs. */
void ConvertIdCustomsToGenerics(IdNode *node);
void ConvertASTCustomsToGenerics(Node *node);
#endif /* WRAITH_TYPEUTILS_H */

View File

@ -5,7 +5,7 @@
char *strdup(const char *s)
{
size_t slen = strlen(s);
char *result = (char *)malloc(sizeof(char) * (slen + 1));
char *result = (char *)malloc(slen + 1);
if (result == NULL)
{
return NULL;
@ -15,15 +15,6 @@ char *strdup(const char *s)
return result;
}
char *w_strcat(char *s, char *s2)
{
size_t slen = strlen(s);
size_t slen2 = strlen(s2);
s = realloc(s, sizeof(char) * (slen + slen2 + 1));
strcat(s, s2);
return s;
}
uint64_t str_hash(char *str)
{
uint64_t hash = 5381;

View File

@ -5,7 +5,6 @@
#include <string.h>
char *strdup(const char *s);
char *w_strcat(char *s, char *s2);
uint64_t str_hash(char *str);
#endif /* WRAITH_UTIL_H */

View File

@ -1,493 +0,0 @@
#include "validation.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Node *GetIdFromStruct(Node *structDecl)
{
if (structDecl->syntaxKind != StructDeclaration)
{
fprintf(
stderr,
"wraith: Attempted to call GetIdFromStruct on node with kind: "
"%s.\n",
SyntaxKindString(structDecl->syntaxKind));
return NULL;
}
return structDecl->structDeclaration.identifier;
}
Node *GetIdFromFunction(Node *funcDecl)
{
if (funcDecl->syntaxKind != FunctionDeclaration)
{
fprintf(
stderr,
"wraith: Attempted to call GetIdFromFunction on node with kind: "
"%s.\n",
SyntaxKindString(funcDecl->syntaxKind));
return NULL;
}
Node *sig = funcDecl->functionDeclaration.functionSignature;
return sig->functionSignature.identifier;
}
Node *GetIdFromDeclaration(Node *decl)
{
if (decl->syntaxKind != Declaration)
{
fprintf(
stderr,
"wraith: Attempted to call GetIdFromDeclaration on node with kind: "
"%s.\n",
SyntaxKindString(decl->syntaxKind));
}
return decl->declaration.identifier;
}
bool AssignmentHasDeclaration(Node *assign)
{
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",
SyntaxKindString(assign->syntaxKind));
}
if (AssignmentHasDeclaration(assign))
{
return GetIdFromDeclaration(assign->assignmentStatement.left);
}
return NULL;
}
bool NodeMayHaveId(Node *node)
{
switch (node->syntaxKind)
{
case StructDeclaration:
case FunctionDeclaration:
case Declaration:
case Assignment:
return true;
default:
return false;
}
}
Node *TryGetId(Node *node)
{
switch (node->syntaxKind)
{
case Assignment:
return GetIdFromAssignment(node);
case Declaration:
return GetIdFromDeclaration(node);
case FunctionDeclaration:
return GetIdFromFunction(node);
case StructDeclaration:
return GetIdFromStruct(node);
default:
return NULL;
}
}
Node *LookupFunctionArgId(Node *funcDecl, char *target)
{
Node *args = funcDecl->functionDeclaration.functionSignature
->functionSignature.arguments;
uint32_t i;
for (i = 0; i < args->functionArgumentSequence.count; i += 1)
{
Node *arg = args->functionArgumentSequence.sequence[i];
if (arg->syntaxKind != Declaration)
{
fprintf(
stderr,
"wraith: Encountered %s node in function signature args "
"list.\n",
SyntaxKindString(arg->syntaxKind));
continue;
}
Node *argId = GetIdFromDeclaration(arg);
if (argId != NULL && strcmp(target, argId->identifier.name) == 0)
return argId;
}
return NULL;
}
Node *LookupStructInternalId(Node *structDecl, char *target)
{
Node *decls = structDecl->structDeclaration.declarationSequence;
uint32_t i;
for (i = 0; i < decls->declarationSequence.count; i += 1)
{
Node *match = TryGetId(decls->declarationSequence.sequence[i]);
if (match != NULL && strcmp(target, match->identifier.name) == 0)
return match;
}
return NULL;
}
Node *InspectNode(Node *node, char *target)
{
/* 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)
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 (node->syntaxKind == FunctionDeclaration)
{
Node *match = LookupFunctionArgId(node, target);
if (match != NULL)
return match;
}
/* 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;
}
return NULL;
}
/* FIXME: Handle staged lookups for AccessExpressions. */
/* FIXME: Similar to above, disallow inspection of struct internals outside of
* AccessExpressions. */
Node *LookupId(Node *current, Node *prev, char *target)
{
if (current == NULL)
return NULL;
Node *match;
/* First inspect the current node to see if it contains the target
* identifier. */
match = InspectNode(current, target);
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.
*
* 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. The same is true for functions. */
if (prev == NULL)
return LookupId(current->parent, current, target);
uint32_t i;
uint32_t idxLimit;
switch (current->syntaxKind)
{
case DeclarationSequence:
for (i = 0; i < current->declarationSequence.count; i += 1)
{
Node *decl = current->declarationSequence.sequence[i];
match = InspectNode(decl, target);
if (match != NULL)
return match;
}
break;
case StatementSequence:
idxLimit = current->statementSequence.count;
for (i = 0; i < current->statementSequence.count; i += 1)
{
if (current->statementSequence.sequence[i] == prev)
{
idxLimit = i;
break;
}
}
for (i = 0; i < idxLimit; i += 1)
{
Node *stmt = current->statementSequence.sequence[i];
if (stmt == prev)
break;
match = InspectNode(stmt, target);
if (match != NULL)
return match;
}
break;
}
return LookupId(current->parent, current, target);
}
/* FIXME: This function should be extended to handle multi-stage ID lookups for
* AccessExpression nodes. */
/* FIXME: Make this function return an error status object of some kind.
* A non-OK status should halt compilation. */
void ValidateIdentifiers(Node *node)
{
if (node == NULL)
return;
/* Skip over generic arguments. They contain Identifiers but are not
* actually identifiers, they declare types. */
if (node->syntaxKind == GenericDeclarations)
return;
if (node->syntaxKind != Identifier)
{
Recurse(node, *ValidateIdentifiers);
return;
}
char *name = node->identifier.name;
Node *decl = LookupId(node, NULL, name);
if (decl == NULL)
{
/* FIXME: Express this case as an error with AST information, see the
* FIXME comment above. */
fprintf(
stderr,
"wraith: Could not find definition of identifier %s.\n",
name);
}
}
/* FIXME: This function should be extended to handle multi-stage ID lookups for
* AccessExpression nodes. */
void TagIdentifierTypes(Node *node)
{
if (node == NULL)
return;
switch (node->syntaxKind)
{
case AllocExpression:
node->typeTag = MakeTypeTag(node);
break;
case Declaration:
node->declaration.identifier->typeTag = MakeTypeTag(node);
break;
case FunctionDeclaration:
node->functionDeclaration.functionSignature->functionSignature
.identifier->typeTag = MakeTypeTag(node);
break;
case StructDeclaration:
node->structDeclaration.identifier->typeTag = MakeTypeTag(node);
break;
case GenericDeclaration:
node->genericDeclaration.identifier->typeTag = MakeTypeTag(node);
break;
case Type:
node->typeTag = MakeTypeTag(node);
break;
case Identifier:
{
if (node->typeTag != NULL)
return;
char *name = node->identifier.name;
Node *declaration = LookupId(node, NULL, name);
/* FIXME: Remove this case once ValidateIdentifiers returns error status
* info and halts compilation. See ValidateIdentifiers FIXME. */
if (declaration == NULL)
{
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
tag->type = Unknown;
node->typeTag = tag;
}
else
{
node->typeTag = declaration->typeTag;
}
break;
}
}
Recurse(node, *TagIdentifierTypes);
}
Node *LookupType(Node *current, char *target)
{
if (current == NULL)
return NULL;
switch (current->syntaxKind)
{
/* If we've encountered a function declaration, check to see if it's generic
* and, if so, if one of its type parameters is the target. */
case FunctionDeclaration:
{
Node *typeArgs = current->functionDeclaration.functionSignature
->functionSignature.genericDeclarations;
uint32_t i;
for (i = 0; i < typeArgs->genericDeclarations.count; i += 1)
{
Node *arg = typeArgs->genericDeclarations.declarations[i];
Node *argId = arg->genericDeclaration.identifier;
char *argName = argId->identifier.name;
/* note: return the GenericDeclaration, not the Identifier, so that
* the caller can differentiate between generics and customs. */
if (strcmp(target, argName) == 0)
{
return arg;
}
}
return LookupType(current->parent, target);
}
case StructDeclaration:
{
uint32_t i;
Node *typeArgs = current->structDeclaration.genericDeclarations;
for (i = 0; i < typeArgs->genericDeclarations.count; i += 1)
{
Node *arg = typeArgs->genericDeclarations.declarations[i];
Node *argId = arg->genericDeclaration.identifier;
char *argName = argId->identifier.name;
/* note: return the GenericDeclaration, not the Identifier, so that
* the caller can differentiate between generics and customs. */
if (strcmp(target, argName) == 0)
{
return arg;
}
}
Node *structId = GetIdFromStruct(current);
if (strcmp(target, structId->identifier.name) == 0)
{
return structId;
}
return LookupType(current->parent, target);
}
/* If we encounter a declaration sequence, search each of its children for
* struct definitions in case one of them is the target. */
case DeclarationSequence:
{
uint32_t i;
for (i = 0; i < current->declarationSequence.count; i += 1)
{
Node *decl = current->declarationSequence.sequence[i];
if (decl->syntaxKind == StructDeclaration)
{
Node *structId = GetIdFromStruct(decl);
if (strcmp(target, structId->identifier.name) == 0)
return structId;
}
}
return LookupType(current->parent, target);
}
default:
return LookupType(current->parent, target);
}
}
/* FIXME: This function should be modified to handle type parameters over
* structs. */
void ConvertCustomsToGenerics(Node *node)
{
if (node == NULL)
return;
switch (node->syntaxKind)
{
case Declaration:
{
Node *id = node->declaration.identifier;
Node *type = node->declaration.type->type.typeNode;
if (type->syntaxKind == CustomTypeNode)
{
char *target = id->typeTag->value.customType;
Node *typeLookup = LookupType(node, target);
if (typeLookup != NULL &&
typeLookup->syntaxKind == GenericDeclaration)
{
id->typeTag->type = Generic;
free(node->declaration.type);
node->declaration.type =
MakeGenericTypeNode(id->typeTag->value.genericType);
}
}
break;
}
case FunctionSignature:
{
Node *id = node->functionSignature.identifier;
Node *type = node->functionSignature.type->type.typeNode;
if (type->syntaxKind == CustomTypeNode)
{
char *target = id->typeTag->value.customType;
Node *typeLookup = LookupType(node, target);
if (typeLookup != NULL &&
typeLookup->syntaxKind == GenericDeclaration)
{
id->typeTag->type = Generic;
free(node->functionSignature.type);
node->functionSignature.type =
MakeGenericTypeNode(id->typeTag->value.genericType);
}
}
break;
}
case GenericArgument:
{
Node *typeNode = node->genericArgument.type;
if (typeNode->typeTag->type == Custom)
{
char *target = typeNode->typeTag->value.customType;
Node *typeLookup = LookupType(node, target);
if (typeLookup != NULL &&
typeLookup->syntaxKind == GenericDeclaration)
{
typeNode->typeTag->type = Generic;
}
}
break;
}
}
Recurse(node, *ConvertCustomsToGenerics);
}

View File

@ -1,10 +0,0 @@
#ifndef WRAITH_VALIDATION_H
#define WRAITH_VALIDATION_H
#include "ast.h"
void ValidateIdentifiers(Node *node);
void TagIdentifierTypes(Node *node);
void ConvertCustomsToGenerics(Node *node);
#endif /* WRAITH_VALIDATION_H */