Modified how registers and opcodes are represented. This commit is a bit buggy but doesn't crash and burn at least. I just wanted the new structs push out before making a bigger mess of things as I try to organize this all.
This commit is contained in:
+20
-17
@@ -9,23 +9,26 @@
|
|||||||
#include "tokenizer.h"
|
#include "tokenizer.h"
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TK_Add,
|
PLUS,
|
||||||
TK_Sub,
|
MINUS,
|
||||||
TK_Mul,
|
STAR,
|
||||||
TK_Div,
|
SLASH,
|
||||||
TK_Power,
|
POWER,
|
||||||
TK_LParam,
|
LPARAM,
|
||||||
TK_RParam,
|
RPARAM,
|
||||||
TK_LBracket,
|
LBRACKET,
|
||||||
TK_RBracket,
|
RBracket,
|
||||||
TK_String,
|
COMMA,
|
||||||
TK_Number,
|
IDENTIFIER,
|
||||||
TK_Hex,
|
NUMBER,
|
||||||
TK_Opcode,
|
HEX,
|
||||||
TK_Register,
|
//Keywords
|
||||||
TK_Directive,
|
DB, ORG,
|
||||||
TK_Symbol,
|
//Opcodes
|
||||||
TK_Comma
|
COPY, ADD, SUB, JZ, INT, YLD, RET,
|
||||||
|
CMP, IN, OUT,
|
||||||
|
//Registers
|
||||||
|
R1, R2, R3, R4, R5, R6, R7, R8
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+25
-5
@@ -2,14 +2,34 @@
|
|||||||
#define OPCODES_H
|
#define OPCODES_H
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "lexer.h"
|
||||||
|
|
||||||
#define OPCODECOUNT 7
|
#define OPCODECOUNT 10
|
||||||
#define REGISTERCOUNT 8
|
#define REGISTERCOUNT 8
|
||||||
|
|
||||||
extern char *opcodes[OPCODECOUNT];
|
typedef enum {
|
||||||
extern char *registers[REGISTERCOUNT];
|
None = 0,
|
||||||
|
Reg,
|
||||||
|
Imm8,
|
||||||
|
Imm16
|
||||||
|
} ParameterType;
|
||||||
|
|
||||||
int IsOpcode(const char*);
|
typedef struct {
|
||||||
int IsRegister(const char*);
|
char* lexeme;
|
||||||
|
TokenType op;
|
||||||
|
ParameterType parameter_one;
|
||||||
|
ParameterType parameter_two;
|
||||||
|
} OpCode;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* lexeme;
|
||||||
|
TokenType type;
|
||||||
|
} Register;
|
||||||
|
|
||||||
|
extern OpCode opcodes[OPCODECOUNT];
|
||||||
|
extern Register registers[REGISTERCOUNT];
|
||||||
|
|
||||||
|
int IsOpcode(const char*, TokenType*);
|
||||||
|
int IsRegister(const char*, TokenType*);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+21
-8
@@ -2,6 +2,7 @@
|
|||||||
#include "../includes/opcodes.h"
|
#include "../includes/opcodes.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
Token* CreateToken(TokenType, char*);
|
Token* CreateToken(TokenType, char*);
|
||||||
Token* GetNextToken(char *);
|
Token* GetNextToken(char *);
|
||||||
@@ -27,14 +28,17 @@ List* GetTokensFromLine(char* line) {
|
|||||||
Token* GetNextToken(char *string) {
|
Token* GetNextToken(char *string) {
|
||||||
char* string_token = GetToken(string);
|
char* string_token = GetToken(string);
|
||||||
int base = 0;
|
int base = 0;
|
||||||
|
TokenType type;
|
||||||
|
|
||||||
while (strlen(string_token) != 0) {
|
while (strlen(string_token) != 0) {
|
||||||
|
|
||||||
if (string_token[0] == '.') return CreateToken(TK_Directive, string_token);
|
if (strcmp(string_token, ".db") == 0) {
|
||||||
|
return CreateToken(DB, string_token);
|
||||||
|
}
|
||||||
|
|
||||||
if (TokenIsNumeric(string_token, &base)) {
|
if (TokenIsNumeric(string_token, &base)) {
|
||||||
if (base == 10) return CreateToken(TK_Number, string_token);
|
if (base == 10) return CreateToken(NUMBER, string_token);
|
||||||
if (base == 16) return CreateToken(TK_Hex, string_token);
|
if (base == 16) return CreateToken(HEX, string_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* next = PeekNextToken();
|
char* next = PeekNextToken();
|
||||||
@@ -43,17 +47,26 @@ Token* GetNextToken(char *string) {
|
|||||||
if (strcmp(":", next) == 0) {
|
if (strcmp(":", next) == 0) {
|
||||||
free(next);
|
free(next);
|
||||||
free(GetToken(NULL));
|
free(GetToken(NULL));
|
||||||
return CreateToken(TK_Symbol, string_token);
|
return CreateToken(IDENTIFIER, string_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(next);
|
free(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token);
|
if (IsOpcode(string_token, &type)) {
|
||||||
if (IsRegister(string_token)) return CreateToken(TK_Register, string_token);
|
return CreateToken(type, string_token);
|
||||||
if (strcmp(",", string_token) == 0) return CreateToken(TK_Comma, string_token);
|
}
|
||||||
|
|
||||||
return CreateToken(TK_String, string_token);
|
if (IsRegister(string_token, &type)) {
|
||||||
|
return CreateToken(type, string_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (op) return CreateToken(op->op, op->lexeme);
|
||||||
|
// if (reg) return CreateToken(reg->type, reg->lexeme);
|
||||||
|
|
||||||
|
if (strcmp(",", string_token) == 0) return CreateToken(COMMA, string_token);
|
||||||
|
|
||||||
|
return CreateToken(IDENTIFIER, string_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(string_token);
|
free(string_token);
|
||||||
|
|||||||
+1
-1
@@ -48,7 +48,7 @@ int main(int argc, char* args[]) {
|
|||||||
|
|
||||||
printf("Token Type: '%d' Value: '%s' (%d)\n", t->type, t->value, line_number);
|
printf("Token Type: '%d' Value: '%s' (%d)\n", t->type, t->value, line_number);
|
||||||
|
|
||||||
free(t->value);
|
if (t->type <= ORG) free(t->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
DestroyList(tokens);
|
DestroyList(tokens);
|
||||||
|
|||||||
+32
-21
@@ -1,41 +1,52 @@
|
|||||||
#include "../includes/opcodes.h"
|
#include "../includes/opcodes.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
char *opcodes[OPCODECOUNT] = {
|
OpCode opcodes[OPCODECOUNT] = {
|
||||||
"copy",
|
{ "copy", COPY, Reg, Reg | Imm8 | Imm16 },
|
||||||
"add",
|
{ "add", ADD, Reg, Reg | Imm8 | Imm16 },
|
||||||
"sub",
|
{ "sub", SUB, Reg, Reg | Imm8 | Imm16 },
|
||||||
"jz",
|
{ "jz", JZ, Imm8 | Imm16, None },
|
||||||
"int",
|
{ "int", INT, Imm8, None },
|
||||||
"yld",
|
{ "yld", YLD, None, None },
|
||||||
"ret"
|
{ "ret", RET, None, None },
|
||||||
|
{ "cmp", CMP, Reg | Imm8 | Imm16, Reg | Imm8 | Imm16 },
|
||||||
|
{ "in", IN, None, None},
|
||||||
|
{ "out", OUT, None, None}
|
||||||
};
|
};
|
||||||
|
|
||||||
char *registers[REGISTERCOUNT] = {
|
Register registers[REGISTERCOUNT] = {
|
||||||
"r1",
|
{ "r1", R1 },
|
||||||
"r2",
|
{ "r2", R2 },
|
||||||
"r3",
|
{ "r3", R3 },
|
||||||
"r4",
|
{ "r4", R4 },
|
||||||
"r5",
|
{ "r5", R5 },
|
||||||
"r6",
|
{ "r6", R6 },
|
||||||
"r7",
|
{ "r7", R7 },
|
||||||
"r8"
|
{ "r8", R8 }
|
||||||
};
|
};
|
||||||
|
|
||||||
int IsOpcode(const char* text) {
|
int IsOpcode(const char* text, TokenType* opcode) {
|
||||||
if (!text) return 0;
|
if (!text) return 0;
|
||||||
|
|
||||||
for(int i = 0; i < OPCODECOUNT; i++) {
|
for(int i = 0; i < OPCODECOUNT; i++) {
|
||||||
if (strcmp(opcodes[i], text) == 0) return 1;
|
if (strcmp(opcodes[i].lexeme, text) == 0) {
|
||||||
|
opcode = &opcodes[i].op;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int IsRegister(const char* text) {
|
int IsRegister(const char* text, TokenType* reg) {
|
||||||
if (!text) return 0;
|
if (!text) return 0;
|
||||||
|
|
||||||
for(int i = 0; i < REGISTERCOUNT; i++) {
|
for(int i = 0; i < REGISTERCOUNT; i++) {
|
||||||
if (strcmp(registers[i], text) == 0) return 1;
|
if (strcmp(registers[i].lexeme, text) == 0) {
|
||||||
|
reg = ®isters[i].type;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+7
-7
@@ -9,11 +9,11 @@ void ProcessDirective(List*);
|
|||||||
void ProcessVariableDeclaration(List*);
|
void ProcessVariableDeclaration(List*);
|
||||||
|
|
||||||
void ParseTokens(List* tokens) {
|
void ParseTokens(List* tokens) {
|
||||||
for(int i = 0; i < tokens->size; i++) {
|
// for(int i = 0; i < tokens->size; i++) {
|
||||||
if (((Token *) tokens->content[i])->type == TK_Directive){
|
// if (((Token *) tokens->content[i])->type == TK_Directive){
|
||||||
ProcessDirective(tokens);
|
// ProcessDirective(tokens);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProcessDirective(List* tokens) {
|
void ProcessDirective(List* tokens) {
|
||||||
@@ -26,11 +26,11 @@ void ProcessDirective(List* tokens) {
|
|||||||
|
|
||||||
if (strcmp(".org", directive->value) == 0) {
|
if (strcmp(".org", directive->value) == 0) {
|
||||||
Token* address_start = (Token*) tokens->content[1];
|
Token* address_start = (Token*) tokens->content[1];
|
||||||
if (address_start->type == TK_Number) {
|
if (address_start->type == NUMBER) {
|
||||||
long address = strtol(address_start->value, NULL, 10);
|
long address = strtol(address_start->value, NULL, 10);
|
||||||
printf("Address starting at '%ld'\n", address);
|
printf("Address starting at '%ld'\n", address);
|
||||||
}
|
}
|
||||||
else if (address_start->type == TK_Hex) {
|
else if (address_start->type == HEX) {
|
||||||
long address = strtol(address_start->value, NULL, 16);
|
long address = strtol(address_start->value, NULL, 16);
|
||||||
printf("Setting address to '%ld'\n", address);
|
printf("Setting address to '%ld'\n", address);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user