Skip to content

Commit 59f994f

Browse files
authored
Merge pull request #161 from Brainrotlang/feat/gang-keyword
feat: add `gang` (struct) keyword
2 parents cfeaaa7 + a926a64 commit 59f994f

26 files changed

Lines changed: 1373 additions & 461 deletions

‎README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Join our community on:
209209
| nut | signed | ✅ |
210210
| maxxing | sizeof | ✅ |
211211
| salty | static | ✅ |
212-
| gang | struct | ❌ |
212+
| gang | struct | ✅ |
213213
| ohio | switch | ✅ |
214214
| chungus | union | ❌ |
215215
| nonut | unsigned | ✅ |
@@ -255,7 +255,6 @@ Current limitations include:
255255
- Limited support for complex expressions
256256
- Basic error reporting
257257
- No support for arrays in user-defined functions
258-
- No support for pointers
259258

260259
## 🔌 VSCode Extension
261260

‎ast.c‎

Lines changed: 513 additions & 127 deletions
Large diffs are not rendered by default.

‎ast.h‎

Lines changed: 92 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "lib/hm.h"
77
#include "lib/arena.h"
88
#include "lib/mem.h"
9+
#include "lib/string_value.h"
910
#include <stdio.h>
1011
#include <stdlib.h>
1112
#include <stdbool.h>
@@ -25,7 +26,7 @@ typedef struct CaseNode CaseNode;
2526

2627
typedef struct
2728
{
28-
char *name;
29+
String name;
2930
int pointer_level;
3031
} Declarator;
3132

@@ -73,15 +74,33 @@ typedef enum
7374
VAR_BOOL,
7475
VAR_CHAR,
7576
VAR_STRING,
77+
VAR_STRUCT,
7678
NONE,
7779
} VarType;
7880

81+
/* A single field inside a struct definition */
82+
typedef struct StructField {
83+
String name;
84+
VarType type;
85+
int pointer_level;
86+
size_t offset; /* byte offset within the struct blob */
87+
struct StructField *next;
88+
} StructField;
89+
90+
/* A struct definition (the "template") */
91+
typedef struct StructDef {
92+
String name;
93+
StructField *fields;
94+
size_t total_size; /* total byte size of one instance */
95+
struct StructDef *next_def;
96+
} StructDef;
97+
7998
/* AST helper functions */
8099
ASTNode* arena_alloc_astnode(void);
81100

82101
typedef struct Parameter
83102
{
84-
char *name;
103+
String name;
85104
VarType type;
86105
int pointer_level;
87106
TypeModifiers modifiers;
@@ -90,7 +109,7 @@ typedef struct Parameter
90109

91110
typedef struct Function
92111
{
93-
char *name;
112+
String name;
94113
VarType return_type;
95114
int return_pointer_level;
96115
Parameter *parameters;
@@ -107,7 +126,7 @@ typedef struct
107126
double dvalue;
108127
bool bvalue;
109128
short svalue;
110-
char *strvalue;
129+
String strvalue;
111130
uintptr_t pvalue;
112131
} value;
113132
VarType type;
@@ -117,7 +136,7 @@ typedef struct
117136
/* Symbol table structure */
118137
typedef struct
119138
{
120-
char *name;
139+
String name;
121140
union
122141
{
123142
int ivalue;
@@ -126,7 +145,7 @@ typedef struct
126145
float fvalue;
127146
double dvalue;
128147
void *array_data;
129-
char *strvalue;
148+
String strvalue;
130149
uintptr_t pvalue;
131150
} value;
132151
TypeModifiers modifiers;
@@ -135,6 +154,7 @@ typedef struct
135154
bool is_array;
136155
int array_length; // lets keep it for now for backword compatibility
137156
ArrayDimensions array_dimensions;
157+
String struct_name; /* non-NULL when var_type == VAR_STRUCT */
138158
} Variable;
139159

140160
typedef union
@@ -147,7 +167,7 @@ typedef union
147167
bool bvalue;
148168
float fvalue;
149169
double dvalue;
150-
char *strvalue;
170+
String strvalue;
151171
uintptr_t pvalue;
152172
};
153173
int pointer_level;
@@ -211,11 +231,13 @@ typedef enum
211231
NODE_FUNC_CALL,
212232
NODE_FUNCTION_DEF,
213233
NODE_RETURN,
234+
NODE_STRUCT_DEF,
235+
NODE_STRUCT_ACCESS,
214236
} NodeType;
215237

