Expanded the tokenizer to recognize some opcodes and registers.

This commit is contained in:
2022-01-24 17:09:02 +00:00
parent 2570c5cac0
commit f88613a160
4 changed files with 68 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
#ifndef OPCODES_H
#define OPCODES_H
#include <string.h>
#define OPCODECOUNT 7
#define REGISTERCOUNT 8
extern char *opcodes[OPCODECOUNT];
extern char *registers[REGISTERCOUNT];
int IsOpcode(const char*);
int IsRegister(const char*);
#endif
+2
View File
@@ -21,6 +21,8 @@ typedef enum {
TK_Text,
TK_Number,
TK_Hex,
TK_Opcode,
TK_Register,
TK_Invalid
} TokenType;
+42
View File
@@ -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;
}
+9
View File
@@ -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);
}