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

fix: compiler annoying warnings #117

Merged
merged 1 commit into from
Feb 1, 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
33 changes: 19 additions & 14 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ bool set_variable(const char *name, void *value, VarType type, TypeModifiers mod
case VAR_CHAR:
var->value.ivalue = *(char *)value;
break;
case NONE:
break;
}
return true;
}
Expand Down Expand Up @@ -238,7 +240,6 @@ void execute_switch_statement(ASTNode *node)
static ASTNode *create_node(NodeType type, VarType var_type, TypeModifiers modifiers)
{
ASTNode *node = SAFE_MALLOC(ASTNode);
int a;
if (!node)
{
yyerror("Error: Memory allocation failed for ASTNode.\n");
Expand Down Expand Up @@ -451,6 +452,7 @@ void *handle_identifier(ASTNode *node, const char *contextErrorMessage, int prom
case VAR_CHAR:
case VAR_SHORT:
promoted_value.fvalue = (float)var->value.svalue;
return &promoted_value.svalue;
case VAR_BOOL:
promoted_value.fvalue = (float)var->value.ivalue;
return &promoted_value.fvalue;
Expand Down Expand Up @@ -569,7 +571,7 @@ int get_expression_type(ASTNode *node)
}
}

void *handle_binary_operation(ASTNode *node, int result_type)
void *handle_binary_operation(ASTNode *node)
{
if (!node || node->type != NODE_OPERATION)
{
Expand Down Expand Up @@ -1127,7 +1129,7 @@ float evaluate_expression_float(ASTNode *node)
case NODE_OPERATION:
{
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
void *result = handle_binary_operation(node);
float result_float = 0.0f;
result_float = (result_type == VAR_INT)
? (float)(*(int *)result)
Expand Down Expand Up @@ -1228,7 +1230,7 @@ double evaluate_expression_double(ASTNode *node)
case NODE_OPERATION:
{
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
void *result = handle_binary_operation(node);
double result_double = 0.0L;
result_double = (result_type == VAR_INT)
? (double)(*(int *)result)
Expand Down Expand Up @@ -1340,7 +1342,6 @@ size_t handle_sizeof(ASTNode *node)
{
ASTNode *expr = node->data.sizeof_stmt.expr;
VarType type = get_expression_type(node->data.sizeof_stmt.expr);
bool is_array = node->data.sizeof_stmt.expr->is_array;
if (expr->type == NODE_IDENTIFIER)
{
return get_type_size(expr->data.name);
Expand Down Expand Up @@ -1417,7 +1418,7 @@ short evaluate_expression_short(ASTNode *node)

// Regular integer operations
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
void *result = handle_binary_operation(node);
short result_short = 0;
result_short = (result_type == VAR_SHORT)
? *(short *)result
Expand Down Expand Up @@ -1544,7 +1545,7 @@ int evaluate_expression_int(ASTNode *node)

// Regular integer operations
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
void *result = handle_binary_operation(node);
int result_int = 0;
result_int = (result_type == VAR_INT)
? *(int *)result
Expand Down Expand Up @@ -1704,7 +1705,7 @@ bool evaluate_expression_bool(ASTNode *node)

// Regular integer operations
int result_type = get_expression_type(node);
void *result = handle_binary_operation(node, result_type);
void *result = handle_binary_operation(node);
bool result_bool = 0;
result_bool = (result_type == VAR_INT)
? (bool)(*(int *)result)
Expand Down Expand Up @@ -2172,10 +2173,13 @@ void execute_statement(ASTNode *node)
switch (node->type)
{
case NODE_DECLARATION:
{
char *name = node->data.op.left->data.name;
Variable *var = variable_new(name);
add_variable_to_scope(name, var);
SAFE_FREE(var);
}
__attribute__((fallthrough));
case NODE_ASSIGNMENT:
{
char *name = node->data.op.left->data.name;
Expand Down Expand Up @@ -2287,8 +2291,7 @@ void execute_statement(ASTNode *node)
case NODE_ARRAY_ACCESS:
if (node->data.array.name && node->data.array.index)
{
int length = node->data.array.index->data.ivalue;
if (!(node->data.array.name, length, node->modifiers, node->var_type))
if (!(node->data.array.name))
{
yyerror("Failed to create array");
}
Expand Down Expand Up @@ -2757,7 +2760,7 @@ void execute_yapping_call(ArgumentList *args)
}

// Check for buffer overflow
if (buffer_offset >= sizeof(buffer))
if (buffer_offset >= (int)sizeof(buffer))
{
yyerror("Buffer overflow in yapping call");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -2907,7 +2910,7 @@ void execute_yappin_call(ArgumentList *args)
}

// Check for buffer overflow
if (buffer_offset >= sizeof(buffer))
if (buffer_offset >= (int)sizeof(buffer))
{
yyerror("Buffer overflow in yappin call");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -3200,7 +3203,7 @@ void populate_array_variable(char *name, ExpressionList *list)
yyerror("Not an array!");
return;
}
if (var->array_length < count_expression_list(list))
if (var->array_length < (int)count_expression_list(list))
{
yyerror("Too many elements in array initialization");
exit(1);
Expand Down Expand Up @@ -3604,6 +3607,8 @@ void execute_function_call(const char *name, ArgumentList *args)
case VAR_CHAR:
set_int_variable(curr_param->name, evaluate_expression_int(curr_arg->expr), get_current_modifiers());
break;
case NONE:
break;
}

Parameter *tmp = curr_param;
Expand Down Expand Up @@ -3668,7 +3673,7 @@ void handle_return_statement(ASTNode *expr)
}
}
// skibidi main function do not have jump buffer
if (jump_buffer && CURRENT_JUMP_BUFFER() != NULL)
if (CURRENT_JUMP_BUFFER())
LONGJMP();
}

Expand Down
1 change: 1 addition & 0 deletions ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ Parameter *create_parameter(char *name, VarType type, Parameter *next);
void execute_function_call(const char *name, ArgumentList *args);
ASTNode *create_function_def_node(char *name, VarType return_type, Parameter *params, ASTNode *body);
void handle_return_statement(ASTNode *expr);
void *handle_binary_operation(ASTNode *node);
void free_parameters(Parameter *param);
void free_function_table(void);

Expand Down
2 changes: 1 addition & 1 deletion lang.y
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ void cleanup() {
}

TypeModifiers get_variable_modifiers(const char* name) {
TypeModifiers mods = {false, false, false, false}; // Default modifiers
TypeModifiers mods = {false, false, false, false, false}; // Default modifiers
Variable *var = get_variable(name);
if (var != NULL) {
return var->modifiers;
Expand Down
11 changes: 6 additions & 5 deletions lib/hm.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void dump(HashMap *hm)
if (hm->nodes[i])
{
Variable *v = (Variable *)hm->nodes[i]->value;
printf("key: %s, value: %s, is_array: %s\n", hm->nodes[i]->key, v->name, v->is_array ? "true" : "false");
printf("key: %p, value: %s, is_array: %s\n", hm->nodes[i]->key, v->name, v->is_array ? "true" : "false");
}
}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ void *hm_get(HashMap *hm, const void *key, size_t key_size)
* Resizes hashmap if load factor threshold would be exceeded.
* Uses linear probing to handle collisions.
*/
void hm_put(HashMap *hm, void *key, size_t key_size, void *value, size_t value_size)
void hm_put(HashMap *hm, const void *key, size_t key_size, void *value, size_t value_size)
{
if (hm->size >= hm->capacity * LOAD_FACTOR)
{
Expand Down Expand Up @@ -235,11 +235,12 @@ void hm_free(HashMap *hm)
{
SAFE_FREE(hm->nodes[i]->key);
Variable *var = hm->nodes[i]->value;
if (var != NULL){
if(var->is_array){
if (var != NULL)
{
if (var->is_array)
{
SAFE_FREE(var->value.array_data);
}

}

SAFE_FREE(hm->nodes[i]->value);
Expand Down
2 changes: 1 addition & 1 deletion lib/hm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ HashMap *hm_new();
void hm_resize(HashMap *hm);
void dump(HashMap *hm);
void *hm_get(HashMap *hm, const void *key, size_t key_size);
void hm_put(HashMap *hm, void *key, size_t key_size, void *value, size_t value_size);
void hm_put(HashMap *hm, const void *key, size_t key_size, void *value, size_t value_size);
void hm_free(HashMap *hm);

#endif
4 changes: 2 additions & 2 deletions lib/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define ALIGNMENT sizeof(void *)

// Magic number to detect buffer overruns and validate pointers
#define MEMORY_GUARD 0xDEADBEEFDEADBEEFULL
#define MEMORY_GUARD 0xDEADBEEFDEADBEEFULL

typedef struct
{
Expand All @@ -30,7 +30,7 @@ void *handle_malloc_error(size_t size);
size_t align_size(size_t size);
void *safe_malloc(size_t size);
void *safe_malloc_array(size_t nmemb, size_t size);
void safe_free(void **ptr, const char* file, int line, const char* func);
void safe_free(void **ptr, const char *file, int line, const char *func);
void *safe_memcpy(void *dest, const void *src, size_t n);
char *safe_strdup(const char *str);
int is_safe_malloc_ptr(const void *ptr);
Expand Down