From 2f2cfdc5410f644cb8779def15726f6e501fb693 Mon Sep 17 00:00:00 2001 From: Logikisto Date: Wed, 16 Sep 2026 22:21:15 +0200 Subject: [PATCH] Logique du parsing finie --- README.md | 6 ++- src/main.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++---- test.c | 5 +++ 3 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 test.c diff --git a/README.md b/README.md index 9cafca3..5c743d6 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -Refonte du projet en une commande C \ No newline at end of file +Refonte du projet en une commande C + +Détails : +- Développement solo +- Apprentissage du C via Deepseek V4 Pro (venice.ai) \ No newline at end of file diff --git a/src/main.c b/src/main.c index 0e9f819..5338f29 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,128 @@ +#include #include #include +#include + +// On définit chaque liste avec exactement le nombre d'arguments que ça peut contenir +char *CRUN_ARGS[2] = {NULL, NULL}; +char *CC_ARGS[2]; +char *BIN_ARGS[100]; + +// Capacités maximales des tableaux +#define MAX_ARGS 100 + +const char *list_O_args[] = {"-O0", "-O1", "-O2", "-O3", "-Os"}; +const char *list_cc_args[] = {"-Wall"}; + + +int is_crun_option(const char *arg) { + // Si on a l'option de traduction + if (strcmp(arg, "--translate") == 0) {return 1;} + + // Si on a l'option pour choisir le compilateur + // Pas encore disponible !! + //else if (strncmp(arg, "--cc=", 5) == 0) {return 1;} + + else {return 0;} +}; + +// Dans la signature de la fonction : +int is_cc_option(const char *arg, int *cc_o_arg) { + // D'abord vérifier si c'est un -Ox + for (int i = 0; i < 5; i++) { + if (strcmp(arg, list_O_args[i]) == 0) { + // C'est un -Ox + if (!(*cc_o_arg)) { + // Pas encore de -Ox, on l'accepte + *cc_o_arg = 1; + return 1; // Va dans CC_ARGS + } + // Déjà un -Ox, on ne fait rien + return 2; // Ignoré + } + } + + // Sinon vérifier les autres options compilateur + for (int i = 0; i < 1; i++) { + if (strcmp(arg, list_cc_args[i]) == 0) { + return 1; // Va dans CC_ARGS + } + } + + // Pas une option compilateur + return 0; // Va dans BIN_ARGS +} + +void print_list(char *liste[], const int taille) { + for (int i = 0; i < taille; i++) { + if (liste[i] != NULL) { + printf("[%d] %s\n", i, liste[i]); + } + } +}; int main(int argc, char *argv[]) { - printf("Nombre d'arguments : %d\n", argc); - for (int i = 0; i < argc; i++) { - if (strncmp(argv[i], "--cc=", 5) == 0) { - printf("arg --cc\n"); - } else { - printf("argv[%d] = \"%s\"\n", i, argv[i]); - }; + int crun_count = 0; + int cc_count = 0; + int bin_count = 0; - + // Vérifier si deux arguments du type "-Ox" ont été mis au lieu d'un seul + int cc_o_arg = false; + // Variable temporaire pour "is_cc_option" + bool temp_result = false; + + + if (argc < 2) { + printf("Erreur, aucun argument n'a été donné !!\n"); + return 1; + }; + + // Prendre la taille du premier argument + size_t taille_arg1 = strlen(argv[1]); + + // Couper le programme si le premier argument n'est pas un C + if (strcmp(argv[1] + (taille_arg1 - 2), ".c")) { + printf("Aucun fichier C n'a été donné !!\n"); + return 1; + }; + + // On itère dans tous les arguments + for (int i = 2; i < argc; i++) { + + // Si l'argument est de c-run, on le stocke comme tel + if (is_crun_option(argv[i])) { + CRUN_ARGS[crun_count++] = argv[i]; + + } else { + // Vérifier si c'est une option compilateur + // is_cc_option retourne : + // 1 = option compilateur valide → CC_ARGS + // 0 = pas une option compilateur → BIN_ARGS + // 2 = -Ox déjà présent → ignoré + int result = is_cc_option(argv[i], &cc_o_arg); + + if (result == 1) { + CC_ARGS[cc_count++] = argv[i]; + } else if (result == 0) { + BIN_ARGS[bin_count++] = argv[i]; + } + // Si result == 2, on ne fait rien + } } + + // Si à aucun moment on ne spécifie le niveau d'optimisation, on l'ajoute : + if (!(cc_o_arg)) { + CC_ARGS[cc_count] = "-O2"; + }; + + + // Tout afficher pour vérifier temporairement : + printf("Arguments pour c-run :\n"); + print_list(CRUN_ARGS, 2); + printf("Arguments pour le compilateur :\n"); + print_list(CC_ARGS, 2); + printf("Arguments pour le binaire :\n"); + print_list(BIN_ARGS, 10); + return 0; } \ No newline at end of file diff --git a/test.c b/test.c new file mode 100644 index 0000000..2d0757e --- /dev/null +++ b/test.c @@ -0,0 +1,5 @@ +#include + +int main() { + printf("Le test fonctionne !!\n"); +} \ No newline at end of file