From bca2ad67f18a037b289b17356b5afbc2832b4b2d Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sat, 8 Oct 2022 22:02:51 -0500 Subject: [PATCH] Fixed a bug with single parameter instructions that land at the end of a file expecting a comma. --- src/parser.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/parser.c b/src/parser.c index 3538538..a83e615 100644 --- a/src/parser.c +++ b/src/parser.c @@ -37,7 +37,7 @@ IRState* ParseTokens(List* tokens) { Symbol* symbol = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable); if (symbol && symbol->Type != UnresolvedSymbol) { - fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe); + fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s' (type: %d)\n", t->LineNumber, t->Lemexe, symbol->Type); exit(1); } @@ -51,7 +51,7 @@ IRState* ParseTokens(List* tokens) { Instruction* ins = HandleOperation(); if (!ins) { - fprintf(stderr, "[Error] Line %d: Expected instruction following label %s\n", t->LineNumber, t->Lemexe); + fprintf(stderr, "[Error] Line %d: Expected instruction following label '%s'.\n", t->LineNumber, t->Lemexe); exit(1); } @@ -111,14 +111,12 @@ void HandleAssemblerDirective() { if (token->Class != NumberClass) { fprintf(stderr, "[Error] Line %d: Expected terminating byte.\n", token->LineNumber); - IgnoreParserLine(); - return; + exit(1); } if (token->Value.Number > 255) { - fprintf(stderr, "[Error] Line %d: Number must fit within a byte.\n", token->LineNumber); - IgnoreParserLine(); - return; + fprintf(stderr, "[Error] Line %d: The terminating byte must be smaller than 256, got %d.\n", token->LineNumber, token->Value.Number); + exit(1); } s->Value.String = CreateSymbolString(string, 1); @@ -242,8 +240,13 @@ Parameter* GetParameterType() { } Instruction* HandleOperation() { + if (ParserAtEnd()) { + //fprintf(stderr, "[Error] Expected instruction mnemonic\n"); + return NULL; + } + Token* token = PeekToken(); - char mn[12]; + //char mn[12]; if (token->Class != MnemonicClass) { fprintf(stderr, "[Error] Line %d: Expected instruction mnemonic\n", token->LineNumber); @@ -253,7 +256,7 @@ Instruction* HandleOperation() { Instruction* ins = CreateInstruction(token->Value.Mnemonic); - GetMnemonicText(ins->Mnemonic, mn); + //GetMnemonicText(ins->Mnemonic, mn); AdvanceParser(); @@ -274,13 +277,13 @@ Instruction* HandleOperation() { goto InsertInstruction; } - if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == NewLine) { + if (PeekToken()->EndOfFile || (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == NewLine)) { goto InsertInstruction; } if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != Comma) { //Syntax error - fprintf(stderr, "[Error] Line %d: Expected comma.\n", token->LineNumber); + fprintf(stderr, "[Error] Line %d: Expected comma got %d.\n", token->LineNumber, token->Class); exit(1); }