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 | |
| parent | 39e2a605f6d8ebcc3cb454daae3d0a4298df2eb6 (diff) | |
make code shorter
| -rw-r--r-- | src/as_codegen.h | 5 | ||||
| -rw-r--r-- | src/as_op.h | 5 | ||||
| -rw-r--r-- | src/as_parser.c | 45 | ||||
| -rw-r--r-- | src/as_parser.h | 21 | ||||
| -rw-r--r-- | src/as_tokenizer.c | 20 | ||||
| -rw-r--r-- | src/as_tokenizer.h | 14 | ||||
| -rw-r--r-- | src/utils.c | 12 | ||||
| -rw-r--r-- | src/utils.h | 14 |
8 files changed, 78 insertions, 58 deletions
diff --git a/src/as_codegen.h b/src/as_codegen.h index 17e6ca9..1d7392a 100644 --- a/src/as_codegen.h +++ b/src/as_codegen.h @@ -9,9 +9,10 @@ struct bytearray { size_t len; char* buf; }; +typedef struct bytearray bytearray; -void bytearray_emit8(struct bytearray *self, int8_t data); -void bytearray_emit64(struct bytearray *self, int64_t data); +void bytearray_emit8(bytearray *self, int8_t data); +void bytearray_emit64(bytearray *self, int64_t data); struct result codegen(struct allocator *alct, struct prog *prog, struct sym_table tbl); diff --git a/src/as_op.h b/src/as_op.h index cb45fc1..3dd546e 100644 --- a/src/as_op.h +++ b/src/as_op.h @@ -20,11 +20,12 @@ enum op { OP_LDARG, OP_LDVAR, OP_STARG, OP_STVAR, OP_END }; +typedef enum op op; enum op str2op(const char *str); int isOp(const char *str); -int op_size(enum op op); -int is_pseudo_op(enum op op); +int op_size(op op); +int is_pseudo_op(op op); #endif 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); } + diff --git a/src/as_parser.h b/src/as_parser.h index 2171fe4..f1d3b62 100644 --- a/src/as_parser.h +++ b/src/as_parser.h @@ -10,46 +10,53 @@ struct arg { int64_t ival; double fval; }; +typedef struct arg arg; struct instr { enum op op; - struct arg * arg; + arg* arg; const char* tag_name; + int lineno; }; +typedef struct instr instr; struct label { const char* name; }; +typedef struct label label; struct stmt { struct label * label; struct instr * instr; }; +typedef struct stmt stmt; struct stmts { struct stmt ** stmts; }; +typedef struct stmts stmts; struct prog { struct stmts * stmts; }; +typedef struct prog prog; // result<prog> -struct result parse_prog(struct allocator * alct, struct token_stream * ts); +result parse_prog(allocator* alct, token_stream* ts); // result<stmt> -struct result parse_stmt(struct allocator * alct, struct token_stream * ts); +result parse_stmt(allocator* alct, token_stream* ts); // result<stmts> -struct result parse_stmts(struct allocator * alct, struct token_stream * ts); +result parse_stmts(allocator* alct, token_stream* ts); // result<instr> -struct result parse_instr(struct allocator * alct, struct token_stream * ts); +result parse_instr(allocator* alct, token_stream* ts); // result<label> -struct result parse_label(struct allocator * alct, struct token_stream * ts); +result parse_label(allocator* alct, token_stream* ts); // result<enum op> -struct result parse_op(struct allocator * alct, struct token_stream * ts); +result parse_op(allocator* alct, token_stream* ts); #endif diff --git a/src/as_tokenizer.c b/src/as_tokenizer.c index 7a71707..aa22000 100644 --- a/src/as_tokenizer.c +++ b/src/as_tokenizer.c @@ -6,7 +6,7 @@ #include "as_op.h" #include "utils.h" -int input_stream_next_char(struct input_stream * s) { +int input_stream_next_char(input_stream* s) { if (s->cursor == -1) { return EOF; } @@ -30,7 +30,7 @@ int input_stream_next_char(struct input_stream * s) { return c; } -int input_stream_peek_char(struct input_stream * s) { +int input_stream_peek_char(input_stream* s) { if (s->cursor == -1) { return EOF; } @@ -71,9 +71,9 @@ int is_part_of_identifier(int c) { return 0; } -struct result next_token_impl(struct allocator * alct, struct input_stream * s) { +result next_token_impl(allocator* alct, input_stream* s) { const char *errmsg; - struct token * t = allocate(alct, sizeof(struct token)); + token* t = allocate(alct, sizeof(token)); int c; while (1) { c = input_stream_peek_char(s); @@ -138,7 +138,7 @@ struct result next_token_impl(struct allocator * alct, struct input_stream * s) return ok(t); } -struct result next_token(struct allocator * alct, struct token_stream * ts) { +result next_token(allocator* alct, token_stream* ts) { if (ts->buf != NULL) { struct token * t = ts->buf; ts->buf = NULL; @@ -147,7 +147,7 @@ struct result next_token(struct allocator * alct, struct token_stream * ts) { return next_token_impl(alct, ts->s); } -struct result peek_token(struct allocator * alct, struct token_stream * ts) { +result peek_token(allocator* alct, token_stream* ts) { if (ts->buf != NULL) { return ok(ts->buf); } @@ -155,7 +155,7 @@ struct result peek_token(struct allocator * alct, struct token_stream * ts) { return ok(ts->buf); } -void print_token(struct token * t) { +void print_token(token* t) { switch (t->type) { case TK_OP: printf("OP: %s, line: %d, col: %d\n", t->sval, t->line, t->col); @@ -178,15 +178,15 @@ void print_token(struct token * t) { } } -struct token_stream * new_token_stream(struct allocator * alct, FILE* fp) { - struct input_stream * s = allocate(alct, sizeof(struct input_stream)); +token_stream* new_token_stream(allocator* alct, FILE* fp) { + input_stream* s = allocate(alct, sizeof(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; - struct token_stream * ts = allocate(alct, sizeof(struct token_stream)); + token_stream* ts = allocate(alct, sizeof(token_stream)); ts->s = s; ts->buf = NULL; return ts; diff --git a/src/as_tokenizer.h b/src/as_tokenizer.h index 192e381..1027530 100644 --- a/src/as_tokenizer.h +++ b/src/as_tokenizer.h @@ -9,6 +9,7 @@ enum token_type { TK_OP, TK_ARG, TK_TAG, TK_COLON, TK_NEWLINE, TK_ENDOFFILE }; +typedef enum token_type token_type; struct token { enum token_type type; @@ -18,6 +19,7 @@ struct token { int64_t ival; double fval; }; +typedef struct token token; #define INPUT_STREAM_BUF_SIZE 1024 @@ -29,20 +31,22 @@ struct input_stream{ int line; int col; }; +typedef struct input_stream input_stream; struct token_stream { - struct token *buf; - struct input_stream *s; + token *buf; + input_stream *s; }; +typedef struct token_stream token_stream; // result<token*> -struct result next_token(struct allocator * alct, struct token_stream * ts); +result next_token(allocator * alct, token_stream * ts); // result<token*> -struct result peek_token(struct allocator * alct, struct token_stream * ts); +result peek_token(allocator * alct, token_stream * ts); void print_token(struct token *t); -struct token_stream * new_token_stream(struct allocator * alct, FILE* fp); +token_stream* new_token_stream(allocator * alct, FILE* fp); #endif // FMV_AS_TOKENIZER_H_ diff --git a/src/utils.c b/src/utils.c index b79c87c..70f9eb6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -4,11 +4,11 @@ #include <assert.h> #include <stdarg.h> -struct result ok(void *value) { +result ok(void *value) { return (struct result){.value = value, .errmsg = NULL}; } -struct result err(const char *errmsg) { +result err(const char *errmsg) { return (struct result){.value = NULL, .errmsg = errmsg}; } @@ -18,7 +18,7 @@ struct allocator { size_t len; }; -struct allocator * new_allocator() { +allocator* new_allocator() { struct allocator * alct = malloc(sizeof(struct allocator)); alct->bufs = malloc(sizeof(void*) * 16); alct->cap = 16; @@ -27,7 +27,7 @@ struct allocator * new_allocator() { return alct; } -void delete_allocator(struct allocator * alct) { +void delete_allocator(allocator* alct) { for (size_t i = 0; i < alct->len; i++) { free(alct->bufs[i]); } @@ -35,7 +35,7 @@ void delete_allocator(struct allocator * alct) { free(alct); } -void * allocate(struct allocator * alct, size_t size) { +void * allocate(allocator* alct, size_t size) { assert(size > 0); if (alct->len >= alct->cap) { alct->cap = alct->cap * 2; // Doubling the capacity @@ -48,7 +48,7 @@ void * allocate(struct allocator * alct, size_t size) { return ptr; } -char* safe_sprintf(struct allocator *alct, const char* format, ...) { +char* safe_sprintf(allocator* alct, const char* format, ...) { va_list args; va_list args_copy; int length; diff --git a/src/utils.h b/src/utils.h index 8bc76f8..1c1804b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -4,25 +4,27 @@ #include <stdlib.h> struct allocator; +typedef struct allocator allocator; struct result { void *value; const char* errmsg; }; +typedef struct result result; #define unwrap(x__) ({ \ struct result res__ = (x__); \ if (res__.errmsg != NULL) return res__; \ res__.value;}) -struct result ok(void *value); -struct result err(const char *errmsg); +result ok(void *value); +result err(const char *errmsg); -struct allocator * new_allocator(); -void delete_allocator(struct allocator * allocator); +allocator* new_allocator(); +void delete_allocator(allocator* allocator); -void* allocate(struct allocator * allocator, size_t size); +void* allocate(allocator* allocator, size_t size); -char* safe_sprintf(struct allocator *alct, const char* format, ...); +char* safe_sprintf(allocator* alct, const char* format, ...); #endif // FVM_UTILS_H_ |
