aboutsummaryrefslogtreecommitdiff
path: root/vxrefbas.c
blob: 839b7761225942a6045219b1269d26c1c2527dac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
}