Add a error analyzer
This commit is contained in:
parent
dc905669a9
commit
35a485d1d7
4 changed files with 124 additions and 1 deletions
12
header/error_analyzer.h
Normal file
12
header/error_analyzer.h
Normal file
|
@ -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
|
95
src/error_analyzer.c
Normal file
95
src/error_analyzer.c
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
#include "../header/error_analyzer.h"
|
||||||
|
#include "../header/instruction_table.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -15,6 +15,8 @@ It also is responsible for Argument Parsing
|
||||||
#include "../header/image_saver.h"
|
#include "../header/image_saver.h"
|
||||||
#include "../header/lexer.h"
|
#include "../header/lexer.h"
|
||||||
#include "../header/target_code_generator.h"
|
#include "../header/target_code_generator.h"
|
||||||
|
#include "../header/error_analyzer.h"
|
||||||
|
#include "../header/instruction_table.h"
|
||||||
|
|
||||||
|
|
||||||
const char *argp_program_version =
|
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
|
// A variable for storing retrun values of functions that return errors on failure
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
|
shash_hashtable_t instruction_informations = create_instruction_information_hastable();
|
||||||
|
|
||||||
// set defaults
|
// set defaults
|
||||||
arguments.output_file = DEFAULT_OUT_FILE;
|
arguments.output_file = DEFAULT_OUT_FILE;
|
||||||
arguments.input_file = "";
|
arguments.input_file = "";
|
||||||
|
@ -142,6 +146,8 @@ int main(int argc, char **argv)
|
||||||
lexer(assembly_file, tokens);
|
lexer(assembly_file, tokens);
|
||||||
fclose(assembly_file);
|
fclose(assembly_file);
|
||||||
|
|
||||||
|
printf("Found %d errors\n", check_token_errors(tokens, instruction_informations));
|
||||||
|
|
||||||
if(target_code == NULL)
|
if(target_code == NULL)
|
||||||
{
|
{
|
||||||
printf("Error: Could not allocate memory for the target code\n");
|
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)
|
if (arguments.print_target_code)
|
||||||
print_target_code(target_code);
|
print_target_code(target_code);
|
||||||
|
|
||||||
|
destroy_instruction_information_hashtable(&instruction_informations);
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
8
test/errors.eipaasm
Normal file
8
test/errors.eipaasm
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
INP;
|
||||||
|
INP 30 ; helo
|
||||||
|
INC 43;
|
||||||
|
INC;
|
||||||
|
EOJJ
|
||||||
|
EOJ
|
||||||
|
STA 45 hmmm
|
||||||
|
RER;xs
|
Loading…
Reference in a new issue