Skip to content

Commit 7bdd66e

Browse files
authored
Merge pull request #124 from Brainrotlang/feat/func_type_modifier
Feat(func): enable type modifer in user defined function
2 parents 0886781 + 2cea433 commit 7bdd66e

File tree

6 files changed

+64
-29
lines changed

6 files changed

+64
-29
lines changed

ast.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
JumpBuffer *jump_buffer = {0};
1313

1414
Function *function_table = NULL;
15-
jmp_buf return_jump_buf;
1615
ReturnValue current_return_value;
1716
Arena arena;
1817

@@ -27,6 +26,7 @@ bool set_variable(const char *name, void *value, VarType type, TypeModifiers mod
2726
Variable *var = get_variable(name);
2827
if (var != NULL)
2928
{
29+
3030
var->modifiers = mods;
3131
var->var_type = type;
3232
switch (type)
@@ -160,6 +160,7 @@ TypeModifiers get_current_modifiers(void)
160160

161161
/* Include the symbol table functions */
162162
extern void yyerror(const char *s);
163+
extern void cleanup(void);
163164
extern void ragequit(int exit_code);
164165
extern void chill(unsigned int seconds);
165166
extern void yapping(const char *format, ...);
@@ -1893,6 +1894,8 @@ void check_const_assignment(const char *name)
18931894
{
18941895
yylineno = yylineno - 2;
18951896
yyerror("Cannot modify const variable");
1897+
// TODO: make proper cleanup when error occurs
1898+
cleanup();
18961899
exit(EXIT_FAILURE);
18971900
}
18981901
}
@@ -3476,7 +3479,7 @@ void handle_return_statement(ASTNode *expr)
34763479
}
34773480
}
34783481

