fix memory leak in operations

This commit is contained in:
2025-06-26 18:08:17 +01:00
parent 6828cc5f1a
commit 358127a145
3 changed files with 17 additions and 11 deletions

View File

@@ -1 +1 @@
let f(x)="bruh" let x(hello,lol,world, WORLD,HELLOOOO,LOLLLLLL, WORLD)=let f(x)="bruh"

View File

@@ -23,6 +23,7 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
darray_push(declarations, &_declaration); darray_push(declarations, &_declaration);
ParsedSingleDeclaration *declaration = ParsedSingleDeclaration *declaration =
darray_get(declarations, declarations->size - 1); darray_get(declarations, declarations->size - 1);
bool isFunction=false;
DArray parameters; DArray parameters;
declaration->from = parse_null(); declaration->from = parse_null();
@@ -39,6 +40,7 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
return parsedValue; return parsedValue;
token = darray_get(tokens, *index); token = darray_get(tokens, *index);
if (token->type == TOKEN_LPAREN) { if (token->type == TOKEN_LPAREN) {
isFunction = true;
darray_init(&parameters, sizeof(char *)); darray_init(&parameters, sizeof(char *));
(*index)++; (*index)++;
error_if_finished(file, tokens, index); error_if_finished(file, tokens, index);
@@ -98,7 +100,7 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (parameters.resizable) { if (isFunction) {
declaration->from = create_parsed_function(declaration->name, parameters, declaration->from = create_parsed_function(declaration->name, parameters,
declaration->from); declaration->from);
} }

View File

@@ -6,9 +6,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) { ParsedValue convert_to_operation(DArray *to_operate_on, DArray *operations) {
if (to_operate_on->size == 1) { if (to_operate_on->size == 1) {
return darray_get(to_operate_on, 0); return *((ParsedValue*)darray_get(to_operate_on, 0));
} }
TokenType operation = 0; TokenType operation = 0;
DArray positions; DArray positions;
@@ -23,10 +23,10 @@ ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) {
} }
darray_push(&positions, &i); darray_push(&positions, &i);
} }
ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue)); ParsedValue parsedValue;
parsedValue->type = AST_OPERATION; parsedValue.type = AST_OPERATION;
ParsedOperation *operationStruct = checked_malloc(sizeof(ParsedOperation)); ParsedOperation *operationStruct = checked_malloc(sizeof(ParsedOperation));
parsedValue->data = operationStruct; parsedValue.data = operationStruct;
operationStruct->operation = operation; operationStruct->operation = operation;
darray_init(&operationStruct->to_operate_on, sizeof(ParsedValue)); darray_init(&operationStruct->to_operate_on, sizeof(ParsedValue));
size_t last_position = 0; size_t last_position = 0;
@@ -37,16 +37,18 @@ ParsedValue *convert_to_operation(DArray *to_operate_on, DArray *operations) {
to_operate_on, to_operate_on_last_position, (*position) + 1); to_operate_on, to_operate_on_last_position, (*position) + 1);
DArray operations_slice = DArray operations_slice =
darray_slice(operations, last_position, *position); darray_slice(operations, last_position, *position);
ParsedValue result = convert_to_operation(&to_operate_on_slice, &operations_slice);
darray_push(&operationStruct->to_operate_on, darray_push(&operationStruct->to_operate_on,
convert_to_operation(&to_operate_on_slice, &operations_slice)); &result);
last_position = (*position); last_position = (*position);
to_operate_on_last_position = (*position) + 1; to_operate_on_last_position = (*position) + 1;
} }
DArray to_operate_on_slice = DArray to_operate_on_slice =
darray_slice(to_operate_on, to_operate_on_last_position, to_operate_on->size); darray_slice(to_operate_on, to_operate_on_last_position, to_operate_on->size);
DArray operations_slice = darray_slice(operations, last_position, operations->size); DArray operations_slice = darray_slice(operations, last_position, operations->size);
ParsedValue result =convert_to_operation(&to_operate_on_slice, &operations_slice);
darray_push(&operationStruct->to_operate_on, darray_push(&operationStruct->to_operate_on,
convert_to_operation(&to_operate_on_slice, &operations_slice)); &result);
darray_free(&positions, NULL); darray_free(&positions, NULL);
return parsedValue; return parsedValue;
} }
@@ -80,10 +82,12 @@ ParsedValue *parse_operations(char *file, DArray *tokens, size_t *index,
darray_push(&to_operate_on, parsedValue); darray_push(&to_operate_on, parsedValue);
free(parsedValue); free(parsedValue);
} }
ParsedValue *output = convert_to_operation(&to_operate_on, &operations); ParsedValue *parsedValue = checked_malloc(sizeof(ParsedValue));
ParsedValue output = convert_to_operation(&to_operate_on, &operations);
memcpy(parsedValue, &output,sizeof(ParsedValue));
darray_free(&to_operate_on, NULL); darray_free(&to_operate_on, NULL);
darray_free(&operations, NULL); darray_free(&operations, NULL);
return output; return parsedValue;
} }
void free_operation(void *ptr) { void free_operation(void *ptr) {