From 0a2a255993b20038e3881d9c61ac11467d463d58 Mon Sep 17 00:00:00 2001 From: shabani005 Date: Wed, 12 Nov 2025 14:15:12 +0300 Subject: [PATCH] parser improved --- nb.h | 205 +++++++++++++++++++++++++++++++++++++++++++++------ src/parser.h | 2 +- 2 files changed, 185 insertions(+), 22 deletions(-) diff --git a/nb.h b/nb.h index 0147c0a..1fa4a67 100644 --- a/nb.h +++ b/nb.h @@ -6,6 +6,13 @@ #include #include #include +#include +#include +#include + +typedef struct { + int debug; +} nb_opt; typedef struct{ int capacity; @@ -13,17 +20,33 @@ typedef struct{ char** value; } nb_arr; +typedef struct{ + FILE *filep; + size_t filesize; + int chars; + char *buf; +} nb_file; + +typedef struct { + char** urls; + char** filenames; + size_t size; + size_t capacity; +} nb_downloads; + + +static nb_downloads nb_default_down; + #define nb_append_da(nb_arr, ...) \ nb_append_va(nb_arr, \ ((const char*[]){__VA_ARGS__}), \ (sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*))) - typedef struct{ - FILE *filep; - size_t filesize; - int chars; - char *buf; - } nb_file; + +#define nb_qsortsa(arr) nb_qsorts_impl((arr), sizeof(arr)/sizeof(arr[0])) +#define nb_qsortf(arr) nb_qsortf_impl((arr), sizeof(arr)/sizeof(arr[0])) +#define nb_qsorti(arr) nb_qsorti_impl((arr), sizeof(arr)/sizeof(arr[0])) +#define nb_split(string, ...) nb_split_impl(string, (nb_opt) {__VA_ARGS__}) void nb_init(nb_arr *newarr, int initial_capacity); // obsolete @@ -39,32 +62,44 @@ char* nb_strdup(const char* s); // make this void that uses realloc later. void nb_print(nb_arr *newarr); void nb_print_info(nb_arr *newarr); - - void nb_cmd(nb_arr *newarr); // File utils void nb_copy_file(char* old_file_name, char* new_file_name); char* nb_read_file(char* file_name); +void nb_write_file(char* name, char* buf); +nb_file nb_read_file_c(char* file_name); bool nb_did_file_change(char *filename); bool nb_does_file_exist(char *filename); - -//bool needs_rebuild(); // need to implement rename file first to .old or something like nob does - - void nb_rebuild(int argc, char **argv); +void nb_end(); +void include_http_custom(const char* url, const char* filename); +//bool needs_rebuild(); // need to implement rename file first to .old or something like nob does TODO +// Misc utils +int nb_compf(const void *a, const void *b); +int nb_compi(const void *a, const void *b); +char* nb_slice_str(char* a, size_t start, size_t end); // python slicing in c :Kappa: +void nb_qsortf_impl(void *base, size_t nmemb); // these functions macros +void nb_qsorti_impl(void *base, size_t nmemb); // two have + #ifdef NB_IMPLEMENTATION // make sure to define this before using the header -#include -#include -#include + +char* nb_slice_str(char* a, size_t start, size_t end){ + size_t len = end-start; + char* result = malloc(len+1); + memmove(result, a+start, len); + result[len] = '\0'; + return result; +} + /* char* nb_strdup(const char* s) { @@ -86,7 +121,7 @@ void nb_init(nb_arr *newarr, int initial_capacity){ void nb_append(nb_arr *newarr, char *newval){ if (newarr->value == NULL){ newarr->capacity =16; - if ((newarr->capacity > 16) | (newarr->arrsize > newarr->capacity)) { + if (newarr->capacity > 16 | newarr->arrsize > newarr->capacity) { newarr->capacity *=2; } newarr->value = (char**)realloc(newarr->value, sizeof(char*) * newarr->capacity); @@ -178,14 +213,30 @@ void nb_com(nb_arr *newarr){ } -// void append_c_file(FILE *filepointer){ -// filepointer = NULL; -// } +void append_c_file(FILE *filepointer){ + +} + + +void nb_write_file(char* name, char* buf){ // old name shouldnt be nobuild.c. it should be the name of the current file. + nb_file new_file; + + new_file.filep = fopen(name, "wb"); + fwrite(buf, 1, strlen(buf), new_file.filep); + fclose(new_file.filep); + // printf("Current buf size: %zu\n", strlen(buf)); +} + void nb_copy_file(char* old_file_name, char* new_file_name){ // old name shouldnt be nobuild.c. it should be the name of the current file. nb_file old_file; nb_file new_file; + if (!nb_does_file_exist){ + printf("%s does not exit", old_file_name); + return; + } + old_file.filep = fopen(old_file_name, "rb"); fseek(old_file.filep, 0, SEEK_END); @@ -203,6 +254,11 @@ void nb_copy_file(char* old_file_name, char* new_file_name){ // old name shouldn bool nb_did_file_change(char *filename){ struct stat file_old; stat(filename, &file_old); + + if (!nb_does_file_exist){ + printf("%s does not exist\n", filename); + return 0; + } struct stat file_new; char buf[64]; @@ -216,8 +272,9 @@ bool nb_did_file_change(char *filename){ bool nb_does_file_exist(char *filename){ if (access(filename, F_OK) == 0){ return true; - } + } else { return false; + } } void nb_rebuild(int argc, char **argv){ @@ -269,7 +326,8 @@ void nb_rebuild(int argc, char **argv){ } } -char* nb_read_file(char* file_name){ // old name shouldnt be nobuild.c. it should be the name of the current file. I should think more about adding error handling + +nb_file nb_read_file_c(char* file_name){ // old name shouldnt be nobuild.c. it should be the name of the current file. I should think more about adding error handling nb_file file; file.filep = fopen(file_name, "rb"); @@ -281,6 +339,22 @@ char* nb_read_file(char* file_name){ // old name shouldnt be nobuild.c. it shoul fread(file.buf, 1, file.filesize, file.filep); fclose(file.filep); file.buf[file.filesize] = '\0'; + return file; +} + + +char* nb_read_file(char* file_name){ // old name shouldnt be nobuild.c. it should be the name of the current file. I should think more about adding error handling + nb_file file; + + file.filep = fopen(file_name, "r"); + fseek(file.filep, 0, SEEK_END); + + file.filesize = ftell(file.filep); + file.buf = (char*)malloc(file.filesize+1); + fseek(file.filep, 0, SEEK_SET); + fread(file.buf, 1, file.filesize, file.filep); + file.buf[file.filesize] = '\0'; // null termination + fclose(file.filep); return file.buf; } @@ -290,5 +364,94 @@ void nb_append_va(nb_arr *newarr, const char *items[], int count) { } } +int nb_compf(const void *a, const void *b){ + float fa = *(const float*)a; + float fb = *(const float*)b; + if (fa < fb) return -1; + else if (fa > fb) return 1; + else return 0; +} + +int nb_compi(const void *a, const void *b){ + float ia = *(const int*)a; + float ib = *(const int*)b; + if (ia < ib) return -1; + else if (ia > ib) return 1; + else return 0; +} + +int nb_compsa(const void *a, const void *b) { + const char *sa = *(const char **)a; + const char *sb = *(const char **)b; + + size_t la = strlen(sa); + size_t lb = strlen(sb); + + if (la < lb) return -1; + else if (la > lb) return 1; + else return 0; +} + +void nb_qsortf_impl(void *base, size_t nmemb){ + qsort(base, nmemb, sizeof(float), nb_compf); +} + +void nb_qsortsa_impl(void *base, size_t nmemb){ + qsort(base, nmemb, sizeof(char*), nb_compsa); +} + +void nb_qsorti_impl(void *base, size_t nmemb){ + qsort(base, nmemb, sizeof(int), nb_compi); +} + +char** nb_split_impl(char* string, nb_opt opt){ + size_t n = strlen(string); + char** split = malloc(sizeof(char*)*n); + for (int i=0; i= nb_default_down.capacity) { + nb_default_down.capacity*=2; + nb_default_down.filenames = realloc(nb_default_down.filenames, nb_default_down.capacity); + nb_default_down.urls = realloc(nb_default_down.urls, nb_default_down.capacity); + } + nb_default_down.urls[nb_default_down.size] = (char*)url; + nb_default_down.filenames[nb_default_down.size] = (char*)filename; + nb_default_down.size++; + nb_append_da(&cmd, "wget", "-q", "-O", filename, url); // TODO: use libcurl or implement own http thingy + nb_cmd(&cmd); +} + +void nb_end(){ + for (size_t i=0; itype[*idx] != TOKEN_IDENTIFIER){ + if (inp->type[*idx] != TOKEN_IDENT_INT){ fprintf(stderr, "Expected return type after ')'\n"); exit(1); }