Started rewrite
Added tokenizer (bugs!)
This commit is contained in:
parent
60c0e47668
commit
f4a0b7b2c3
1 changed files with 58 additions and 48 deletions
106
main.c
106
main.c
|
@ -3,9 +3,10 @@
|
|||
#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 INSTRUCTION_COMMENT 2
|
||||
|
||||
#define ALPHA 0
|
||||
#define BETA 1
|
||||
|
@ -16,70 +17,79 @@
|
|||
#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)
|
||||
// strcat(content,"\0"); //REMOVE
|
||||
if (part == INSTRUCTION_OPERATION)
|
||||
{
|
||||
printf("Operation: %s\n",content);
|
||||
printf("Operation: %s\n", content);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Adress: %s\n",content);
|
||||
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:
|
||||
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);
|
||||
}
|
||||
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],¤t_char,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
tokens[token_index+1][0]=';';
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
printf("-----------------\nEIPA Interpreter\n-----------------\n");
|
||||
printf("-----------------\nEIPA Interpreter\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH);
|
||||
|
||||
char assembly_path[]= "test.eipa";
|
||||
FILE *assembly_file = fopen(assembly_path,"r");
|
||||
if(assembly_file==NULL)
|
||||
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;
|
||||
}
|
||||
|
||||
int *instructions = malloc(MAX_MEMORY*sizeof(int8_t));
|
||||
char current_char = fgetc(assembly_file);
|
||||
char instruction_part_string[10];
|
||||
int instruction_part=0; // 0 = Operation | 1 = Adress | 2 = Comment REMOVE COMMENT
|
||||
while(current_char!=EOF)
|
||||
{
|
||||
//printf("%c",current_char);
|
||||
if(current_char=='\n')
|
||||
{
|
||||
saveinstructionpart(instruction_part,instruction_part_string);
|
||||
instruction_part = 0;
|
||||
strcpy(instruction_part_string,"");
|
||||
}
|
||||
else if(current_char==';')
|
||||
{
|
||||
saveinstructionpart(instruction_part,instruction_part_string);
|
||||
instruction_part=0;
|
||||
strcpy(instruction_part_string,"");
|
||||
|
||||
while(current_char!='\n' && current_char!=EOF)
|
||||
{
|
||||
current_char = fgetc(assembly_file);
|
||||
}
|
||||
}
|
||||
|
||||
else if(current_char==' ')
|
||||
{
|
||||
saveinstructionpart(instruction_part,instruction_part_string);
|
||||
instruction_part++;
|
||||
strcpy(instruction_part_string,"");
|
||||
}
|
||||
else
|
||||
{
|
||||
strncat(instruction_part_string,¤t_char,1);
|
||||
}
|
||||
|
||||
current_char = fgetc(assembly_file);
|
||||
}
|
||||
saveinstructionpart(instruction_part,instruction_part_string);
|
||||
char(*tokens)[MAX_TOKEN_SIZE] = malloc(sizeof(*tokens) * MAX_MEMORY);
|
||||
tokenize(assembly_file, tokens);
|
||||
fclose(assembly_file);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue