Updated the lexer to pick out some more language specific grammar.
This commit is contained in:
+16
-45
@@ -13,6 +13,7 @@ List* GenerateTokensFromFile(const char* file_path) {
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
ssize_t bytes_read;
|
||||
List* tokens = CreateList();
|
||||
|
||||
file = fopen(file_path, "r");
|
||||
|
||||
@@ -21,39 +22,14 @@ List* GenerateTokensFromFile(const char* 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 if (token->type == TK_Directive) {
|
||||
printf("Directive '%s'\n", token->value);
|
||||
}
|
||||
else {
|
||||
printf("Found text: '%s'\n", token->value);
|
||||
}
|
||||
|
||||
free(token->value);
|
||||
free(token);
|
||||
|
||||
AddListItem(token, sizeof(Token), tokens);
|
||||
|
||||
token = GetNextToken(NULL);
|
||||
}
|
||||
@@ -65,7 +41,7 @@ List* GenerateTokensFromFile(const char* file_path) {
|
||||
|
||||
if (line) free(line);
|
||||
|
||||
return NULL;
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Token* GetNextToken(char *string) {
|
||||
@@ -81,32 +57,27 @@ Token* GetNextToken(char *string) {
|
||||
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 = PeekNextToken();
|
||||
|
||||
// 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();
|
||||
if (next) {
|
||||
printf("String variable name: '%s'\n", next);
|
||||
if (next) {
|
||||
if (strcmp(":", next) == 0) {
|
||||
free(next);
|
||||
free(GetToken(NULL));
|
||||
return CreateToken(TK_Symbol, string_token);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
free(string_token);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
+11
-1
@@ -21,5 +21,15 @@ int main(int argc, char* args[]) {
|
||||
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