216238
typedef struct
217239
{
218-
char *name;
240+
String name;
219241
ASTNode *index;
220242
ASTNode *indices[MAX_DIMENSIONS];
221243
int num_dimensions;
@@ -267,9 +289,19 @@ struct ASTNode
267289
int ivalue;
268290
float fvalue;
269291
double dvalue;
270-
char *strvalue;
271-
char *name;
292+
String strvalue;
293+
String name;
272294
Array array;
295+
struct {
296+
String struct_name; /* name of the struct type */
297+
String member_name; /* field being accessed */
298+
ASTNode *object; /* the struct-valued expr */
299+
} struct_access;
300+
301+
struct {
302+
String name; /* struct tag */
303+
StructField *fields; /* linked list of fields */
304+
} struct_def;
273305
struct
274306
{
275307
ASTNode *left;
@@ -295,7 +327,7 @@ struct ASTNode
295327
} while_stmt;
296328
struct
297329
{
298-
char *function_name;
330+
String function_name;
299331
ArgumentList *arguments;
300332
} func_call;
301333
StatementList *statements;
@@ -311,7 +343,7 @@ struct ASTNode
311343
} sizeof_stmt;
312344
struct
313345
{
314-
char *name;
346+
String name;
315347
VarType return_type;
316348
Parameter *parameters;
317349
ASTNode *body;
@@ -325,7 +357,7 @@ typedef struct Scope
325357
HashMap *variables;
326358
struct Scope *parent;
327359
bool is_function_scope;
328-
char *function_name;
360+
String function_name;
329361
} Scope;
330362

331363
/* Global variable declarations */
@@ -335,53 +367,53 @@ extern HashMap *function_map;
335367
extern ReturnValue current_return_value;
336368
extern JumpBuffer *jump_buffer;
337369
/* Function prototypes */
338-
bool set_int_variable(const char *name, int value, TypeModifiers mods);
339-
bool set_array_variable(char *name, int length, TypeModifiers mods, VarType type);
340-
bool set_short_variable(const char *name, short value, TypeModifiers mods);
341-
bool set_float_variable(const char *name, float value, TypeModifiers mods);
342-
bool set_double_variable(const char *name, double value, TypeModifiers mods);
343-
TypeModifiers get_variable_modifiers(const char *name);
370+
bool set_int_variable(const String name, int value, TypeModifiers mods);
371+
bool set_array_variable(String name, int length, TypeModifiers mods, VarType type);
372+
bool set_short_variable(const String name, short value, TypeModifiers mods);
373+
bool set_float_variable(const String name, float value, TypeModifiers mods);
374+
bool set_double_variable(const String name, double value, TypeModifiers mods);
375+
TypeModifiers get_variable_modifiers(const String name);
344376
void reset_modifiers(void);
345377
TypeModifiers get_current_modifiers(void);
346-
Variable *get_variable(const char *name);
378+
Variable *get_variable(const String name);
347379
Scope *create_scope(Scope *parent);
348380
void enter_function_scope(Function *func, ArgumentList *args);
349381
void exit_scope();
350382
void enter_scope();
351383
void free_scope(Scope *scope);
352-
void add_variable_to_scope(const char *name, Variable *var);
353-
Variable *variable_new(char *name);
354-
Function *get_function(const char *name);
355-
VarType get_function_return_type(const char *name);
384+
void add_variable_to_scope(const String name, Variable *var);
385+
Variable *variable_new(String name);
386+
Function *get_function(const String name);
387+
VarType get_function_return_type(const String name);
356388

