diff --git a/include/SterlingCompiler.h b/include/SterlingCompiler.h index e69de29..7108dbc 100644 --- a/include/SterlingCompiler.h +++ b/include/SterlingCompiler.h @@ -0,0 +1,29 @@ +#ifndef STERLING_COMPILER_H +# define STERLING_COMPILER_H + +typedef enum { + TOK_NONE, + TOK_STRING, + TOK_TOKEN, + TOK_PREPROCESSOR, +} TKN_CTX; + +typedef struct Token_s { + int size; + char *data; + TKN_CTX ctx; +} Token_t; + +typedef struct { + int size; + Token_t *token; +} TokenList; + +#include +#include +#include +#include +#include +#include + +#endif diff --git a/source/main.c b/source/main.c index 67ada91..44b55eb 100644 --- a/source/main.c +++ b/source/main.c @@ -1,11 +1,9 @@ -#include -#include -#include +#include char *LoadFile(const char *filename) { char *data = NULL; FILE *file = NULL; - const int buff_size = 2048; + const int buff_size = 2048;//may change for max size of file in character char buffer[buff_size]; int count = 0; file = fopen(filename, "r"); @@ -16,64 +14,44 @@ char *LoadFile(const char *filename) { return (data); } -typedef enum { - TOK_NONE, - TOK_STRING, - TOK_TOKEN, - TOK_PREPROCESSOR, -} TKN_CTX; - -typedef struct Token_s { - int size; - char *data; - TKN_CTX ctx; -} Token_t; - -typedef struct { - int size; - Token_t *data; -} TokenList; - TokenList SeparateString(char *data) { - char* span = NULL; - char* tok = NULL; - char* tmp = NULL; - Token_t buffer[256]; + char *span = NULL; + char *tok = NULL; + char *tmp = NULL; + bool toggle = false; + Token_t buffer[256]; tok = strtok_r(data, "\"\'", &span); int i = 0; buffer[i].ctx = TOK_NONE; - while (tok) { + while (tok && i < (sizeof(buffer) / sizeof(Token_t))) { size_t size = (span - tok); tmp = calloc(size + 1, sizeof(char)); memcpy_s(tmp, size, tok, size); tmp[size] = 0x00; buffer[i].data = tmp; buffer[i].size = size; - - //printf("|%s|\n", tmp); - //free(tmp); + if (toggle) { + buffer[i].ctx = TOK_STRING; + } else { + buffer[i].ctx = TOK_NONE; + } tok = strtok_r(NULL, "\"\'", &span); i++; - buffer[i].ctx = TOK_STRING; + toggle ^= toggle; + } TokenList token_list; - token_list.data = calloc(i, sizeof(Token_t)); + token_list.token = calloc(i, sizeof(Token_t)); token_list.size = i; + memcpy(token_list.token, buffer, i); + for (int k = 0; k < i; k++) { + token_list.token[k] = buffer[i]; + } return (token_list); } -int main(int ac, char **av) { - if (ac <= 1) { - printf("no file specified"); - return (-1); - } - char* data = LoadFile(av[1]); - TokenList tkn_lst; - //first pass on string - tkn_lst = SeparateString(data); - //second pass on ; and \n - //third pass on \t and space - +void tmpFunction() { + char* data = NULL; char* span = NULL; char* tmp = NULL; char* delim = " \t\n"; @@ -89,6 +67,22 @@ int main(int ac, char **av) { free(tmp); tok = strtok_r(NULL, " \t\n", &span); } +} + +int main(int ac, char **av) { + if (ac <= 1) { + printf("no file specified"); + return (-1); + } + char* data = LoadFile(av[1]); + TokenList tkn_lst; + //first pass on string + tkn_lst = SeparateString(data); + //second pass on ; and \n + //third pass on \t and space + for (int i = 0; i < tkn_lst.size; i++) { + free(tkn_lst.token[i].data); + } free(data); return(0); }