summaryrefslogtreecommitdiff
path: root/src/compiler/ast-tree.h
blob: 798a6b480a23f1ffb0e6d1422b7f48e431792780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once

#include "compiler/parser.h"
#include <stddef.h>
#include <stdint.h>

typedef enum AstTreeToken {
  AST_TREE_TOKEN_FUNCTION,

  AST_TREE_TOKEN_KEYWORD_PRINT_U64,
  AST_TREE_TOKEN_KEYWORD_RETURN,
  AST_TREE_TOKEN_KEYWORD_IF,

  AST_TREE_TOKEN_TYPE_TYPE,
  AST_TREE_TOKEN_TYPE_FUNCTION,
  AST_TREE_TOKEN_TYPE_VOID,
  AST_TREE_TOKEN_TYPE_I64,
  AST_TREE_TOKEN_TYPE_U64,
  AST_TREE_TOKEN_TYPE_BOOL,

  AST_TREE_TOKEN_FUNCTION_CALL,
  AST_TREE_TOKEN_VARIABLE,
  AST_TREE_TOKEN_VARIABLE_DEFINE,
  AST_TREE_TOKEN_VALUE_VOID,
  AST_TREE_TOKEN_VALUE_INT,
  AST_TREE_TOKEN_VALUE_BOOL,

  AST_TREE_TOKEN_OPERATOR_ASSIGN,
  AST_TREE_TOKEN_OPERATOR_PLUS,
  AST_TREE_TOKEN_OPERATOR_MINUS,
  AST_TREE_TOKEN_OPERATOR_SUM,
  AST_TREE_TOKEN_OPERATOR_SUB,
  AST_TREE_TOKEN_OPERATOR_MULTIPLY,
  AST_TREE_TOKEN_OPERATOR_DIVIDE,
  AST_TREE_TOKEN_OPERATOR_MODULO,
  AST_TREE_TOKEN_OPERATOR_EQUAL,
  AST_TREE_TOKEN_OPERATOR_NOT_EQUAL,
  AST_TREE_TOKEN_OPERATOR_GREATER,
  AST_TREE_TOKEN_OPERATOR_SMALLER,
  AST_TREE_TOKEN_OPERATOR_GREATER_OR_EQUAL,
  AST_TREE_TOKEN_OPERATOR_SMALLER_OR_EQUAL,

  AST_TREE_TOKEN_SCOPE,

  AST_TREE_TOKEN_NONE,
} AstTreeToken;

extern const char *AST_TREE_TOKEN_STRINGS[];

typedef struct AstTree {
  AstTreeToken token;
  void *metadata;
  struct AstTree *type;
  char *str_begin;
  char *str_end;
} AstTree;

extern AstTree AST_TREE_TYPE_TYPE;
extern AstTree AST_TREE_VOID_TYPE;
extern AstTree AST_TREE_BOOL_TYPE;
extern AstTree AST_TREE_I64_TYPE;
extern AstTree AST_TREE_U64_TYPE;
extern AstTree AST_TREE_VOID_VALUE;

typedef struct AstTreeVariable {
  char *name_begin;
  char *name_end;
  AstTree *type;
  AstTree *value;
  bool isConst;
} AstTreeVariable;

typedef struct AstTreeVariables {
  AstTreeVariable **data;
  size_t size;
} AstTreeVariables;

typedef struct AstTreeRoot {
  AstTreeVariables variables;
} AstTreeRoot;

typedef struct AstTreeScope {
  AstTreeVariables variables;
  AstTree **expressions;
  size_t expressions_size;
} AstTreeScope;

typedef struct AstTreeFunction {
  AstTreeVariables arguments;
  AstTreeScope scope;
  AstTree *returnType;
} AstTreeFunction;

typedef struct AstTreeTypeFunction {
  AstTree **arguments;
  size_t arguments_size;
  AstTree *returnType;
} AstTreeTypeFunction;

typedef struct AstTreeFunctionCall {
  AstTree *function;
  AstTree **parameters;
  size_t parameters_size;
} AstTreeFunctionCall;

typedef uint64_t AstTreeInt;

typedef bool AstTreeBool;

typedef AstTree AstTreeSingleChild;

typedef struct AstTreeInfix {
  AstTree left;
  AstTree right;
} AstTreeInfix;

