#include "../includes/tokenizer.h" #include #include #include #include #include List* TokenizeLine(char *); TokenType GetOperatorType(char); char* SplitOnWhiteSpace(char*); List* TokenizeString(const char *file_path) { FILE *file; char* line = NULL; size_t len = 0; ssize_t bytes_read; List* tokens = CreateList(); file = fopen(file_path, "r"); if (!file) { printf("Failed to open '%s' for reading.\n", file_path); return NULL; } while((bytes_read = getline(&line, &len, file)) != -1) { List* tokens = TokenizeLine(line); // for(int i = 0; i < tokens->size; i++) { // printf("Token type '%d' with value '%s'\n", ((Token* ) tokens->content[i])->type, ((Token*) tokens->content[i])->value); // } } fclose(file); if (line) free(line); return tokens; } List* TokenizeLine(char *line) { if (!line) return NULL; if (strlen(line) == 0) return NULL; if (line[0] == ';') return NULL; char* token = SplitOnWhiteSpace(line); while (strlen(token) != 0) { printf("'%s' ", token); //if (token[0] == '"') printf("String token: '%s'\n", token); free(token); token = SplitOnWhiteSpace(NULL); } printf("\n"); //free(currentWord); free(token); return NULL; } char* SplitOnWhiteSpace(char* line) { static char* string; static unsigned long position; if (line) { string = line; position = 0; } char* token = calloc(1, strlen(string) + 1); int parsing_string = 0; for (int i = 0; position < strlen(string); i++, position++) { if (parsing_string) { if (string[position] == '\n') break; token[i] = string[position]; continue; } if (string[position] == ';') break; if (string[position] == '"') { parsing_string = 1; } if (string[position] == ' ') { while(string[position] == ' ') { position++; } if (strlen(token) != 0) break; } if (string[position] == ',') { if (strlen(token) == 0) { token[0] = string[position]; position++; } break; } if (string[position] != '\n') token[i] = string[position]; } //token[strlen(token)] = '\0'; return token; } TokenType GetOperatorType(char c) { switch (c){ case '+': return TK_Add; case '-': return TK_Sub; case '*': return TK_Mul; case '/': return TK_Div; case '^': return TK_Power; case ':': return TK_Colon; case '(': return TK_LParam; case ')': return TK_RParam; case '[': return TK_LBracket; case ']': return TK_RBracket; }; return TK_Invalid; }