started work on generics
parent
41bf2bece8
commit
a529bb6e4f
10
example.w
10
example.w
|
@ -1,6 +1,16 @@
|
||||||
|
interface Increments
|
||||||
|
{
|
||||||
|
Increment(): void;
|
||||||
|
}
|
||||||
|
|
||||||
struct YourStruct
|
struct YourStruct
|
||||||
{
|
{
|
||||||
yourInt: int;
|
yourInt: int;
|
||||||
|
|
||||||
|
IncrementOther<T: Increments>(other: T): void
|
||||||
|
{
|
||||||
|
other.Increment();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MyStruct
|
struct MyStruct
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
"string" return STRING;
|
"string" return STRING;
|
||||||
"bool" return BOOL;
|
"bool" return BOOL;
|
||||||
"struct" return STRUCT;
|
"struct" return STRUCT;
|
||||||
|
"interface" return INTERFACE;
|
||||||
"return" return RETURN;
|
"return" return RETURN;
|
||||||
"static" return STATIC;
|
"static" return STATIC;
|
||||||
"Reference" return REFERENCE;
|
"Reference" return REFERENCE;
|
||||||
|
|
|
@ -26,6 +26,7 @@ extern FILE *yyin;
|
||||||
%token STRING
|
%token STRING
|
||||||
%token BOOL
|
%token BOOL
|
||||||
%token STRUCT
|
%token STRUCT
|
||||||
|
%token INTERFACE
|
||||||
%token RETURN
|
%token RETURN
|
||||||
%token STATIC
|
%token STATIC
|
||||||
%token REFERENCE
|
%token REFERENCE
|
||||||
|
@ -307,14 +308,40 @@ Body : LEFT_BRACE Statements RIGHT_BRACE
|
||||||
$$ = $2;
|
$$ = $2;
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionSignature : Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
|
GenericConstraint : Identifier COLON Identifier
|
||||||
{
|
{
|
||||||
$$ = MakeFunctionSignatureNode($1, $6, $3, MakeFunctionModifiersNode(NULL, 0));
|
$$ = MakeGenericConstraintNode($1, $3);
|
||||||
|
}
|
||||||
|
|
||||||
|
GenericConstraints : GenericConstraint
|
||||||
|
{
|
||||||
|
$$ = StartGenericConstraintsNode($1);
|
||||||
|
}
|
||||||
|
| GenericConstraints COMMA GenericConstraint
|
||||||
|
{
|
||||||
|
$$ = AddGenericConstraint($1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
GenericConstraintClause : LESS_THAN GenericConstraints GREATER_THAN
|
||||||
|
{
|
||||||
|
$$ = $2;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
{
|
||||||
|
$$ = MakeEmptyGenericConstraintsNode();
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
/* FIXME: modifiers should be recursive */
|
||||||
|
FunctionSignature : Identifier GenericConstraintClause LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
|
||||||
|
{
|
||||||
|
$$ = MakeFunctionSignatureNode($1, $7, $4, MakeFunctionModifiersNode(NULL, 0), $2);
|
||||||
}
|
}
|
||||||
| STATIC Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
|
| STATIC Identifier LEFT_PAREN SignatureArguments RIGHT_PAREN COLON Type
|
||||||
{
|
{
|
||||||
Node *modifier = MakeStaticNode();
|
Node *modifier = MakeStaticNode();
|
||||||
$$ = MakeFunctionSignatureNode($2, $7, $4, MakeFunctionModifiersNode(&modifier, 1));
|
$$ = MakeFunctionSignatureNode($2, $7, $4, MakeFunctionModifiersNode(&modifier, 1), MakeEmptyGenericConstraintsNode());
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionDeclaration : FunctionSignature Body
|
FunctionDeclaration : FunctionSignature Body
|
||||||
|
@ -327,6 +354,24 @@ StructDeclaration : STRUCT Identifier LEFT_BRACE Declarations RIGHT_BRACE
|
||||||
$$ = MakeStructDeclarationNode($2, $4);
|
$$ = MakeStructDeclarationNode($2, $4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InterfaceDeclaration : INTERFACE Identifier LEFT_BRACE InterfaceChildren RIGHT_BRACE
|
||||||
|
{
|
||||||
|
$$ = MakeInterfaceDeclarationNode($2, $4);
|
||||||
|
}
|
||||||
|
|
||||||
|
InterfaceChild : FunctionSignature SEMICOLON
|
||||||
|
;
|
||||||
|
|
||||||
|
InterfaceChildren : InterfaceChild
|
||||||
|
{
|
||||||
|
$$ = StartDeclarationSequenceNode($1);
|
||||||
|
}
|
||||||
|
| InterfaceChildren InterfaceChild
|
||||||
|
{
|
||||||
|
$$ = AddDeclarationNode($1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
Declaration : FunctionDeclaration
|
Declaration : FunctionDeclaration
|
||||||
| VariableDeclaration SEMICOLON
|
| VariableDeclaration SEMICOLON
|
||||||
;
|
;
|
||||||
|
@ -340,7 +385,9 @@ Declarations : Declaration
|
||||||
$$ = AddDeclarationNode($1, $2);
|
$$ = AddDeclarationNode($1, $2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TopLevelDeclaration : StructDeclaration;
|
TopLevelDeclaration : StructDeclaration
|
||||||
|
| InterfaceDeclaration
|
||||||
|
;
|
||||||
|
|
||||||
TopLevelDeclarations : TopLevelDeclaration
|
TopLevelDeclarations : TopLevelDeclaration
|
||||||
{
|
{
|
||||||
|
|
56
src/ast.c
56
src/ast.c
|
@ -276,20 +276,59 @@ Node *MakeEmptyFunctionSignatureArgumentsNode()
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node* MakeGenericConstraintNode(Node *identifierNode, Node *interfaceNode)
|
||||||
|
{
|
||||||
|
Node* node = (Node*) malloc(sizeof(Node));
|
||||||
|
node->syntaxKind = GenericConstraint;
|
||||||
|
node->childCount = 2;
|
||||||
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
||||||
|
node->children[0] = identifierNode;
|
||||||
|
node->children[1] = interfaceNode;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* StartGenericConstraintsNode(Node *genericNode)
|
||||||
|
{
|
||||||
|
Node* node = (Node*) malloc(sizeof(Node));
|
||||||
|
node->syntaxKind = GenericConstraints;
|
||||||
|
node->childCount = 1;
|
||||||
|
node->children = (Node**) malloc(sizeof(Node*));
|
||||||
|
node->children[0] = genericNode;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* AddGenericConstraint(Node *genericsNode, Node *genericNode)
|
||||||
|
{
|
||||||
|
genericsNode->children = realloc(genericsNode->children, sizeof(Node*) * (genericsNode->childCount + 1));
|
||||||
|
genericsNode->children[genericsNode->childCount] = genericNode;
|
||||||
|
genericsNode->childCount += 1;
|
||||||
|
return genericsNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* MakeEmptyGenericConstraintsNode()
|
||||||
|
{
|
||||||
|
Node* node = (Node*) malloc(sizeof(Node));
|
||||||
|
node->syntaxKind = GenericConstraints;
|
||||||
|
node->childCount = 0;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
Node* MakeFunctionSignatureNode(
|
Node* MakeFunctionSignatureNode(
|
||||||
Node *identifierNode,
|
Node *identifierNode,
|
||||||
Node* typeNode,
|
Node* typeNode,
|
||||||
Node* arguments,
|
Node* arguments,
|
||||||
Node* modifiersNode
|
Node* modifiersNode,
|
||||||
|
Node* genericConstraintsNode
|
||||||
) {
|
) {
|
||||||
Node* node = (Node*) malloc(sizeof(Node));
|
Node* node = (Node*) malloc(sizeof(Node));
|
||||||
node->syntaxKind = FunctionSignature;
|
node->syntaxKind = FunctionSignature;
|
||||||
node->childCount = 4;
|
node->childCount = 5;
|
||||||
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount));
|
node->children = (Node**) malloc(sizeof(Node*) * (node->childCount));
|
||||||
node->children[0] = identifierNode;
|
node->children[0] = identifierNode;
|
||||||
node->children[1] = typeNode;
|
node->children[1] = typeNode;
|
||||||
node->children[2] = arguments;
|
node->children[2] = arguments;
|
||||||
node->children[3] = modifiersNode;
|
node->children[3] = modifiersNode;
|
||||||
|
node->children[4] = genericConstraintsNode;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,6 +358,19 @@ Node* MakeStructDeclarationNode(
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node* MakeInterfaceDeclarationNode(
|
||||||
|
Node *identifierNode,
|
||||||
|
Node *declarationSequenceNode
|
||||||
|
) {
|
||||||
|
Node* node = (Node*) malloc(sizeof(Node));
|
||||||
|
node->syntaxKind = InterfaceDeclaration;
|
||||||
|
node->childCount = 2;
|
||||||
|
node->children = (Node**) malloc(sizeof(Node*) * 2);
|
||||||
|
node->children[0] = identifierNode;
|
||||||
|
node->children[1] = declarationSequenceNode;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
Node* StartDeclarationSequenceNode(
|
Node* StartDeclarationSequenceNode(
|
||||||
Node *declarationNode
|
Node *declarationNode
|
||||||
) {
|
) {
|
||||||
|
|
20
src/ast.h
20
src/ast.h
|
@ -22,9 +22,12 @@ typedef enum
|
||||||
FunctionModifiers,
|
FunctionModifiers,
|
||||||
FunctionSignature,
|
FunctionSignature,
|
||||||
FunctionSignatureArguments,
|
FunctionSignatureArguments,
|
||||||
|
GenericConstraint,
|
||||||
|
GenericConstraints,
|
||||||
Identifier,
|
Identifier,
|
||||||
IfStatement,
|
IfStatement,
|
||||||
IfElseStatement,
|
IfElseStatement,
|
||||||
|
InterfaceDeclaration,
|
||||||
Number,
|
Number,
|
||||||
PrimitiveTypeNode,
|
PrimitiveTypeNode,
|
||||||
ReferenceTypeNode,
|
ReferenceTypeNode,
|
||||||
|
@ -177,11 +180,22 @@ Node* AddFunctionSignatureArgumentNode(
|
||||||
Node *argumentNode
|
Node *argumentNode
|
||||||
);
|
);
|
||||||
Node* MakeEmptyFunctionSignatureArgumentsNode();
|
Node* MakeEmptyFunctionSignatureArgumentsNode();
|
||||||
|
Node* MakeGenericConstraintNode(
|
||||||
|
Node *identifierNode,
|
||||||
|
Node *interfaceNode
|
||||||
|
);
|
||||||
|
Node* StartGenericConstraintsNode(Node *genericNode);
|
||||||
|
Node* AddGenericConstraint(
|
||||||
|
Node *genericsNode,
|
||||||
|
Node *genericNode
|
||||||
|
);
|
||||||
|
Node* MakeEmptyGenericConstraintsNode();
|
||||||
Node* MakeFunctionSignatureNode(
|
Node* MakeFunctionSignatureNode(
|
||||||
Node *identifierNode,
|
Node *identifierNode,
|
||||||
Node *typeNode,
|
Node *typeNode,
|
||||||
Node *argumentsNode,
|
Node *argumentsNode,
|
||||||
Node* modifiersNode
|
Node *modifiersNode,
|
||||||
|
Node *genericConstraintsNode
|
||||||
);
|
);
|
||||||
Node* MakeFunctionDeclarationNode(
|
Node* MakeFunctionDeclarationNode(
|
||||||
Node *functionSignatureNode,
|
Node *functionSignatureNode,
|
||||||
|
@ -191,6 +205,10 @@ Node* MakeStructDeclarationNode(
|
||||||
Node *identifierNode,
|
Node *identifierNode,
|
||||||
Node *declarationSequenceNode
|
Node *declarationSequenceNode
|
||||||
);
|
);
|
||||||
|
Node* MakeInterfaceDeclarationNode(
|
||||||
|
Node *identifierNode,
|
||||||
|
Node *declarationSequenceNode
|
||||||
|
);
|
||||||
Node* StartDeclarationSequenceNode(
|
Node* StartDeclarationSequenceNode(
|
||||||
Node *declarationNode
|
Node *declarationNode
|
||||||
);
|
);
|
||||||
|
|
|
@ -984,6 +984,11 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
|
||||||
PopScopeFrame(scope);
|
PopScopeFrame(scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void CompileInterface(LLVMModuleRef module, LLVMContextRef context, Node *node)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *node)
|
static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *node)
|
||||||
{
|
{
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
@ -994,9 +999,13 @@ static void Compile(LLVMModuleRef module, LLVMContextRef context, Node *node)
|
||||||
{
|
{
|
||||||
CompileStruct(module, context, node->children[i]);
|
CompileStruct(module, context, node->children[i]);
|
||||||
}
|
}
|
||||||
|
else if (node->children[i]->syntaxKind == InterfaceDeclaration)
|
||||||
|
{
|
||||||
|
CompileInterface(module, context, node->children[i]);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "top level declarations that are not structs are forbidden!\n");
|
fprintf(stderr, "Top-level declarations that are not structs or interfaces are forbidden!\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue