Expanded the tokenizer to recognize some opcodes and registers.
This commit is contained in:
@@ -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
|
||||||
@@ -21,6 +21,8 @@ typedef enum {
|
|||||||
TK_Text,
|
TK_Text,
|
||||||
TK_Number,
|
TK_Number,
|
||||||
TK_Hex,
|
TK_Hex,
|
||||||
|
TK_Opcode,
|
||||||
|
TK_Register,
|
||||||
TK_Invalid
|
TK_Invalid
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
|
|||||||
@@ -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/tokenizer.h"
|
||||||
|
#include "../includes/opcodes.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -42,6 +43,12 @@ List* TokenizeString(const char *file_path) {
|
|||||||
else if (token->type == TK_Comma) {
|
else if (token->type == TK_Comma) {
|
||||||
printf("Comma\n");
|
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 {
|
else {
|
||||||
printf("Found text: '%s'\n", token->value);
|
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_Colon, string_token);
|
||||||
if (strcmp(string_token, ",") == 0) return CreateToken(TK_Comma, 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);
|
return CreateToken(TK_Text, string_token);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user