updated Separate string function and moved some part of main.c into SterlingCompiler.h

This commit is contained in:
Emilia(SleepeeSoftware) 2026-03-03 01:06:52 +01:00
parent 45030b0791
commit 0a66fcbd80
2 changed files with 67 additions and 44 deletions

View File

@ -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 <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif

View File

@ -1,11 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SterlingCompiler.h>
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);
}