wraith-lang/src/ast.h

344 lines
7.3 KiB
C

#ifndef WRAITH_AST_H
#define WRAITH_AST_H
#include "identcheck.h"
#include <stdint.h>
/* -Wpedantic nameless union/struct silencing */
#ifndef WRAITHNAMELESS
#ifdef __GNUC__
#define WRAITHNAMELESS __extension__
#else
#define WRAITHNAMELESS
#endif /* __GNUC__ */
#endif /* WRAITHNAMELESS */
typedef enum
{
AccessExpression,
AllocExpression,
Assignment,
BinaryExpression,
Comment,
CustomTypeNode,
Declaration,
DeclarationSequence,
ForLoop,
FunctionArgumentSequence,
FunctionCallExpression,
FunctionDeclaration,
FunctionModifiers,
FunctionSignature,
FunctionSignatureArguments,
Identifier,
IfStatement,
IfElseStatement,
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 Node;
struct Node
{
Node *parent;
SyntaxKind syntaxKind;
WRAITHNAMELESS union
{
struct
{
Node *accessee;
Node *accessor;
} accessExpression;
struct
{
Node *type;
} allocExpression;
struct
{
Node *left;
Node *right;
} assignmentStatement;
struct
{
Node *left;
Node *right;
BinaryOperator operator;
} binaryExpression;
struct
{
} comment;
struct
{
char *name;
} customType;
struct
{
Node *type;
Node *identifier;
} declaration;
struct
{
Node **sequence;
uint32_t count;
} declarationSequence;
struct
{
Node *declaration;
Node *startNumber;
Node *endNumber;
Node *statementSequence;
} forLoop;
struct
{
Node **sequence;
uint32_t count;
} functionArgumentSequence;
struct
{
Node *identifier; /* FIXME: need better name */
Node *argumentSequence;
} functionCallExpression;
struct
{
Node *functionSignature;
Node *functionBody;
} functionDeclaration;
struct
{
Node **sequence;
uint32_t count;
} functionModifiers;
struct
{
Node *identifier;
Node *type;
Node *arguments;
Node *modifiers;
} functionSignature;
struct
{
Node **sequence;
uint32_t count;
} functionSignatureArguments;
struct
{
char *name;
} identifier;
struct
{
Node *expression;
Node *statementSequence;
} ifStatement;
struct
{
Node *ifStatement;
Node *elseStatement;
} ifElseStatement;
struct
{
uint64_t value;
} number;
struct
{
PrimitiveType type;
} primitiveType;
struct
{
Node *type;
} referenceType;
struct
{
Node *expression;
} returnStatement;
struct
{
} returnVoidStatement;
struct
{
Node **sequence;
uint32_t count;
} statementSequence;
struct
{
} staticModifier; /* FIXME: modifiers should just be an enum */
struct
{
char *string;
} stringLiteral;
struct
{
Node *identifier;
Node *declarationSequence;
} structDeclaration;
struct
{
Node *typeNode;
} type; /* FIXME: this needs a refactor */
struct
{
Node *child;
UnaryOperator operator;
} unaryExpression;
};
TypeTag *typeTag;
IdNode *idLink;
};
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);
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 *MakeFunctionSignatureNode(
Node *identifierNode,
Node *typeNode,
Node *argumentsNode,
Node *modifiersNode);
Node *MakeFunctionDeclarationNode(
Node *functionSignatureNode,
Node *functionBodyNode);
Node *MakeStructDeclarationNode(
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 *elseNode /* can be a conditional or a statement sequence */
);
Node *MakeForLoopNode(
Node *identifierNode,
Node *startNumberNode,
Node *endNumberNode,
Node *statementSequenceNode);
void PrintNode(Node *node, uint32_t tabCount);
const char *SyntaxKindString(SyntaxKind syntaxKind);
TypeTag *MakeTypeTag(Node *node);
char *TypeTagToString(TypeTag *tag);
#endif /* WRAITH_AST_H */