aboutsummaryrefslogtreecommitdiff
path: root/linetab.c
blob: 55a5c979143fd6e1816b8b047b255e3ade57a9fd (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "linetab.h"

lineref_t *linerefs[32769];
int refcounts[32769];
int lines_exist[32769];
unsigned char last_cmd, on_op;
int last_cmd_pos;

void add_lineref(unsigned short from, unsigned short pos) {
	lineref_t *p;
	int c;
	unsigned short to;

	to = fp2int(program + pos);
	if(to > 32767) return;

	p = linerefs[to];
	c = refcounts[to];

	if(c) {
		p = realloc(p, sizeof(lineref_t) * (c + 1));
	} else {
		p = malloc(sizeof(lineref_t));
	}

	if(!p) die("Out of memory.");

	linerefs[to] = p;
	linerefs[to][c].lineno = from;
	linerefs[to][c].pos = pos;
	linerefs[to][c].cmd = last_cmd;
	c++;
	refcounts[to] = c;
}

/* makes sure a numeric constant isn't start of an expression. */
int is_standalone_num(unsigned short pos) {
	if(program[pos] != OP_NUMCONST) return 0;
	switch(program[pos + 7]) {
		case OP_EOS:
		case OP_EOL:
		case OP_COMMA:
			return 1;
		default:
			return 0;
	}
}

CALLBACK(start_stmt) {
	lines_exist[lineno] = 1;
}

CALLBACK(got_cmd) {
	last_cmd = tok;
	last_cmd_pos = pos;
	on_op = 0;
}

void computed_msg(unsigned short lineno) {
	static int last_lineno = -1;
	char *cmd;

	/* avoid duplicate warnings */
	if(lineno == last_lineno) return;
	last_lineno = lineno;

	switch(last_cmd) {
		case CMD_GOTO:
			cmd = "GOTO"; break;
		case CMD_GO_TO:
			cmd = "GO TO"; break;
		case CMD_GOSUB:
			cmd = "GOSUB"; break;
		case CMD_RESTORE:
			cmd = "RESTORE"; break;
		case CMD_TRAP:
			cmd = "TRAP"; break;
		case CMD_ON:
			if(on_op == OP_GOSUB)
				cmd = "ON/GOSUB";
			else
				cmd = "ON/GOTO";
			break;
		case CMD_LIST:
			cmd = "LIST";
			break;
		default: /* should never happen! */
			cmd = "???"; break;
	}

	fprintf(stderr, "%s: Warning: Computed %s at line %d.\n", self, cmd, lineno);
}

CALLBACK(got_var) {
	switch(last_cmd) {
		/* any use of a variable in the arguments to these means
		   we can't renumber that argument. */
		case CMD_GOTO:
		case CMD_GO_TO:
		case CMD_GOSUB:
		case CMD_RESTORE:
		case CMD_TRAP:
		case CMD_LIST:
			computed_msg(lineno);
			break;
		case CMD_ON:
			/* vars are OK in ON, before the GOTO or GOSUB */
			if(on_op) computed_msg(lineno);
			break;
		default:
			break;
	}
}

CALLBACK(got_exp) {
	unsigned char last_tok = program[pos - 1];
	int standalone;

	if(last_cmd == CMD_ON) {
		if(tok == OP_GOTO || tok == OP_GOSUB)
			on_op = tok;
	}

	if(tok != OP_NUMCONST) return;

	/* beware: standalone only means nothing *follows* the constant
	   in the same expression. still have to check last_tok to see
	   what came before. */
	standalone = is_standalone_num(pos);

	switch(last_cmd) {
		/* these take a single argument */
		case CMD_GOTO:
		case CMD_GO_TO:
		case CMD_GOSUB:
		case CMD_RESTORE:
		case CMD_TRAP:
			if((pos == last_cmd_pos + 1) && standalone) {
				add_lineref(lineno, pos + 1);
			} else {
				computed_msg(lineno);
			}
			break;
		case CMD_IF:
			/* this only applies to bare line numbers, like IF A THEN 1000,
			   not IF A THEN GOTO 1000 (or anything else after THEN). */
			if(last_tok == OP_THEN) {
				add_lineref(lineno, pos + 1);
			}
			break;
		case CMD_ON: {
				/* takes arbitrary number of arguments */
				switch(last_tok) {
					case OP_GOTO:
					case OP_GOSUB:
					case OP_COMMA:
						if(standalone)
							add_lineref(lineno, pos + 1);
						else
							computed_msg(lineno);
						break;
					default:
						break;
				}
			}
			break;
		case CMD_LIST: {
				/* takes one or two arguments */
				switch(last_tok) {
					case CMD_LIST:
					case OP_COMMA:
						if(standalone)
							add_lineref(lineno, pos + 1);
						else
							computed_msg(lineno);
						break;
					default:
						break;
				}
			}
		default:
			break;
	}
}

void build_ref_table(void) {
	on_start_stmt = start_stmt;
	on_cmd_token = got_cmd;
	on_exp_token = got_exp;
	on_var_token = got_var;
	walk_all_code();
	on_start_stmt = on_cmd_token = on_exp_token = on_var_token = 0;
}

void free_ref_table(void) {
	int i;
	for(i = 0; i < 32768; i++)
		if(linerefs[i]) free(linerefs[i]);
}