From 666a39cc4d2db67c201e18ac451c1df50d7c981e Mon Sep 17 00:00:00 2001 From: wilyJ80 Date: Sun, 8 Dec 2024 09:18:41 -0300 Subject: [PATCH] feat: update main.c for manual testing feedback --- build.sh | 11 ++++--- debug.c | 73 ++++++++++++++++++++++++++++++++++++++++++ doc/examples/code.proc | 6 +--- main.c | 19 ++++++++++- parser/syntax_error.c | 7 +++- 5 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 debug.c diff --git a/build.sh b/build.sh index feca73c..9a47261 100755 --- a/build.sh +++ b/build.sh @@ -1,9 +1,12 @@ #!/bin/bash if [[ $1 == 'test' ]]; then - gcc -g ./test/*.c ./lexer/*.c ./parser/*.c -o croc - echo 'built test.' + gcc -g ./test/*.c ./lexer/*.c ./parser/*.c -o croc + echo 'built test.' +elif [[ $1 == 'debug' ]]; then + gcc -g ./debug.c ./lexer/*.c ./parser/*.c -o croc + echo 'built debug.' else - gcc -g ./main.c ./lexer/*.c ./parser/*.c -o croc - echo 'built main.' + gcc -g ./main.c ./lexer/*.c ./parser/*.c -o croc + echo 'built main.' fi diff --git a/debug.c b/debug.c new file mode 100644 index 0000000..18a9e40 --- /dev/null +++ b/debug.c @@ -0,0 +1,73 @@ +#include "./lexer/lexer.h" +#include "./lexer/printer.h" +#include "./lexer/types.h" +#include "./parser/parser.h" +#include "./parser/syntax_error.h" +#include +#include +#include + +// ANSI escape codes +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" + +int main(int argc, char *argv[]) { + /*if (argc != 2) {*/ + /* fprintf(stderr, "Error. Usage: croc \n");*/ + /* return EXIT_FAILURE;*/ + /*}*/ + + FILE *fd; + int lineCount = 1; + + fd = fopen("./doc/examples/code.proc", "r"); + if (fd == NULL) { + fprintf(stderr, "Error opening file\n"); + return EXIT_FAILURE; + } + + // lexing! + while (true) { + struct Token token = lexerGetNextChar(fd, &lineCount); + if (token.category == END_OF_FILE) { + fclose(fd); + break; + } + // handle malformed manually to + // keep the printing callback simple. + if (token.category == MALFORMED_TOKEN) { + fprintf(stderr, RED "ERROR: MALFORMED TOKEN %s ON LINE %d\n" RESET, token.lexeme, + lineCount); + exit(EXIT_FAILURE); + } + printToken(token); + printf("\n"); + } + + printf(GREEN "Lexing successful!" RESET "\n"); + + int *lc; + int line = 1; + lc = &line; + + fd = fopen("./doc/examples/code.proc", "r"); + if (fd == NULL) { + fprintf(stderr, "Error opening file\n"); + return EXIT_FAILURE; + } + + // for integration tests, prog itself needs a previously initialized parser + // with a token too + struct Token token = lexerGetNextChar(fd, lc); + struct Parser parser = { + .fd = fd, .lineCount = lc, .token = token}; + + enum SYNTAX_ERROR error = prog(&parser); + if (error != NO_ERROR) { + printSyntaxError(error, lc); + exit(EXIT_FAILURE); + } + + printf(GREEN "Parsing successful!" RESET "\n"); +} diff --git a/doc/examples/code.proc b/doc/examples/code.proc index 6dda013..24c5735 100644 --- a/doc/examples/code.proc +++ b/doc/examples/code.proc @@ -1,5 +1 @@ -var a = 80 // something -if (a == 80) // something else -for (int i = 0) 'a' '\n' banana '\0' -4.4 -int a[2] = {1, 2} +def diff --git a/main.c b/main.c index 6e9ef47..5ce1558 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,11 @@ #include #include +// ANSI escape codes +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" + int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Error. Usage: croc \n"); @@ -32,7 +37,7 @@ int main(int argc, char *argv[]) { // handle malformed manually to // keep the printing callback simple. if (token.category == MALFORMED_TOKEN) { - fprintf(stderr, "ERROR: MALFORMED TOKEN %s ON LINE %d\n", token.lexeme, + fprintf(stderr, RED "ERROR: MALFORMED TOKEN %s ON LINE %d\n" RESET, token.lexeme, lineCount); exit(EXIT_FAILURE); } @@ -40,10 +45,19 @@ int main(int argc, char *argv[]) { printf("\n"); } + printf(GREEN "Lexing successful!" RESET "\n"); + int *lc; int line = 1; lc = &line; + // opening the file again... + fd = fopen("./doc/examples/code.proc", "r"); + if (fd == NULL) { + fprintf(stderr, "Error opening file\n"); + return EXIT_FAILURE; + } + // for integration tests, prog itself needs a previously initialized parser // with a token too struct Token token = lexerGetNextChar(fd, lc); @@ -53,5 +67,8 @@ int main(int argc, char *argv[]) { enum SYNTAX_ERROR error = prog(&parser); if (error != NO_ERROR) { printSyntaxError(error, lc); + exit(EXIT_FAILURE); } + + printf(GREEN "Parsing successful!" RESET "\n"); } diff --git a/parser/syntax_error.c b/parser/syntax_error.c index 0f8413e..ed47194 100644 --- a/parser/syntax_error.c +++ b/parser/syntax_error.c @@ -4,6 +4,11 @@ #define ERROR_QTY 83 +// ANSI escape codes +#define RESET "\033[0m" +#define RED "\033[31m" +#define GREEN "\033[32m" + void printSyntaxError(enum SYNTAX_ERROR error, int *lineCount) { struct ErrorMessage messages[ERROR_QTY] = { {NO_ERROR, "No syntax errors"}, @@ -144,7 +149,7 @@ void printSyntaxError(enum SYNTAX_ERROR error, int *lineCount) { for (int i = 0; i < ERROR_QTY; i++) { if (error == messages[i].error) { - fprintf(stderr, "Syntax error: %s on line %d\n", messages[i].message, + fprintf(stderr, RED "Syntax error: %s on line %d\n" RESET, messages[i].message, *lineCount); } }