wraith-lang/src/ast.h

409 lines
7.5 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
/* -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-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-04-29 19:42:51 +00:00
Node *MakeEmptyFunctionSignatureArgumentsNode();
2021-04-18 20:14:50 +00:00
Node* MakeFunctionSignatureNode(
Node *identifierNode,
Node* typeNode,
2021-04-29 19:42:51 +00:00
Node* argumentsNode,
2021-04-22 07:35:42 +00:00
Node* modifiersNode
2021-04-18 22:29:54 +00:00
);
2021-04-18 20:14:50 +00:00
Node* MakeFunctionDeclarationNode(
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-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 *elseNode /* can be a conditional or a statement sequence */
2021-04-29 19:42:51 +00:00
);
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
void PrintNode(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 */