Skip to content

Commit 17fa36b

Browse files
committed
fix bug in retrieve_array_access_value
1 parent 5e3b83a commit 17fa36b

File tree

6 files changed

+90
-39
lines changed

6 files changed

+90
-39
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ FLEX := flex
55
PYTHON := python3
66

77
# Compiler and linker flags
8-
CFLAGS := -Wall -Wextra -Wpedantic -Werror -O2 -Wno-array-bounds -Wno-switch
8+
CFLAGS := -Wall -Wextra -Wpedantic -Werror -O2
99
LDFLAGS := -lfl -lm
1010

1111
# Source files and directories

ast.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ ASTNode *create_int_node(int value)
281281

282282
ASTNode *create_long_node(long value)
283283
{
284-
ASTNode *node = create_node(NODE_INT, VAR_LONG, current_modifiers);
284+
ASTNode *node = create_node(NODE_LONG, VAR_LONG, current_modifiers);
285285
SET_DATA_LONG(node, value);
286286
return node;
287287
}
@@ -424,9 +424,9 @@ ASTNode *create_double_node(double value)
424424
return node;
425425
}
426426

427-
ASTNode *create_long_double_node(double value)
427+
ASTNode *create_long_double_node(long double value)
428428
{
429-
ASTNode *node = create_node(NODE_DOUBLE, VAR_LONG_DOUBLE, current_modifiers);
429+
ASTNode *node = create_node(NODE_LONG_DOUBLE, VAR_LONG_DOUBLE, current_modifiers);
430430
SET_DATA_LONG_DOUBLE(node, value);
431431
return node;
432432
}
@@ -1154,7 +1154,7 @@ Value retrieve_array_access_value(ASTNode *node, Value default_return_value)
11541154
case VAR_BOOL:
11551155
return (Value){.type=VAR_BOOL, .bvalue=((bool *)var->value.array_data)[idx]};
11561156
case VAR_CHAR:
1157-
return (Value){.type=VAR_CHAR, .svalue=((char *)var->value.array_data)[idx]};
1157+
return (Value){.type=VAR_CHAR, .ivalue=((char *)var->value.array_data)[idx]};
11581158
default:
11591159
yyerror("Unsupported array type");
11601160
return default_return_value;
@@ -1182,14 +1182,14 @@ float evaluate_expression_float(ASTNode *node)
11821182
return (float)array_value.dvalue;
11831183
case VAR_LONG_DOUBLE:
11841184
return (float)array_value.ldvalue;
1185+
case VAR_CHAR:
11851186
case VAR_INT:
11861187
return (float)array_value.ivalue;
11871188
case VAR_LONG:
11881189
return (float)array_value.lvalue;
11891190
case VAR_BOOL:
11901191
return (float)array_value.bvalue;
11911192
case VAR_SHORT:
1192-
case VAR_CHAR:
11931193
return (float)array_value.svalue;
11941194
default:
11951195
return 0.0f;
@@ -1199,6 +1199,7 @@ float evaluate_expression_float(ASTNode *node)
11991199
return node->data.fvalue;
12001200
case NODE_DOUBLE:
12011201
return (float)node->data.dvalue;
1202+
case NODE_CHAR:
12021203
case NODE_INT:
12031204
return (float)node->data.ivalue;
12041205
case NODE_IDENTIFIER:
@@ -1265,14 +1266,14 @@ double evaluate_expression_double(ASTNode *node)
12651266
return array_value.dvalue;
12661267
case VAR_LONG_DOUBLE:
12671268
return (double)array_value.ldvalue;
1269+
case VAR_CHAR:
12681270
case VAR_INT:
12691271
return (double)array_value.ivalue;
12701272
case VAR_LONG:
12711273
return (double)array_value.lvalue;
12721274
case VAR_BOOL:
12731275
return (double)array_value.bvalue;
12741276
case VAR_SHORT:
1275-
case VAR_CHAR:
12761277
return (double)array_value.svalue;
12771278
default:
12781279
return 0.0L;
@@ -1284,8 +1285,11 @@ double evaluate_expression_double(ASTNode *node)
12841285
return (double)node->data.ldvalue;
12851286
case NODE_FLOAT:
12861287
return (double)node->data.fvalue;
1288+
case NODE_CHAR:
12871289
case NODE_INT:
12881290
return (double)node->data.ivalue;
1291+
case NODE_SHORT:
1292+
return (double)node->data.svalue;
12891293
case NODE_LONG:
12901294
return (double)node->data.lvalue;
12911295
case NODE_IDENTIFIER:
@@ -1352,14 +1356,14 @@ long double evaluate_expression_long_double(ASTNode *node)
13521356
return (long double)array_value.dvalue;
13531357
case VAR_LONG_DOUBLE:
13541358
return array_value.ldvalue;
1359+
case VAR_CHAR:
13551360
case VAR_INT:
13561361
return (long double)array_value.ivalue;
13571362
case VAR_LONG:
13581363
return (long double)array_value.lvalue;
13591364
case VAR_BOOL:
13601365
return (long double)array_value.bvalue;
13611366
case VAR_SHORT:
1362-
case VAR_CHAR:
13631367
return (long double)array_value.svalue;
13641368
default:
13651369
return 0.0L;
@@ -1371,6 +1375,9 @@ long double evaluate_expression_long_double(ASTNode *node)
13711375
return (long double)node->data.fvalue;
13721376
case NODE_DOUBLE:
13731377
return (long double)node->data.dvalue;
1378+
case NODE_SHORT:
1379+
return (long double)node->data.svalue;
1380+
case VAR_CHAR:
13741381
case NODE_INT:
13751382
return (long double)node->data.ivalue;
13761383
case NODE_IDENTIFIER:
@@ -1437,14 +1444,14 @@ long evaluate_expression_long(ASTNode *node)
14371444
return (long)array_value.dvalue;
14381445
case VAR_LONG_DOUBLE:
14391446
return (long)array_value.ldvalue;
1447+
case VAR_CHAR:
14401448
case VAR_INT:
14411449
return (long)array_value.ivalue;
14421450
case VAR_LONG:
14431451
return array_value.lvalue;
14441452
case VAR_BOOL:
14451453
return (long)array_value.bvalue;
14461454
case VAR_SHORT:
1447-
case VAR_CHAR:
14481455
return (long)array_value.svalue;
14491456
default:
14501457
return 0L;
@@ -1458,6 +1465,9 @@ long evaluate_expression_long(ASTNode *node)
14581465
return (long)node->data.fvalue;
14591466
case NODE_DOUBLE:
14601467
return (long)node->data.dvalue;
1468+
case NODE_SHORT:
1469+
return (long)node->data.svalue;
1470+
case NODE_CHAR:
14611471
case NODE_INT:
14621472
return (long)node->data.ivalue;
14631473
case NODE_IDENTIFIER:
@@ -1688,14 +1698,14 @@ short evaluate_expression_short(ASTNode *node)
16881698
return (short)array_value.dvalue;
16891699
case VAR_LONG_DOUBLE:
16901700
return (short)array_value.ldvalue;
1701+
case VAR_CHAR:
16911702
case VAR_INT:
16921703
return (short)array_value.ivalue;
16931704
case VAR_LONG:
16941705
return (short)array_value.lvalue;
16951706
case VAR_BOOL:
16961707
return (short)array_value.bvalue;
16971708
case VAR_SHORT:
1698-
case VAR_CHAR:
16991709
return array_value.svalue;
17001710
default:
17011711
return 0;
@@ -1802,14 +1812,14 @@ int evaluate_expression_int(ASTNode *node)
18021812
return (int)array_value.dvalue;
18031813
case VAR_LONG_DOUBLE:
18041814
return (int)array_value.ldvalue;
1815+
case VAR_CHAR:
18051816
case VAR_INT:
18061817
return array_value.ivalue;
18071818
case VAR_LONG:
18081819
return (int)array_value.lvalue;
18091820
case VAR_BOOL:
18101821
return (int)array_value.bvalue;
18111822
case VAR_SHORT:
1812-
case VAR_CHAR:
18131823
return (int)array_value.svalue;
18141824
default:
18151825
return 0;
@@ -1958,14 +1968,14 @@ bool evaluate_expression_bool(ASTNode *node)
19581968
return (bool)array_value.dvalue;
19591969
case VAR_LONG_DOUBLE:
19601970
return (bool)array_value.ldvalue;
1971+
case VAR_CHAR:
19611972
case VAR_INT:
19621973
return (bool)array_value.ivalue;
19631974
case VAR_LONG:
19641975
return (bool)array_value.lvalue;
19651976
case VAR_BOOL:
19661977
return array_value.bvalue;
19671978
case VAR_SHORT:
1968-
case VAR_CHAR:
19691979
return (bool)array_value.svalue;
19701980
default:
19711981
return 0;

