diff --git a/includes/opcodes.h b/includes/opcodes.h new file mode 100644 index 0000000..667d5ea --- /dev/null +++ b/includes/opcodes.h @@ -0,0 +1,15 @@ +#ifndef OPCODES_H +#define OPCODES_H + +#include + +#define OPCODECOUNT 7 +#define REGISTERCOUNT 8 + +extern char *opcodes[OPCODECOUNT]; +extern char *registers[REGISTERCOUNT]; + +int IsOpcode(const char*); +int IsRegister(const char*); + +#endif \ No newline at end of file diff --git a/includes/tokenizer.h b/includes/tokenizer.h index 4e3887e..7be1f97 100644 --- a/includes/tokenizer.h +++ b/includes/tokenizer.h @@ -21,6 +21,8 @@ typedef enum { TK_Text, TK_Number, TK_Hex, + TK_Opcode, + TK_Register, TK_Invalid } TokenType; diff --git a/src/opcodes.c b/src/opcodes.c new file mode 100644 index 0000000..b00c771 --- /dev/null +++ b/src/opcodes.c @@ -0,0 +1,42 @@ +#include "../includes/opcodes.h" + +char *opcodes[OPCODECOUNT] = { + "copy", + "add", + "sub", + "jz", + "int", + "hlt", + "ret" +}; + +char *registers[REGISTERCOUNT] = { + "r1", + "r2", + "r3", + "r4", + "r5", + "r6", + "r7", + "r8" +}; + +int IsOpcode(const char* text) { + if (!text) return 0; + + for(int i = 0; i < OPCODECOUNT; i++) { + if (strcmp(opcodes[i], text) == 0) return 1; + } + + return 0; +} + +int IsRegister(const char* text) { + if (!text) return 0; + + for(int i = 0; i < REGISTERCOUNT; i++) { + if (strcmp(registers[i], text) == 0) return 1; + } + + return 0; +} \ No newline at end of file diff --git a/src/tokenizer.c b/src/tokenizer.c index 1f4d4c9..319efe5 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -1,4 +1,5 @@ #include "../includes/tokenizer.h" +#include "../includes/opcodes.h" #include #include #include @@ -42,6 +43,12 @@ List* TokenizeString(const char *file_path) { else if (token->type == TK_Comma) { printf("Comma\n"); } + else if (token->type == TK_Opcode) { + printf("Found op: '%s'\n", token->value); + } + else if (token->type == TK_Register) { + printf("Found Reg: '%s'\n", token->value); + } else { printf("Found text: '%s'\n", token->value); } @@ -73,6 +80,8 @@ Token* GetNextToken(char *string) { if (strcmp(string_token, ":") == 0) return CreateToken(TK_Colon, string_token); if (strcmp(string_token, ",") == 0) return CreateToken(TK_Comma, string_token); + if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token); + if (IsRegister(string_token)) return CreateToken(TK_Register, string_token); return CreateToken(TK_Text, string_token); }