Added a function to remove an item from the generic list, which is mainly used for the tokens.
This commit is contained in:
@@ -16,6 +16,7 @@ typedef struct {
|
|||||||
|
|
||||||
List* CreateList(void);
|
List* CreateList(void);
|
||||||
int AddListItem(void *, List *);
|
int AddListItem(void *, List *);
|
||||||
|
void RemoveItem(int index, List * list);
|
||||||
void DestroyList(List*);
|
void DestroyList(List*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "../includes/list.h"
|
#include "../includes/list.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
List* CreateList() {
|
List* CreateList() {
|
||||||
List *new = malloc(sizeof(List));
|
List *new = malloc(sizeof(List));
|
||||||
@@ -44,6 +45,14 @@ int AddListItem(void *item, List* list) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RemoveItem(int index, List * list) {
|
||||||
|
memmove(&list->content[index], &list->content[index + 1], (list->size - index) * sizeof(void*));
|
||||||
|
|
||||||
|
list->size--;
|
||||||
|
|
||||||
|
list->content[list->size] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void DestroyList(List* list) {
|
void DestroyList(List* list) {
|
||||||
if (!list) return;
|
if (!list) return;
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -20,6 +20,9 @@ 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++) {
|
||||||
@@ -75,8 +78,5 @@ int main(int argc, char* args[]) {
|
|||||||
printf("%s ", t->Lemexe);
|
printf("%s ", t->Lemexe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ParseTokens(list);
|
|
||||||
|
|
||||||
free(source_code);
|
free(source_code);
|
||||||
}
|
}
|
||||||
+3
-165
@@ -27,12 +27,14 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
|
|
||||||
while(!ParserAtEnd()) {
|
while(!ParserAtEnd()) {
|
||||||
Token* t = PeekToken();
|
Token* t = PeekToken();
|
||||||
|
if (!t) break;
|
||||||
Symbol* tmp;
|
Symbol* tmp;
|
||||||
//printf("Tyoe: %d\n", t->Class);
|
|
||||||
|
|
||||||
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();
|
||||||
|
RemoveItem(CurrentToken, tokens);
|
||||||
|
if (CurrentToken > 0) CurrentToken--;
|
||||||
break;
|
break;
|
||||||
case MnemonicClass:
|
case MnemonicClass:
|
||||||
//HandleOperation();
|
//HandleOperation();
|
||||||
@@ -46,21 +48,10 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
AdvanceParser(); //Consume the label token
|
|
||||||
|
|
||||||
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
|
AdvanceParser(); //Consume the NewLine
|
||||||
|
|
||||||
//Instruction* ins = HandleOperation();
|
|
||||||
|
|
||||||
// if (!ins) {
|
|
||||||
// fprintf(stderr, "[Error] Line %d: Expected instruction following label '%s'.\n", t->LineNumber, t->Lemexe);
|
|
||||||
// exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
//symbol->Value.Instruction = ins;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -133,160 +124,7 @@ void HandleAssemblerDirective() {
|
|||||||
IgnoreParserLine();
|
IgnoreParserLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameter* GetParameterType() {
|
|
||||||
// Token* token = PeekToken();
|
|
||||||
// Symbol* symbol = NULL;
|
|
||||||
// int found = 0;
|
|
||||||
// //Parameter* param = CreateParameter(NoParameter);
|
|
||||||
|
|
||||||
// switch(token->Class) {
|
|
||||||
// // case RegisterClass:
|
|
||||||
// // param->ParameterType = RegisterParameter;
|
|
||||||
// // param->InterpretedAs = RegisterParameter;
|
|
||||||
|
|
||||||
// // param->Value.Register = token->Value.Register;
|
|
||||||
|
|
||||||
// // AdvanceParser();
|
|
||||||
|
|
||||||
// // return param;
|
|
||||||
// //case LabelClass:
|
|
||||||
// case IdentifierClass:
|
|
||||||
// found = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable, &symbol);
|
|
||||||
|
|
||||||
// if (!found) {
|
|
||||||
// AddSymbolToTable(token->Lemexe, HeapSize, 0, MachineState.SymbolsTable);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //param->Value.Symbol = symbol->Name;
|
|
||||||
|
|
||||||
// AdvanceParser();
|
|
||||||
|
|
||||||
// //return param;
|
|
||||||
// case PunctuationClass:
|
|
||||||
// if (token->Value.Punctuation != LBracket) {
|
|
||||||
// fprintf(stderr, "[Error] Line %d: Expected opening bracket\n", token->LineNumber);
|
|
||||||
// exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// AdvanceParser(); // [
|
|
||||||
|
|
||||||
// token = PeekToken();
|
|
||||||
|
|
||||||
// if (token->Class == NumberClass) {
|
|
||||||
// param->ParameterType = ConstantParameter;
|
|
||||||
// param->InterpretedAs = AddressParameter;
|
|
||||||
// param->Value.Number = token->Value.Number;
|
|
||||||
// }
|
|
||||||
// else if (token->Class == RegisterClass) {
|
|
||||||
// param->ParameterType = RegisterParameter;
|
|
||||||
// param->InterpretedAs = AddressParameter;
|
|
||||||
// param->Value.Register = token->Value.Register;
|
|
||||||
// }
|
|
||||||
// else if (token->Class & AddressClass) {
|
|
||||||
// param->ParameterType = AddressParameter;
|
|
||||||
// param->InterpretedAs = AddressParameter;
|
|
||||||
|
|
||||||
// symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
|
||||||
|
|
||||||
// if (!symbol) symbol = AddSymbolToTable(token->Lemexe, UnresolvedSymbol, MachineState.SymbolsTable);
|
|
||||||
|
|
||||||
// param->Value.Symbol = symbol->Name;
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// fprintf(stderr, "[Error] Line %d: Expected identifier, constant number or register.\n", token->LineNumber);
|
|
||||||
// IgnoreParserLine();
|
|
||||||
// //return NULL;
|
|
||||||
// exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// AdvanceParser(); //Consume the parameter it self.
|
|
||||||
|
|
||||||
// token = PeekToken();
|
|
||||||
|
|
||||||
// if (token->Class != PunctuationClass || token->Value.Punctuation != RBracket) {
|
|
||||||
// fprintf(stderr, "[Error] Line %d: Expected closing bracket.\n", token->LineNumber);
|
|
||||||
// exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// AdvanceParser(); // ]
|
|
||||||
|
|
||||||
// return param;
|
|
||||||
// case NumberClass:
|
|
||||||
// param->ParameterType = ConstantParameter;
|
|
||||||
// param->InterpretedAs = ConstantParameter;
|
|
||||||
|
|
||||||
// param->Value.Number = token->Value.Number;
|
|
||||||
|
|
||||||
// AdvanceParser();
|
|
||||||
|
|
||||||
// return param;
|
|
||||||
// default:
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return param;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Instruction* HandleOperation() {
|
|
||||||
// if (ParserAtEnd()) {
|
|
||||||
// //fprintf(stderr, "[Error] Expected instruction mnemonic\n");
|
|
||||||
// return NULL;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Token* token = PeekToken();
|
|
||||||
// //char mn[12];
|
|
||||||
|
|
||||||
// if (token->Class != MnemonicClass) {
|
|
||||||
// fprintf(stderr, "[Error] Line %d: Expected instruction mnemonic\n", token->LineNumber);
|
|
||||||
// IgnoreParserLine();
|
|
||||||
// return NULL;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Instruction* ins = CreateInstruction(token->Value.Mnemonic);
|
|
||||||
|
|
||||||
// //GetMnemonicText(ins->Mnemonic, mn);
|
|
||||||
|
|
||||||
// AdvanceParser();
|
|
||||||
|
|
||||||
// //No parameters here, we have a line break.
|
|
||||||
// if (PeekToken()->EndOfFile || (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == NewLine)) {
|
|
||||||
// goto InsertInstruction;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ins->ParameterOne = GetParameterType();
|
|
||||||
|
|
||||||
// if (ins->ParameterOne->ParameterType == NoParameter) {
|
|
||||||
// //This would be an error if the next token isn't a line break.
|
|
||||||
// if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != NewLine) {
|
|
||||||
// fprintf(stderr, "[Error] Line %d: Syntax error, expected line break but got %s.\n", token->LineNumber, token->Lemexe);
|
|
||||||
// exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// goto InsertInstruction;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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 got %d.\n", token->LineNumber, token->Class);
|
|
||||||
// exit(1);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// AdvanceParser(); //Consume the comma
|
|
||||||
|
|
||||||
// ins->ParameterTwo = GetParameterType();
|
|
||||||
|
|
||||||
// InsertInstruction:
|
|
||||||
|
|
||||||
// AddListItem(ins, MachineState.Instructions);
|
|
||||||
|
|
||||||
// AdvanceParser(); //Consume the line break
|
|
||||||
|
|
||||||
// return ins;
|
|
||||||
// }
|
|
||||||
|
|
||||||
void PrintSymbols(void) {
|
void PrintSymbols(void) {
|
||||||
//char mn[12];
|
//char mn[12];
|
||||||
|
|||||||
Reference in New Issue
Block a user