ast.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ typedef struct
111111
int array_length;
112112
} Variable;
113113

114-
typedef union
114+
typedef struct Value
115115
{
116116
VarType type;
117117
union
@@ -322,11 +322,13 @@ VarType get_function_return_type(const char *name);
322322

323323
/* Node creation functions */
324324
ASTNode *create_int_node(int value);
325+
ASTNode *create_long_node(long value);
325326
ASTNode *create_array_declaration_node(char *name, int length, VarType type);
326327
ASTNode *create_array_access_node(char *name, ASTNode *index);
327328
ASTNode *create_short_node(short value);
328329
ASTNode *create_float_node(float value);
329330
ASTNode *create_double_node(double value);
331+
ASTNode *create_long_double_node(long double value);
330332
ASTNode *create_char_node(char value);
331333
ASTNode *create_boolean_node(bool value);
332334
ASTNode *create_identifier_node(char *name);

lang.l

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "lang.tab.h"
99

1010
VarType current_var_type = NONE;
11+
bool giga_modifier_set = false;
1112

1213
char *unescape_string(const char *src) {
1314
// Allocate a buffer big enough for the worst case
@@ -67,7 +68,7 @@ extern int yylineno;
6768
"cringe" { return GOTO; }
6869
"edgy" { return IF; }
6970
"amogus" { return ELSE; }
70-
"giga" { return LONG; }
71+
"giga" { giga_modifier_set = true; return LONG; }
7172
"smol" { current_var_type = VAR_SHORT; return SMOL; }
7273
"nut" { return SIGNED; }
7374
"maxxing" { return SIZEOF; }
@@ -113,38 +114,65 @@ extern int yylineno;
113114
"🚽"[^\n]* ; /* Ignore single line comments */
114115
"W" { yylval.ival = 1; return BOOLEAN; }
115116
"L" { yylval.ival = 0; return BOOLEAN; }
117+
116118
[0-9]+\.[0-9]+([eE][+-]?[0-9]+)?[LlFf]? {
117-
char *endptr;
118-
if (strchr(yytext, 'f') || strchr(yytext, 'F')) {
119+
// handle giga modifer
120+
bool is_long = false;
121+
if (giga_modifier_set)
122+
{
123+
is_long = true;
124+
// giga modifer has been used.
125+
giga_modifier_set = false;
126+
}
127+
128+
char *endptr; /* Ensure endptr is declared as a pointer */
129+
int len = yyleng;
130+
if (len > 0 && (yytext[len-1] == 'f' || yytext[len-1] == 'F')) {
119131
yylval.fval = strtof(yytext, &endptr);
120132
return FLOAT_LITERAL;
121-
} else if (strchr(yytext, 'L') || strchr(yytext, 'l')) {
122-
yylval.dval = strtod(yytext, &endptr);
123-
return DOUBLE_LITERAL;
133+
} else if (len > 0 && (yytext[len-1] == 'l' || yytext[len-1] == 'L' || is_long)) {
134+
/* Remove the trailing suffix if it is present */
135+
yytext[len-1] = '\0';
136+
yylval.ldval = strtold(yytext, &endptr);
137+
return LONG_DOUBLE_LITERAL;
124138
} else {
125139
yylval.dval = strtod(yytext, &endptr);
126140
return DOUBLE_LITERAL;
127141
}
128142
}
129-
[0-9]+ {
130-
int next_char = input(); // Peek at the next character
131-
unput(next_char); // Put it back into the input stream
132143

144+
[0-9]+([lL])? {
145+
// handle giga modifer
146+
bool is_long = false;
147+
if (giga_modifier_set)
148+
{
149+
is_long = true;
150+
// giga modifer has been used.
151+
giga_modifier_set = false;
152+
}
153+
154+
int len = yyleng;
155+
if (len > 0 && (yytext[len-1] == 'l' || yytext[len-1] == 'L' || is_long)) {
156+
/* Remove the suffix before converting */
157+
yytext[len-1] = '\0';
158+
yylval.lval = strtol(yytext, NULL, 10);
159+
return LONG_LITERAL;
160+
}
161+
/* Peek at the next character */
162+
int next_char = input();
163+
/* Put it back into the input stream */
164+
unput(next_char);
133165
if (next_char == ']') {
134-
// If the next character is ']', treat this numeric literal as an integer.
135166
yylval.ival = atoi(yytext);
136167
return INT_LITERAL;
137168
}
138-
139-
// Otherwise, follow the existing type-based logic.
140169
if (current_var_type == VAR_SHORT) {
141170
yylval.sval = (short)atoi(yytext);
142171
return SHORT_LITERAL;
143172
} else if (current_var_type == VAR_INT || current_var_type == NONE) {
144173
yylval.ival = atoi(yytext);
145174
return INT_LITERAL;
146175
} else {
147-
// Default behavior for unexpected types
148176
yylval.ival = atoi(yytext);
149177
return INT_LITERAL;
150178
}

lang.y

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ ASTNode *root = NULL;
4040
short sval;
4141
float fval;
4242
double dval;
43+
long double ldval;
44+
long lval;
4345
char cval;
4446
char *strval;
4547
ASTNode *node;
@@ -64,8 +66,10 @@ ASTNode *root = NULL;
6466
%token <strval> STRING_LITERAL
6567
%token <cval> CHAR
6668
%token <ival> BOOLEAN
69+
%token <lval> LONG_LITERAL
6770
%token <fval> FLOAT_LITERAL
6871
%token <dval> DOUBLE_LITERAL
72+
%token <ldval> LONG_DOUBLE_LITERAL
6973
%token SLORP
7074

7175
/* Declare types for non-terminals */
@@ -212,15 +216,13 @@ if_statement:
212216
;
213217

214218
type:
215-
LONG RIZZ { $$ = VAR_LONG; }
216-
| LONG GIGACHAD { $$ = VAR_LONG_DOUBLE; }
217-
| LONG { $$ = VAR_LONG; }
218-
| RIZZ { $$ = VAR_INT; }
219+
RIZZ { $$ = VAR_INT; }
219220
| CHAD { $$ = VAR_FLOAT; }
220221
| GIGACHAD { $$ = VAR_DOUBLE; }
221222
| SMOL { $$ = VAR_SHORT; }
222223
| YAP { $$ = VAR_CHAR; }
223224
| CAP { $$ = VAR_BOOL; }
225+
| LONG { $$ = VAR_LONG; }
224226
;
225227

226228
declaration:
@@ -413,13 +415,15 @@ sizeof_expression:
413415
SIZEOF LPAREN expression RPAREN{ $$ = create_sizeof_node($3); }
414416
;
415417
literal:
416-
INT_LITERAL { $$ = create_int_node($1); }
417-
| FLOAT_LITERAL { $$ = create_float_node($1); }
418-
| DOUBLE_LITERAL { $$ = create_double_node($1); }
419-
| CHAR { $$ = create_char_node($1); }
420-
| SHORT_LITERAL { $$ = create_short_node($1); }
421-
| BOOLEAN { $$ = create_boolean_node($1); }
422-
| STRING_LITERAL { $$ = create_string_literal_node($1); free($1);}
418+
INT_LITERAL { $$ = create_int_node($1); }
419+
| FLOAT_LITERAL { $$ = create_float_node($1); }
420+
| LONG_LITERAL { $$ = create_long_node($1); }
421+
| DOUBLE_LITERAL { $$ = create_double_node($1); }
422+
| LONG_DOUBLE_LITERAL { $$ = create_long_double_node($1); }
423+
| CHAR { $$ = create_char_node($1); }
424+
| SHORT_LITERAL { $$ = create_short_node($1); }
425+
| BOOLEAN { $$ = create_boolean_node($1); }
426+
| STRING_LITERAL { $$ = create_string_literal_node($1); free($1);}
423427
;
424428

425429
identifier:

test_cases/long.brainrot

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
skibidi main {
2-
giga gigachad num1 = 0.3333333333333333333333333333333333L;
3-
yapping("%Lf", num1);
2+
🚽
3+
giga gigachad num1 = 0.3333333333333333333333333333333333;
4+
yapping("num1 = %Lf", num1);
5+
6+
giga rizz num2 = 69;
7+
yapping("num2 = %Ld", num2);
8+
9+
rizz num3 = 2147483647;
10+
yapping("num3 = %d", num3);
411
}

0 commit comments

Comments
 (0)