wraith-lang/src/ast.h

260 lines
4.9 KiB
C
Raw Normal View History

2021-04-18 20:14:50 +00:00
#ifndef WRAITH_AST_H
#define WRAITH_AST_H
2021-04-16 07:35:35 +00:00
#include <stdint.h>
#include "identcheck.h"
2021-04-16 07:35:35 +00:00
typedef enum
{
2021-04-22 04:29:38 +00:00
AccessExpression,
2021-04-28 19:49:45 +00:00
AllocExpression,
2021-04-16 07:35:35 +00:00
Assignment,
BinaryExpression,
Comment,
2021-04-24 19:59:30 +00:00
CustomTypeNode,
2021-04-16 21:40:28 +00:00
Declaration,
2021-04-18 20:14:50 +00:00
DeclarationSequence,
2021-04-16 07:35:35 +00:00
Expression,
ForLoop,
2021-04-20 17:47:40 +00:00
FunctionArgumentSequence,
FunctionCallExpression,
2021-04-18 20:14:50 +00:00
FunctionDeclaration,
2021-04-22 07:35:42 +00:00
FunctionModifiers,
2021-04-18 20:14:50 +00:00
FunctionSignature,
2021-04-20 01:18:45 +00:00
FunctionSignatureArguments,
2021-05-03 07:06:54 +00:00
GenericConstraint,
GenericConstraints,
2021-04-16 07:35:35 +00:00
Identifier,
2021-04-29 04:25:25 +00:00
IfStatement,
2021-04-29 19:42:51 +00:00
IfElseStatement,
2021-05-03 07:06:54 +00:00
InterfaceDeclaration,
2021-04-16 07:35:35 +00:00
Number,
2021-04-24 19:59:30 +00:00
PrimitiveTypeNode,
ReferenceTypeNode,
2021-04-16 07:35:35 +00:00
Return,
2021-04-21 02:00:18 +00:00
ReturnVoid,
2021-04-18 20:14:50 +00:00
StatementSequence,
2021-04-22 07:35:42 +00:00
StaticModifier,
2021-04-16 21:40:28 +00:00
StringLiteral,
2021-04-18 20:14:50 +00:00
StructDeclaration,
Type,
2021-04-16 07:35:35 +00:00
UnaryExpression
} SyntaxKind;
typedef enum
{
Negate
} UnaryOperator;
typedef enum
{
Add,
2021-04-20 17:47:40 +00:00
Subtract,
2021-04-29 04:25:25 +00:00
Multiply,
2021-04-30 06:49:35 +00:00
Mod,
Equal,
2021-04-29 22:26:30 +00:00
LessThan,
2021-04-30 06:49:35 +00:00
GreaterThan,
LogicalOr
2021-04-16 07:35:35 +00:00
} BinaryOperator;
2021-04-16 21:40:28 +00:00
typedef enum
{
2021-04-21 02:00:18 +00:00
Void,
2021-04-16 21:40:28 +00:00
Bool,
Int,
UInt,
Float,
Double,
2021-04-24 19:59:30 +00:00
String
2021-04-16 21:40:28 +00:00
} PrimitiveType;
2021-04-16 07:35:35 +00:00
typedef union
{
UnaryOperator unaryOperator;
BinaryOperator binaryOperator;
} Operator;
2021-05-08 00:49:35 +00:00
typedef struct TypeTag
{
2021-05-14 18:53:09 +00:00
enum Type
2021-05-08 00:49:35 +00:00
{
Unknown,
Primitive,
Reference,
Custom
} type;
2021-05-14 18:53:09 +00:00
union
2021-05-08 00:49:35 +00:00
{
2021-05-14 18:53:09 +00:00
/* Valid when type = Primitive. */
2021-05-08 00:49:35 +00:00
PrimitiveType primitiveType;
2021-05-14 18:53:09 +00:00
/* Valid when type = Reference. */
2021-05-08 00:49:35 +00:00
struct TypeTag *referenceType;
2021-05-14 18:53:09 +00:00
/* Valid when type = Custom. */
2021-05-08 00:49:35 +00:00
char *customType;
} value;
} TypeTag;
2021-04-16 07:35:35 +00:00
typedef struct Node
{
SyntaxKind syntaxKind;
struct Node **children;
uint32_t childCount;
union
{
UnaryOperator unaryOperator;
BinaryOperator binaryOperator;
} operator;
2021-04-16 21:40:28 +00:00
union
{
char *string;
uint64_t number;
} value;
2021-04-24 19:59:30 +00:00
PrimitiveType primitiveType;
2021-05-14 18:53:09 +00:00
TypeTag *typeTag;
IdNode *idLink;
2021-04-16 07:35:35 +00:00
} Node;
2021-04-18 22:29:54 +00:00
const char* SyntaxKindString(SyntaxKind syntaxKind);
2021-04-16 07:35:35 +00:00
2021-04-24 19:59:30 +00:00
uint8_t IsPrimitiveType(Node *typeNode);
Node* MakePrimitiveTypeNode(
2021-04-18 20:14:50 +00:00
PrimitiveType type
2021-04-18 22:29:54 +00:00
);
2021-04-21 06:51:26 +00:00
Node* MakeCustomTypeNode(
2021-04-24 19:59:30 +00:00
char *string
);
Node* MakeReferenceTypeNode(
Node *typeNode
);
Node* MakeTypeNode(
Node *typeNode /* can be primitive, custom, or reference */
2021-04-21 06:51:26 +00:00
);
2021-04-16 07:35:35 +00:00
Node* MakeIdentifierNode(
const char *id
2021-04-18 22:29:54 +00:00
);
2021-04-16 07:35:35 +00:00
Node* MakeNumberNode(
const char *numberString
2021-04-18 22:29:54 +00:00
);
2021-04-16 07:35:35 +00:00
Node* MakeStringNode(
const char *string
2021-04-18 22:29:54 +00:00
);
2021-04-22 07:35:42 +00:00
Node* MakeStaticNode();
Node* MakeFunctionModifiersNode(
Node **pModifierNodes,
uint32_t modifierCount
);
2021-04-16 07:35:35 +00:00
Node* MakeUnaryNode(
UnaryOperator operator,
Node *child
2021-04-18 22:29:54 +00:00
);
2021-04-16 07:35:35 +00:00
Node* MakeBinaryNode(
BinaryOperator operator,
Node *left,
Node *right
2021-04-18 22:29:54 +00:00
);
2021-04-16 21:40:28 +00:00
Node* MakeDeclarationNode(
2021-04-18 20:14:50 +00:00
Node* typeNode,
Node* identifierNode
2021-04-18 22:29:54 +00:00
);
2021-04-16 21:40:28 +00:00
Node* MakeAssignmentNode(
Node *left,
Node *right
2021-04-18 22:29:54 +00:00
);
2021-04-29 19:42:51 +00:00
Node* StartStatementSequenceNode(
Node* statementNode
);
Node* AddStatement(
Node* statementSequenceNode,
Node *statementNode
2021-04-18 22:29:54 +00:00
);
2021-04-18 20:30:50 +00:00
Node* MakeReturnStatementNode(
Node *expressionNode
2021-04-18 22:29:54 +00:00
);
2021-04-21 02:00:18 +00:00
Node* MakeReturnVoidStatementNode();
2021-04-29 19:42:51 +00:00
Node* StartFunctionSignatureArgumentsNode(
Node *argumentNode
);
Node* AddFunctionSignatureArgumentNode(
Node *argumentsNode,
Node *argumentNode
2021-04-20 01:18:45 +00:00
);
2021-05-03 07:06:54 +00:00
Node* MakeEmptyFunctionSignatureArgumentsNode();
Node* MakeGenericConstraintNode(
Node *identifierNode,
Node *interfaceNode
);
Node* StartGenericConstraintsNode(Node *genericNode);
Node* AddGenericConstraint(
Node *genericsNode,
Node *genericNode
);
Node* MakeEmptyGenericConstraintsNode();
2021-04-18 20:14:50 +00:00
Node* MakeFunctionSignatureNode(
Node *identifierNode,
2021-05-03 07:06:54 +00:00
Node *typeNode,
Node *argumentsNode,
Node *modifiersNode,
Node *genericConstraintsNode
2021-04-18 22:29:54 +00:00
);
2021-04-18 20:14:50 +00:00
Node* MakeFunctionDeclarationNode(
2021-05-03 07:06:54 +00:00
Node *functionSignatureNode,
Node *functionBodyNode
2021-04-18 22:29:54 +00:00
);
2021-04-18 20:14:50 +00:00
Node* MakeStructDeclarationNode(
Node *identifierNode,
Node *declarationSequenceNode
2021-04-18 22:29:54 +00:00
);
2021-05-03 07:06:54 +00:00
Node* MakeInterfaceDeclarationNode(
Node *identifierNode,
Node *declarationSequenceNode
);
2021-04-29 19:42:51 +00:00
Node* StartDeclarationSequenceNode(
Node *declarationNode
);
Node* AddDeclarationNode(
Node *declarationSequenceNode,
Node *declarationNode
2021-04-18 22:29:54 +00:00
);
2021-04-29 19:42:51 +00:00
Node *StartFunctionArgumentSequenceNode(
Node *argumentNode
2021-04-20 17:47:40 +00:00
);
2021-04-29 19:42:51 +00:00
Node *AddFunctionArgumentNode(
Node *argumentSequenceNode,
Node *argumentNode
);
Node *MakeEmptyFunctionArgumentSequenceNode();
2021-04-20 17:47:40 +00:00
Node* MakeFunctionCallExpressionNode(
Node *identifierNode,
Node *argumentSequenceNode
);
2021-04-22 04:29:38 +00:00
Node* MakeAccessExpressionNode(
Node *accessee,
Node *accessor
);
2021-04-28 19:49:45 +00:00
Node* MakeAllocNode(
Node *typeNode
);
2021-04-29 04:25:25 +00:00
Node* MakeIfNode(
Node *expressionNode,
Node *statementSequenceNode
);
2021-04-29 19:42:51 +00:00
Node* MakeIfElseNode(
Node *ifNode,
Node *statementSequenceNode
);
2021-04-30 06:49:35 +00:00
Node* MakeForLoopNode(
Node *identifierNode,
Node *startNumberNode,
Node *endNumberNode,
Node *statementSequenceNode
);
2021-04-18 20:14:50 +00:00
2021-04-18 22:29:54 +00:00
void PrintTree(Node *node, uint32_t tabCount);
const char* SyntaxKindString(SyntaxKind syntaxKind);
2021-04-18 20:14:50 +00:00
2021-05-08 00:49:35 +00:00
TypeTag* MakeTypeTag(Node *node);
char* TypeTagToString(TypeTag *tag);
2021-04-18 20:14:50 +00:00
#endif /* WRAITH_AST_H */