diff options
author | Oxore <oxore@protonmail.com> | 2023-08-12 22:04:24 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2023-08-12 22:42:06 +0300 |
commit | 11e8e014e6c0efb0e4d92c85b99ec675a3b292f5 (patch) | |
tree | 54577b0cb296ee841a6187de6e488ec4557d9907 | |
parent | 8a3b78c2332719084fcf18e58be357af1f1399fb (diff) |
Strip leading '_' and add '.' prefix to L-locals
-rw-r--r-- | main.c | 59 |
1 files changed, 57 insertions, 2 deletions
@@ -2853,6 +2853,25 @@ static int assem_resolve(struct assem *const self) return OK; } +static void emit_token_id( + const struct lex *const lex, + const struct token *const token, + FILE *const s) +{ + assert(token->type == TT_ID); + if (lex->input[token->offset] == '_') { + // Strip leading underscore + // FIXME It should be sort of an option that may be disabled. + fprintf(s, "%.*s", (int)token->length - 1, lex->input + token->offset + 1); + } else if (lex->input[token->offset] == 'L') { + // Add leading dot to all local symbols (they start with 'L'). + // FIXME It should be sort of an option that may be disabled. + fprintf(s, ".%.*s", (int)token->length, lex->input + token->offset); + } else { + fprintf(s, "%.*s", (int)token->length, lex->input + token->offset); + } +} + static void emit_expr( const struct lex *const lex, const struct expr_tokens_span *const expr, @@ -2862,8 +2881,11 @@ static void emit_expr( const struct token token = lex->tokbuf[expr->first_token + i]; if (token.type == TT_NEWLINE) { break; + } else if (token.type == TT_ID) { + emit_token_id(lex, &token, s); + } else { + fprintf(s, "%.*s", (int)token.length, lex->input + token.offset); } - fprintf(s, "%.*s", (int)token.length, lex->input + token.offset); } } @@ -2972,6 +2994,30 @@ static void emit_directive_same( } for (size_t i = 0; i < dir->num_tokens; i++) { const struct token token = lex->tokbuf[dir->first_token + i]; + if (token.type == TT_ID) { + emit_token_id(lex, &token, s); + } else { + fprintf(s, "%.*s ", (int)token.length, lex->input + token.offset); + } + } +} + +static void emit_directive_byte( + const struct lex *const lex, + const struct directive *const dir, + FILE *const s) +{ + if (dir->num_tokens < 1) { + // We won't emit this because it is invalid + return; + } + if (lex->tokbuf[dir->first_token].type == TT_STRING) { + fprintf(s, "\t.ascii\t"); + } else { + fprintf(s, "\t.byte\t"); + } + for (size_t i = 0; i < dir->num_tokens; i++) { + const struct token token = lex->tokbuf[dir->first_token + i]; fprintf(s, "%.*s ", (int)token.length, lex->input + token.offset); } } @@ -2994,7 +3040,12 @@ static int assem_emit(struct assem *const self, FILE *const stream) const struct stmt *stmt = pars->stmttab + i; if (stmt->label_token) { const struct token token = lex->tokbuf[stmt->label_token]; - fprintf(stream, "%.*s:", (int)token.length, lex->input + token.offset); + if (token.type == TT_ID) { + emit_token_id(lex, &token, stream); + fprintf(stream, ":"); + } else { + fprintf(stream, "%.*s:", (int)token.length, lex->input + token.offset); + } } if (stmt->type == ST_INSTRUCTION) { const struct instruction instr = stmt->instruction; @@ -3019,6 +3070,10 @@ static int assem_emit(struct assem *const self, FILE *const stream) case DT_TEXT: emit_directive_same(lex, dir, stream); break; + case DT_ASCII: + case DT_BYTE: + emit_directive_byte(lex, dir, stream); + break; default: break; } |