aboutsummaryrefslogtreecommitdiff
path: root/vxrefbas.c
blob: acc830c1b8380d8a2c44f2ef1f31563b45faf903 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#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, is_dim, is_read, is_input;
int in_dim, in_read, in_input;
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 = is_dim = is_read = is_input = 0;
}

CALLBACK(end_line) {
	if(lastline != lineno) return;

	printf("%d", lineno);
	if(assign || is_for || is_next || is_dim || is_read || is_input) {
		putchar('=');
		if(assign)   putchar('A');
		if(is_for)   putchar('F');
		if(is_next)  putchar('N');
		if(is_dim)   putchar('D');
		if(is_read)  putchar('R');
		if(is_input) putchar('I');
	}
	putchar(' ');
}

CALLBACK(new_command) {
	switch(tok) {
		case CMD_LET:
		case CMD_ILET:
			assign = 1; break;
		case CMD_FOR:
			is_for = 1; break;
		case CMD_NEXT:
			is_next = 1; break;
		case CMD_DIM:
			in_dim = 1; break;
		case CMD_READ:
			in_read= 1; break;
		case CMD_INPUT:
			in_input= 1; break;
		default: break;
	}
}

CALLBACK(end_stmt) {
	in_dim = in_read = in_input = 0;
}

CALLBACK(handle_var) {
	if(in_dim) is_dim = 1;
	if(in_input) is_input = 1;
	if(in_read) is_read = 1;
	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;
	on_end_stmt = end_stmt;

	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;
}