Fix issue with spaces between

the Adress of the Instruction and
the semicolon or newline
e.g. 'INC 23     ; comment'
This commit is contained in:
XOR 2022-12-31 18:41:55 +01:00
parent 95dcc08346
commit 1f40bea437

30
main.c
View file

@ -39,36 +39,44 @@ void tokenize(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
{
char current_char = 0;
unsigned int token_index = 0;
while ((current_char = fgetc(input_file)) != EOF)
{
switch (current_char)
{
case ASCII_TAB:
case ASCII_SPACE:
while(*input_file->_IO_read_ptr==ASCII_SPACE||*input_file->_IO_read_ptr==ASCII_TAB) //The character which fgetc will read the next time when its called
// Loop to the characters until the next character fgetc() would read is not space or tab
while (*input_file->_IO_read_ptr == ASCII_SPACE || *input_file->_IO_read_ptr == ASCII_TAB) // The character which fgetc will read the next time when its called
{
current_char=fgetc(input_file);
current_char = fgetc(input_file);
}
//Between the Adress and the newline in an Instruction is usually no space.
//Therefore the token_index gets increased, whenever a newline is found.
//However, there can also be a space between the Adress and the newline.
//To not increase the token_index 2 times, we need to not increase it here if the next character is a \n
if(*input_file->_IO_read_ptr != ';' && *input_file->_IO_read_ptr != ASCII_NEWLINE)
{
token_index++;
}
token_index++;
break;
case ASCII_NEWLINE:
token_index++;
tokens[token_index][0]=';';
tokens[token_index][0] = ';';
token_index++;
break;
case ';':
while(*input_file->_IO_read_ptr!=ASCII_NEWLINE) //The character which fgetc will read the next time when its called
while (*input_file->_IO_read_ptr != ASCII_NEWLINE) // The character which fgetc will read the next time when its called
{
current_char=fgetc(input_file);
current_char = fgetc(input_file);
}
break;
default:
strncat(tokens[token_index],&current_char,1);
strncat(tokens[token_index], &current_char, 1);
break;
}
}
tokens[token_index+1][0]=';';
tokens[token_index + 1][0] = ';';
}
int main(int argc, char const *argv[])
@ -90,6 +98,10 @@ int main(int argc, char const *argv[])
char(*tokens)[MAX_TOKEN_SIZE] = malloc(sizeof(*tokens) * MAX_MEMORY);
tokenize(assembly_file, tokens);
for (int i = 0; i<30;i++)
{
printf("%s\n",tokens[i]);
}
fclose(assembly_file);
return 0;
}