diff options
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); } + |
