Arguments parser rewrite
This commit is contained in:
parent
2f90171c30
commit
3dc3af8eba
28
.vscode/launch.json
vendored
28
.vscode/launch.json
vendored
@ -3,5 +3,31 @@
|
|||||||
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
|
// Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
|
||||||
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
|
// Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": []
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Запустить",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/ntsys",
|
||||||
|
"args": ["--asm", "${workspaceFolder}/example.asm"],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${fileDirname}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": true,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Включить автоматическое форматирование для gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Задать для варианта приложения дизассемблирования значение Intel",
|
||||||
|
"text": "-gdb-set disassembly-flavor intel",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
]
|
||||||
}
|
}
|
||||||
0
.vscode/out.exe
vendored
Normal file
0
.vscode/out.exe
vendored
Normal file
72
asm-savers.h
72
asm-savers.h
@ -1,7 +1,8 @@
|
|||||||
#if (!defined(NTSYS__S_ASSEMBLER_H___))
|
#if (!defined(NTSYS__S_ASSEMBLER_H___))
|
||||||
#define NTSYS__S_ASSEMBLER_H___
|
#define NTSYS__S_ASSEMBLER_H___
|
||||||
|
|
||||||
#define create_ntsys_asm_parser(cache, fp, str, table) ((ntsys_argument_parser_t){(cache), (fp), (str), (table)})
|
#define ntsys_run_asm_parser(ss) __ntsys_arg_function_loader(&(ss))
|
||||||
|
#define create_ntsys_asm_parser(cache, fp, str, table, table_size, labels) ((ntsys_argument_parser_t){(cache), (fp), (str), (table), (table_size), (labels), (labels)})
|
||||||
#define create_ntsys_asm_save(ss) ((ntsys_asm_save_t){(ss), (ss)})
|
#define create_ntsys_asm_save(ss) ((ntsys_asm_save_t){(ss), (ss)})
|
||||||
#define create_ntsys_asm_cache(ss) ((ntsys_asm_cache_t)&(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_new(str, __cache) ((ntsys_get_arg((__cache))) && ((str) = __cache->string))
|
||||||
@ -83,7 +84,7 @@ char* ntsys_get_arg(ntsys_asm_cache_t save) {
|
|||||||
save->cache = &str[n_p + 1];
|
save->cache = &str[n_p + 1];
|
||||||
str[n_p] = '\0';
|
str[n_p] = '\0';
|
||||||
}
|
}
|
||||||
if (strlen(str) == 1 || strlen(str) == 0) {
|
if (strlen(str) == 0) {
|
||||||
str = NULL;
|
str = NULL;
|
||||||
save->string = NULL;
|
save->string = NULL;
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -126,17 +127,76 @@ char* ntsys_get_arg(ntsys_asm_cache_t save) {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef char** ntsys_labels_array_t;
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ntsys_asm_cache_t cache;
|
ntsys_asm_cache_t cache;
|
||||||
FILE* fp;
|
FILE* fp;
|
||||||
char* string;
|
char* string;
|
||||||
char** table;
|
char** table;
|
||||||
|
size_t table_length;
|
||||||
|
char** labels;
|
||||||
} ntsys_argument_parser_t;
|
} ntsys_argument_parser_t;
|
||||||
|
|
||||||
int ntsys_run_asm_parser(ntsys_argument_parser_t parser) {
|
static void __input_datatype(ntsys_argument_parser_t* parser, byte_t* mode) {
|
||||||
char* str = parser.string;
|
|
||||||
while (ntsys_asm_arg_new(str, parser.cache)) {
|
}
|
||||||
puts(str);
|
|
||||||
|
static void __input_data(ntsys_argument_parser_t* parser, byte_t* mode) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int __arg_writer(char* str, ntsys_argument_parser_t* parser, byte_t mode) {
|
||||||
|
if (str[0] == ':') {
|
||||||
|
if (str[1] == 'T') {
|
||||||
|
__input_datatype(parser, &mode);
|
||||||
|
} else if (str[1] == 'D') {
|
||||||
|
__input_data(parser, &mode);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
byte_t x = strtol(str, NULL, 16);
|
||||||
|
fputc((char)x, parser->fp);
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int select_argument(char* str, ntsys_argument_parser_t* parser) {
|
||||||
|
size_t i = 0;
|
||||||
|
char buf[64];
|
||||||
|
char** table = parser->table;
|
||||||
|
while (i < parser->table_length) {
|
||||||
|
size_t t = 0;
|
||||||
|
while (t < strlen(table[i])) {
|
||||||
|
if (table[i][t] == '=') break;
|
||||||
|
t ++;
|
||||||
|
}
|
||||||
|
strncpy(buf, table[i], t);
|
||||||
|
buf[t] = '\0';
|
||||||
|
if (strcmp(str, buf) == 0) {
|
||||||
|
char* se = &table[i][t + 1];
|
||||||
|
byte_t internal = 0x00;
|
||||||
|
while (strlen(se) > 1) {
|
||||||
|
char is[3];
|
||||||
|
strncpy(is, se, 2);
|
||||||
|
is[2] = '\0';
|
||||||
|
if (__arg_writer(is, parser, internal) == EXIT_FAILURE) {
|
||||||
|
ntsys_error("Internal program error!");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
se += sizeof(char) * 2;
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
i ++;
|
||||||
|
}
|
||||||
|
ntsys_error("Undefined operator!");
|
||||||
|
/* Обязательно заменить на EXIT_FAILURE! EXIT_SUCCESS нужен для отладки! */
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int __ntsys_arg_function_loader(ntsys_argument_parser_t* parser) {
|
||||||
|
char* str = parser->string;
|
||||||
|
while (ntsys_asm_arg_new(str, parser->cache)) {
|
||||||
|
if (select_argument(str, parser) == EXIT_FAILURE) return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,15 +5,24 @@
|
|||||||
|
|
||||||
#include "asm-savers.h"
|
#include "asm-savers.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* :T - тип данных
|
||||||
|
* :D - данные
|
||||||
|
* XX - шестнадцатиричные данные
|
||||||
|
*/
|
||||||
|
|
||||||
|
const size_t ntsys_command_count = 1;
|
||||||
|
|
||||||
char* ntsys_asm_table[] = {
|
char* ntsys_asm_table[] = {
|
||||||
"print"
|
"push=00:T:D"
|
||||||
};
|
};
|
||||||
|
|
||||||
int ntsys_asm_from_string(size_t* mempos, char* str, FILE* fp) {
|
int ntsys_asm_from_string(size_t* mempos, char* str, FILE* fp) {
|
||||||
|
ntsys_labels_array_t labels = NULL;
|
||||||
ntsys_asm_save_t __save = create_ntsys_asm_save(str);
|
ntsys_asm_save_t __save = create_ntsys_asm_save(str);
|
||||||
ntsys_asm_cache_t __cache = create_ntsys_asm_cache(__save);
|
ntsys_asm_cache_t __cache = create_ntsys_asm_cache(__save);
|
||||||
ntsys_argument_parser_t __parser = create_ntsys_asm_parser(__cache, fp, str, ntsys_asm_table);
|
ntsys_argument_parser_t parser = create_ntsys_asm_parser(__cache, fp, str, ntsys_asm_table, ntsys_command_count, labels);
|
||||||
return ntsys_run_asm_parser(__parser);
|
return ntsys_run_asm_parser(parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ntsys_compile_asm(char* filename, char* asm_out) {
|
int ntsys_compile_asm(char* filename, char* asm_out) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user