Added grammar checking for DB declarations.
This commit is contained in:
+6
-6
@@ -20,9 +20,6 @@ int main(int argc, char* args[]) {
|
|||||||
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
||||||
|
|
||||||
List* list = GenerateTokenList(source_code);
|
List* list = GenerateTokenList(source_code);
|
||||||
|
|
||||||
ParseTokens(list);
|
|
||||||
|
|
||||||
char mnemonic[12];
|
char mnemonic[12];
|
||||||
|
|
||||||
for(int i = 0; i < list->size; i++) {
|
for(int i = 0; i < list->size; i++) {
|
||||||
@@ -39,7 +36,7 @@ int main(int argc, char* args[]) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf(" %c ", t->Value.Punctuation);
|
printf("[P]%c", t->Value.Punctuation);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +72,11 @@ int main(int argc, char* args[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (t->Class == CharacterClass) {
|
if (t->Class == CharacterClass) {
|
||||||
printf("%s ", t->Lemexe);
|
printf("[C]'%s'", t->Lemexe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ParseTokens(list);
|
||||||
|
|
||||||
|
|
||||||
free(source_code);
|
free(source_code);
|
||||||
}
|
}
|
||||||
+39
-15
@@ -6,7 +6,7 @@ List* TokensList;
|
|||||||
int CurrentToken = 0;
|
int CurrentToken = 0;
|
||||||
|
|
||||||
void PrintSymbols(void);
|
void PrintSymbols(void);
|
||||||
Instruction* HandleOperation(void);
|
void HandleOperation(void);
|
||||||
void RemoveCurrentToken(void);
|
void RemoveCurrentToken(void);
|
||||||
void HandleAssemblerDirective(void);
|
void HandleAssemblerDirective(void);
|
||||||
void HandleAssemblerDirective(void);
|
void HandleAssemblerDirective(void);
|
||||||
@@ -35,10 +35,9 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
switch(t->Class) {
|
switch(t->Class) {
|
||||||
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
||||||
HandleAssemblerDirective();
|
HandleAssemblerDirective();
|
||||||
RemoveCurrentToken(); // clear new line
|
|
||||||
break;
|
break;
|
||||||
case MnemonicClass:
|
case MnemonicClass:
|
||||||
//HandleOperation();
|
HandleOperation();
|
||||||
break;
|
break;
|
||||||
case LabelClass:
|
case LabelClass:
|
||||||
{
|
{
|
||||||
@@ -51,8 +50,6 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
|
|
||||||
if (!found) AddSymbolToTable(t->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
if (!found) AddSymbolToTable(t->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||||
else tmp->Resolved = 1;
|
else tmp->Resolved = 1;
|
||||||
|
|
||||||
AdvanceParser(); //Consume the NewLine
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -89,23 +86,48 @@ void HandleAssemblerDirective() {
|
|||||||
if (found) {
|
if (found) {
|
||||||
symbol->Address = HeapSize;
|
symbol->Address = HeapSize;
|
||||||
symbol->Resolved = 1;
|
symbol->Resolved = 1;
|
||||||
|
|
||||||
HeapSize += symbol->Length;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
symbol = AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||||
|
}
|
||||||
|
|
||||||
RemoveCurrentToken();
|
RemoveCurrentToken(); //Remove the Idenifier name token.
|
||||||
|
|
||||||
identifier = PeekToken();
|
Token* value = PeekToken();
|
||||||
|
|
||||||
if (identifier->Class == CharacterClass) {
|
if (value->Class == CharacterClass) {
|
||||||
HeapSize += strlen(identifier->Lemexe);
|
symbol->Length = strlen(value->Lemexe);
|
||||||
|
|
||||||
|
RemoveCurrentToken(); //Remove the string declared by this DB command.
|
||||||
|
|
||||||
|
if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == Comma) {
|
||||||
|
if (PeekToken()->Value.Punctuation != Comma) {
|
||||||
|
fprintf(stderr, "Syntax error line %d: Expected COMMA got %c.", PeekToken()->LineNumber, PeekToken()->Value.Punctuation);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
RemoveCurrentToken(); //Remove COMMA
|
||||||
|
|
||||||
|
if (PeekToken()->Class != NumberClass) {
|
||||||
|
fprintf(stderr, "Syntax error line %d: Expected terminating byte.\n", PeekToken()->LineNumber);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
RemoveCurrentToken(); //Remove terminating byte
|
||||||
|
|
||||||
|
symbol->Length++;
|
||||||
}
|
}
|
||||||
else {
|
else if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != NewLine) {
|
||||||
HeapSize += 2;
|
fprintf(stderr, "Syntax error line %d: Expected COMMA got %c.", PeekToken()->LineNumber, PeekToken()->Value.Punctuation);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (PeekToken()->Class == NumberClass) {
|
||||||
|
symbol->Length = 2;
|
||||||
|
|
||||||
|
RemoveCurrentToken(); //Clear the number token.
|
||||||
|
}
|
||||||
|
|
||||||
|
HeapSize += symbol->Length;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -114,7 +136,9 @@ void HandleAssemblerDirective() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HandleOperation(void) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void PrintSymbols(void) {
|
void PrintSymbols(void) {
|
||||||
//char mn[12];
|
//char mn[12];
|
||||||
@@ -122,7 +146,7 @@ void PrintSymbols(void) {
|
|||||||
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
||||||
Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
|
Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
|
||||||
|
|
||||||
printf("[%s] %s [%d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address);
|
printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length);
|
||||||
|
|
||||||
// switch(symbol->Type) {
|
// switch(symbol->Type) {
|
||||||
// case InstructionPointerSymbol:
|
// case InstructionPointerSymbol:
|
||||||
|
|||||||
Reference in New Issue
Block a user