Assembler/main.c
XOR 1f40bea437 Fix issue with spaces between
the Adress of the Instruction and
the semicolon or newline
e.g. 'INC 23     ; comment'
2022-12-31 18:41:55 +01:00

107 lines
No EOL
3 KiB
C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_MEMORY 1000000
#define MAX_TOKEN_SIZE 10
#define INSTRUCTION_OPERATION 0
#define INSTRUCTION_ADRESS 1
// #define INSTRUCTION_COMMENT 2
#define ALPHA 0
#define BETA 1
#define STABLE 2
#define VER_MAJOR 0
#define VER_MINOR 1
#define VER_PATCH 0
#define TAG ALPHA
#define ASCII_TAB 9
#define ASCII_SPACE 32
#define ASCII_NEWLINE 10
int saveinstructionpart(unsigned short int part, char content[])
{
// strcat(content,"\0"); //REMOVE
if (part == INSTRUCTION_OPERATION)
{
printf("Operation: %s\n", content);
}
else
{
printf("Adress: %s\n", content);
}
return 0;
}
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:
// 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);
}
//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++;
}
break;
case ASCII_NEWLINE:
token_index++;
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
{
current_char = fgetc(input_file);
}
break;
default:
strncat(tokens[token_index], &current_char, 1);
break;
}
}
tokens[token_index + 1][0] = ';';
}
int main(int argc, char const *argv[])
{
printf("-----------------\nEIPA Interpreter\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH);
char assembly_path[] = "test.eipa";
char image_path[] = "a.eipaimg";
FILE *output_file = fopen(image_path, "w");
FILE *assembly_file = fopen(assembly_path, "r");
if (assembly_file == NULL)
{
return 0;
}
if (output_file == NULL)
{
return 0;
}
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;
}