From 0a80e61272725c53bf2b29294ad4e8972d1fdf6a Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Tue, 11 Mar 2014 22:29:56 -0400 Subject: [PATCH] de-tokenize AppleSoft BASIC files --- blist.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 blist.c diff --git a/blist.c b/blist.c new file mode 100644 index 0000000..ddf27e6 --- /dev/null +++ b/blist.c @@ -0,0 +1,207 @@ +#include +#include + + + +const char *tokens[]={ +/*80*/ " END ", + " FOR ", + " NEXT ", + " DATA ", + " INPUT ", + " DEL ", + " DIM ", + " READ ", + " GR ", + " TEXT ", + " PR# ", + " IN# ", + " CALL ", + " PLOT ", + " HLIN ", + " VLIN ", + " HGR2 ", + " HGR ", + " HCOLOR= ", + " HPLOT ", + " DRAW ", + " XDRAW ", + " HTAB ", + " HOME ", + " ROT= ", + " SCALE= ", + " SHLOAD ", + " TRACE ", + " NOTRACE ", + " NORMAL ", + " INVERSE ", + " FLASH ", + " COLOR= ", + " POP ", + " VTAB ", + " HIMEM: ", + " LOMEM: ", + " ONERR ", + " RESUME ", + " RECALL ", + " STORE ", + " SPEED= ", + " LET ", + " GOTO ", + " RUN ", + " IF ", + " RESTORE ", + " & ", +/*B0*/ " GOSUB ", + " RETURN ", + " REM ", + " STOP ", + " ON ", + " WAIT ", + " LIST ", + " SAVE ", + " DEF ", + " POKE ", + " PRINT ", + " CONT ", + " LIST ", + " CLEAR ", + " GET ", + " NEW ", + " TAB( ", + " TO ", + " FN ", + " SPC( ", + " THEN ", + " AT ", + " NOT ", + " STEP ", + " + ", + " - ", + " * ", + " / ", + " ^ ", + " AND ", + " OR ", + " > ", + " = ", + " < ", + " SGN ", + " INT ", + " ABS ", + " USR ", + " FRE ", + " SCRN( ", + " PDL ", + " POS ", + " SQR ", + " RND ", + " LOG ", + " EXP ", + " COS ", + " SIN ", + " TAN ", + " ATN ", + " PEEK ", + " LEN ", + " STR$ ", + " VAL ", + " ASC ", + " CHR$ ", + " LEFT$ ", + " RIGHT$ ", + " MID$ ", +/*EB*/ " ?? ", /* I'm pretty sure these aren't valid ... */ + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", + " ?? ", +}; + +/* + + This is where the actual work is done. + An Applesoft file is laid out like so: + + word word [variable # of bytes] <0x00> <-eol marker + | | | + | | |if >0x7F, it is a token, otherwise, it's a printed + | | + | |The line # (65535 max) + | + | This is an offset to the next line (?) if 0, the file is done. + + repeating over and over... + +*/ + + +void translate(FILE *f) +{ + for(;;) + { + int x; + //offset to next line + x = fgetc(f) | (fgetc(f) << 8); + if (feof(f)) + { + fprintf(stderr, "Unexpected eof.\n"); + break; + } + + // last line. + if (x == 0) break; + + // line number. + x = fgetc(f) | (fgetc(f) << 8); + if (feof(f)) + { + fprintf(stderr, "Unexpected eof.\n"); + break; + } + + fprintf(stdout, "% 5d: ", x); + + for (;;) + { + x = fgetc(f); + if (x == 0) break; + + if (x < 0) + { + fprintf(stderr, "Unexpected eof.\n"); + break; + } + if (x > 0x7f) + fputs(tokens[x & 0x7f], stdout); + else if (x > 0x1f) fputc(x, stdout); + + } + + fprintf(stdout, "\n"); + } + + +} + + +int main(int argc, char **argv) +{ + translate(stdin); + return 0; +} \ No newline at end of file