-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
188 lines (147 loc) · 3.97 KB
/
ast.h
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
//
// Created by fathi on 10/19/2024.
//
#ifndef TIGE_AST_H
#define TIGE_AST_H
#include "lexer.h"
#include "object.h"
typedef enum {
AST_BINARY_OP,
AST_UNARY_OP,
AST_COMPARE,
AST_ASSIGN,
AST_TERNARY_OP,
AST_INTEGER,
AST_FLOAT,
AST_SYMBOL,
AST_BOOL,
AST_STRING,
AST_CALL,
AST_EXPRESSION_STMT,
AST_VAR_DECL,
AST_FN_DECL,
AST_BLOCK,
AST_IF,
AST_LOOP,
AST_FOR,
AST_BREAK,
AST_RETURN,
AST_RANGE,
} ASTNodeType;
typedef union {
TString *str_value; // For strings, identifiers
int64_t int_value; // For integers
double float_value; // For floats
bool bool_value; // For booleans
void *value; // For other types, especially pointers
} ASTValue;
typedef struct ASTNode ASTNode;
typedef struct ASTNodeList {
ASTNode **nodes;
size_t count;
size_t capacity;
} ASTNodeList;
struct ASTNode {
ASTNodeType type;
union {
ASTValue *value;
TObject* object;
// Binary operation
struct {
ASTNode *left;
TokenType operator;
ASTNode *right;
} binary_op_expr;
// Unary operation
struct {
ASTNode *operand;
TokenType operator;
} unary_op_expr;
// Comparison expression
struct {
ASTNode *left;
TokenType operator;
ASTNode *right;
} compare_expr;
// Assignment expression
struct {
ASTNode *left;
TokenType operator;
ASTNode *right;
} assignment_expr;
// Ternary operation
struct {
ASTNode *condition;
ASTNode *true_expr;
ASTNode *false_expr;
} ternary_op_expr;
// Call expressions
struct {
ASTNode *callee;
ASTNodeList *arguments;
} call_expr;
// Expression Statement
struct {
ASTNode *expression;
} expression_stmt;
// Variable declaration
struct {
char* identifier;
ASTNode* value;
} var_decl_expr;
// Block
struct {
ASTNode **statements;
size_t statement_count;
} block;
// If Statement
struct {
ASTNode *condition;
ASTNode *then_branch;
ASTNode *else_branch; // Optional
} if_stmt;
// Loop Statement
struct {
ASTNode *body;
} loop_stmt;
struct {
ASTNode *start;
ASTNode *end;
} range_expr;
struct {
char *identifier;
ASTNode *range;
ASTNode *body;
} for_stmt;
// Break Statement
struct {
// no additional data
} break_stmt;
struct {
char* identifier; // none if the function is a closure
ASTNodeList* params;
ASTNode* body;
} fn_decl_stmt;
// Return Statement
struct {
ASTNode *value; // Could be NULL
} return_stmt;
};
};
// Function prototypes
ASTNode *create_ast(ASTNodeType type, ASTValue *value);
void free_ast_node(ASTNode *node);
ASTValue *create_float_value(double value);
ASTValue *create_int_value(long long val);
ASTValue *create_string_value(const char *str);
ASTValue *create_bool_value(bool val);
ASTNodeList *create_ast_node_list();
void ast_node_list_add(ASTNodeList *list, ASTNode *node);
void free_ast_node_list(ASTNodeList *list);
ASTNode *create_for_stmt(const char *identifier, ASTNode *range, ASTNode *body);
ASTNode *create_range_expr(ASTNode *start, ASTNode *end);
ASTNode *create_block(ASTNode **statements, size_t count);
ASTNode* create_if_stmt(ASTNode* condition, ASTNode* then_branch, ASTNode* else_branch);
ASTNode* create_expression_stmt(ASTNode* expression);
ASTNode* create_fn_decl_stmt(char* name, ASTNodeList* params, ASTNode* body);
#endif //TIGE_AST_H