94 lines
2.3 KiB
C
94 lines
2.3 KiB
C
#include <bits/types/FILE.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sysexits.h>
|
|
#include "../includes/list.h"
|
|
#include "../includes/parser.h"
|
|
#include "../includes/futil.h"
|
|
#include "../includes/scanner.h"
|
|
|
|
int main(int argc, char* args[]) {
|
|
if (argc == 1) {
|
|
printf("Usage: assm <file1.asm>\n");
|
|
return EX_USAGE;
|
|
}
|
|
|
|
char* source_code;
|
|
size_t bytes_read;
|
|
|
|
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
|
|
|
List* list = GenerateTokenList(source_code);
|
|
char mnemonic[12];
|
|
|
|
for(int i = 0; i < list->size; i++) {
|
|
Token* t = (Token*) list->content[i];
|
|
|
|
if (t->EndOfFile) {
|
|
printf("EOF\n");
|
|
break;
|
|
}
|
|
|
|
if (t->Class == PunctuationClass){
|
|
if(t->Value.Punctuation == NewLine) {
|
|
if(i - 1 >= 0 && (((Token*)list->content[i - 1])->Class == PunctuationClass && ((Token*)list->content[i - 1])->Value.Punctuation == NewLine)) continue;
|
|
|
|
printf("\n");
|
|
continue;
|
|
}
|
|
|
|
printf("%c ", t->Value.Punctuation);
|
|
continue;
|
|
}
|
|
|
|
if (t->Class == LabelClass) {
|
|
printf("[L]%s* ", t->Lemexe);
|
|
continue;
|
|
}
|
|
|
|
if (t->Class == IdentifierClass) {
|
|
printf("[I]%s ", t->Lemexe);
|
|
continue;
|
|
}
|
|
|
|
if (t->Class == RegisterClass) {
|
|
printf("[R]%d", t->Value.Register);
|
|
continue;
|
|
}
|
|
|
|
if (t->Class == NumberClass) {
|
|
printf("[N]%d ", t->Value.Number);
|
|
continue;
|
|
}
|
|
|
|
if (t->Class == MnemonicClass) {
|
|
GetMnemonicText(t->Value.Mnemonic, mnemonic);
|
|
printf("[M]%s ", mnemonic);
|
|
continue;
|
|
}
|
|
|
|
if (t->Class == DirectiveClass) {
|
|
printf("[D]%d ", t->Value.Directive);
|
|
|
|
}
|
|
|
|
if (t->Class == CharacterClass) {
|
|
printf("%s ", t->Lemexe);
|
|
}
|
|
}
|
|
|
|
//IRState* image = ParseTokens(list);
|
|
//Disassemble(image);
|
|
//for(int i = 0; i < image->Opcodes->size; i++) {
|
|
// printf("%d\n", ((IROpcode*) image->Opcodes->content[i])->Mnemonic);
|
|
//}
|
|
|
|
//FILE* program = fopen("program.bin", "w+b");
|
|
|
|
//fwrite(image, 1, image[2] + image[3], program);
|
|
|
|
//fclose(program);
|
|
|
|
free(source_code);
|
|
} |