typedef struct AstTreeReturn {
  AstTree *value;
} AstTreeReturn;

typedef struct AstTreeIf {
  AstTree *condition;
  AstTree *ifBody;
  AstTree *elseBody;
} AstTreeIf;

typedef struct AstTreeHelper {
  AstTreeVariables **variables;
  size_t variables_size;
  AstTreeVariable *variable;
  AstTreeVariables *globalDeps;
} AstTreeHelper;

typedef struct AstTreeSetTypesHelper {
  AstTree *lookingType;
} AstTreeSetTypesHelper;

void astTreePrint(const AstTree *tree, int indent);
void astTreeRootPrint(const AstTreeRoot *root);

void astTreeDestroy(AstTree tree);
void astTreeVariableDestroy(AstTreeVariable variable);
void astTreeVariableDelete(AstTreeVariable *variable);
void astTreeDelete(AstTree *tree);
void astTreeRootDelete(AstTreeRoot *root);

AstTree *newAstTree(AstTreeToken token, void *metadata, AstTree *type,
                    char *str_begin, char *str_end);
AstTree *copyAstTree(AstTree *tree);
AstTreeVariables copyAstTreeVariables(AstTreeVariables variables);

AstTreeRoot *makeAstTree(ParserNode *parsedRoot);

bool pushVariable(AstTreeHelper *helper, AstTreeVariables *variables,
                  AstTreeVariable *variable);
AstTreeVariable *getVariable(AstTreeHelper *helper, char *name_begin,
                             char *name_end);

AstTree *astTreeParse(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseFunction(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseTypeFunction(ParserNode *parserNode,
                                  AstTreeHelper *helper);
AstTree *astTreeParseFunctionCall(ParserNode *parserNode,
                                  AstTreeHelper *helper);
AstTree *astTreeParseIdentifier(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParsePrintU64(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseReturn(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseBinaryOperator(ParserNode *parserNode,
                                    AstTreeHelper *helper, AstTreeToken token);
AstTree *astTreeParseUnaryOperator(ParserNode *parserNode,
                                   AstTreeHelper *helper, AstTreeToken token);
bool astTreeParseConstant(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseVariable(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseIf(ParserNode *parserNode, AstTreeHelper *helper);
AstTree *astTreeParseCurlyBracket(ParserNode *parserNode,
                                  AstTreeHelper *helper);

AstTreeFunction *getFunction(AstTree *value);
bool isConst(AstTree *value);
AstTree *makeTypeOf(AstTree *value);
bool typeIsEqual(const AstTree *type0, const AstTree *type1);

bool isCircularDependencies(AstTreeHelper *helper, AstTreeVariable *variable,
                            AstTree *tree);

bool setAllTypesRoot(AstTreeRoot *root, AstTreeHelper *helper);
bool setAllTypes(AstTree *tree, AstTreeSetTypesHelper helper,
                 AstTreeFunction *function);
bool setTypesValueInt(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesFunction(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesPrintU64(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesReturn(AstTree *tree, AstTreeSetTypesHelper helper,
                    AstTreeFunction *function);
bool setTypesTypeFunction(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesFunctionCall(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesVariable(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesOperatorAssign(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesOperatorInfix(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesOperatorInfixWithRet(AstTree *tree, AstTree *retType,
                                  AstTreeSetTypesHelper helper);
bool setTypesOperatorUnary(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesVariableDefine(AstTree *tree, AstTreeSetTypesHelper helper);
bool setTypesIf(AstTree *tree, AstTreeSetTypesHelper helper,
                AstTreeFunction *function);
bool setTypesScope(AstTree *tree, AstTreeSetTypesHelper helper,
                   AstTreeFunction *function);

bool setTypesAstVariable(AstTreeVariable *variable,
                         AstTreeSetTypesHelper helper);
bool setTypesAstInfix(AstTreeInfix *infix, AstTreeSetTypesHelper helper);

bool astTreeCleanRoot(AstTreeRoot *root);
bool astTreeClean(AstTree *tree);
bool astTreeCleanFunction(AstTree *tree);
bool astTreeCleanVariable(AstTree *tree);
bool astTreeCleanAstVariable(AstTreeVariable *variable);

size_t astTreeTypeSize(AstTree tree);