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