aboutsummaryrefslogtreecommitdiff
path: root/bas.h
blob: a464e0cb30ea00c63a0c2d5ff5d07716c7b16157 (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
/* bas.h - API for writing standalone programs that deal with
	tokenized Atari 8-bit BASIC program. */

/* maximum size of the program in memory. 64KB is actually way overkill. */
#define BUFSIZE 65536

/* the difference between the VVTP and VNTP values in the file, and the
	actual file positions of the variable names and values. */
#define TBL_OFFSET 0xf2

/* minimum program size, for a program that has no variables and
	only one line of code (the immediate line 32768, consisting only of
	one token, which would be CSAVE). anything smaller than this, we
	can't process. */
#define MIN_PROG_SIZE 21

/* maximum practical size for a BASIC program. if a file exceeds this
	size, we warn about it, but otherwise process it normally.
	this value is derived by subtracting the default LOMEM without DOS
	($0700) from the start of the display list in GR.0 ($9c20, on a 48K Atari).
 */
#define MAX_PROG_SIZE 38176

/* maximum number of variables in the variable name and value tables. this
	could be 128, but "ERROR- 4" still expands the tables. Entries >128
	don't have tokens, can't be referred to in code, but we'll preserve
	them anyway. */
#define MAXVARS 256

/* tokenized colon (statement separator) */
#define TOK_COLON 0x14

/* BASIC tokens. Not a full set. BASIC uses 2 sets of tokens, one
   for commands and the other for operators (which is to say, everything
   *not* a command). */
#define CMD_GOTO     0x0a
#define CMD_GO_TO    0x0b
#define CMD_ON       0x1e
#define CMD_GOSUB    0x0c
#define CMD_TRAP     0x0d
#define CMD_IF       0x07
#define CMD_LIST     0x04
#define CMD_RESTORE  0x23
#define CMD_REM      0x00
#define CMD_DATA     0x01
#define CMD_ERROR    0x37
#define CMD_FOR      0x08
#define CMD_NEXT     0x09
#define CMD_LET      0x06
#define CMD_ILET     0x36
#define CMD_DIM      0x14
#define CMD_READ     0x22
#define CMD_INPUT    0x02
#define CMD_GET      0x29
#define CMD_LOCATE   0x31
#define CMD_NOTE     0x1b
#define OP_GOTO      0x17
#define OP_GOSUB     0x18
#define OP_THEN      0x1b
#define OP_COMMA     0x12
#define OP_ARR_COMMA 0x3c
#define OP_SEMICOLON 0x15
#define OP_EOS       0x14
#define OP_EOL       0x16
#define OP_NUMCONST  0x0e
#define OP_STRCONST  0x0f
#define OP_HASH      0x1c

/* variable types, bits 6-7 of byte 0 of each vvtable entry. */
#define TYPE_SCALAR 0
#define TYPE_ARRAY  1
#define TYPE_STRING 2

/* callbacks */
#define CALLBACK(x) void x(unsigned int lineno, unsigned int pos, unsigned int tok, unsigned int end)
#define CALLBACK_PTR(x) void (*x)(unsigned int lineno, unsigned int pos, unsigned int tok, unsigned int end)
#define walk_all_code() walk_code(0, 32768)

void walk_code(unsigned int startlineno, unsigned int endlineno);
unsigned char get_vartype(unsigned char tok);

extern CALLBACK_PTR(on_start_line);
extern CALLBACK_PTR(on_bad_line_length);
extern CALLBACK_PTR(on_end_line);
extern CALLBACK_PTR(on_start_stmt);
extern CALLBACK_PTR(on_end_stmt);
extern CALLBACK_PTR(on_cmd_token);
extern CALLBACK_PTR(on_text);
extern CALLBACK_PTR(on_exp_token);
extern CALLBACK_PTR(on_var_token);
extern CALLBACK_PTR(on_string_const);
extern CALLBACK_PTR(on_num_const);
extern CALLBACK_PTR(on_trailing_garbage);

/* BASIC 14-byte header values */
extern unsigned short lomem;
extern unsigned short vntp;
extern unsigned short vntd;
extern unsigned short vvtp;
extern unsigned short stmtab;
extern unsigned short stmcur;
extern unsigned short starp;

/* positions where various parts of the file start,
	derived from the header vars above. */
extern unsigned short codestart;
extern unsigned short code_end;
extern unsigned short vnstart;
extern unsigned short vvstart;
extern int filelen;

/* name of executable, taken from argv[0] */
extern const char *self;

/* entire file gets read into memory (for now) */
extern unsigned char program[BUFSIZE];

/* file handles */
extern FILE *input_file;
extern FILE *output_file;

extern char *output_filename;

extern int verbose;

extern void set_self(const char *argv0);
extern void die(const char *msg);
extern void parse_general_args(int argc, char **argv, void (*helpfunc)());
extern int writefile(void);
extern void readfile(void);
extern unsigned short getword(int addr);
extern void setword(int addr, int value);
extern void dump_header_vars(void);
extern void parse_header(void);
extern void update_header(void);
extern void move_code(int offset);
extern void adjust_vntable_size(int oldsize, int newsize);
extern int vntable_ok(void);
extern void invalid_args(const char *arg);
extern FILE *open_file(const char *name, const char *mode);
extern void open_input(const char *name);
extern void open_output(const char *name);