wraith-lang/src/ast.h

260 lines
4.9 KiB
C

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