diff options
| author | Mistivia <i@mistivia.com> | 2025-03-23 23:37:25 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-03-23 23:37:25 +0800 |
| commit | 4f7f0aa49844756dbf430f35600d7c88e1a6a730 (patch) | |
| tree | 7b3fcc235f33ebec5c7623b6cc3946a138162682 | |
| parent | 45e53e55aa555d5a53d9d489e8447e9112eac1f0 (diff) | |
refactor names
| -rw-r--r-- | src/as_analyzer.c | 33 | ||||
| -rw-r--r-- | src/as_analyzer.h | 10 | ||||
| -rw-r--r-- | src/as_main.c | 8 | ||||
| -rw-r--r-- | src/as_op.c | 10 | ||||
| -rw-r--r-- | src/as_parser.c | 109 | ||||
| -rw-r--r-- | src/as_parser.h | 38 | ||||
| -rw-r--r-- | src/as_tokenizer.c | 76 | ||||
| -rw-r--r-- | src/as_tokenizer.h | 28 | ||||
| -rw-r--r-- | src/main.c | 7 | ||||
| -rw-r--r-- | src/utils.c | 8 | ||||
| -rw-r--r-- | src/utils.h | 8 | ||||
| -rw-r--r-- | tests/test_as_parser.c | 14 | ||||
| -rw-r--r-- | 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 <stddef.h> #include <string.h> -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 <inputfile>\n"); + fprintf(stderr, "usage: fvm-as <inputfile>\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 <string.h> -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 @@ // <label> ::= tag ":" // <op> ::= "add" | "sub" | "mul" | "div" | "mod" | "eq" | ... - -Prog parseProg(Allocator alct, TokenStream ts) { - Prog p = allocate(alct, sizeof(struct prog)); - p->stmts = parseStmts(alct, ts); +prog_t parse_prog(allocator_t alct, token_stream_t ts) { + prog_t p = allocate(alct, sizeof(struct prog)); + p->stmts = parse_stmts(alct, ts); return p; } -Stmts parseStmts(Allocator alct, TokenStream ts) { - Stmts ss = allocate(alct, sizeof(struct stmts)); - ss->stmts = allocate(alct, sizeof(Stmt)); +stmts_t parse_stmts(allocator_t alct, token_stream_t ts) { + stmts_t ss = allocate(alct, sizeof(struct stmts)); + ss->stmts = allocate(alct, sizeof(stmt_t)); ss->stmts[0] = NULL; int capacity = 0; int len = 0; - while (peekToken(alct, ts)->type != ENDOFFILE) { - Stmt s = parseStmt(alct, ts); + while (peek_token(alct, ts)->type != TK_ENDOFFILE) { + stmt_t s = parse_stmt(alct, ts); if (s == NULL) continue; if (len == capacity) { - size_t newCapacity = capacity * 2 + 1; - void* newStmts = allocate(alct, sizeof(Stmt*) * newCapacity); - memcpy(newStmts, ss->stmts, sizeof(Stmt*) * capacity); - ss->stmts = newStmts; - capacity = newCapacity; + size_t new_capacity = capacity * 2 + 1; + void* new_stmts = allocate(alct, sizeof(stmt_t*) * new_capacity); + memcpy(new_stmts, ss->stmts, sizeof(stmt_t*) * capacity); + ss->stmts = new_stmts; + capacity = new_capacity; } // expect newline - if (peekToken(alct, ts)->type == NEWLINE) { - nextToken(alct, ts); + if (peek_token(alct, ts)->type == TK_NEWLINE) { + next_token(alct, ts); } else { - fprintf(stderr, "%d:%d Expect NEWLINE.\n", peekToken(alct, ts)->line, peekToken(alct, ts)->col); + fprintf(stderr, "%d:%d expect newline.\n", peek_token(alct, ts)->line, peek_token(alct, ts)->col); } ss->stmts[len] = s; len++; @@ -52,81 +51,81 @@ Stmts parseStmts(Allocator alct, TokenStream ts) { return ss; } -Label parseLabel(Allocator alct, TokenStream ts) { - Token t = nextToken(alct, ts); - if (t->type != TAG) { - fprintf(stderr, "%d:%d Expect LABEL.\n", t->line, t->col); +label_t parse_label(allocator_t alct, token_stream_t ts) { + token_t t = next_token(alct, ts); + if (t->type != TK_TAG) { + fprintf(stderr, "%d:%d expect label.\n", t->line, t->col); exit(-1); } - Label l = allocate(alct, sizeof(Label)); + label_t l = allocate(alct, sizeof(label_t)); l->name = t->sval; - t = nextToken(alct, ts); - if (t->type != COLON) { - fprintf(stderr, "%d:%d Expect COLON.\n", t->line, t->col); + t = next_token(alct, ts); + if (t->type != TK_COLON) { + fprintf(stderr, "%d:%d expect colon.\n", t->line, t->col); exit(-1); } return l; } -Stmt parseStmt(Allocator alct, TokenStream ts) { - Token t = peekToken(alct, ts); - Stmt stmt = allocate(alct, sizeof(struct stmt)); +stmt_t parse_stmt(allocator_t alct, token_stream_t ts) { + token_t t = peek_token(alct, ts); + stmt_t stmt = allocate(alct, sizeof(struct stmt)); stmt->label = NULL; stmt->instr = NULL; - if (t->type == TAG) { - stmt->label = parseLabel(alct, ts); - if (peekToken(alct, ts)->type == NEWLINE) { + if (t->type == TK_TAG) { + stmt->label = parse_label(alct, ts); + if (peek_token(alct, ts)->type == TK_NEWLINE) { return stmt; } - t = peekToken(alct, ts); + t = peek_token(alct, ts); } - if (t->type == OP) { - stmt->instr = parseInstr(alct, ts); - if (peekToken(alct, ts)->type == NEWLINE) { + if (t->type == TK_OP) { + stmt->instr = parse_instr(alct, ts); + if (peek_token(alct, ts)->type == TK_NEWLINE) { return stmt; } } - if (t->type == NEWLINE) { + if (t->type == TK_NEWLINE) { return NULL; } - fprintf(stderr, "%d:%d Expect lable + instruction, lable, or instruction.\n", t->line, t->col); + fprintf(stderr, "%d:%d expect lable + instruction, lable, or instruction.\n", t->line, t->col); exit(-1); } -enum op parseOp(Allocator alct, TokenStream ts) { - Token t = nextToken(alct, ts); +enum op parse_op(allocator_t alct, token_stream_t ts) { + token_t t = next_token(alct, ts); enum op op; - if (t->type == OP) { + if (t->type == TK_OP) { op = str2op(t->sval); if (op == OP_END) { - fprintf(stderr, "%d:%d Invalid OP.\n", t->line, t->col); + fprintf(stderr, "%d:%d invalid op.\n", t->line, t->col); exit(-1); } } else { - fprintf(stderr, "%d:%d Expect OP.\n", t->line, t->col); + fprintf(stderr, "%d:%d expect op.\n", t->line, t->col); exit(-1); } return op; } -Instr parseInstr(Allocator alct, TokenStream ts) { - Token t = peekToken(alct, ts); - Instr i = allocate(alct, sizeof(struct instr)); - i->tagName = NULL; +instr_t parse_instr(allocator_t alct, token_stream_t ts) { + token_t t = peek_token(alct, ts); + instr_t i = allocate(alct, sizeof(struct instr)); + i->tag_name = NULL; i->arg = NULL; i->op = OP_END; - if (t->type == OP) { - i->op = parseOp(alct, ts); - t = peekToken(alct, ts); - if (t->type == ARG) { - Arg a = allocate(alct, sizeof(struct arg)); + if (t->type == TK_OP) { + i->op = parse_op(alct, ts); + t = peek_token(alct, ts); + if (t->type == TK_ARG) { + arg_t a = allocate(alct, sizeof(struct arg)); a->ival = t->ival; a->fval = t->fval; i->arg = a; - nextToken(alct, ts); - } else if (t->type == TAG) { - i->tagName = t->sval; - nextToken(alct, ts); + next_token(alct, ts); + } else if (t->type == TK_TAG) { + i->tag_name = t->sval; + next_token(alct, ts); } } return i; diff --git a/src/as_parser.h b/src/as_parser.h index a3db024..0c31df7 100644 --- a/src/as_parser.h +++ b/src/as_parser.h @@ -10,41 +10,41 @@ struct arg { int64_t ival; double fval; }; -typedef struct arg * Arg; +typedef struct arg * arg_t; struct instr { enum op op; - Arg arg; - const char* tagName; + arg_t arg; + const char* tag_name; }; -typedef struct instr *Instr; +typedef struct instr * instr_t; struct label { const char* name; }; -typedef struct label *Label; +typedef struct label * label_t; struct stmt { - Label label; - Instr instr; + label_t label; + instr_t instr; }; -typedef struct stmt *Stmt; +typedef struct stmt * stmt_t; struct stmts { - Stmt* stmts; + stmt_t * stmts; }; -typedef struct stmts *Stmts; +typedef struct stmts * stmts_t; struct prog { - Stmts stmts; + stmts_t stmts; }; -typedef struct prog *Prog; - -Prog parseProg(Allocator alct, TokenStream ts); -Stmt parseStmt(Allocator alct, TokenStream ts); -Stmts parseStmts(Allocator alct, TokenStream ts); -Instr parseInstr(Allocator alct, TokenStream ts); -Label parseLabel(Allocator alct, TokenStream ts); -enum op parseOp(Allocator alct, TokenStream ts); +typedef struct prog * prog_t; + +prog_t parse_prog(allocator_t alct, token_stream_t ts); +stmt_t parse_stmt(allocator_t alct, token_stream_t ts); +stmts_t parse_stmts(allocator_t alct, token_stream_t ts); +instr_t parse_instr(allocator_t alct, token_stream_t ts); +label_t parse_label(allocator_t alct, token_stream_t ts); +enum op parse_op(allocator_t alct, token_stream_t ts); #endif diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c index 4a5353a..54e0969 100644 --- a/src/as_tokenizer.c +++ b/src/as_tokenizer.c @@ -6,7 +6,7 @@ #include "as_op.h" #include "utils.h" -int InputStream_nextChar(InputStream s) { +int input_stream_next_char(input_stream_t s) { if (s->cursor == -1) { return EOF; } @@ -30,7 +30,7 @@ int InputStream_nextChar(InputStream s) { return c; } -int InputStream_peekChar(InputStream s) { +int input_stream_peek_char(input_stream_t s) { if (s->cursor == -1) { return EOF; } @@ -45,7 +45,7 @@ int InputStream_peekChar(InputStream s) { return s->buf[s->cursor]; } -int isStartOfIndentifier(int c) { +int is_start_of_identifier(int c) { if (c >= 'a' && c <= 'z') { return 1; } @@ -58,8 +58,8 @@ int isStartOfIndentifier(int c) { return 0; } -int isPartOfIndentifier(int c) { - if (isStartOfIndentifier(c)) { +int is_part_of_identifier(int c) { + if (is_start_of_identifier(c)) { return 1; } if (c >= '0' && c <= '9') { @@ -68,42 +68,42 @@ int isPartOfIndentifier(int c) { return 0; } -Token nextTokenImpl(Allocator alct, InputStream s) { - Token t = allocate(alct, sizeof(struct token)); +token_t next_token_impl(allocator_t alct, input_stream_t s) { + token_t t = allocate(alct, sizeof(struct token)); int c; while (1) { - c = InputStream_peekChar(s); + c = input_stream_peek_char(s); if (c == EOF) { break; } if (c == '\n') { - InputStream_nextChar(s); - *t = (struct token){.type = NEWLINE, .line = s->line, .col = s->col}; + input_stream_next_char(s); + *t = (struct token){.type = TK_NEWLINE, .line = s->line, .col = s->col}; return t; } if (c == ':') { - InputStream_nextChar(s); - *t = (struct token){.type = COLON, .line = s->line, .col = s->col}; + input_stream_next_char(s); + *t = (struct token){.type = TK_COLON, .line = s->line, .col = s->col}; return t; } if (c == ' ' || c == '\t') { - InputStream_nextChar(s); + input_stream_next_char(s); continue; } if (c >= '0' && c <= '9') { int64_t ival = 0; while (1) { - InputStream_nextChar(s); + input_stream_next_char(s); ival = ival * 10 + (c - '0'); - c = InputStream_peekChar(s); + c = input_stream_peek_char(s); if (c < '0' || c > '9') { break; } } - *t = (struct token){.type = ARG, .ival = ival, .line = s->line, .col = s->col}; + *t = (struct token){.type = TK_ARG, .ival = ival, .line = s->line, .col = s->col}; return t; } - if (isStartOfIndentifier(c)) { + if (is_start_of_identifier(c)) { size_t line = s->line; size_t col = s->col; char *sval = allocate(alct, 256); @@ -113,78 +113,78 @@ Token nextTokenImpl(Allocator alct, InputStream s) { fprintf(stderr, "error: identifier too long\n"); exit(1); } - InputStream_nextChar(s); + input_stream_next_char(s); sval[i++] = c; - c = InputStream_peekChar(s); - if (!isPartOfIndentifier(c)) { + c = input_stream_peek_char(s); + if (!is_part_of_identifier(c)) { break; } } sval[i] = '\0'; if (isOp(sval)) { - *t = (struct token){.type = OP, .sval = sval, .line = line, .col = col}; + *t = (struct token){.type = TK_OP, .sval = sval, .line = line, .col = col}; return t; } - *t = (struct token){.type = TAG, .sval = sval, .line = line, .col = col}; + *t = (struct token){.type = TK_TAG, .sval = sval, .line = line, .col = col}; return t; } fprintf(stderr, "error: invalid character %c at line %d, col %d\n", c, s->line, s->col); } // end of file - *t = (struct token){.type = ENDOFFILE}; + *t = (struct token){.type = TK_ENDOFFILE}; return t; } -Token nextToken(Allocator alct, TokenStream ts) { +token_t next_token(allocator_t alct, token_stream_t ts) { if (ts->buf != NULL) { - Token t = ts->buf; + token_t t = ts->buf; ts->buf = NULL; return t; } - Token t = nextTokenImpl(alct, ts->s); + token_t t = next_token_impl(alct, ts->s); return t; } -Token peekToken(Allocator alct, TokenStream ts) { +token_t peek_token(allocator_t alct, token_stream_t ts) { if (ts->buf != NULL) { return ts->buf; } - ts->buf = nextTokenImpl(alct, ts->s); + ts->buf = next_token_impl(alct, ts->s); return ts->buf; } -void printToken(Token t) { +void print_token(token_t t) { switch (t->type) { - case OP: + case TK_OP: printf("OP: %s, line: %d, col: %d\n", t->sval, t->line, t->col); break; - case ARG: + case TK_ARG: printf("ARG: %ld, line: %d, col: %d\n", t->ival, t->line, t->col); break; - case TAG: + case TK_TAG: printf("LABEL: %s, line: %d, col: %d\n", t->sval, t->line, t->col); break; - case COLON: + case TK_COLON: printf("COLON\n"); break; - case NEWLINE: + case TK_NEWLINE: printf("NEWLINE\n"); break; - case ENDOFFILE: + case TK_ENDOFFILE: printf("ENDOFFILE\n"); break; } } -TokenStream makeTokenStream(Allocator alct, FILE* fp) { - InputStream s = allocate(alct, sizeof(struct inputStream)); +token_stream_t new_token_stream(allocator_t alct, FILE* fp) { + input_stream_t s = allocate(alct, sizeof(struct input_stream)); s->fp = fp; s->buf = allocate(alct, INPUT_STREAM_BUF_SIZE); s->buf_pos = 0; s->cursor = 0; s->line = 1; s->col = 1; - TokenStream ts = allocate(alct, sizeof(struct tokenStream)); + token_stream_t ts = allocate(alct, sizeof(struct token_stream)); ts->s = s; ts->buf = NULL; return ts; diff --git a/src/as_tokenizer.h b/src/as_tokenizer.h index e9025ba..72a5d8b 100644 --- a/src/as_tokenizer.h +++ b/src/as_tokenizer.h @@ -6,23 +6,23 @@ #include "utils.h" -enum tokenType { - OP, ARG, TAG, COLON, NEWLINE, ENDOFFILE +enum token_type { + TK_OP, TK_ARG, TK_TAG, TK_COLON, TK_NEWLINE, TK_ENDOFFILE }; struct token { - enum tokenType type; + enum token_type type; int line; int col; char *sval; int64_t ival; double fval; }; -typedef struct token *Token; +typedef struct token * token_t; #define INPUT_STREAM_BUF_SIZE 1024 -struct inputStream { +struct input_stream{ FILE *fp; char *buf; int buf_pos; @@ -30,17 +30,17 @@ struct inputStream { int line; int col; }; -typedef struct inputStream *InputStream; +typedef struct input_stream *input_stream_t; -struct tokenStream { - Token buf; - InputStream s; +struct token_stream { + token_t buf; + input_stream_t s; }; -typedef struct tokenStream *TokenStream; +typedef struct token_stream *token_stream_t; -Token nextToken(Allocator alct, TokenStream ts); -Token peekToken(Allocator alct, TokenStream ts); -void printToken(Token t); -TokenStream makeTokenStream(Allocator alct, FILE* fp); +token_t next_token(allocator_t alct, token_stream_t ts); +token_t peek_token(allocator_t alct, token_stream_t ts); +void print_token(token_t t); +token_stream_t new_token_stream(allocator_t alct, FILE* fp); #endif // FMV_AS_TOKENIZER_H_ @@ -1,11 +1,12 @@ #include <stdio.h> #include <stdlib.h> +#include <inttypes.h> #include "fvm.h" -static void printnum(struct fvm *vm) { +static void print_num(struct fvm *vm) { int64_t n = fvm_pop(vm); - printf("%lld\n", n); + printf("%" PRId64 "\n", n); } int main(int argc, char **argv) { @@ -15,7 +16,7 @@ int main(int argc, char **argv) { return -1; } vm.syscall_table = malloc(256 * sizeof(fvm_syscall_fn_t)); - vm.syscall_table[1] = &printnum; + vm.syscall_table[1] = &print_num; FILE* fp = fopen(argv[1], "rb"); if (fp == NULL) { fprintf(stderr, "Failed to open file.\n"); diff --git a/src/utils.c b/src/utils.c index 1baf283..b9e7eaf 100644 --- a/src/utils.c +++ b/src/utils.c @@ -9,8 +9,8 @@ struct allocator { size_t len; }; -Allocator newAllocator() { - Allocator alct = malloc(sizeof(struct allocator)); +allocator_t new_allocator() { + allocator_t alct = malloc(sizeof(struct allocator)); alct->bufs = malloc(sizeof(void*) * 16); alct->cap = 16; alct->len = 0; @@ -18,7 +18,7 @@ Allocator newAllocator() { return alct; } -void deleteAllocator(Allocator alct) { +void delete_allocator(allocator_t alct) { for (size_t i = 0; i < alct->len; i++) { free(alct->bufs[i]); } @@ -26,7 +26,7 @@ void deleteAllocator(Allocator alct) { free(alct); } -void * allocate(Allocator alct, size_t size) { +void * allocate(allocator_t alct, size_t size) { assert(size > 0); if (alct->len >= alct->cap) { alct->cap = alct->cap * 2; // Doubling the capacity diff --git a/src/utils.h b/src/utils.h index af5e820..1a88220 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,11 +4,11 @@ #include <stdlib.h> struct allocator; -typedef struct allocator *Allocator; +typedef struct allocator * allocator_t; -Allocator newAllocator(); -void deleteAllocator(Allocator allocator); +allocator_t new_allocator(); +void delete_allocator(allocator_t allocator); -void* allocate(Allocator allocator, size_t size); +void* allocate(allocator_t allocator, size_t size); #endif // FVM_UTILS_H_ diff --git a/tests/test_as_parser.c b/tests/test_as_parser.c index e71f190..1cf26ce 100644 --- a/tests/test_as_parser.c +++ b/tests/test_as_parser.c @@ -6,7 +6,7 @@ #include "as_parser.h" #include "utils.h" -char *inputBuffer = +char *input_buffer = "start:\n" " add 1\n" " sub start\n" @@ -16,13 +16,13 @@ char *inputBuffer = int main(int argc, char** argv) { printf("[TEST] assembler parser\n"); // make a memory buffer to FILE* - FILE *fp = fmemopen(inputBuffer, strlen(inputBuffer), "r"); - Allocator alct = newAllocator(); - TokenStream ts = makeTokenStream(alct, fp); - Prog prog = parseProg(alct, ts); + FILE *fp = fmemopen(input_buffer, strlen(input_buffer), "r"); + allocator_t alct = new_allocator(); + token_stream_t ts = new_token_stream(alct, fp); + prog_t prog = parse_prog(alct, ts); // compare output - Stmt* stmts = prog->stmts->stmts; + stmt_t * stmts = prog->stmts->stmts; assert(stmts[0]->instr == NULL); assert(strcmp("start", stmts[0]->label->name) == 0); @@ -35,6 +35,6 @@ int main(int argc, char** argv) { assert(stmts[4]->instr->op == OP_EQ); printf("[PASS] assembler parser\n"); - deleteAllocator(alct); + delete_allocator(alct); return 0; } diff --git a/tests/test_as_tokenizer.c b/tests/test_as_tokenizer.c index 4f5aac6..f791324 100644 --- a/tests/test_as_tokenizer.c +++ b/tests/test_as_tokenizer.c @@ -6,14 +6,14 @@ #include "as_tokenizer.h" #include "utils.h" -char *inputBuffer = +char *input_buffer = "start:\n" " add 1\n" " sub start\n" " div\n" " eq\n"; -char *expectedOutput = +char *expected_output = "LABEL: start, line: 1, col: 1\n" "COLON\n" "NEWLINE\n" @@ -31,26 +31,26 @@ char *expectedOutput = int main(int argc, char** argv) { printf("[TEST] assembler tokenizer\n"); // make a memory buffer to FILE* - FILE *fp = fmemopen(inputBuffer, strlen(inputBuffer), "r"); - Allocator alct = newAllocator(); - TokenStream ts = makeTokenStream(alct, fp); + FILE *fp = fmemopen(input_buffer, strlen(input_buffer), "r"); + allocator_t alct = new_allocator(); + token_stream_t ts = new_token_stream(alct, fp); - char *outputBuffer = malloc(10240); + char *output_buffer = malloc(10240); // redirect stdout to a file - FILE *out = fmemopen(outputBuffer, 10240, "w"); + FILE *out = fmemopen(output_buffer, 10240, "w"); FILE *origin_stdout = stdout; stdout = out; - while (peekToken(alct, ts)->type != ENDOFFILE) { - printToken(peekToken(alct, ts)); - nextToken(alct, ts); + while (peek_token(alct, ts)->type != TK_ENDOFFILE) { + print_token(peek_token(alct, ts)); + next_token(alct, ts); } fclose(out); stdout = origin_stdout; - // compare outputBuffer with expectedOutput - assert(strcmp(outputBuffer, expectedOutput) == 0); + + assert(strcmp(output_buffer, expected_output) == 0); printf("[PASS] assembler tokenizer\n"); - free(outputBuffer); - deleteAllocator(alct); + free(output_buffer); + delete_allocator(alct); return 0; } |
