Updated the lexer to pick out some more language specific grammar.
This commit is contained in:
+3
-2
@@ -23,8 +23,9 @@ typedef enum {
|
|||||||
TK_Hex,
|
TK_Hex,
|
||||||
TK_Opcode,
|
TK_Opcode,
|
||||||
TK_Register,
|
TK_Register,
|
||||||
TK_Label,
|
TK_Directive,
|
||||||
TK_Directive
|
TK_Symbol,
|
||||||
|
TK_Comma
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+15
-44
@@ -13,6 +13,7 @@ List* GenerateTokensFromFile(const char* file_path) {
|
|||||||
char* line = NULL;
|
char* line = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
ssize_t bytes_read;
|
ssize_t bytes_read;
|
||||||
|
List* tokens = CreateList();
|
||||||
|
|
||||||
file = fopen(file_path, "r");
|
file = fopen(file_path, "r");
|
||||||
|
|
||||||
@@ -21,8 +22,6 @@ List* GenerateTokensFromFile(const char* file_path) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//char* context;// = malloc(sizeof(char *));
|
|
||||||
|
|
||||||
while((bytes_read = getline(&line, &len, file)) != -1) {
|
while((bytes_read = getline(&line, &len, file)) != -1) {
|
||||||
Token* token = GetNextToken(line);
|
Token* token = GetNextToken(line);
|
||||||
|
|
||||||
@@ -30,30 +29,7 @@ List* GenerateTokensFromFile(const char* file_path) {
|
|||||||
|
|
||||||
if (!token) break;
|
if (!token) break;
|
||||||
|
|
||||||
if(token->type == TK_Number) {
|
AddListItem(token, sizeof(Token), tokens);
|
||||||
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 if (token->type == TK_Directive) {
|
|
||||||
printf("Directive '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
printf("Found text: '%s'\n", token->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(token->value);
|
|
||||||
free(token);
|
|
||||||
|
|
||||||
token = GetNextToken(NULL);
|
token = GetNextToken(NULL);
|
||||||
}
|
}
|
||||||
@@ -65,7 +41,7 @@ List* GenerateTokensFromFile(const char* file_path) {
|
|||||||
|
|
||||||
if (line) free(line);
|
if (line) free(line);
|
||||||
|
|
||||||
return NULL;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
Token* GetNextToken(char *string) {
|
Token* GetNextToken(char *string) {
|
||||||
@@ -81,32 +57,27 @@ Token* GetNextToken(char *string) {
|
|||||||
if (base == 16) return CreateToken(TK_Hex, 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);
|
|
||||||
|
|
||||||
if (strcmp(".db", string_token) == 0) {
|
|
||||||
char* next = PeekNextToken();
|
char* next = PeekNextToken();
|
||||||
|
|
||||||
if (next) {
|
if (next) {
|
||||||
printf("String variable name: '%s'\n", next);
|
if (strcmp(":", next) == 0) {
|
||||||
|
free(next);
|
||||||
|
free(GetToken(NULL));
|
||||||
|
return CreateToken(TK_Symbol, string_token);
|
||||||
|
}
|
||||||
|
|
||||||
free(next);
|
free(next);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (IsOpcode(string_token)) return CreateToken(TK_Opcode, string_token);
|
||||||
|
if (IsRegister(string_token)) return CreateToken(TK_Register, string_token);
|
||||||
|
if (strcmp(",", string_token) == 0) return CreateToken(TK_Comma, string_token);
|
||||||
|
|
||||||
return CreateToken(TK_String, string_token);
|
return CreateToken(TK_String, string_token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(string_token);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-1
@@ -21,5 +21,15 @@ int main(int argc, char* args[]) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
GenerateTokensFromFile(args[1]);
|
List* tokens = GenerateTokensFromFile(args[1]);
|
||||||
|
|
||||||
|
for (int i = 0; i < tokens->size; i++) {
|
||||||
|
Token* t = (Token*) tokens->content[i];
|
||||||
|
|
||||||
|
printf("Token Type: '%d' Value: '%s'\n", t->type, t->value);
|
||||||
|
|
||||||
|
free(t->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
DestroyList(tokens);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user