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 /src/as_parser.c | |
| parent | 45e53e55aa555d5a53d9d489e8447e9112eac1f0 (diff) | |
refactor names
Diffstat (limited to 'src/as_parser.c')
| -rw-r--r-- | src/as_parser.c | 109 |
1 files changed, 54 insertions, 55 deletions
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; |
