From 63438b551b8ef751eb65c81cc36f4c2522c8dd04 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 29 Sep 2022 21:32:08 -0500 Subject: [PATCH] Got the scanner running again. Seems to be picking up punctuation, labels, identifiers and strings as expected. --- src/main.c | 66 ++++++++++++++++++++++++++++++++++++++++----------- src/opcodes.c | 23 +++++++++++++++++- 2 files changed, 74 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 093df12..31df72d 100644 --- a/src/main.c +++ b/src/main.c @@ -21,27 +21,65 @@ int main(int argc, char* args[]) { 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]; + for(int i = 0; i < list->size; i++) { + Token* t = (Token*) list->content[i]; - // if (t->type == LineEnd) { - // if (i - 1 >= 0 && ((Token*) list->content[i - 1])->type != LineEnd) - // printf("\n"); + 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; - // continue; - // } + printf("\n"); + continue; + } - // printf("[T: %i][C: %i] '%s' ", t->type, t->token_class, t->lexeme); - // if (t->type == LABEL) printf("* "); - // } + printf("%c ", t->Value.Punctuation); + continue; + } - IRState* image = ParseTokens(list); - //Disassemble(image); - for(int i = 0; i < image->Opcodes->size; i++) { - printf("%d\n", ((IROpcode*) image->Opcodes->content[i])->Mnemonic); + 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("%d ", t->Value.Number); + continue; + } + + if (t->Class == MnemonicClass) { + GetMnemonicText(t->Value.Mnemonic, mnemonic); + printf("%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); diff --git a/src/opcodes.c b/src/opcodes.c index 78541ee..d8d78cc 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -156,9 +156,30 @@ int IsRegister(const char* text, Registers* reg) { if (!text) return 0; int length = strlen(text); + Registers r = R8; if (length != 2) return 0; if (text[0] != 'r') return 0; - return text[1] >= '1' && text[1] <= '8'; + switch(text[1]) { + case '1': + r--; + case '2': + r--; + case '3': + r--; + case '4': + r--; + case '5': + r--; + case '6': + r--; + case '7': + r--; + case '8': + if (reg) *reg = r; + return 1; + default: + return 0; + } } \ No newline at end of file