aboutsummaryrefslogtreecommitdiff
path: root/vxrefbas.c
diff options
context:
space:
mode:
authorB. Watson <urchlay@slackware.uk>2024-06-13 15:30:33 -0400
committerB. Watson <urchlay@slackware.uk>2024-06-13 15:30:33 -0400
commit9d1ce7321f27d47b76cba907127af777d6672188 (patch)
tree9d0af044cbead9f251cbf401f5664d6c766edb72 /vxrefbas.c
parent79cbc8914d90ced367b68c4aaf899dfe596449a3 (diff)
downloadbw-atari8-tools-9d1ce7321f27d47b76cba907127af777d6672188.tar.gz
major surgery: callback API, port dumpbas to use callbacks, add vxrefbas.
Diffstat (limited to 'vxrefbas.c')
-rw-r--r--vxrefbas.c95
1 files changed, 95 insertions, 0 deletions
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 <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <time.h>
+
+#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;
+}