From 9d1ce7321f27d47b76cba907127af777d6672188 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Thu, 13 Jun 2024 15:30:33 -0400 Subject: major surgery: callback API, port dumpbas to use callbacks, add vxrefbas. --- vxrefbas.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 vxrefbas.c (limited to 'vxrefbas.c') diff --git a/vxrefbas.c b/vxrefbas.c new file mode 100644 index 0000000..839b776 --- /dev/null +++ b/vxrefbas.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include + +#include "bas.h" + +int target_var, lastline, assign, is_for, is_next; +int refcounts[128]; + +void print_help(void) { + fprintf(stderr, "Usage: %s program.bas\n", self); + exit(0); +} + +CALLBACK(new_line) { + assign = is_for = is_next = 0; +} + +CALLBACK(end_line) { + if(lastline != lineno) return; + printf("%d%s%s%s ", + lineno, + assign ? "=" : "", + is_for ? "{": "", + is_next ? "}": ""); +} + +CALLBACK(new_command) { + if(program[pos + 1] == (target_var | 0x80)) { + if(tok == CMD_LET || tok == CMD_ILET) { + assign = 1; + } else if(tok == CMD_FOR) { + is_for = 1; + } else if(tok == CMD_NEXT) { + is_next = 1; + } + } +} + +CALLBACK(handle_var) { + if(lastline == lineno) return; + if((tok & 0x7f) == target_var) { + refcounts[target_var]++; + lastline = lineno; + } +} + +int main(int argc, char **argv) { + unsigned short pos; + unsigned short vnpos; + int unref = 0; + + set_self(*argv); + parse_general_args(argc, argv, print_help); + + open_input(argv[1]); + + readfile(); + parse_header(); + + on_var_token = handle_var; + on_start_line = new_line; + on_end_line = end_line; + on_cmd_token = new_command; + + target_var = 0; + vnpos = vnstart; + + for(pos = vvstart; pos < codestart; pos += 8) { + /* print the variable name */ + while(program[vnpos] < 0x80) + putchar(program[vnpos++]); + putchar(program[vnpos++] & 0x7f); + printf("/%02x: ", target_var | 0x80); + + lastline = -1; + walk_all_code(); + + if(!refcounts[target_var]) { + unref++; + printf("(no references)"); + } else { + printf("(%d)", refcounts[target_var]); + } + putchar('\n'); + + if(target_var++ == 128) break; + } + + printf(" %d variables, %d unreferenced.\n", target_var, unref); + return 0; +} -- cgit v1.2.3