138 lines
3.3 KiB
C
138 lines
3.3 KiB
C
#include "../includes/lexer.h"
|
|
#include "../includes/opcodes.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
Token* CreateToken(TokenType, char*);
|
|
Token* GetNextToken(char *);
|
|
int TokenIsNumeric(const char*, int *);
|
|
|
|
List* GenerateTokensFromFile(const char* file_path) {
|
|
FILE *file;
|
|
int line_count = 1;
|
|
char* line = NULL;
|
|
size_t len = 0;
|
|
ssize_t bytes_read;
|
|
|
|
file = fopen(file_path, "r");
|
|
|
|
if (!file) {
|
|
printf("Failed to open '%s' for reading.\n", file_path);
|
|
return NULL;
|
|
}
|
|
|
|
//char* context;// = malloc(sizeof(char *));
|
|
|
|
while((bytes_read = getline(&line, &len, file)) != -1) {
|
|
Token* token = GetNextToken(line);
|
|
|
|
while (token) {
|
|
|
|
if (!token) break;
|
|
|
|
if(token->type == TK_Number) {
|
|
printf("Found number: '%s'\n", token->value);
|
|
}
|
|
else if (token->type == TK_Hex) {
|
|
printf("Hex number: '%s'\n", token->value);
|
|
}
|
|
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 if(token->type == TK_Label) {
|
|
printf("Label: '%s'\n", token->value);
|
|
}
|
|
else {
|
|
printf("Found text: '%s'\n", token->value);
|
|
}
|
|
|
|
free(token->value);
|
|
free(token);
|
|
|
|
token = GetNextToken(NULL);
|
|
}
|
|
|
|
line_count++;
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
if (line) free(line);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
Token* GetNextToken(char *string) {
|
|
char* string_token = SplitOnBasicGrammar(string);
|
|
int base = 0;
|
|
|
|
while (strlen(string_token) != 0) {
|
|
|
|
if (TokenIsNumeric(string_token, &base)) {
|
|
if (base == 10) return CreateToken(TK_Number, string_token);
|
|
if (base == 16) return CreateToken(TK_Hex, 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 (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token);
|
|
if (IsRegister(string_token)) return CreateToken(TK_Register, string_token);
|
|
|
|
char* next = SplitOnBasicGrammar(NULL);
|
|
|
|
if (next && strcmp(next, ":") == 0) {
|
|
free(next);
|
|
return CreateToken(TK_Label, string_token);
|
|
}
|
|
|
|
if (next) free(next);
|
|
|
|
return CreateToken(TK_String, string_token);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
Token* CreateToken(TokenType type, char* value) {
|
|
Token* token = calloc(1, sizeof(Token));
|
|
|
|
if (!token) return NULL;
|
|
|
|
token->type = type;
|
|
token->value = value;
|
|
|
|
return token;
|
|
}
|
|
|
|
int TokenIsNumeric(const char* token, int *base) {
|
|
*base = 0;
|
|
|
|
if (!token) return 0;
|
|
|
|
unsigned long length = strlen(token);
|
|
int i = 0;
|
|
|
|
if (length == 0) return 0;
|
|
|
|
*base = 10;
|
|
|
|
if (length > 2) {
|
|
if (token[0] == '0' && token[1] == 'x') {
|
|
*base = 16;
|
|
i = 2;
|
|
}
|
|
}
|
|
|
|
for(; i < length; i++) {
|
|
if (!isdigit(token[i])) {
|
|
if (*base == 16 && token[i] >= 'A' && token[i] <= 'F') continue;
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
} |