From 1a1744fe80624b68b18fdb44819729b86c8f12b7 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Sun, 11 Oct 2026 01:02:38 +0200 Subject: [PATCH] Modified operand type. --- src/main.c | 64 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/main.c b/src/main.c index 53511b9..dc78e74 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,8 @@ enum operations_t { typedef uint8_t operation_t; +typedef int operand_t; + int main(int argc, char *argv[]) { int status = EXIT_FAILURE; @@ -135,15 +137,15 @@ int main(int argc, char *argv[]) goto end; } - if(fwrite(&operation, sizeof(operation), 1, intermediate_representation_file) != 1) + if(fwrite(&operation, sizeof(operation_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; } - size_t nb = 2; + operand_t nb = 2; - if(fwrite(&nb, sizeof(nb), 1, intermediate_representation_file) != 1) + if(fwrite(&nb, sizeof(operand_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; @@ -153,15 +155,15 @@ int main(int argc, char *argv[]) { operation++; - size_t nb; + operand_t nb; - if(fseek(intermediate_representation_file, -sizeof(nb), SEEK_CUR)) + if(fseek(intermediate_representation_file, -sizeof(operand_t), SEEK_CUR)) { fprintf(stderr, "Error seeking file: %d\n", errno); goto end; } - if(fread(&nb, sizeof(nb), 1, intermediate_representation_file) != 1) + if(fread(&nb, sizeof(operand_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error reading file: %d\n", errno); goto end; @@ -169,13 +171,13 @@ int main(int argc, char *argv[]) nb++; - if(fseek(intermediate_representation_file, -sizeof(nb), SEEK_CUR)) + if(fseek(intermediate_representation_file, -sizeof(operand_t), SEEK_CUR)) { fprintf(stderr, "Error seeking file: %d\n", errno); goto end; } - if(fwrite(&nb, sizeof(nb), 1, intermediate_representation_file) != 1) + if(fwrite(&nb, sizeof(operand_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; @@ -183,7 +185,7 @@ int main(int argc, char *argv[]) } else { - if(fwrite(&operation, sizeof(operation), 1, intermediate_representation_file) != 1) + if(fwrite(&operation, sizeof(operation_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; @@ -200,9 +202,9 @@ int main(int argc, char *argv[]) goto end; } - size_t jump_pos = 0; + operand_t jump_pos = 0; - if(fwrite(&jump_pos, sizeof(jump_pos), 1, intermediate_representation_file) != 1) + if(fwrite(&jump_pos, sizeof(operand_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; @@ -244,7 +246,7 @@ int main(int argc, char *argv[]) while(true) { operation_t operation; - size_t nb_read = fread(&operation, sizeof(operation), 1, intermediate_representation_file); + size_t nb_read = fread(&operation, sizeof(operation_t), 1, intermediate_representation_file); if(nb_read != 1) { @@ -273,7 +275,7 @@ int main(int argc, char *argv[]) for(size_t bracket_index = 1; bracket_index;) { operation_t operation_search; - nb_read = fread(&operation_search, sizeof(operation_search), 1, intermediate_representation_file); + nb_read = fread(&operation_search, sizeof(operation_t), 1, intermediate_representation_file); if(nb_read != 1) { @@ -296,7 +298,7 @@ int main(int argc, char *argv[]) case MEM_ADD_NB: case MEM_SUB_NB: case JUMP_IF_NOT_0: - if(fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR)) + if(fseek(intermediate_representation_file, sizeof(operand_t), SEEK_CUR)) { fprintf(stderr, "Error seeking file: %d\n", errno); goto end; @@ -325,15 +327,15 @@ int main(int argc, char *argv[]) goto end; } - if(fseek(intermediate_representation_file, -sizeof(size_t), SEEK_CUR)) + if(fseek(intermediate_representation_file, -sizeof(operand_t), SEEK_CUR)) { fprintf(stderr, "Error seeking file: %d\n", errno); goto end; } - size_t bracket_pos_size_t = (size_t)bracket_pos + sizeof(size_t); + operand_t bracket_pos_operand = (operand_t)bracket_pos + sizeof(operand_t); - if(fwrite(&bracket_pos_size_t, sizeof(bracket_pos_size_t), 1, intermediate_representation_file) != 1) + if(fwrite(&bracket_pos_operand, sizeof(operand_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; @@ -345,8 +347,8 @@ int main(int argc, char *argv[]) goto end; } - size_t corresponding_bracket_pos_size_t = corresponding_bracket_pos; - if(fwrite(&corresponding_bracket_pos_size_t, sizeof(corresponding_bracket_pos_size_t), 1, intermediate_representation_file) != 1) + operand_t corresponding_bracket_pos_operand = (operand_t)corresponding_bracket_pos; + if(fwrite(&corresponding_bracket_pos_operand, sizeof(operand_t), 1, intermediate_representation_file) != 1) { fprintf(stderr, "Error writing file: %d\n", errno); goto end; @@ -360,7 +362,7 @@ int main(int argc, char *argv[]) case MEM_ADD_NB: case MEM_SUB_NB: case JUMP_IF_NOT_0: - fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR); + fseek(intermediate_representation_file, sizeof(operand_t), SEEK_CUR); break; default: @@ -374,13 +376,13 @@ int main(int argc, char *argv[]) uint8_t mem[DATA_SIZE_MAX] = {0}; size_t mem_pointer = 0; - size_t nb_read; - size_t operand; + uint8_t nb_read; + operand_t operand; while(true) { operation_t operation; - nb_read = fread(&operation, sizeof(operation), 1, intermediate_representation_file); + nb_read = fread(&operation, sizeof(operation_t), 1, intermediate_representation_file); if(nb_read != 1) { @@ -402,7 +404,7 @@ int main(int argc, char *argv[]) break; case MEM_POINTER_ADD_NB: - nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file); + nb_read = fread(&operand, sizeof(operand_t), 1, intermediate_representation_file); if(nb_read != 1) { fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file)); @@ -416,7 +418,7 @@ int main(int argc, char *argv[]) break; case MEM_POINTER_SUB_NB: - nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file); + nb_read = fread(&operand, sizeof(operand_t), 1, intermediate_representation_file); if(nb_read != 1) { fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file)); @@ -430,7 +432,7 @@ int main(int argc, char *argv[]) break; case MEM_ADD_NB: - nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file); + nb_read = fread(&operand, sizeof(operand_t), 1, intermediate_representation_file); if(nb_read != 1) { fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file)); @@ -444,7 +446,7 @@ int main(int argc, char *argv[]) break; case MEM_SUB_NB: - nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file); + nb_read = fread(&operand, sizeof(operand_t), 1, intermediate_representation_file); if(nb_read != 1) { fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file)); @@ -456,7 +458,7 @@ int main(int argc, char *argv[]) case JUMP_IF_0: if(mem[mem_pointer] == 0) { - nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file); + nb_read = fread(&operand, sizeof(operand_t), 1, intermediate_representation_file); if(nb_read != 1) { fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file)); @@ -471,7 +473,7 @@ int main(int argc, char *argv[]) } else { - if(fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR)) + if(fseek(intermediate_representation_file, sizeof(operand_t), SEEK_CUR)) { fprintf(stderr, "Error seeking file: %d\n", errno); goto end; @@ -482,7 +484,7 @@ int main(int argc, char *argv[]) case JUMP_IF_NOT_0: if(mem[mem_pointer]) { - nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file); + nb_read = fread(&operand, sizeof(operand_t), 1, intermediate_representation_file); if(nb_read != 1) { fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file)); @@ -497,7 +499,7 @@ int main(int argc, char *argv[]) } else { - if(fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR)) + if(fseek(intermediate_representation_file, sizeof(operand_t), SEEK_CUR)) { fprintf(stderr, "Error seeking file: %d\n", errno); goto end;