From 4f7f0aa49844756dbf430f35600d7c88e1a6a730 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Sun, 23 Mar 2025 23:37:25 +0800 Subject: refactor names --- src/as_analyzer.c | 33 ++++++++------ src/as_analyzer.h | 10 +++-- src/as_main.c | 8 ++-- src/as_op.c | 10 ++--- src/as_parser.c | 109 +++++++++++++++++++++++----------------------- src/as_parser.h | 38 ++++++++-------- src/as_tokenizer.c | 76 ++++++++++++++++---------------- src/as_tokenizer.h | 28 ++++++------ src/main.c | 7 +-- src/utils.c | 8 ++-- src/utils.h | 8 ++-- tests/test_as_parser.c | 14 +++--- tests/test_as_tokenizer.c | 28 ++++++------ 13 files changed, 193 insertions(+), 184 deletions(-) diff --git a/src/as_analyzer.c b/src/as_analyzer.c index 39616da..dbc1bd2 100644 --- a/src/as_analyzer.c +++ b/src/as_analyzer.c @@ -3,31 +3,31 @@ #include #include -struct symTableEntry { +struct sym_table_entry { const char * name; size_t offset; }; -const char * composeSectionLabel(Allocator alct, const char * section, const char * name) { - size_t sectionLen = strlen(section); - size_t nameLen = strlen(name); - size_t sz = sectionLen + nameLen; +const char * compose_section_label(allocator_t alct, const char * section, const char * name) { + size_t section_len = strlen(section); + size_t name_len = strlen(name); + size_t sz = section_len + name_len; char * buf = allocate(alct, sz + 1); - memcpy(buf, section, sectionLen); - memcpy(buf + sectionLen, name, nameLen); + memcpy(buf, section, section_len); + memcpy(buf + section_len, name, name_len); buf[sz] = '\0'; return buf; } -void processSectionLabel(Allocator alct, Prog prog) { +void process_section_label(allocator_t alct, prog_t prog) { const char * section = ""; - Stmt* stmts = prog->stmts->stmts; + stmt_t* stmts = prog->stmts->stmts; for (size_t i = 0; ; i++) { if (stmts[i] == NULL) break; if (stmts[i]->label == NULL) continue; const char* name = stmts[i]->label->name; if (name[0] == '.') { - stmts[i]->label->name = composeSectionLabel(alct, section, name); + stmts[i]->label->name = compose_section_label(alct, section, name); } else { section = name; continue; @@ -35,14 +35,19 @@ void processSectionLabel(Allocator alct, Prog prog) { } } -size_t instrSize(Instr instr) { +size_t instr_size(instr_t instr) { // TODO return 0; } -SymTableEntry* analyzeProg(Allocator alct, Prog prog) { - processSectionLabel(alct, prog); +struct sym_table analyze_prog(allocator_t alct, prog_t prog) { + process_section_label(alct, prog); + stmt_t * stmts = prog->stmts->stmts; + for (int i = 0; ; i++) { + if (stmts[i] == NULL) break; + } + struct sym_table tbl; // TODO - return NULL; + return tbl; } diff --git a/src/as_analyzer.h b/src/as_analyzer.h index 10cce38..793d14a 100644 --- a/src/as_analyzer.h +++ b/src/as_analyzer.h @@ -4,9 +4,13 @@ #include "as_parser.h" #include "utils.h" -struct symTableEntry; -typedef struct symTableEntry * SymTableEntry; +struct sym_table_entry; +struct sym_table { + int size; + int cap; + struct sym_table_entry *buf; +}; -SymTableEntry * analyzeProg(Allocator alct, Prog prog); +struct sym_table analyze_prog(allocator_t alct, prog_t prog); #endif // FVM_AS_ANALYZER_H_ diff --git a/src/as_main.c b/src/as_main.c index 2809c59..dc61ea9 100644 --- a/src/as_main.c +++ b/src/as_main.c @@ -5,14 +5,14 @@ int main(int argc, char** argv) { if (argc != 2) { - fprintf(stderr, "Usage: fvm-as \n"); + fprintf(stderr, "usage: fvm-as \n"); return 1; } - Allocator alct = newAllocator(); + allocator_t alct = new_allocator(); FILE *fp = fopen(argv[1], "r"); - TokenStream ts = makeTokenStream(alct, fp); + token_stream_t ts = new_token_stream(alct, fp); - deleteAllocator(alct); + delete_allocator(alct); return 0; } diff --git a/src/as_op.c b/src/as_op.c index 6cfd8ce..8b5d887 100644 --- a/src/as_op.c +++ b/src/as_op.c @@ -2,13 +2,13 @@ #include -struct opTableEntry{ +struct op_table_entry { enum op op; const char* name; }; -struct opTableEntry opTable[] = { +struct op_table_entry op_table [] = { // OP_SP, OP_SSP, OP_BP, OP_SBP, OP_PC, OP_RV, OP_SRV, {OP_SP, "sp"}, {OP_SSP, "ssp"}, @@ -93,9 +93,9 @@ struct opTableEntry opTable[] = { }; enum op str2op(const char* str) { - for (int i = 0; opTable[i].name != NULL; i++) { - if (strcmp(opTable[i].name, str) == 0) { - return opTable[i].op; + for (int i = 0; op_table[i].name != NULL; i++) { + if (strcmp(op_table[i].name, str) == 0) { + return op_table[i].op; } } return OP_END; diff --git a/src/as_parser.c b/src/as_parser.c index 17d0b87..1044e3f 100644 --- a/src/as_parser.c +++ b/src/as_parser.c @@ -16,34 +16,33 @@ //