3479-
Parameter *create_parameter(char *name, VarType type, Parameter *next)
3482+
Parameter *create_parameter(char *name, VarType type, Parameter *next, TypeModifiers mods)
34803483
{
34813484
Parameter *param = ARENA_ALLOC(Parameter);
34823485
if (!param)
@@ -3488,6 +3491,7 @@ Parameter *create_parameter(char *name, VarType type, Parameter *next)
34883491
param->name = ARENA_STRDUP(name);
34893492
param->type = type;
34903493
param->next = next;
3494+
param->modifiers = mods;
34913495

34923496
return param;
34933497
}
@@ -3605,26 +3609,27 @@ void enter_function_scope(Function *func, ArgumentList *args)
36053609
{
36063610
Variable *var = variable_new(curr_param->name);
36073611
var->var_type = curr_param->type;
3612+
TypeModifiers mods = curr_param->modifiers;
36083613
add_variable_to_scope(curr_param->name, var);
36093614
SAFE_FREE(var);
36103615

36113616
switch (curr_param->type)
36123617
{
36133618
case VAR_INT:
36143619
case VAR_CHAR:
3615-
set_int_variable(curr_param->name, arg_values[i].ivalue, get_current_modifiers());
3620+
set_int_variable(curr_param->name, arg_values[i].ivalue, mods);
36163621
break;
36173622
case VAR_FLOAT:
3618-
set_float_variable(curr_param->name, arg_values[i].fvalue, get_current_modifiers());
3623+
set_float_variable(curr_param->name, arg_values[i].fvalue, mods);
36193624
break;
36203625
case VAR_DOUBLE:
3621-
set_double_variable(curr_param->name, arg_values[i].dvalue, get_current_modifiers());
3626+
set_double_variable(curr_param->name, arg_values[i].dvalue, mods);
36223627
break;
36233628
case VAR_BOOL:
3624-
set_bool_variable(curr_param->name, arg_values[i].bvalue, get_current_modifiers());
3629+
set_bool_variable(curr_param->name, arg_values[i].bvalue, mods);
36253630
break;
36263631
case VAR_SHORT:
3627-
set_short_variable(curr_param->name, arg_values[i].svalue, get_current_modifiers());
3632+
set_short_variable(curr_param->name, arg_values[i].svalue, mods);
36283633
break;
36293634
case NONE:
36303635
break;

ast.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct Parameter
5959
{
6060
char *name;
6161
VarType type;
62+
TypeModifiers modifiers;
6263
struct Parameter *next;
6364
} Parameter;
6465

@@ -286,7 +287,7 @@ extern TypeModifiers current_modifiers;
286287
extern Scope *current_scope;
287288
extern Function *function_table;
288289
extern ReturnValue current_return_value;
289-
extern jmp_buf return_jump_buf;
290+
extern JumpBuffer *jump_buffer;
290291
/* Function prototypes */
291292
bool set_int_variable(const char *name, int value, TypeModifiers mods);
292293
bool set_array_variable(char *name, int length, TypeModifiers mods, VarType type);
@@ -380,7 +381,7 @@ void *handle_function_call(ASTNode *node);
380381

381382
/* User-defined functions */
382383
Function *create_function(char *name, VarType return_type, Parameter *params, ASTNode *body);
383-
Parameter *create_parameter(char *name, VarType type, Parameter *next);
384+
Parameter *create_parameter(char *name, VarType type, Parameter *next, TypeModifiers mods);
384385
void execute_function_call(const char *name, ArgumentList *args);
385386
ASTNode *create_function_def_node(char *name, VarType return_type, Parameter *params, ASTNode *body);
386387
void handle_return_statement(ASTNode *expr);
@@ -473,4 +474,13 @@ extern Arena arena;
473474

474475
#define CURRENT_JUMP_BUFFER() (jump_buffer->data)
475476

477+
#define CLEAN_JUMP_BUFFER() \
478+
do \
479+
{ \
480+
while (jump_buffer) \
481+
{ \
482+
POP_JUMP_BUFFER(); \
483+
} \
484+
} while (0)
485+
476486
#endif /* AST_H */

lang.y

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ params
136136
;
137137

138138
param_list
139-
: type IDENTIFIER
140-
{ $$ = create_parameter($2, $1, NULL); SAFE_FREE($2); }
141-
| param_list COMMA type IDENTIFIER
142-
{ $$ = create_parameter($4, $3, $1); SAFE_FREE($4); }
139+
: optional_modifiers type IDENTIFIER
140+
{ $$ = create_parameter($3, $2, NULL, get_current_modifiers()); SAFE_FREE($3); }
141+
| param_list COMMA optional_modifiers type IDENTIFIER
142+
{ $$ = create_parameter($5, $4, $1, get_current_modifiers()); SAFE_FREE($5); }
143143
;
144144

145145

@@ -705,6 +705,10 @@ void cleanup() {
705705

706706
// Free the scope
707707
free_scope(current_scope);
708+
709+
free_function_table();
710+
711+
CLEAN_JUMP_BUFFER();
708712

709713
// Clean up flex's internal state
710714
yylex_destroy();

run_valgrind_tests.sh

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
for f in test_cases/*.brainrot; do
44
echo "Running Valgrind on $f..."
55
base=$(basename "$f" .brainrot)
6-
if [[ "$base" == slorp_int ]]; then
7-
echo 42 | valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
8-
elif [[ "$base" == slorp_short ]]; then
9-
echo 69 | valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
10-
elif [[ "$base" == slorp_float ]]; then
11-
echo "3.14" | valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
12-
elif [[ "$base" == slorp_double ]]; then
13-
echo "3.141592" | valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
14-
elif [[ "$base" == slorp_char ]]; then
15-
echo "c" | valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
16-
elif [[ "$base" == slorp_string ]]; then
17-
echo "skibidi bop bop yes yes" | valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
6+
7+
case "$base" in
8+
slorp_int) input="42" ;;
9+
slorp_short) input="69" ;;
10+
slorp_float) input="3.14" ;;
11+
slorp_double) input="3.141592" ;;
12+
slorp_char) input="c" ;;
13+
slorp_string) input="skibidi bop bop yes yes" ;;
14+
*) input="" ;;
15+
esac
16+
17+
if [[ -n "$input" ]]; then
18+
echo "$input" | valgrind --leak-check=full --error-exitcode=100 ./brainrot "$f"
1819
else
19-
valgrind --leak-check=full --error-exitcode=1 ./brainrot "$f"
20+
valgrind --leak-check=full --error-exitcode=100 ./brainrot "$f"
2021
fi
21-
if [[ $? -ne 0 ]]; then
22-
echo "Valgrind failed on $f"
22+
23+
valgrind_exit_code=$? # Capture only valgrind’s exit code
24+
25+
if [[ $valgrind_exit_code -eq 100 ]]; then
26+
echo "Valgrind detected memory issues in $f"
2327
exit 1
2428
fi
29+
2530
echo
2631
done
32+

test_cases/func-modifier.brainrot

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
rizz modifier_test(deadass rizz a) {
2+
a = 3;
3+
bussin a;
4+
}
5+
6+
skibidi main {
7+
modifier_test(6);
8+
bussin 0;
9+
}

tests/expected_results.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@
5050
"slorp_char": "You typed: c",
5151
"slorp_string": "You typed: skibidi bop bop yes yes",
5252
"fib": "55",
53-
"func_scope": "from inner 10\nfrom outer 4\n"
53+
"func_scope": "from inner 10\nfrom outer 4\n",
54+
"func-modifier": "Error: Cannot modify const variable at line 7\n"
5455
}

0 commit comments

Comments
 (0)