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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#ifndef AMALGAM_AST_H
#define AMALGAM_AST_H
#include "defs.h"
#include "nullable.h"
#include "std/defs.h"
#include "std/buffer_view.h"
#include "std/buffer.h"
#include "std/misc.h"
#include "std/arena_allocator.h"
#include "std/hash_map.h"
#include "binop_type.h"
#include "ssa/ssa.h"
#include <setjmp.h>
/* General error */
#define AST_ERR -1
#define AST_ERR_DEF_DUP -20
typedef struct Ast Ast;
typedef struct FunctionSignature FunctionSignature;
typedef struct FunctionCall FunctionCall;
typedef struct StructDecl StructDecl;
typedef struct StructField StructField;
typedef struct LhsExpr LhsExpr;
typedef struct AssignmentExpr AssignmentExpr;
typedef struct Import Import;
typedef struct String String;
typedef struct Variable Variable;
typedef struct Number Number;
typedef struct Binop Binop;
typedef struct IfStatement IfStatement;
typedef struct ElseIfStatement ElseIfStatement;
typedef struct WhileStatement WhileStatement;
/* TODO: Instead of using pointers, use the data directly */
typedef union {
void *data;
FunctionDecl *func_decl;
FunctionCall *func_call;
StructDecl *struct_decl;
StructField *struct_field;
LhsExpr *lhs_expr;
AssignmentExpr *assign_expr;
Import *import;
String *string;
Number *number;
Variable *variable;
Binop *binop;
IfStatement *if_stmt;
WhileStatement *while_stmt;
} AstValue;
typedef enum {
AST_FUNCTION_DECL,
AST_FUNCTION_CALL,
AST_STRUCT_DECL,
AST_STRUCT_FIELD,
AST_LHS,
AST_ASSIGN,
AST_IMPORT,
AST_STRING,
AST_NUMBER,
AST_VARIABLE,
AST_BINOP,
AST_IF_STATEMENT,
AST_WHILE_STATEMENT
} AstType;
typedef enum {
AST_NOT_RESOLVED,
AST_RESOLVING,
AST_RESOLVED,
AST_SSA_RESOLVED
} AstResolveStatus;
typedef struct {
LhsExpr *type;
AstResolveStatus status;
Parser *parser; /* Borrowed. This is the parser that is currently parsing the expression */
} AstResolveData;
struct Ast {
AstValue value;
AstType type;
AstResolveData resolve_data;
SsaRegister ssa_reg;
};
struct Scope {
Buffer/*<Ast*>*/ ast_objects;
HashMapType(BufferView, Ast*) named_objects; /* Value is always an Ast* with type LhsExpr */
Scope *parent;
/* Is null unless the scope is a file scope, in which case this is the parser that owns the scope */
Parser *parser;
};
struct FileScopeReference {
Parser *parser;
Buffer canonical_path;
};
struct Variable {
BufferView name;
Ast *resolved_var; /* resolved_var will always be a LhsExpr after resolved */
};
struct FunctionSignature {
int params; /* TODO: Implement signatures */
bool resolved;
};
struct FunctionDecl {
FunctionSignature *signature;
Scope body;
SsaFuncIndex ssa_func_index;
};
struct FunctionCall {
Variable func;
Buffer/*<Ast*>*/ args;
};
struct StructDecl {
Scope body;
};
struct StructField {
BufferView name;
Variable type;
};
typedef struct {
enum {
VARIABLE_TYPE_NONE,
VARIABLE_TYPE_VARIABLE,
VARIABLE_TYPE_SIGNATURE
} type;
union {
Variable *variable;
FunctionSignature *signature;
} value;
} VariableType;
typedef enum {
DECL_FLAG_NONE = 0,
DECL_FLAG_EXTERN = 1 << 0,
DECL_FLAG_EXPORT = 1 << 1,
DECL_FLAG_PUB = 1 << 2,
DECL_FLAG_CONST = 1 << 3
} DeclFlag;
/*
Note: When resolving AST, multiple threads can end up resolving the same expressions at the same time.
This is intentional. Instead of using mutex for every expression and locking/unlocking everytime
which uses more memory and affects performance, we assume such race conditions are rare and let them happen.
TODO: Investigate possible deadlock when multiple threads resolve the same expression and that
expression has a recursive dependency and the threads execute @ast_resolve at the same speed,
leading to @ast_resolve running again for the same expression.
*/
struct LhsExpr {
u8 decl_flags;
BufferView var_name;
VariableType type;
nullable Ast *rhs_expr;
};
#define LHS_EXPR_IS_EXTERN(expr) ((expr)->decl_flags & DECL_FLAG_EXTERN)
#define LHS_EXPR_IS_EXPORT(expr) ((expr)->decl_flags & DECL_FLAG_EXPORT)
#define LHS_EXPR_IS_PUB(expr) ((expr)->decl_flags & DECL_FLAG_PUB)
#define LHS_EXPR_IS_CONST(expr) ((expr)->decl_flags & DECL_FLAG_CONST)
struct AssignmentExpr {
Ast *lhs_expr;
Ast *rhs_expr;
};
struct Import {
BufferView path;
FileScopeReference *file_scope;
};
struct String {
BufferView str;
};
struct Number {
union {
i64 integer;
f64 floating;
} value;
bool is_integer;
BufferView code_ref;
};
struct Binop {
Ast *lhs;
Ast *rhs;
BinopType type;
/* Is the binop already ordered - no need to reorder it */
bool grouped;
};
struct IfStatement {
Ast *condition;
Scope body;
ElseIfStatement *else_if_stmt;
};
/* ElseIfStatement where condition == NULL is an Else statement */
struct ElseIfStatement {
Ast *condition;
Scope body;
ElseIfStatement *next_else_if_stmt;
};
struct WhileStatement {
Ast *condition;
Scope body;
};
typedef struct {
jmp_buf env;
amal_compiler *compiler; /* Borrowed */
Parser *parser; /* Borrowed. This is the parser that belongs to the thread */
/*
Borrowed. This is the current scope. Note that this scope can belong to another parser (and thread),
as such, @parser and scope_get_parser(@scope) parser may not be the same.
*/
Scope *scope;
} AstCompilerContext;
CHECK_RESULT int ast_create(ArenaAllocator *allocator, void *value, AstType type, Ast **result);
BufferView ast_get_name(Ast *self);
void function_signature_init(FunctionSignature *self);
CHECK_RESULT int funcdecl_init(FunctionDecl *self, FunctionSignature *signature, Scope *parent, ArenaAllocator *allocator);
CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ArenaAllocator *allocator);
CHECK_RESULT int structdecl_init(StructDecl *self, Scope *parent, ArenaAllocator *allocator);
LhsExpr* structdecl_get_field_by_name(StructDecl *self, BufferView field_name);
void structfield_init(StructField *self, BufferView name, BufferView type_name);
void lhsexpr_init(LhsExpr *self, DeclFlag decl_flag, BufferView var_name);
void assignmentexpr_init(AssignmentExpr *self, Ast *lhs_expr, Ast *rhs_expr);
void import_init(Import *self, BufferView path);
CHECK_RESULT int string_init(String *self, BufferView str);
void number_init(Number *self, i64 value, bool is_integer, BufferView code_ref);
void variable_init(Variable *self, BufferView name);
void binop_init(Binop *self);
CHECK_RESULT int if_statement_init(IfStatement *self, Scope *parent, ArenaAllocator *allocator);
CHECK_RESULT int else_if_statement_init(ElseIfStatement *self, Scope *parent, ArenaAllocator *allocator);
CHECK_RESULT int while_statement_init(WhileStatement *self, Scope *parent, ArenaAllocator *allocator);
CHECK_RESULT int scope_init(Scope *self, Scope *parent, ArenaAllocator *allocator);
CHECK_RESULT int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ArenaAllocator *allocator);
CHECK_RESULT int scope_add_child(Scope *self, Ast *child);
/* longjump to compiler env on failure */
void scope_resolve(Scope *self, AstCompilerContext *context);
CHECK_RESULT bool resolved_type_is_func_decl(Ast *self);
#endif
|