Expanded the tokenizer to recognize some opcodes and registers.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "../includes/tokenizer.h"
|
||||
#include "../includes/opcodes.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user