From 35a485d1d7e8704252c97332ecd700e89e77a0df Mon Sep 17 00:00:00 2001 From: XOR Date: Mon, 1 May 2023 21:19:52 +0200 Subject: [PATCH] Add a error analyzer --- header/error_analyzer.h | 12 ++++++ src/error_analyzer.c | 95 +++++++++++++++++++++++++++++++++++++++++ src/main.c | 10 ++++- test/errors.eipaasm | 8 ++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 header/error_analyzer.h create mode 100644 src/error_analyzer.c create mode 100644 test/errors.eipaasm diff --git a/header/error_analyzer.h b/header/error_analyzer.h new file mode 100644 index 0000000..328769d --- /dev/null +++ b/header/error_analyzer.h @@ -0,0 +1,12 @@ +#ifndef _ERROR_ANALYZER_H_ +#define _ERROR_ANALYZER_H_ + +#include "../header/common.h" +#include "../header/instruction_table.h" +#include "../header/shash/shash.h" + +/* Check if there exist errors in the Assembly using the tokens array + Returns amount of errors found, and prints out the error's to stdout */ +unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table); + +#endif diff --git a/src/error_analyzer.c b/src/error_analyzer.c new file mode 100644 index 0000000..a3ac3df --- /dev/null +++ b/src/error_analyzer.c @@ -0,0 +1,95 @@ +#include "../header/error_analyzer.h" +#include "../header/instruction_table.h" +#include +#include +#include +#include + +unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table) +{ + printf("=====================\nErrors found:\n"); + unsigned int error_count = 0; + + for (int i = 0; tokens[i][0] != EOF; i++) + { + instruction_information_t *current_token_info = shash_get(tokens[i], (unsigned int)strlen(tokens[i]), &instruction_information_table); + if (current_token_info == NULL) + { + // Couldn't find the instruction + printf("ERROR: Insruction %s does not exist\n", tokens[i]); + error_count++; + + // Loop to the next ; + for (; tokens[i][0] != ';'; i++) + { + if (tokens[i][0] == EOF) + break; + } + continue; + } + + if (current_token_info->requires_adress == 1) + { + switch (current_token_info->supports_adress_label) + { + case 1: + /* Labels are not implemented yet + When they are, we need to check wether we got a label, + And if yes, we need to make sure the label exists */ + case 0: + { + // Check the adress stored at the next token + i++; + if(tokens[i][0] == ';') + { + printf("ERROR: expected adress at token %d\n", i); + error_count++; + break; + } + + + int adress = atoi(tokens[i]); + if (adress > MAX_MEMORY || adress < 1) + { + printf("ERROR: Adress %s at token %d is invalid\n", tokens[i], i); + error_count++; + } + + // Check that the next token is a ; + i++; + if (tokens[i][0] != ';') + { + printf("ERROR: Expected end of instruction at %d but found \"%s\"\n", i, tokens[i]); + error_count++; + + // Loop to the next ; + for (; tokens[i][0] != ';'; i++) + { + if (tokens[i][0] == EOF) + break; + } + } + break; + } + } + } + else + { + // Check that the next token is a ; + i++; + if (tokens[i][0] != ';') + { + printf("ERROR: Expected end of instruction at %d but found \"%s\"\n", i, tokens[i]); + error_count++; + + // Loop to the next ; + for (; tokens[i][0] != ';'; i++) + { + if (tokens[i][0] == EOF) + break; + } + } + } + } + return error_count; +} diff --git a/src/main.c b/src/main.c index 763a0f5..9d7098d 100755 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,8 @@ It also is responsible for Argument Parsing #include "../header/image_saver.h" #include "../header/lexer.h" #include "../header/target_code_generator.h" +#include "../header/error_analyzer.h" +#include "../header/instruction_table.h" const char *argp_program_version = @@ -117,6 +119,8 @@ int main(int argc, char **argv) // A variable for storing retrun values of functions that return errors on failure int error; + shash_hashtable_t instruction_informations = create_instruction_information_hastable(); + // set defaults arguments.output_file = DEFAULT_OUT_FILE; arguments.input_file = ""; @@ -141,7 +145,9 @@ int main(int argc, char **argv) lexer(assembly_file, tokens); fclose(assembly_file); - + + printf("Found %d errors\n", check_token_errors(tokens, instruction_informations)); + if(target_code == NULL) { printf("Error: Could not allocate memory for the target code\n"); @@ -167,5 +173,7 @@ int main(int argc, char **argv) if (arguments.print_target_code) print_target_code(target_code); + destroy_instruction_information_hashtable(&instruction_informations); + return EXIT_SUCCESS; } diff --git a/test/errors.eipaasm b/test/errors.eipaasm new file mode 100644 index 0000000..d56fc43 --- /dev/null +++ b/test/errors.eipaasm @@ -0,0 +1,8 @@ +INP; +INP 30 ; helo +INC 43; +INC; +EOJJ +EOJ +STA 45 hmmm +RER;xs \ No newline at end of file