aboutsummaryrefslogtreecommitdiff
path: root/vxrefbas.c
blob: e05e2d71de0f2a4d6f22b2f5c65c734fa9eac4c6 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include "bas.h"

int A, F, N, D, I, R, G;
int target_var, lastline;
unsigned char last_cmd = 0, last_op = 0;
int refcounts[128];

void print_help(void) {
	fprintf(stderr, "Usage: %s program.bas\n", self);
	exit(0);
}

CALLBACK(new_line) {
	A = F = N = D = I = R = G = 0;
}

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

	if(A || F || N || D || I || R || G) {
		putchar('=');
		if(A) putchar('A');
		if(F) putchar('F');
		if(N) putchar('N');
		if(D) putchar('D');
		if(I) putchar('I');
		if(R) putchar('R');
		if(G) putchar('G');
	}

	putchar(' ');
}

CALLBACK(new_command) {
	last_cmd = tok;
}

CALLBACK(end_stmt) {
	last_cmd = last_op = 0;
}

CALLBACK(handle_var) {
	unsigned char last_tok;

	if(tok != (target_var | 0x80)) return;

	if(lastline != lineno) {
		printf("%d", lineno);
		refcounts[target_var]++;
	}

	lastline = lineno;
	last_tok = program[pos - 1];

	switch(last_cmd) {
		case CMD_LET:
		case CMD_ILET:
			if(last_tok == last_cmd) A = 1;
			break;
		case CMD_FOR:
			if(last_tok == last_cmd) F = 1;
			break;
		case CMD_NEXT:
			if(last_tok == last_cmd) N = 1;
			break;
		case CMD_DIM:
			if(last_tok == last_cmd || last_tok == OP_COMMA) D = 1;
			break;
		case CMD_INPUT:
			if(last_tok == last_cmd || last_tok == OP_COMMA) I = 1;
			break;
		case CMD_READ:
			if(last_tok == last_cmd || last_tok == OP_COMMA) R = 1;
			break;
		case CMD_GET:
			if(last_tok == OP_COMMA) G = 1;
			break;
	}
}

void parse_args(int argc, char **argv) {
	int opt;

	while( (opt = getopt(argc, argv, "v")) != -1) {
		switch(opt) {
			case 'v': verbose = 1; break;
			default: print_help(); exit(1);
		}
	}

	if(optind >= argc)
		die("No input file given (use - for stdin).");
	else
		open_input(argv[optind]);
}

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);
	parse_args(argc, argv);

	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;

	/* walk the variable value table */
	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');

		/* ignore any ERROR-4 vars, since they don't have tokens anyway. */
		if(++target_var == 128) break;
	}

	printf("  %d variables, %d unreferenced.\n", target_var, unref);
	return 0;
}