104 lines
2.3 KiB
C
104 lines
2.3 KiB
C
#include "../includes/lexer.h"
|
|
#include "../includes/opcodes.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
Token* GetNextToken(char *);
|
|
int TokenIsNumeric(const char*, int *);
|
|
|
|
List* GetTokensFromLine(char* line) {
|
|
List* tokens = CreateList();
|
|
|
|
Token* token = GetNextToken(line);
|
|
|
|
while (token) {
|
|
|
|
if (!token) break;
|
|
|
|
AddListItem(token, sizeof(Token), tokens);
|
|
|
|
token = GetNextToken(NULL);
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
Token* GetNextToken(char *string) {
|
|
char* string_token = GetToken(string);
|
|
int base = 0;
|
|
TokenType type;
|
|
|
|
while (strlen(string_token) != 0) {
|
|
|
|
if (strcmp(string_token, ".db") == 0) {
|
|
return CreateToken(DB, string_token);
|
|
}
|
|
|
|
if (TokenIsNumeric(string_token, &base)) {
|
|
if (base == 10) return CreateToken(NUMBER, string_token);
|
|
if (base == 16) return CreateToken(HEX, string_token);
|
|
}
|
|
|
|
char* next = PeekNextToken();
|
|
|
|
if (next) {
|
|
if (strcmp(":", next) == 0) {
|
|
free(next);
|
|
free(GetToken(NULL));
|
|
return CreateToken(IDENTIFIER, string_token);
|
|
}
|
|
|
|
free(next);
|
|
}
|
|
|
|
if (IsOpcode(string_token, &type)) {
|
|
return CreateToken(type, string_token);
|
|
}
|
|
|
|
if (IsRegister(string_token, &type)) {
|
|
return CreateToken(type, string_token);
|
|
}
|
|
|
|
// if (op) return CreateToken(op->op, op->lexeme);
|
|
// if (reg) return CreateToken(reg->type, reg->lexeme);
|
|
|
|
if (strcmp(",", string_token) == 0) return CreateToken(COMMA, string_token);
|
|
|
|
return CreateToken(IDENTIFIER, string_token);
|
|
}
|
|
|
|
free(string_token);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
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;
|
|
} |