parser improved
This commit is contained in:
205
nb.h
205
nb.h
@@ -6,6 +6,13 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int debug;
|
||||||
|
} nb_opt;
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
int capacity;
|
int capacity;
|
||||||
@@ -13,17 +20,33 @@ typedef struct{
|
|||||||
char** value;
|
char** value;
|
||||||
} nb_arr;
|
} 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, ...) \
|
#define nb_append_da(nb_arr, ...) \
|
||||||
nb_append_va(nb_arr, \
|
nb_append_va(nb_arr, \
|
||||||
((const char*[]){__VA_ARGS__}), \
|
((const char*[]){__VA_ARGS__}), \
|
||||||
(sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*)))
|
(sizeof((const char*[]){__VA_ARGS__})/sizeof(const char*)))
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
FILE *filep;
|
#define nb_qsortsa(arr) nb_qsorts_impl((arr), sizeof(arr)/sizeof(arr[0]))
|
||||||
size_t filesize;
|
#define nb_qsortf(arr) nb_qsortf_impl((arr), sizeof(arr)/sizeof(arr[0]))
|
||||||
int chars;
|
#define nb_qsorti(arr) nb_qsorti_impl((arr), sizeof(arr)/sizeof(arr[0]))
|
||||||
char *buf;
|
#define nb_split(string, ...) nb_split_impl(string, (nb_opt) {__VA_ARGS__})
|
||||||
} nb_file;
|
|
||||||
|
|
||||||
void nb_init(nb_arr *newarr, int initial_capacity); // obsolete
|
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(nb_arr *newarr);
|
||||||
void nb_print_info(nb_arr *newarr);
|
void nb_print_info(nb_arr *newarr);
|
||||||
|
|
||||||
|
|
||||||
void nb_cmd(nb_arr *newarr);
|
void nb_cmd(nb_arr *newarr);
|
||||||
|
|
||||||
// File utils
|
// File utils
|
||||||
void nb_copy_file(char* old_file_name, char* new_file_name);
|
void nb_copy_file(char* old_file_name, char* new_file_name);
|
||||||
char* nb_read_file(char* 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_did_file_change(char *filename);
|
||||||
bool nb_does_file_exist(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_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
|
#ifdef NB_IMPLEMENTATION // make sure to define this before using the header
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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) {
|
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){
|
void nb_append(nb_arr *newarr, char *newval){
|
||||||
if (newarr->value == NULL){
|
if (newarr->value == NULL){
|
||||||
newarr->capacity =16;
|
newarr->capacity =16;
|
||||||
if ((newarr->capacity > 16) | (newarr->arrsize > newarr->capacity)) {
|
if (newarr->capacity > 16 | newarr->arrsize > newarr->capacity) {
|
||||||
newarr->capacity *=2;
|
newarr->capacity *=2;
|
||||||
}
|
}
|
||||||
newarr->value = (char**)realloc(newarr->value, sizeof(char*) * newarr->capacity);
|
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){
|
void append_c_file(FILE *filepointer){
|
||||||
// filepointer = NULL;
|
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
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 old_file;
|
||||||
nb_file new_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");
|
old_file.filep = fopen(old_file_name, "rb");
|
||||||
fseek(old_file.filep, 0, SEEK_END);
|
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){
|
bool nb_did_file_change(char *filename){
|
||||||
struct stat file_old;
|
struct stat file_old;
|
||||||
stat(filename, &file_old);
|
stat(filename, &file_old);
|
||||||
|
|
||||||
|
if (!nb_does_file_exist){
|
||||||
|
printf("%s does not exist\n", filename);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
struct stat file_new;
|
struct stat file_new;
|
||||||
char buf[64];
|
char buf[64];
|
||||||
@@ -216,8 +272,9 @@ bool nb_did_file_change(char *filename){
|
|||||||
bool nb_does_file_exist(char *filename){
|
bool nb_does_file_exist(char *filename){
|
||||||
if (access(filename, F_OK) == 0){
|
if (access(filename, F_OK) == 0){
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void nb_rebuild(int argc, char **argv){
|
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;
|
nb_file file;
|
||||||
|
|
||||||
file.filep = fopen(file_name, "rb");
|
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);
|
fread(file.buf, 1, file.filesize, file.filep);
|
||||||
fclose(file.filep);
|
fclose(file.filep);
|
||||||
file.buf[file.filesize] = '\0';
|
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;
|
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<n; ++i){
|
||||||
|
split[i] = malloc(2);
|
||||||
|
split[i][0] = string[i];
|
||||||
|
split[i][1] = '\0';
|
||||||
|
}
|
||||||
|
split[n] = NULL;
|
||||||
|
|
||||||
|
if (opt.debug){
|
||||||
|
printf("[");
|
||||||
|
for (int i=0; i<n; ++i){
|
||||||
|
printf("%s,", split[i]);
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
}
|
||||||
|
return split;
|
||||||
|
}
|
||||||
|
|
||||||
|
void include_http_custom(const char* url, const char* filename){ // this function is for builder not regular c file.
|
||||||
|
nb_arr cmd = {0};
|
||||||
|
if (nb_default_down.capacity == 0) {
|
||||||
|
nb_default_down.capacity = 256;
|
||||||
|
nb_default_down.size = 0;
|
||||||
|
nb_default_down.filenames = malloc(sizeof(char*) * nb_default_down.capacity);
|
||||||
|
nb_default_down.urls = malloc(sizeof(char*) * nb_default_down.capacity);
|
||||||
|
}
|
||||||
|
if (nb_default_down.size >= 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; i<nb_default_down.size; ++i){
|
||||||
|
// printf("debug\n");
|
||||||
|
if (!remove(nb_default_down.filenames[i])) exit(-1);
|
||||||
|
// printf("removed file: %s\n", nb_default_down.filenames[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif //NB_IMPLEMENTATION
|
#endif //NB_IMPLEMENTATION
|
||||||
|
|
||||||
|
// TODO: add #ifdef NB_STRIP_PREFIX in the future
|
||||||
|
|||||||
@@ -288,7 +288,7 @@ Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
|||||||
skip_space(inp, idx);
|
skip_space(inp, idx);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
if (inp->type[*idx] != TOKEN_IDENT_INT){
|
||||||
fprintf(stderr, "Expected return type after ')'\n");
|
fprintf(stderr, "Expected return type after ')'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user