Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for arrays #65

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
525 changes: 516 additions & 9 deletions ast.c

Large diffs are not rendered by default.

69 changes: 43 additions & 26 deletions ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ typedef struct
bool is_const;
} TypeModifiers;


typedef struct JumpBuffer
{
jmp_buf data;
Expand Down Expand Up @@ -56,9 +55,17 @@ typedef struct
bool bvalue;
float fvalue;
double dvalue;
int *iarray;
short *sarray;
bool *barray;
float *farray;
double *darray;
char *carray;
} value;
TypeModifiers modifiers;
VarType var_type;
bool is_array;
int array_length;
} Variable;

typedef union
Expand Down Expand Up @@ -120,7 +127,8 @@ typedef enum
NODE_DEFAULT_CASE,
NODE_BREAK_STATEMENT,
NODE_FUNC_CALL,
NODE_SIZEOF
NODE_SIZEOF,
NODE_ARRAY_ACCESS,
} NodeType;

/* Rest of the structure definitions */
Expand Down Expand Up @@ -156,8 +164,10 @@ struct ASTNode
NodeType type;
TypeModifiers modifiers;
VarType var_type;
bool alreadyChecked;
bool isValidSymbol;
bool already_checked;
bool is_valid_symbol;
bool is_array;
int array_length;
union
{
short svalue;
Expand All @@ -167,6 +177,11 @@ struct ASTNode
double dvalue;
char *name;
struct
{
char *name;
ASTNode *index;
} array;
struct
{
ASTNode *left;
ASTNode *right;
Expand Down Expand Up @@ -212,6 +227,7 @@ extern int var_count;

/* Function prototypes */
bool set_int_variable(const char *name, int value, TypeModifiers mods);
bool set_array_variable(char *name, int length, TypeModifiers mods, VarType type);
bool set_short_variable(const char *name, short value, TypeModifiers mods);
bool set_float_variable(const char *name, float value, TypeModifiers mods);
bool set_double_variable(const char *name, double value, TypeModifiers mods);
Expand All @@ -221,6 +237,8 @@ TypeModifiers get_current_modifiers(void);

/* Node creation functions */
ASTNode *create_int_node(int value);
ASTNode *create_array_declaration_node(char *name, int length, VarType type);
ASTNode *create_array_access_node(char *name, ASTNode *index);
ASTNode *create_short_node(short value);
ASTNode *create_float_node(float value);
ASTNode *create_double_node(double value);
Expand Down Expand Up @@ -322,36 +340,35 @@ extern TypeModifiers current_modifiers;
(node)->data.func_call.arguments = (args); \
} while (0)


/* Macros for handling jump buffer */
#define PUSH_JUMP_BUFFER() \
do \
{ \
#define PUSH_JUMP_BUFFER() \
do \
{ \
JumpBuffer *jb = malloc(sizeof(JumpBuffer)); \
jb->next = jump_buffer; \
jump_buffer = jb; \
jb->next = jump_buffer; \
jump_buffer = jb; \
} while (0)

#define POP_JUMP_BUFFER() \
do \
{ \
JumpBuffer *jb = jump_buffer; \
#define POP_JUMP_BUFFER() \
do \
{ \
JumpBuffer *jb = jump_buffer; \
jump_buffer = jump_buffer->next; \
free(jb); \
free(jb); \
} while (0)

#define LONGJMP() \
do \
{ \
if (jump_buffer != NULL) \
{ \
longjmp(jump_buffer->data, 1); \
} \
else \
{ \
#define LONGJMP() \
do \
{ \
if (jump_buffer != NULL) \
{ \
longjmp(jump_buffer->data, 1); \
} \
else \
{ \
yyerror("No jump buffer available"); \
exit(1); \
} \
exit(1); \
} \
} while (0)

#define CURRENT_JUMP_BUFFER() (jump_buffer->data)
Expand Down
14 changes: 14 additions & 0 deletions examples/bool_array.brainrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
skibidi main {
cap bool_array[3];
rizz i;

bool_array[0] = W;
bool_array[1] = L;
bool_array[2] = W;

flex (i = 0; i < 3; i = i + 1) {
yapping("%b", bool_array[i]);
}

bussin 0;
}
15 changes: 15 additions & 0 deletions examples/char_array.brainrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
skibidi main {
yap a[4];
rizz i;

a[0] = 'r';
a[1] = 'i';
a[2] = 'z';
a[3] = 'z';

flex (i = 0; i < 4; i = i + 1) {
yappin("%c", a[i]);
}

bussin 0;
}
14 changes: 14 additions & 0 deletions examples/double_array.brainrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
skibidi main {
gigachad double_array[3];

double_array[0] = 3.14L;
double_array[1] = 3.1415L;
double_array[2] = 3.141592L;

rizz i;
flex (i = 0; i < 3; i = i + 1) {
yapping("%f", double_array[i]);
}

bussin 0;
}
14 changes: 14 additions & 0 deletions examples/float_array.brainrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
skibidi main {
chad float_array[3];

float_array[0] = 3.14f;
float_array[1] = 3.1415f;
float_array[2] = 3.141592f;

rizz i;
flex (i = 0; i < 3; i = i + 1) {
yapping("%f", float_array[i]);
}

bussin 0;
}
14 changes: 14 additions & 0 deletions examples/int_array.brainrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
skibidi main {
rizz int_array[3];
rizz i;

flex (i = 1; i < 4; i = i + 1) {
int_array[i-1] = i;
}

flex (i = 0; i < 3; i = i + 1) {
yapping("%d", int_array[i]);
}

bussin 0;
}
14 changes: 14 additions & 0 deletions examples/short_array.brainrot
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
skibidi main {
smol short_array[3];
rizz i;

flex (i = 1; i < 4; i = i + 1) {
short_array[i-1] = i;
}

flex (i = 0; i < 3; i = i + 1) {
yapping("%d", short_array[i]);
}

bussin 0;
}
17 changes: 16 additions & 1 deletion lang.l
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,36 @@ extern int yylineno;
";" { return SEMICOLON; }
"," { return COMMA; }
":" { return COLON; }
"[" { return LBRACKET; }
"]" { return RBRACKET; }

"🚽"[^\n]* ; /* Ignore single line comments */
"W" { yylval.ival = 1; return BOOLEAN; }
"L" { yylval.ival = 0; return BOOLEAN; }
[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[fF]? {
[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[LlFf]? {
char *endptr;
if (strchr(yytext, 'f') || strchr(yytext, 'F')) {
yylval.fval = strtof(yytext, &endptr);
return FLOAT_LITERAL;
} else if (strchr(yytext, 'L') || strchr(yytext, 'l')) {
yylval.dval = strtod(yytext, &endptr);
return DOUBLE_LITERAL;
} else {
yylval.dval = strtod(yytext, &endptr);
return DOUBLE_LITERAL;
}
}
[0-9]+ {
int next_char = input(); // Peek at the next character
unput(next_char); // Put it back into the input stream

if (next_char == ']') {
// If the next character is ']', treat this numeric literal as an integer.
yylval.ival = atoi(yytext);
return INT_LITERAL;
}

// Otherwise, follow the existing type-based logic.
if (current_var_type == VAR_SHORT) {
yylval.sval = (short)atoi(yytext);
return SHORT_LITERAL;
Expand Down
55 changes: 55 additions & 0 deletions lang.y
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ASTNode *root = NULL;
%token BREAK CASE DEADASS CONTINUE DEFAULT DO DOUBLE ELSE ENUM
%token EXTERN CHAD GIGACHAD FOR GOTO IF LONG SMOL SIGNED
%token SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED VOID VOLATILE GOON
%token LBRACKET RBRACKET
%token <strval> IDENTIFIER
%token <ival> INT_LITERAL
%token <sval> SHORT_LITERAL
Expand Down Expand Up @@ -201,6 +202,42 @@ declaration:
current_var_type = VAR_INT;
$$ = create_assignment_node($3, $5);
}
| optional_modifiers RIZZ IDENTIFIER LBRACKET INT_LITERAL RBRACKET
{
current_var_type = VAR_INT;
set_array_variable($3, $5, get_current_modifiers(), current_var_type);
$$ = create_array_declaration_node($3, $5, current_var_type);
}
| optional_modifiers CHAD IDENTIFIER LBRACKET INT_LITERAL RBRACKET
{
current_var_type = VAR_FLOAT;
set_array_variable($3, $5, get_current_modifiers(), current_var_type);
$$ = create_array_declaration_node($3, $5, current_var_type);
}
| optional_modifiers SMOL IDENTIFIER LBRACKET INT_LITERAL RBRACKET
{
current_var_type = VAR_SHORT;
set_array_variable($3, $5, get_current_modifiers(), current_var_type);
$$ = create_array_declaration_node($3, $5, current_var_type);
}
| optional_modifiers GIGACHAD IDENTIFIER LBRACKET INT_LITERAL RBRACKET
{
current_var_type = VAR_DOUBLE;
set_array_variable($3, $5, get_current_modifiers(), current_var_type);
$$ = create_array_declaration_node($3, $5, current_var_type);
}
| optional_modifiers YAP IDENTIFIER LBRACKET INT_LITERAL RBRACKET
{
current_var_type = VAR_CHAR;
set_array_variable($3, $5, get_current_modifiers(), current_var_type);
$$ = create_array_declaration_node($3, $5, current_var_type);
}
| optional_modifiers CAP IDENTIFIER LBRACKET INT_LITERAL RBRACKET
{
current_var_type = VAR_BOOL;
set_array_variable($3, $5, get_current_modifiers(), current_var_type);
$$ = create_array_declaration_node($3, $5, current_var_type);
}
| optional_modifiers SMOL IDENTIFIER
{
current_var_type = VAR_SHORT;
Expand Down Expand Up @@ -422,6 +459,24 @@ expression:
{
$$ = create_unary_operation_node(OP_PRE_DEC, $2); // Pre-decrement
}
| IDENTIFIER LBRACKET expression RBRACKET
{
$$ = create_array_access_node($1, $3);
}
| IDENTIFIER LBRACKET expression RBRACKET EQUALS expression
{
ASTNode *access = create_array_access_node($1, $3);
ASTNode *node = malloc(sizeof(ASTNode));
if (!node) {
yyerror("Memory allocation failed");
exit(1);
}
node->type = NODE_ASSIGNMENT;
node->data.op.left = access; // Keep the entire array access node
node->data.op.right = $6;
node->data.op.op = OP_ASSIGN;
$$ = node;
}
;


Expand Down
8 changes: 7 additions & 1 deletion tests/expected_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@
"const": "1\n1\n",
"do-while": "0\n1\n1\n2\n3\n5\n8\n",
"nest-loop": "0\ngoon\ngoon\ngoon\nbreak goon 2\ngoon\n1\ngoon\ngoon\ngoon\nbreak goon 2\ngoon\n2\ngoon\ngoon\ngoon\nbreak goon 2\ngoon\nbreak flex",
"next-prime": "11"
"next-prime": "11",
"int_array": "1\n2\n3\n",
"short_array": "1\n2\n3\n",
"bool_array": "W\nL\nW\n",
"float_array": "3.140000\n3.141500\n3.141592\n",
"double_array": "3.140000\n3.141500\n3.141592\n",
"char_array": "rizz"
}
Loading