types.h (7282B)
1 #pragma once 2 3 #include <libgccjit.h> 4 #include <stdlib.h> 5 #include <stdbool.h> 6 7 typedef enum { 8 TOKEN_IDENT = 1006, 9 TOKEN_LPAREN, 10 TOKEN_RPAREN, 11 TOKEN_LBRACE, 12 TOKEN_RBRACE, 13 TOKEN_LBRACKET, 14 TOKEN_RBRACKET, 15 TOKEN_EQUAL, 16 TOKEN_SEMICOLON, 17 TOKEN_PERCENT, 18 TOKEN_COMMA, 19 TOKEN_STRUCT, 20 TOKEN_EXTEND, 21 TOKEN_FX, 22 TOKEN_FN, 23 TOKEN_INT_LITERAL, 24 TOKEN_FLOAT_LITERAL, 25 TOKEN_STRING_LITERAL, 26 TOKEN_BOOL_TRUE_LITERAL, 27 TOKEN_BOOL_FALSE_LITERAL, 28 TOKEN_COMP_TIME, 29 TOKEN_VARIADIC, 30 TOKEN_SLASH, 31 TOKEN_STAR, 32 TOKEN_PLUS, 33 TOKEN_PLUSPLUS, 34 TOKEN_MINUS, 35 TOKEN_MINUSMINUS, 36 TOKEN_EQUALITY, 37 TOKEN_INEQUALITY, 38 TOKEN_BANG, 39 TOKEN_LT, 40 TOKEN_GT, 41 TOKEN_LT_EQ, 42 TOKEN_GT_EQ, 43 TOKEN_IF, 44 TOKEN_ELSE, 45 TOKEN_WHILE, 46 TOKEN_FOR, 47 TOKEN_BREAK, 48 TOKEN_CONTINUE, 49 TOKEN_RETURN, 50 TOKEN_UNKNOWN, // NOTE: also update print_token 51 TOKEN_EOF 52 } TokenType; // NOTE also update token_type_str! 53 54 const char* token_type_str(TokenType t); 55 56 typedef struct { 57 size_t start; 58 size_t end; 59 size_t line; 60 size_t col; 61 TokenType type; 62 } Token; 63 64 typedef struct { 65 Token* tokens; 66 size_t token_count; 67 size_t token_cap; 68 size_t pos; 69 size_t line; 70 size_t col; 71 const char* src; 72 size_t src_len; 73 const char* filename; 74 } Lexer; 75 76 typedef enum { 77 NODE_PROGRAM = 11, 78 NODE_FUNCTION_DECL, 79 NODE_PARAM, 80 NODE_VAR_DECL, 81 NODE_VAR_ASSIGN, 82 NODE_BLOCK, 83 NODE_CALL_EXPR, 84 NODE_RETURN, 85 NODE_BREAK, 86 NODE_CONTINUE, 87 NODE_INT_LITERAL, 88 NODE_FLOAT_LITERAL, 89 NODE_STRING_LITERAL, 90 NODE_BOOL_LITERAL, 91 NODE_IDENT, 92 NODE_TYPE, 93 NODE_BINARY_EXPR, 94 NODE_UNARY_EXPR, 95 NODE_EXPR_STATEMENT, 96 NODE_SUBSCRIPT_EXPR, 97 NODE_IF, 98 NODE_WHILE, 99 NODE_FOR, 100 NODE_EMPTY_STATEMENT, 101 NODE_UNKNOWN, 102 } NodeType; // note: if changed, edit node_type_str! 103 104 const char* node_type_str(NodeType); 105 void print_node_type_str(NodeType); 106 107 /* 108 typedef enum { 109 OP_ADD, OP_SUB, OP_MUL, OP_DIV, OP_MOD, 110 OP_POS, OP_NEG, OP_INC, OP_DEC, 111 OP_BITAND, OP_BITOR, OP_BITXOR, OP_BITNOT, 112 OP_SHL, OP_SHR, 113 OP_LOGAND, OP_LOGOR, OP_LOGNOT, 114 OP_LT, OP_LE, OP_GT, OP_GE, OP_EQ, OP_NE, 115 OP_ASSIGN, OP_ADD_ASSIGN, OP_SUB_ASSIGN, 116 OP_MUL_ASSIGN, OP_DIV_ASSIGN, OP_MOD_ASSIGN, 117 OP_SHL_ASSIGN, OP_SHR_ASSIGN, 118 OP_AND_ASSIGN, OP_XOR_ASSIGN, OP_OR_ASSIGN, 119 OP_CONDITIONAL, OP_COMMA, 120 OP_ADDR, OP_DEREF, OP_MEMBER, OP_PTR_MEMBER, 121 OP_SUBSCRIPT, OP_CALL, 122 OP_SIZEOF, OP_ALIGNOF 123 } OpType; 124 */ 125 126 typedef enum { 127 OP_PLUS = 300, 128 OP_MINUS, 129 OP_MUL, 130 OP_DIV, 131 OP_MOD, 132 OP_BIT_AND, // & ampersand 133 OP_BIT_OR, // | 134 OP_ASSIGN, 135 OP_EQUALITY, // == 136 OP_INEQUALITY, // != 137 OP_LT_EQ, 138 OP_GT_EQ, 139 OP_LT, 140 OP_GT, 141 } OpType; 142 143 typedef enum { 144 OPER_MINUS = 0, 145 OPER_BANG, 146 OPER_PREINC, 147 OPER_PREDEC, 148 OPER_POSTINC, 149 OPER_POSTDEC, 150 } UnaryOp; 151 152 typedef struct { 153 size_t start; 154 size_t end; 155 } Span; 156 157 typedef struct Node { 158 NodeType type; 159 struct Node* next; 160 struct Scope* scope; 161 const char* filename; 162 size_t line, col; 163 164 /* NOTE we will eventually add spans for condition info, etc. to print out in errors */ 165 166 union { 167 /* clang-format off */ 168 struct { struct Node** decl; size_t len, cap; } program; 169 struct { Span name; struct Node* return_type; struct Node** params; size_t p_cap, p_len; struct Node* body; bool exported; } function_decl; 170 struct { Span name; struct Node* type; } param; 171 struct { struct Node* cond; struct Node* then_body; struct Node* else_body; } if_statement; 172 struct { struct Node* cond; struct Node* body; } while_statement; 173 struct { struct Node* init; struct Node* cond; struct Node* increment; struct Node* body; } for_statement; 174 struct { struct Node** stmts; size_t cap, len; } block; 175 struct { Span name; struct Node* type; struct Node* init; } var_decl; 176 struct { struct Node* lhs; struct Node* rhs; } var_assign; 177 struct { struct Node* callee; struct Node** args; size_t cap, len; } call_expr; 178 struct { struct Node* expr; } ret; 179 struct { struct Node* expr; } cont; 180 struct { struct Node* expr; } expr_statement; 181 struct { OpType op; struct Node* lhs; struct Node* rhs; } binary_expr; 182 struct { UnaryOp op; struct Node* operand; bool is_postfix; } unary_expr; 183 struct { struct Node* array; struct Node* index; } subscript_expr; 184 struct { double value; } number; 185 struct { bool value; } boolean; 186 struct { Span value; } string; 187 struct { Span name; } ident; 188 struct { Span name; bool is_comp_time; bool is_variadic; } ident_type; 189 /* clang-format on */ 190 } data; 191 } Node; 192 193 typedef struct { 194 Token* tokens; 195 size_t token_count; 196 size_t pos; 197 const char* src; 198 size_t src_len; 199 const char* filename; 200 } Parser; 201 202 typedef struct { 203 Node* node; 204 const char* src; 205 } Ast; 206 207 typedef struct { 208 Node** items; 209 size_t len, cap; 210 } NodeVec; 211 212 typedef enum { 213 // todo distinguish local/exported/param 214 SYMTYPE_VOID = 108, 215 SYMTYPE_INT, 216 SYMTYPE_I8, 217 SYMTYPE_I16, 218 SYMTYPE_I32, 219 SYMTYPE_I64, 220 SYMTYPE_I128, 221 SYMTYPE_UINT, 222 SYMTYPE_U8, 223 SYMTYPE_U16, 224 SYMTYPE_U32, 225 SYMTYPE_U64, 226 SYMTYPE_U128, 227 SYMTYPE_F32, 228 SYMTYPE_F64, 229 SYMTYPE_F128, 230 SYMTYPE_FLOAT, 231 SYMTYPE_STR, 232 SYMTYPE_CHR, 233 SYMTYPE_BOOL, 234 SYMTYPE_STRUCT, 235 SYMTYPE_USER, 236 SYMTYPE_ARRAY, 237 SYMTYPE_ENUM, 238 SYMTYPE_FUNC, 239 SYMTYPE_TODO, 240 } SymbolType; // note also update type_kind_str! 241 242 const char* type_kind_str(SymbolType); 243 244 typedef enum { 245 ENUM_VALUE_INT, 246 ENUM_VALUE_STRING, 247 } EnumValueKind; 248 249 typedef struct StructField { 250 char* name; 251 struct Type* type; 252 } StructField; 253 254 typedef struct EnumField { 255 char* name; 256 EnumValueKind kind; 257 union { // not used? 258 int int_value; 259 char* string_value; 260 } val; 261 } EnumField; 262 263 typedef struct StructMethod { 264 char* name; 265 struct Type* return_type; 266 struct Type** param_types; 267 int params_count; 268 int params_cap; 269 // TODO add ptr to func decl of this struct method 270 } StructMethod; 271 272 typedef struct Type { 273 SymbolType type; 274 275 // union { 276 // struct StructType { 277 // const char* struct_name; 278 // int fields_count; 279 // int methods_count; 280 // StructField* fields; 281 // StructMethod* methods; 282 // } struct_t; 283 284 // struct ArrayType { 285 // int array_size; // -1 or fixed 286 // struct Type* of_type; 287 // bool dynamic; 288 // } array_t; 289 290 // struct EnumType { 291 // const char* enum_name; 292 // const int fields_count; 293 // EnumField* fields; 294 // EnumValueKind value_kind; 295 // } enum_t; 296 // }; 297 } TypeInfo; 298 299 typedef struct Symbol { 300 Span name; 301 Node* decl; 302 TypeInfo* type; // todo print all found in gen 303 304 gcc_jit_type* ctype; 305 const char* english_type; 306 bool is_variadic; 307 bool is_comp_time; 308 union { 309 gcc_jit_function* funcvalue; 310 gcc_jit_lvalue* lvalue; 311 gcc_jit_param* param; 312 gcc_jit_rvalue* const_rvalue; 313 } d; 314 } Symbol; 315 316 // @later NOTE: our symbols are a list, there is an argument to be made to change to a linked list; 317 // as a linked list will outperform an allocated list up to 12-16 elements, or so. 318 // @later NOTE: also consider their relation between each other and the rest of the compiler is a bit of a circulas mess; 319 // which is fine as long as we're boostrapping and getting it off the ground. 320 typedef struct Scope { 321 struct Node* owner; 322 struct Scope* parent; 323 Symbol** symbols; 324 size_t len; 325 size_t cap; 326 struct Scope** children; 327 size_t ch_len; 328 size_t ch_cap; 329 int depth; 330 int id; 331 } Scope;