Fixed a bug with the parse discarding identifier tokens when parsing a COPY instruction. Added the first batch of code to encode the final token array to a binary form.
This commit is contained in:
+160
-11
@@ -11,7 +11,9 @@
|
||||
List* LIST;
|
||||
SymbolTable* Symbols;
|
||||
|
||||
unsigned char mem[128] = {0};
|
||||
void print(void);
|
||||
void assemble(void);
|
||||
|
||||
int main(int argc, char* args[]) {
|
||||
if (argc == 1) {
|
||||
@@ -33,18 +35,17 @@ int main(int argc, char* args[]) {
|
||||
Symbols = state->SymbolsTable;
|
||||
|
||||
free(source_code);
|
||||
|
||||
assemble();
|
||||
|
||||
printf("\nBytes:\n");
|
||||
for(int i = 0; i < sizeof(mem); i++) {
|
||||
if (i != 0 && i % 8 == 0) printf("\n");
|
||||
printf("%02X ", mem[i] & 0xFF);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// void assemble() {
|
||||
// for(int i = 0; i < LIST->size; i++) {
|
||||
// Token* current = LIST->content[i];
|
||||
|
||||
// if (current->Class != MnemonicClass) continue;
|
||||
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
void print(void) {
|
||||
char mnemonic[12];
|
||||
printf("printing tokens...\n");
|
||||
@@ -103,4 +104,152 @@ void print(void) {
|
||||
printf("[C]'%s'", t->Lemexe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int CurrentIndex = 0;
|
||||
int PC2 = 0;
|
||||
|
||||
void WriteByte(unsigned char byte) {
|
||||
mem[PC2] = byte;
|
||||
PC2++;
|
||||
}
|
||||
|
||||
void WriteWord(unsigned short word) {
|
||||
mem[PC2] = word;
|
||||
}
|
||||
|
||||
unsigned short GetValueFromToken(Token* token) {
|
||||
if (token->Class & IdentifierClass) {
|
||||
Symbol* symbol;
|
||||
|
||||
if (TryGetSymbol(token->Lemexe, Symbols, &symbol)) {
|
||||
printf("Returning %d for address of %s.\n", symbol->Address, symbol->Name);
|
||||
return symbol->Address;
|
||||
}
|
||||
else {
|
||||
fprintf(stderr, "Failed to find symbol %s while assembling.\n", token->Lemexe);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if (token->Class == NumberClass) return token->Value.Number;
|
||||
|
||||
fprintf(stderr, "Failed to find token '%d' while assmbling.\n", token->Class);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int IsAtEnd(void){
|
||||
return CurrentIndex >= LIST->size;
|
||||
}
|
||||
|
||||
void assemble() {
|
||||
while(!IsAtEnd()) {
|
||||
Token* current = LIST->content[CurrentIndex];
|
||||
switch(current->Value.Mnemonic) {
|
||||
case COPYA:
|
||||
case COPYAB:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteWord(GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 2;
|
||||
break;
|
||||
case COPYRA:
|
||||
case COPYRAB:
|
||||
case COPYRARA:
|
||||
case COPYRARAB:
|
||||
case COPY:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteByte(((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
break;
|
||||
case COPYI: //TODO: should explicitly get number from value I feel
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteWord(GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 2;
|
||||
break;
|
||||
case COPYB:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteWord((unsigned char)GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 1;
|
||||
break;
|
||||
case CMP:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteByte(((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
break;
|
||||
case CMPI:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteWord(GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 2;
|
||||
break;
|
||||
case ADD:
|
||||
case SUB:
|
||||
case AND:
|
||||
case OR:
|
||||
case XOR:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteByte(((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
break;
|
||||
case SHL:
|
||||
case SHR:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
CurrentIndex++;
|
||||
WriteWord(GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 2;
|
||||
break;
|
||||
case INC:
|
||||
case DEC:
|
||||
case PUSH:
|
||||
case POP:
|
||||
case JMPI:
|
||||
CurrentIndex++;
|
||||
|
||||
WriteByte(current->Value.Mnemonic | ((Token*) LIST->content[CurrentIndex])->Value.Register);
|
||||
break;
|
||||
case JMP:
|
||||
case JZ:
|
||||
case JG:
|
||||
case JL:
|
||||
WriteByte(current->Value.Mnemonic);
|
||||
CurrentIndex++;
|
||||
WriteWord(GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 2;
|
||||
break;
|
||||
case NOP:
|
||||
case RET:
|
||||
WriteByte(current->Value.Mnemonic);
|
||||
break;
|
||||
case CALL:
|
||||
WriteByte(current->Value.Mnemonic);
|
||||
CurrentIndex++;
|
||||
WriteWord(GetValueFromToken(LIST->content[CurrentIndex]));
|
||||
PC2 += 2;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
CurrentIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-13
@@ -148,7 +148,7 @@ void HandleOpcode(void) {
|
||||
if (arg->Class == RegisterClass) {
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
ExpectPuncuation(Comma, RemoveExpected);
|
||||
|
||||
arg = PeekToken();
|
||||
|
||||
@@ -182,11 +182,11 @@ void HandleOpcode(void) {
|
||||
ExpectIdentifier(0, ForwardParser);
|
||||
}
|
||||
else {
|
||||
ExpectPuncuation(LBracket, ForwardParser);
|
||||
ExpectPuncuation(LBracket, RemoveExpected);
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(RBracket, ForwardParser);
|
||||
ExpectPuncuation(RBracket, RemoveExpected);
|
||||
|
||||
opcode->Value.Mnemonic = isByte ? COPYRAB : COPYRA;
|
||||
opcode->Lemexe = isByte ? "copyrab" : "copyra";
|
||||
@@ -194,19 +194,19 @@ void HandleOpcode(void) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
ExpectPuncuation(LBracket, ForwardParser);
|
||||
ExpectPuncuation(LBracket, RemoveExpected);
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(RBracket, ForwardParser);
|
||||
ExpectPuncuation(RBracket, RemoveExpected);
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
ExpectPuncuation(Comma, RemoveExpected);
|
||||
|
||||
ExpectPuncuation(LBracket, ForwardParser);
|
||||
ExpectPuncuation(LBracket, RemoveExpected);
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(RBracket, ForwardParser);
|
||||
ExpectPuncuation(RBracket, RemoveExpected);
|
||||
|
||||
opcode->Value.Mnemonic = isByte ? COPYRARAB : COPYRARA;
|
||||
opcode->Lemexe = isByte ? "copyrarab" : "copyrara";
|
||||
@@ -219,7 +219,7 @@ void HandleOpcode(void) {
|
||||
{
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
ExpectPuncuation(Comma, RemoveExpected);
|
||||
|
||||
TokenClass class = PeekToken()->Class;
|
||||
|
||||
@@ -257,7 +257,7 @@ void HandleOpcode(void) {
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
ExpectPuncuation(Comma, RemoveExpected);
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
@@ -268,7 +268,7 @@ void HandleOpcode(void) {
|
||||
PC++;
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(Comma, ForwardParser);
|
||||
ExpectPuncuation(Comma, RemoveExpected);
|
||||
|
||||
if (PeekToken()->Class != NumberClass) {
|
||||
fprintf(stderr, "[Line %d] Syntax error, expected numeric literal.\n", PeekToken()->LineNumber);
|
||||
@@ -303,11 +303,11 @@ void HandleOpcode(void) {
|
||||
}
|
||||
else
|
||||
{
|
||||
ExpectPuncuation(LBracket, ForwardParser);
|
||||
ExpectPuncuation(LBracket, RemoveExpected);
|
||||
|
||||
ExpectRegister();
|
||||
|
||||
ExpectPuncuation(RBracket, ForwardParser);
|
||||
ExpectPuncuation(RBracket, RemoveExpected);
|
||||
|
||||
opcode->Value.Mnemonic = JMPI;
|
||||
opcode->Lemexe = "jmpi";
|
||||
|
||||
Reference in New Issue
Block a user