wraith-lang/src/ast.h

344 lines
7.3 KiB
C
Raw Normal View History

2021-04-18 20:14:50 +00:00
#ifndef WRAITH_AST_H
#define WRAITH_AST_H
#include "identcheck.h"
2021-05-16 07:42:37 +00:00
#include <stdint.h>
2021-04-16 07:35:35 +00:00
/* -Wpedantic nameless union/struct silencing */
#ifndef WRAITHNAMELESS
#ifdef __GNUC__
#define WRAITHNAMELESS __extension__
#else
#define WRAITHNAMELESS
#endif /* __GNUC__ */
#endif /* WRAITHNAMELESS */
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
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-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-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;
typedef struct Node Node;
struct Node
2021-04-16 07:35:35 +00:00
{
Node *parent;
2021-04-16 07:35:35 +00:00
SyntaxKind syntaxKind;
WRAITHNAMELESS union
2021-04-16 21:40:28 +00:00
{
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;
};
2021-05-14 18:53:09 +00:00
TypeTag *typeTag;
IdNode *idLink;
};
2021-04-16 07:35:35 +00:00
2021-05-16 07:42:37 +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);
2021-05-16 07:42:37 +00:00
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);
2021-04-29 19:42:51 +00:00
Node *MakeEmptyFunctionSignatureArgumentsNode();
2021-05-16 07:42:37 +00:00
Node *MakeFunctionSignatureNode(
2021-04-18 20:14:50 +00:00
Node *identifierNode,
2021-05-16 07:42:37 +00:00
Node *typeNode,
Node *argumentsNode,
Node *modifiersNode);
Node *MakeFunctionDeclarationNode(
Node *functionSignatureNode,
Node *functionBodyNode);
Node *MakeStructDeclarationNode(
2021-04-18 20:14:50 +00:00
Node *identifierNode,
2021-05-16 07:42:37 +00:00
Node *declarationSequenceNode);
Node *StartDeclarationSequenceNode(Node *declarationNode);
Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode);
Node *StartFunctionArgumentSequenceNode(Node *argumentNode);
Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode);
2021-04-29 19:42:51 +00:00
Node *MakeEmptyFunctionArgumentSequenceNode();
2021-05-16 07:42:37 +00:00
Node *MakeFunctionCallExpressionNode(
2021-04-20 17:47:40 +00:00
Node *identifierNode,
2021-05-16 07:42:37 +00:00
Node *argumentSequenceNode);
Node *MakeAccessExpressionNode(Node *accessee, Node *accessor);
Node *MakeAllocNode(Node *typeNode);
Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode);
Node *MakeIfElseNode(
2021-04-29 19:42:51 +00:00
Node *ifNode,
Node *elseNode /* can be a conditional or a statement sequence */
2021-04-29 19:42:51 +00:00
);
2021-05-16 07:42:37 +00:00
Node *MakeForLoopNode(
2021-04-30 06:49:35 +00:00
Node *identifierNode,
Node *startNumberNode,
Node *endNumberNode,
2021-05-16 07:42:37 +00:00
Node *statementSequenceNode);
2021-04-18 20:14:50 +00:00
void PrintNode(Node *node, uint32_t tabCount);
2021-05-16 07:42:37 +00:00
const char *SyntaxKindString(SyntaxKind syntaxKind);
2021-04-18 20:14:50 +00:00
2021-05-16 07:42:37 +00:00
TypeTag *MakeTypeTag(Node *node);
char *TypeTagToString(TypeTag *tag);
2021-05-08 00:49:35 +00:00
2021-04-18 20:14:50 +00:00
#endif /* WRAITH_AST_H */