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

50 lines
1.3 KiB
C

#if (!defined(NTSYS_ASSEMBLER_H___))
#define NTSYS_ASSEMBLER_H___
#define NTSYS_MAX_LABEL_LENGTH 32
#include "asm-savers.h"
char* ntsys_asm_table[] = {
"print"
};
int ntsys_asm_from_string(size_t* mempos, char* str, FILE* fp) {
ntsys_asm_save_t __save = create_ntsys_asm_save(str);
ntsys_asm_cache_t __cache = create_ntsys_asm_cache(__save);
ntsys_argument_parser_t __parser = create_ntsys_asm_parser(__cache, str, ntsys_asm_table);
return ntsys_run_asm_parser(__parser);
}
int ntsys_compile_asm(char* filename, char* asm_out) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
ntsys_error("Cannot open assembler file!");
return EXIT_FAILURE;
}
FILE* fw = fopen(asm_out, "wb");
if (fw == NULL) {
ntsys_error("Cannot create or rewrite output file!");
fclose(fp);
return EXIT_FAILURE;
}
char* st = NULL;
size_t mem_pos = 0;
size_t lb_size = 1;
int c;
while ((c = getc(fp)) != EOF) {
if (c == ':') {
lb_size ++;
}
}
rewind(fp);
while ((st = ntsys_get_str(st, fp)) != NULL) {
if (ntsys_asm_from_string(&mem_pos, st, fw) != EXIT_SUCCESS) return EXIT_FAILURE;
}
free(st);
fclose(fp);
fclose(fw);
return EXIT_SUCCESS;
}
#endif /* NTSYS_ASSEMBLER_H___ */