357389
/* Node creation functions */
358390
ASTNode *create_int_node(int value);
359-
ASTNode *create_array_declaration_node(char *name, int length, VarType type);
360-
ASTNode *create_array_access_node(char *name, ASTNode *index);
391+
ASTNode *create_array_declaration_node(String name, int length, VarType type);
392+
ASTNode *create_array_access_node(String name, ASTNode *index);
361393
ASTNode *create_short_node(short value);
362394
ASTNode *create_float_node(float value);
363395
ASTNode *create_double_node(double value);
364396
ASTNode *create_char_node(char value);
365397
ASTNode *create_boolean_node(bool value);
366-
ASTNode *create_identifier_node(char *name);
367-
ASTNode *create_identifier_node_ex(char *name, int pointer_level);
368-
ASTNode *create_assignment_node(char *name, ASTNode *expr);
398+
ASTNode *create_identifier_node(String name);
399+
ASTNode *create_identifier_node_ex(String name, int pointer_level);
400+
ASTNode *create_assignment_node(String name, ASTNode *expr);
369401
ASTNode *create_assignment_target_node(ASTNode *target, ASTNode *expr);
370-
ASTNode *create_declaration_node(char *name, ASTNode *expr);
371-
ASTNode *create_declaration_node_ex(char *name, ASTNode *expr, int pointer_level);
402+
ASTNode *create_declaration_node(String name, ASTNode *expr);
403+
ASTNode *create_declaration_node_ex(String name, ASTNode *expr, int pointer_level);
372404
ASTNode *create_operation_node(OperatorType op, ASTNode *left, ASTNode *right);
373405
ASTNode *create_unary_operation_node(OperatorType op, ASTNode *operand);
374406
ASTNode *create_for_statement_node(ASTNode *init, ASTNode *cond, ASTNode *incr, ASTNode *body);
375407
ASTNode *create_while_statement_node(ASTNode *cond, ASTNode *body);
376408
ASTNode *create_do_while_statement_node(ASTNode *cond, ASTNode *body);
377-
ASTNode *create_function_call_node(char *func_name, ArgumentList *args);
409+
ASTNode *create_function_call_node(String func_name, ArgumentList *args);
378410
ArgumentList *create_argument_list(ASTNode *expr, ArgumentList *existing_list);
379411
ASTNode *create_print_statement_node(ASTNode *expr);
380412
ASTNode *create_sizeof_node(ASTNode *node);
381413
ASTNode *create_error_statement_node(ASTNode *expr);
382414
ASTNode *create_statement_list(ASTNode *statement, ASTNode *next_statement);
383415
ASTNode *create_if_statement_node(ASTNode *condition, ASTNode *then_branch, ASTNode *else_branch);
384-
ASTNode *create_string_literal_node(char *string);
416+
ASTNode *create_string_literal_node(String string);
385417
ASTNode *create_switch_statement_node(ASTNode *expression, CaseNode *cases);
386418
CaseNode *create_case_node(ASTNode *value, ASTNode *statements);
387419
CaseNode *create_default_case_node(ASTNode *statements);
@@ -392,7 +424,7 @@ ASTNode *create_return_node(ASTNode *expr);
392424
ExpressionList *create_expression_list(ASTNode *expr);
393425
ExpressionList *append_expression_list(ExpressionList *list, ASTNode *expr);
394426
void free_expression_list(ExpressionList *list);
395-
void populate_multi_array_variable(char *name, ExpressionList *list, int dimensions[], int num_dimensions);
427+
void populate_multi_array_variable(String name, ExpressionList *list, int dimensions[], int num_dimensions);
396428
void free_ast(void);
397429

