Add label support to error detection

This commit is contained in:
XOR 2023-09-05 00:42:36 +02:00
parent 7ea187efc6
commit 0d2d613bc8
5 changed files with 59 additions and 6 deletions

View file

@ -24,3 +24,13 @@ Instruction Layout:
Y: Memory Adress (28bit)
Labels will get resolved by a preprocessor and transformed to adresses on assembling time
A label can be defined at any position in a instruction (except in the comment) by writing $label_name.
So you could e.g. write
$label OUT 35
but also
OUT $label 35
Note that the first variant is recommendet for readability

View file

@ -7,6 +7,6 @@
/* 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);
unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table, shash_hashtable_t label_table);
#endif

View file

@ -5,7 +5,7 @@
#include <string.h>
#include <sys/types.h>
unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table)
unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t instruction_information_table, shash_hashtable_t label_table)
{
printf("=====================\nErrors found:\n");
unsigned int error_count = 0;
@ -33,9 +33,41 @@ unsigned int check_token_errors(char tokens[][MAX_TOKEN_SIZE], shash_hashtable_t
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 */
{
printf("Got instruction '%s', which supports labels\n", tokens[i]);
// Check label at next token
i++;
if(atoi(tokens[i]) == 0)
{
unsigned int *label_addr = shash_get(tokens[i], strlen(tokens[i]), &label_table);
if(label_addr != NULL)
{
printf("Label %s found. It corrosponds to adress %d\n", tokens[i], *label_addr);
}
else
{
printf("Label %s does not exist\n", tokens[i]);
}
// 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;
}
}
case 0:
{
// Check the adress stored at the next token

View file

@ -1,15 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../header/labels.h"
void build_label_table(char label_tokens[][MAX_TOKEN_SIZE], shash_hashtable_t *label_table)
{
unsigned int token_index = 0;
unsigned int instruction_index = 0;
while (label_tokens[token_index][0] != EOF)
{
if(label_tokens[token_index][0] == '$')
{
printf("Found label: %s\n", label_tokens[token_index]+1);
//Store the adress we are at on the heap, as the hashtable only stores pointers to data
unsigned int *heap_pointer_to_adress = malloc(sizeof(instruction_index));
*heap_pointer_to_adress = instruction_index;
shash_set(label_tokens[token_index]+1, strlen(label_tokens[token_index]+1), heap_pointer_to_adress, label_table);
}
if(label_tokens[token_index][0] == ';')
{
instruction_index++;
}
token_index++;
}

View file

@ -156,7 +156,7 @@ int main(int argc, char **argv)
// Check if the EIPA Assembly contains errors with the no_label_definition_tokens array
shash_hashtable_t instruction_informations = create_instruction_information_hastable();
printf("Found %d errors\n", check_token_errors(no_label_definition_tokens, instruction_informations));
printf("Found %d errors\n", check_token_errors(no_label_definition_tokens, instruction_informations, label_table));
exit(0);
// Part II of processing labels