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