398430
/* Evaluation and execution functions */
@@ -403,8 +435,8 @@ int evaluate_expression_int(ASTNode *node);
403435
short evaluate_expression_short(ASTNode *node);
404436
bool evaluate_expression_bool(ASTNode *node);
405437
int evaluate_expression(ASTNode *node);
406-
bool is_const_variable(const char *name);
407-
void check_const_assignment(const char *name);
438+
bool is_const_variable(const String name);
439+
void check_const_assignment(const String name);
408440
void execute_statement(ASTNode *node);
409441
void execute_statements(ASTNode *node);
410442
void execute_assignment(ASTNode *node);
@@ -413,43 +445,46 @@ void execute_while_statement(ASTNode *node);
413445
void execute_do_while_statement(ASTNode *node);
414446
void execute_if_statement(ASTNode *node);
415447
void reset_modifiers(void);
416-
bool check_and_mark_identifier(ASTNode *node, const char *contextErrorMessage);
448+
bool check_and_mark_identifier(ASTNode *node, const String contextErrorMessage);
417449
bool is_expression(ASTNode *node, VarType type);
418450
int get_expression_pointer_level(ASTNode *node);
419451
uintptr_t evaluate_expression_pointer(ASTNode *node);
420452
void *evaluate_lvalue_address(ASTNode *node);
421453
void bruh();
422454
size_t count_expression_list(ExpressionList *list);
423455
size_t handle_sizeof(ASTNode *node);
424-
size_t get_type_size(char *name);
456+
size_t get_type_size(String name);
425457
size_t get_type_size_for_descriptor(VarType type, int pointer_level, TypeModifiers mods);
426458
void *handle_function_call(ASTNode *node);
427-
ASTNode *create_multi_array_declaration_node(char *name, int dimensions[], int num_dimensions, VarType type);
428-
bool set_multi_array_variable(const char *name, int dimensions[], int num_dimensions, TypeModifiers mods, VarType type);
429-
ASTNode *create_array_access_node_single(char *name, ASTNode *index);
430-
ASTNode *create_multi_array_access_node(char *name, ASTNode *indices[], int num_indices);
431-
432-
/* Built-In functions */
433-
void execute_yapping_call(ArgumentList *args);
434-
void execute_yappin_call(ArgumentList *args);
435-
void execute_baka_call(ArgumentList *args);
436-
void execute_ragequit_call(ArgumentList *args);
437-
void execute_chill_call(ArgumentList *args);
438-
void execute_slorp_call(ArgumentList *args);
459+
ASTNode *create_multi_array_declaration_node(String name, int dimensions[], int num_dimensions, VarType type);
460+
bool set_multi_array_variable(const String name, int dimensions[], int num_dimensions, TypeModifiers mods, VarType type);
461+
ASTNode *create_array_access_node_single(String name, ASTNode *index);
462+
ASTNode *create_multi_array_access_node(String name, ASTNode *indices[], int num_indices);
439463

440464
/* User-defined functions */
441-
Function *create_function(char *name, VarType return_type, Parameter *params, ASTNode *body);
442-
Function *create_function_ex(char *name, VarType return_type, int return_pointer_level, Parameter *params, ASTNode *body);
443-
Parameter *create_parameter(char *name, VarType type, Parameter *next, TypeModifiers mods);
444-
Parameter *create_parameter_ex(char *name, VarType type, int pointer_level, Parameter *next, TypeModifiers mods);
445-
void execute_function_call(const char *name, ArgumentList *args);
446-
ASTNode *create_function_def_node(char *name, VarType return_type, Parameter *params, ASTNode *body);
447-
ASTNode *create_function_def_node_ex(char *name, VarType return_type, int return_pointer_level, Parameter *params, ASTNode *body);
465+
Function *create_function(String name, VarType return_type, Parameter *params, ASTNode *body);
466+
Function *create_function_ex(String name, VarType return_type, int return_pointer_level, Parameter *params, ASTNode *body);
467+
Parameter *create_parameter(String name, VarType type, Parameter *next, TypeModifiers mods);
468+
Parameter *create_parameter_ex(String name, VarType type, int pointer_level, Parameter *next, TypeModifiers mods);
469+
void execute_function_call(const String name, ArgumentList *args);
470+
ASTNode *create_function_def_node(String name, VarType return_type, Parameter *params, ASTNode *body);
471+
ASTNode *create_function_def_node_ex(String name, VarType return_type, int return_pointer_level, Parameter *params, ASTNode *body);
448472
void handle_return_statement(ASTNode *expr);
449473
void *handle_binary_operation(ASTNode *node);
450474
void free_function_table(void);
451475
void free_static_variable_map(void);
452476

477+
/* Struct types */
478+
void register_struct_def(StructDef *def);
479+
StructDef *get_struct_def(const String name);
480+
void free_struct_registry(void);
481+
StructField *find_struct_field(StructDef *def, const String name);
482+
size_t compute_struct_layout(StructField *fields); /* fills offsets, returns total */
483+
ASTNode *create_struct_def_node(String name, StructField *fields);
484+
ASTNode *create_struct_access_node(ASTNode *object, String member);
485+
void *evaluate_struct_member_address(ASTNode *node);
486+
void populate_struct_variable(const String name, ExpressionList *list);
487+
453488
extern TypeModifiers current_modifiers;
454489

455490
extern Arena arena;

0 commit comments

Comments
 (0)