diff options
author | B. Watson <urchlay@slackware.uk> | 2025-03-04 19:02:01 -0500 |
---|---|---|
committer | B. Watson <urchlay@slackware.uk> | 2025-03-04 19:02:01 -0500 |
commit | 4f48e0900084945f687ea0cb7643a7fcd84690ae (patch) | |
tree | 16b6da7d67675235c1867e8713514d68114a257a | |
parent | 1eb3f57437062efaee684fe7b91d9899f9359044 (diff) | |
download | bw-atari8-tools-4f48e0900084945f687ea0cb7643a7fcd84690ae.tar.gz |
listamsb: use list_*() functions.
-rw-r--r-- | listamsb.c | 61 |
1 files changed, 33 insertions, 28 deletions
@@ -266,16 +266,22 @@ void list_char(unsigned char c) { } void list_token(unsigned char c) { - if(printing) fputs(std_tokens[byte - MIN_STD_TOK], outfile); + if(printing) fputs(std_tokens[c - MIN_STD_TOK], outfile); } void list_ext_token(unsigned char c) { - if(printing) fputs(ext_tokens[byte - MIN_EXT_TOK], outfile); + if(printing) fputs(ext_tokens[c - MIN_EXT_TOK], outfile); } void list_lineno(int l) { /* note that AMSB always puts a space after the line number in LIST */ - if(printing) fprintf(outfile, "%d ", lineno); + if(printing) fprintf(outfile, "%d ", l); +} + +void start_listing(void) { + /* AMSB always prints a blank line when it LISTs, so we do, too. + unless it's disabled with -n, of course. */ + if(initial_eol) fputc(EOL, outfile); } /* meat and potatoes. does the actual detokenizing. gets called once @@ -323,19 +329,17 @@ int next_line(void) { warn("line number out range (%d > %d)", lineno, MAX_LINENO); } - /* AMSB always prints a blank line when it LISTs, so we do too. */ - if(initial_eol && (last_lineno == -1)) fputc(EOL, outfile); - last_lineno = lineno; - if(printing) fprintf(outfile, "%d ", lineno); + list_lineno(lineno); - was_ff = 0; - was_colon = 0; - in_string = 0; + was_ff = 0; + was_colon = 0; + in_string = 0; in_comment = 0; - /* walk and print the tokens. when we hit a null byte, we're done. */ + /* walk and print the tokens. when we hit a null byte, break out of the + loop, we're done with this line. */ while(1) { byte = read_prog_byte(); @@ -348,43 +352,42 @@ int next_line(void) { /* pipe is how AMSB stores the closing quote. end the string but not the line of code, and print a " character. */ in_string = 0; - if(printing) fputc('"', outfile); + list_char('"'); } else { /* normal string character. */ - if(printing) { - fputc(byte, outfile); - /* one " character embedded in a string gets printed as "" */ - if(byte == '"') fputc(byte, outfile); - } + list_char(byte); + /* one " character embedded in a string gets printed as "" */ + if(byte == '"') list_char(byte); } continue; } else if(in_comment) { /* null byte ends both the comment and the line of code. */ if(byte == 0x00) break; - if(printing) fputc(byte, outfile); + list_char(byte); continue; } if(was_colon) { if(byte != TOK_SQUOTE && byte != TOK_BANG && byte != TOK_ELSE) { - if(printing) fputc(':', outfile); + list_char(':'); } was_colon = 0; } if(byte == ':') { - /* don't print the colon if the next token is a ! or ' for a comment */ + /* statement separator. don't print the colon yet, the next token + might be a ! or ' for a comment */ was_colon = 1; } else if(byte == '"') { - /* strings start but *don't end* with a double-quote */ + /* begin string. strings start but *don't end* with a double-quote */ in_string = 1; - if(printing) fputc(byte, outfile); + list_char(byte); } else if(was_ff) { /* previous token was $ff, so this is a function token */ if(byte >= MIN_EXT_TOK && byte <= MAX_EXT_TOK) { - if(printing) fputs(ext_tokens[byte - MIN_EXT_TOK], outfile); + list_ext_token(byte); } else { - if(printing) unknown_token(byte, 1); + unknown_token(byte, 1); warn("unknown extended token $ff $%02x at line %d", byte, lineno); } was_ff = 0; @@ -393,12 +396,12 @@ int next_line(void) { was_ff = 1; } else if(byte >= MIN_STD_TOK && byte <= MAX_STD_TOK) { /* statement token */ - if(printing) fputs(std_tokens[byte - MIN_STD_TOK], outfile); + list_token(byte); if(byte == TOK_SQUOTE || byte == TOK_BANG || byte == TOK_REM) in_comment = 1; } else if(byte >= 0x80) { /* invalid token */ - if(printing) unknown_token(byte, 0); + unknown_token(byte, 0); warn("unknown token $%02x at line %d", byte, lineno); } else { /* null byte means the line of code is done */ @@ -408,7 +411,7 @@ int next_line(void) { warn("line %d has character %d outside of a string, maybe Atari BASIC?", lineno, byte); } - if(printing) fputc(byte, outfile); + list_char(byte); } } @@ -434,7 +437,7 @@ int next_line(void) { last_ptr = ptr; - if(printing) fputc(EOL, outfile); + list_char(EOL); return 1; } @@ -608,6 +611,8 @@ int main(int argc, char **argv) { exit(0); /* don't need finish() here, no parsing done */ } + start_listing(); + while(next_line()) linecount++; |