diff options
| author | Mistivia <i@mistivia.com> | 2025-03-25 17:59:11 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-03-25 17:59:11 +0800 |
| commit | 93d6f231d59b413b091b7e15a8af246a8b105c65 (patch) | |
| tree | c6c239e6b0cc8d893561df6435db578bfb7b23a2 /src/as_parser.c | |
| parent | 39e2a605f6d8ebcc3cb454daae3d0a4298df2eb6 (diff) | |
make code shorter
Diffstat (limited to 'src/as_parser.c')
| -rw-r--r-- | src/as_parser.c | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/as_parser.c b/src/as_parser.c index a68fbba..4205179 100644 --- a/src/as_parser.c +++ b/src/as_parser.c @@ -16,18 +16,18 @@ // <label> ::= tag ":" // <op> ::= "add" | "sub" | "mul" | "div" | "mod" | "eq" | ... -struct result parse_prog(struct allocator * alct, struct token_stream * ts) { +result parse_prog(allocator* alct, token_stream* ts) { struct prog * p = allocate(alct, sizeof(struct prog)); p->stmts = unwrap(parse_stmts(alct, ts)); return ok(p); } -struct result parse_stmts(struct allocator * alct, struct token_stream * ts) { - struct token *token; - struct stmts * ss = allocate(alct, sizeof(struct stmts)); - struct stmt * s; +result parse_stmts(allocator* alct, token_stream* ts) { + token *token; + stmts* ss = allocate(alct, sizeof(stmts)); + stmt* s; - ss->stmts = allocate(alct, sizeof(struct stmt *)); + ss->stmts = allocate(alct, sizeof(stmt*)); ss->stmts[0] = NULL; int capacity = 0; int len = 0; @@ -61,7 +61,7 @@ struct result parse_stmts(struct allocator * alct, struct token_stream * ts) { return ok(ss); } -struct result parse_label(struct allocator * alct, struct token_stream * ts) { +result parse_label(allocator* alct, token_stream* ts) { struct token * t; t = unwrap(next_token(alct, ts)); if (t->type != TK_TAG) { @@ -76,11 +76,11 @@ struct result parse_label(struct allocator * alct, struct token_stream * ts) { return ok(l); } -struct result parse_stmt(struct allocator * alct, struct token_stream * ts) { +result parse_stmt(allocator* alct, token_stream* ts) { const char *errmsg; - struct token * token; + token* token; token = unwrap(peek_token(alct, ts)); - struct stmt * stmt = allocate(alct, sizeof(struct stmt)); + stmt* stmt = allocate(alct, sizeof(struct stmt)); stmt->label = NULL; stmt->instr = NULL; if (token->type == TK_TAG) { @@ -104,8 +104,8 @@ struct result parse_stmt(struct allocator * alct, struct token_stream * ts) { return err(safe_sprintf(alct, "%d:%d expect lable + instruction, lable, or instruction.\n", token->line, token->col)); } -struct result parse_op(struct allocator * alct, struct token_stream * ts) { - struct token * t; +result parse_op(allocator* alct, token_stream* ts) { + token* t; t = unwrap(next_token(alct, ts)); enum op op; if (t->type == TK_OP) { @@ -119,18 +119,22 @@ struct result parse_op(struct allocator * alct, struct token_stream * ts) { return ok((void*)op); } -struct result parse_instr(struct allocator * alct, struct token_stream * ts) { - struct token * t; +result parse_instr(allocator* alct, token_stream* ts) { + token* t; t = unwrap(peek_token(alct, ts)); - struct instr * i = allocate(alct, sizeof(struct instr)); - i->tag_name = NULL; - i->arg = NULL; - i->op = OP_END; + instr * i = allocate(alct, sizeof(instr)); + *i = (instr){ + .tag_name = NULL, + .arg = NULL, + .op = OP_END, + .lineno = -1 + }; if (t->type == TK_OP) { - i->op = (enum op)unwrap(parse_op(alct, ts)); + i->lineno = t->line; + i->op = (op)unwrap(parse_op(alct, ts)); t = unwrap(peek_token(alct, ts)); if (t->type == TK_ARG) { - struct arg * a = allocate(alct, sizeof(struct arg)); + arg* a = allocate(alct, sizeof(arg)); a->ival = t->ival; a->fval = t->fval; i->arg = a; @@ -142,3 +146,4 @@ struct result parse_instr(struct allocator * alct, struct token_stream * ts) { } return ok(i); } + |
