KCC - Kayte C Compiler 1.10.0
A C compiler implementation with preprocessor, lexer, parser, and code generator
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1
10
11#ifndef TYPES_H
12#define TYPES_H
13
14#include <stddef.h>
15#include <stdbool.h>
16#include <stdio.h> // Add this for FILE type
17
24typedef enum {
25 TOKEN_EOF = -1,
26 TOKEN_UNKNOWN = 0,
27
28 // C Keywords
29 TOKEN_INT,
30 TOKEN_CHAR_KW,
31 TOKEN_VOID,
32 TOKEN_FLOAT,
33 TOKEN_DOUBLE, // Add double support
34 TOKEN_IF,
35 TOKEN_ELSE,
36 TOKEN_WHILE,
37 TOKEN_FOR,
38 TOKEN_RETURN,
39 TOKEN_BREAK,
40 TOKEN_CONTINUE,
41 TOKEN_STRUCT, // Add struct support
42 TOKEN_UNION, // Add union support
43 TOKEN_ENUM, // Add enum support
44 TOKEN_TYPEDEF, // Add typedef support
45 TOKEN_STATIC, // Add static support
46 TOKEN_EXTERN, // Add extern support
47 TOKEN_CONST, // Add const support
48
49 // Objective-C Keywords and Directives
50 TOKEN_AT, // @
51 TOKEN_INTERFACE, // @interface (also used without @)
52 TOKEN_IMPLEMENTATION, // @implementation (also used without @)
53 TOKEN_PROTOCOL, // @protocol (also used without @)
54 TOKEN_END, // @end (also used without @)
55 TOKEN_PROPERTY, // @property (also used without @)
56 TOKEN_SYNTHESIZE, // @synthesize (also used without @)
57 TOKEN_DYNAMIC, // @dynamic (also used without @)
58 TOKEN_SELECTOR, // @selector (also used without @)
59 TOKEN_CLASS, // @class (forward declaration, also used without @)
60 TOKEN_PRIVATE, // @private
61 TOKEN_PROTECTED, // @protected
62 TOKEN_PUBLIC, // @public
63 TOKEN_PACKAGE, // @package
64 TOKEN_OPTIONAL, // @optional
65 TOKEN_REQUIRED, // @required
66 TOKEN_AUTORELEASEPOOL,// @autoreleasepool
67 TOKEN_TRY, // @try (also used without @)
68 TOKEN_CATCH, // @catch (also used without @)
69 TOKEN_FINALLY, // @finally (also used without @)
70 TOKEN_THROW, // @throw (also used without @)
71 TOKEN_SYNCHRONIZED, // @synchronized (also used without @)
72
73 // Objective-C @ prefixed tokens (specific to @ usage)
74 TOKEN_AT_INTERFACE,
75 TOKEN_AT_IMPLEMENTATION,
76 TOKEN_AT_PROTOCOL,
77 TOKEN_AT_PROPERTY,
78 TOKEN_AT_SYNTHESIZE,
79 TOKEN_AT_DYNAMIC,
80 TOKEN_AT_CLASS,
81 TOKEN_AT_SELECTOR,
82 TOKEN_AT_ENCODE,
83 TOKEN_AT_SYNCHRONIZED,
84 TOKEN_AT_TRY,
85 TOKEN_AT_CATCH,
86 TOKEN_AT_FINALLY,
87 TOKEN_AT_THROW,
88 TOKEN_AT_END,
89 TOKEN_AT_IDENTIFIER,
90
91 // Objective-C literals and expressions
92 TOKEN_NSSTRING_LITERAL, // @"string"
93 TOKEN_NSARRAY_LITERAL, // @[...]
94 TOKEN_NSDICTIONARY_LITERAL, // @{...}
95 TOKEN_BOXED_EXPRESSION, // @(...)
96 TOKEN_BOXED_NUMBER, // @123
97
98 // Objective-C Memory Management
99 TOKEN_RETAIN, // retain
100 TOKEN_RELEASE, // release
101 TOKEN_AUTORELEASE, // autorelease
102 TOKEN_STRONG, // __strong
103 TOKEN_WEAK, // __weak
104 TOKEN_UNSAFE_UNRETAINED, // __unsafe_unretained
105 TOKEN_AUTORELEASING, // __autoreleasing
106
107 // Property attributes
108 TOKEN_ATOMIC, // atomic
109 TOKEN_NONATOMIC, // nonatomic
110 TOKEN_ASSIGN_ATTR, // assign (property attribute, different from =)
111 TOKEN_COPY, // copy
112 TOKEN_READONLY, // readonly
113 TOKEN_READWRITE, // readwrite
114 TOKEN_GETTER, // getter
115 TOKEN_SETTER, // setter
116
117 // Objective-C Types
118 TOKEN_ID, // id
119 TOKEN_CLASS_KW, // Class
120 TOKEN_SEL, // SEL
121 TOKEN_IMP, // IMP
122 TOKEN_BOOL_KW, // BOOL
123 TOKEN_YES, // YES
124 TOKEN_NO, // NO
125 TOKEN_NIL, // nil
126 TOKEN_NULL, // NULL
127 TOKEN_SELF, // self
128 TOKEN_SUPER, // super
129 TOKEN_INSTANCETYPE, // instancetype
130
131 // Foundation framework types
132 TOKEN_NSSTRING, // NSString
133 TOKEN_NSARRAY, // NSArray
134 TOKEN_NSDICTIONARY, // NSDictionary
135 TOKEN_NSOBJECT, // NSObject
136
137 // Additional Objective-C constructs
138 TOKEN_ENCODE, // encode (used with @encode)
139
140 // Identifiers and Literals
141 TOKEN_IDENTIFIER,
142 TOKEN_NUMBER,
143 TOKEN_STRING,
144 TOKEN_STRING_OBJC, // @"Objective-C string" (alternative naming)
145 TOKEN_CHAR, // 'c'
146
147 // Operators
148 TOKEN_PLUS,
149 TOKEN_MINUS,
150 TOKEN_MULTIPLY,
151 TOKEN_DIVIDE,
152 TOKEN_MODULO,
153 TOKEN_ASSIGN,
154 TOKEN_EQUAL,
155 TOKEN_NOT_EQUAL,
156 TOKEN_LESS,
157 TOKEN_LESS_EQUAL,
158 TOKEN_GREATER,
159 TOKEN_GREATER_EQUAL,
160 TOKEN_AND,
161 TOKEN_OR,
162 TOKEN_NOT,
163 TOKEN_BITWISE_AND, // &
164 TOKEN_BITWISE_OR, // |
165 TOKEN_BITWISE_XOR, // ^
166 TOKEN_BITWISE_NOT, // ~
167 TOKEN_LEFT_SHIFT, // <<
168 TOKEN_RIGHT_SHIFT, // >>
169 TOKEN_INCREMENT, // ++
170 TOKEN_DECREMENT, // --
171 TOKEN_PLUS_ASSIGN, // +=
172 TOKEN_MINUS_ASSIGN, // -=
173 TOKEN_ARROW, // ->
174 TOKEN_DOT, // .
175
176 // Delimiters
177 TOKEN_LPAREN,
178 TOKEN_RPAREN,
179 TOKEN_LBRACE,
180 TOKEN_RBRACE,
181 TOKEN_LBRACKET, // [ (also used for message sends)
182 TOKEN_RBRACKET, // ] (also used for message sends)
183 TOKEN_SEMICOLON,
184 TOKEN_COMMA,
185 TOKEN_COLON, // : (used in Objective-C method names)
186 TOKEN_QUESTION, // ? (ternary operator)
187 TOKEN_NEWLINE
188} TokenType;
189
193typedef enum {
194 TYPE_UNKNOWN,
195 TYPE_VOID,
196 TYPE_INT,
197 TYPE_FLOAT,
198 TYPE_DOUBLE,
199 TYPE_CHAR,
200 TYPE_BOOL,
201 TYPE_ID, // Objective-C id type
202 TYPE_CLASS, // Objective-C Class type
203 TYPE_SEL, // Objective-C SEL type
204 TYPE_POINTER, // Pointer types
205 TYPE_STRUCT, // Structure types
206 TYPE_UNION, // Union types
207 TYPE_ENUM // Enumeration types
208} DataType;
209
215typedef enum {
216 // C Declarations
217 AST_FUNCTION_DECLARATION,
218 AST_VARIABLE_DECLARATION,
219 AST_PARAMETER,
220 AST_STRUCT_DECLARATION,
221 AST_UNION_DECLARATION,
222 AST_ENUM_DECLARATION,
223 AST_TYPEDEF_DECLARATION,
224
225 // Objective-C Declarations
226 AST_OBJC_INTERFACE,
227 AST_OBJC_IMPLEMENTATION,
228 AST_OBJC_PROTOCOL,
229 AST_OBJC_CATEGORY,
230 AST_OBJC_METHOD_DECLARATION,
231 AST_OBJC_PROPERTY_DECLARATION,
232 AST_OBJC_SYNTHESIZE,
233 AST_OBJC_DYNAMIC,
234
235 // C Statements
236 AST_COMPOUND_STATEMENT,
237 AST_EXPRESSION_STATEMENT,
238 AST_IF_STATEMENT,
239 AST_WHILE_STATEMENT,
240 AST_FOR_STATEMENT,
241 AST_RETURN_STATEMENT,
242 AST_BREAK_STATEMENT,
243 AST_CONTINUE_STATEMENT,
244
245 // Objective-C Statements
246 AST_OBJC_TRY_STATEMENT,
247 AST_OBJC_CATCH_STATEMENT,
248 AST_OBJC_FINALLY_STATEMENT,
249 AST_OBJC_THROW_STATEMENT,
250 AST_OBJC_SYNCHRONIZED_STATEMENT,
251 AST_OBJC_AUTORELEASEPOOL_STATEMENT,
252
253 // C Expressions
254 AST_BINARY_OP,
255 AST_UNARY_OP,
256 AST_ASSIGNMENT,
257 AST_FUNCTION_CALL,
258 AST_IDENTIFIER,
259 AST_NUMBER_LITERAL,
260 AST_STRING_LITERAL,
261 AST_CHAR_LITERAL,
262 AST_MEMBER_ACCESS, // . and -> operators
263 AST_ARRAY_ACCESS, // [] operator
264 AST_TERNARY_OP, // ? : operator
265
266 // Objective-C Expressions
267 AST_OBJC_MESSAGE_SEND,
268 AST_OBJC_STRING_LITERAL, // @"string"
269 AST_OBJC_SELECTOR_EXPR, // @selector()
270 AST_OBJC_PROTOCOL_EXPR, // @protocol()
271 AST_OBJC_ENCODE_EXPR, // @encode()
272 AST_OBJC_BOOLEAN_LITERAL, // YES/NO
273
274 // Special
275 AST_PROGRAM
277
281typedef enum {
282 OBJC_INSTANCE_METHOD, // - (void)method
283 OBJC_CLASS_METHOD // + (void)method
285
289typedef enum {
290 OBJC_PROPERTY_ASSIGN = 1 << 0,
291 OBJC_PROPERTY_RETAIN = 1 << 1,
292 OBJC_PROPERTY_COPY = 1 << 2,
293 OBJC_PROPERTY_READONLY = 1 << 3,
294 OBJC_PROPERTY_READWRITE = 1 << 4,
295 OBJC_PROPERTY_NONATOMIC = 1 << 5,
296 OBJC_PROPERTY_ATOMIC = 1 << 6,
297 OBJC_PROPERTY_STRONG = 1 << 7,
298 OBJC_PROPERTY_WEAK = 1 << 8
300
304typedef struct Token {
305 TokenType type;
306 char *value; // Token text (lexeme)
307 char *lexeme; // Alternative name for compatibility
308 int line; // Line number
309 int column; // Column number
310 union {
311 int int_value;
312 float float_value;
313 double double_value;
314 char char_value;
315 bool bool_value;
316 } literal; // For literal values
318
322typedef struct ObjCMethodParam {
323 char *selector_part; // The selector part (e.g., "with" in "withString:")
324 DataType param_type; // Parameter type
325 char *param_name; // Parameter name
327
333typedef struct ASTNode {
334 ASTNodeType type;
335 DataType data_type;
336 int line;
337 int column;
338
339 union {
340 struct {
341 struct ASTNode **declarations;
342 int declaration_count;
343 } program;
344
345 struct {
346 DataType return_type;
347 char *name;
348 struct ASTNode **parameters;
349 int parameter_count;
350 struct ASTNode *body;
351 } function_decl;
352
353 struct {
354 DataType var_type;
355 char *name;
356 struct ASTNode *initializer;
357 } var_decl;
358
359 struct {
360 DataType param_type;
361 char *name;
362 } parameter;
363
364 struct {
365 struct ASTNode **statements;
366 int statement_count;
367 } compound_stmt;
368
369 struct {
370 struct ASTNode *expression;
371 } expression_stmt;
372
373 struct {
374 struct ASTNode *expression;
375 } return_stmt;
376
377 struct {
378 struct ASTNode *condition;
379 struct ASTNode *then_stmt;
380 struct ASTNode *else_stmt;
381 } if_stmt;
382
383 struct {
384 struct ASTNode *condition;
385 struct ASTNode *body;
386 } while_stmt;
387
388 struct {
389 struct ASTNode *init;
390 struct ASTNode *condition;
391 struct ASTNode *update;
392 struct ASTNode *body;
393 } for_stmt;
394
395 struct {
396 TokenType operator;
397 struct ASTNode *left;
398 struct ASTNode *right;
399 } binary_expr;
400
401 struct {
402 TokenType operator;
403 struct ASTNode *operand;
404 } unary_expr;
405
406 struct {
407 char *function_name;
408 struct ASTNode **arguments;
409 int argument_count;
410 } call_expr;
411
412 struct {
413 char *name;
414 } identifier;
415
416 struct {
417 int value;
418 } number;
419
420 struct {
421 char *value;
422 } string;
423
424 struct {
425 char *variable;
426 struct ASTNode *value;
427 } assignment;
428
429 // Objective-C specific nodes
430 struct {
431 char *class_name;
432 char *superclass_name;
433 struct ASTNode **protocols; // Adopted protocols
434 int protocol_count;
435 struct ASTNode **methods; // Method declarations
436 int method_count;
437 struct ASTNode **properties; // Property declarations
438 int property_count;
439 } objc_interface;
440
441 struct {
442 char *class_name;
443 char *category_name; // NULL if not a category
444 struct ASTNode **methods; // Method implementations
445 int method_count;
446 struct ASTNode **ivars; // Instance variables
447 int ivar_count;
448 } objc_implementation;
449
450 struct {
451 ObjCMethodType method_type; // + or -
452 DataType return_type;
453 char *selector; // Full selector string
454 ObjCMethodParam *params; // Method parameters
455 int param_count;
456 struct ASTNode *body; // Method body (NULL for declarations)
457 } objc_method;
458
459 struct {
460 struct ASTNode *receiver; // Object receiving the message
461 char *selector; // Method selector
462 struct ASTNode **arguments; // Arguments
463 int argument_count;
464 } objc_message;
465
466 struct {
467 DataType property_type;
468 char *property_name;
469 ObjCPropertyAttributes attributes;
470 char *getter_name; // Custom getter name (optional)
471 char *setter_name; // Custom setter name (optional)
472 } objc_property;
473
474 struct {
475 char *protocol_name;
476 struct ASTNode **methods; // Required/optional methods
477 int method_count;
478 struct ASTNode **properties; // Protocol properties
479 int property_count;
480 } objc_protocol;
481
482 struct {
483 char *value; // @"string content"
484 } objc_string;
485
486 struct {
487 char *selector_name; // @selector(methodName:)
488 } objc_selector;
489
490 struct {
491 bool value; // YES or NO
492 } objc_boolean;
493 } data;
495
499typedef struct Lexer {
500 const char *source;
501 size_t position;
502 size_t current;
503 int line;
504 int column;
505 bool has_error;
506 char *error_message;
507 char *input;
508 size_t pos;
509 size_t input_length;
510 bool objc_mode; // Enable Objective-C syntax
512
516typedef struct Parser {
517 Lexer *lexer;
518 Token current_token;
519 Token peek_token;
520 bool has_error;
521 char *error_message;
522 bool objc_mode; // Enable Objective-C parsing
524
528typedef struct CodeGenerator {
529 FILE *output_file;
530 int label_counter;
531 int temp_counter;
532 bool objc_mode; // Enable Objective-C code generation
534
535// Forward declarations for other complex types
536typedef struct SymbolTable SymbolTable;
537typedef struct Symbol Symbol;
538
539// Utility function declarations
540const char* token_type_to_string(TokenType type);
541const char* data_type_to_string(DataType type);
542const char* ast_node_type_to_string(ASTNodeType type);
543const char* objc_method_type_to_string(ObjCMethodType type);
544DataType token_to_data_type(TokenType type);
545
546// PreProcessor definitions
547#define MAX_MACRO_NAME 64
548#define MAX_MACRO_BODY 512
549#define MAX_MACRO_PARAMS 32
550#define MAX_MACROS 256
551#define MAX_INCLUDE_DEPTH 32
552#define MAX_LINE_LENGTH 1024
553
557typedef enum {
558 MACRO_OBJECT, // #define NAME value
559 MACRO_FUNCTION // #define NAME(params) body
560} MacroType;
561
565typedef struct MacroParam {
566 char name[MAX_MACRO_NAME];
568
572typedef struct Macro {
573 char name[256];
574 char body[1024];
575 MacroType type;
576 MacroParam params[MAX_MACRO_PARAMS];
577 int param_count;
578 bool is_predefined;
579 int line_defined;
580 char *file_defined;
582
586typedef enum {
587 COND_NONE,
588 COND_IF,
589 COND_IFDEF,
590 COND_IFNDEF,
591 COND_ELIF,
592 COND_ELSE
594
598typedef struct ConditionalState {
599 ConditionalType type;
600 bool condition_met;
601 bool else_taken;
602 int line_number;
604
608typedef struct IncludeFile {
609 char *filename;
610 char *content;
611 int line;
612 int pos;
614
615// Forward declaration only - full definition in preprocessor.h
616typedef struct Preprocessor Preprocessor;
617
618#endif // TYPES_H
AST Node structure.
Definition types.h:333
CodeGenerator structure.
Definition types.h:528
Conditional state tracking.
Definition types.h:598
Include file tracking.
Definition types.h:608
Lexer structure.
Definition types.h:499
Macro parameter.
Definition types.h:565
Macro definition.
Definition types.h:572
Objective-C method parameter.
Definition types.h:322
Parser structure.
Definition types.h:516
Token structure.
Definition types.h:304
ObjCMethodType
Objective-C method type.
Definition types.h:281
MacroType
Macro types.
Definition types.h:557
ConditionalType
Conditional compilation state.
Definition types.h:586
ASTNodeType
AST Node types.
Definition types.h:215
ObjCPropertyAttributes
Objective-C property attributes.
Definition types.h:289
TokenType
Token types for lexical analysis.
Definition types.h:24
DataType
Data types supported by the compiler.
Definition types.h:193