ntsys/asm-savers.h
2026-03-04 17:15:27 +04:00

142 lines
4.3 KiB
C

#if (!defined(NTSYS__S_ASSEMBLER_H___))
#define NTSYS__S_ASSEMBLER_H___
#define create_ntsys_asm_parser(cache, str, table) ((ntsys_argument_parser_t){(cache), (str), (table)})
#define create_ntsys_asm_save(ss) ((ntsys_asm_save_t){(ss), (ss)})
#define create_ntsys_asm_cache(ss) ((ntsys_asm_cache_t)&(ss))
#define ntsys_asm_arg_new(str, __cache) ((ntsys_get_arg((__cache))) && ((str) = __cache->string))
#define ntsys_asm_arg_or_zero_len_str(str, __cache) ((((ntsys_get_arg((__cache))) && ((str) = __cache->string)) == NULL) ? ("") : (str))
/*
* #define __SPACE_SHIFT while (is_space(st[pos])) { pos ++; }
* #define __NEXT_ARG {__e_byte_t isStr = 0;while (!is_space(st[save]) || isStr == 1) { if (st[save] == '"' && st[save - 1] != '\\') {isStr = !isStr;}save ++; }}while (is_space(st[save])) { save ++; }{size_t n_p = save;__e_byte_t isStr = 0;while (!is_space(st[n_p]) || isStr == 1) { if (st[n_p] == '"' && st[n_p - 1] != '\\') {isStr = !isStr;}n_p ++; }st[n_p] = '\0';}
* #define get_mem_pos() *mem_pos = ftell(fp) == 0 ? 0 : ftell(fp) - 1;
*/
#define __READ_FILE_WITH_EOF_OR_LN ((c = getc(fp)) != EOF && c != '\n' && c != '\r')
#define is_space(c) (c >= 0 && c <= 32)
char* ntsys_get_str(char* ptr, FILE* fp) {
if (ptr != NULL) {
free(ptr);
}
unsigned long len = 0;
int c;
{
unsigned long save_ptr = ftell(fp);
while (__READ_FILE_WITH_EOF_OR_LN) {
len ++;
}
fseek(fp, save_ptr, SEEK_SET);
}
if (len == 0) return NULL;
ptr = malloc(len);
if (ptr == NULL) {
ntsys_error("Out of memory!");
return NULL;
}
len = 0;
while (__READ_FILE_WITH_EOF_OR_LN) {
ptr[len] = c;
len ++;
}
ptr[len] = '\0';
return ptr;
}
#undef __READ_FILE_WITH_EOF_OR_LN
typedef struct {
char* string;
char* cache;
} ntsys_asm_save_t;
typedef ntsys_asm_save_t* ntsys_asm_cache_t;
static void __str_slice(char* str) {
size_t i = 1;
while (i < strlen(str)) {
str[i - 1] = str[i];
i ++;
}
str[strlen(str) - 1] = '\0';
}
char* ntsys_get_arg(ntsys_asm_cache_t save) {
char* str = save->cache;
while (1) {
if (!is_space(str[0]) || str[0] == '\0') {
break;
}
str ++;
}
{
size_t n_p = 0;
__e_byte_t isStr = 0;
while (1) {
if (str[n_p] == '"' && str[((n_p == 0) ? 1 : 0) - 1] != '\\') {isStr = !isStr;}
if ((is_space(str[n_p]) && isStr == 0) || str[n_p] == '\0') {
break;
}
n_p ++;
}
save->cache = &str[n_p + 1];
str[n_p] = '\0';
}
if (strlen(str) == 1 || strlen(str) == 0) {
str = NULL;
save->string = NULL;
return NULL;
}
if (str[0] == '"') {
str ++;
if (str[strlen(str) - 1] != '"' || str[(strlen(str) < 2 ? 2 : strlen(str)) - 2] == '\\') {
ntsys_error("No close of string!");
str = NULL;
save->string = NULL;
return NULL;
} else {
str[strlen(str) - 1] = '\0';
size_t i = 0;
while (i < strlen(str)) {
if (str[i] == '\\') {
__str_slice(&str[i]);
switch (str[i]) {
case '0': str[i] = '\0'; break;
case 'a': str[i] = '\a'; break;
case 'b': str[i] = '\b'; break;
case 't': str[i] = '\t'; break;
case 'n': str[i] = '\n'; break;
case 'v': str[i] = '\v'; break;
case 'f': str[i] = '\f'; break;
case 'r': str[i] = '\r'; break;
default:
ntsys_error("Unknown ident!");
str = NULL;
save->string = NULL;
return NULL;
break;
}
}
i ++;
}
}
}
save->string = str;
return str;
}
typedef struct {
ntsys_asm_cache_t cache;
char* string;
char** table;
} ntsys_argument_parser_t;
int ntsys_run_asm_parser(ntsys_argument_parser_t parser) {
ntsys_asm_arg_new(parser.string, parser.cache);
return EXIT_SUCCESS;
}
#undef is_space
#endif