aboutsummaryrefslogtreecommitdiff
path: root/listbas.c
blob: 646900907cfcb08622ce14c26ea7e25e77521483 (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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

#include "bas.h"
#include "bcdfp.h"
#include "tokens.h"

int immediate = 0;

void print_help(void) {
	printf("Usage: %s [-v] [-i] <inputfile>\n", self);
}

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

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

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

double bcd2double(const unsigned char *num) {
	double result = 0, sign;
	int exp, i;

	exp = *num;
	if(!exp) {
		return 0.0;
	}

	sign = (exp & 0x80 ? -1.0 : 1.0);
	exp &= 0x7f;
	exp -= 0x40;

	for(i = 1; i < 6; i++) {
		result *= 100.0;
		result += bcd2int(num[i]);
	}

	result *= pow(100, exp - 4);
	result *= sign;

	return result;
}

void print_number(unsigned int pos) {
	printf("%G", bcd2double(program + pos));
}

void print_string(unsigned int pos, unsigned int len) {
	putchar('"');
	while(len--) putchar(program[pos++]);
	putchar('"');
}

CALLBACK(print_lineno) {
	printf("%d ", lineno);
}

CALLBACK(print_cmd) {
	int bad;
	const char *name;

	if(tok == CMD_ILET) return;

	if(tok > last_command) {
		bad = 1;
	} else if( (name = commands[tok]) ) {
		bad = 0;
	} else {
		bad = 1;
	}

	if(bad)
		printf("(bad cmd token $%02x)", tok);
	else
		printf("%s ", name);
}

CALLBACK(print_op) {
	int bad;
	const char *name;

	switch(tok) {
		case OP_NUMCONST:
			print_number(pos + 1);
			return;
		case OP_STRCONST:
			print_string(pos + 2, program[pos + 1]);
			return;
		case OP_EOL:
			return;
		default: break;
	}


	if(tok > last_operator) {
		bad = 1;
	} else if( (name = operators[tok]) ) {
		bad = 0;
	} else {
		bad = 1;
	}

	if(bad)
		printf("(bad op token $%02x)", tok);
	else
		printf("%s", name);
}

CALLBACK(print_varname) {
	int i, count;
	unsigned char c;

	tok &= 0x7f;
	for(i = vnstart, count = 0; count < tok; i++) {
		if(program[i] & 0x80) count++;
	}
	while(1) {
		c = program[i++];
		putchar(c & 0x7f);
		if(c & 0x80) break;
	}
}

CALLBACK(print_text) {
	while(program[pos] != 0x9b) putchar(program[pos++]);
}

CALLBACK(print_newline) {
	putchar('\n');
}

void list(void) {
	on_start_line = print_lineno;
	on_cmd_token = print_cmd;
	on_exp_token = print_op;
	on_var_token = print_varname;
	on_end_line = print_newline;
	on_text = print_text;
	walk_code(0, 32767 + immediate);
}

int main(int argc, char **argv) {
	set_self(*argv);
	parse_general_args(argc, argv, print_help);
	parse_args(argc, argv);

	readfile();
	parse_header();

	list();
	
	return 0;
}