-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tokens.cpp
278 lines (236 loc) · 5.56 KB
/
Tokens.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// awBASIC interpreter (c) 2002 Anthony J. Wood
// www.awsoftware.org
//
// This file is part of awbasic.
// awbasic is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
#include <ctype.h> // isdigit
#include <string.h> // strlen
#include "basic.h"
struct Tokens {
unsigned char token;
char* word;
};
static struct Tokens TokenList[] = {
TOK_END, "END",
TOK_LET, "LET",
TOK_PRINT, "PRINT",
TOK_PRINT,"?", // alternate code for TOK_PRINT.
TOK_IF,"IF",
TOK_ELSE,"ELSE",
TOK_THEN,"THEN",
TOK_APOS,"'",
TOK_REM,"REM",
TOK_GOTO,"GOTO",
TOK_FOR,"FOR",
TOK_TO,"TO",
TOK_STEP,"STEP",
TOK_NEXT,"NEXT",
TOK_GOSUB,"GOSUB",
TOK_RETURN,"RETURN",
TOK_DIM,"DIM",
TOK_NOT,"NOT",
TOK_AND,"AND",
TOK_OR,"OR",
TOK_GT,">",
TOK_LT,"<",
TOK_EQ,"=",
TOK_MUL,"*",
TOK_DIV,"/",
TOK_ADD,"+",
TOK_SUB,"-",
TOK_EXP,"^",
TOK_EXP,"[", // TRS80 uses ASCII 91 ( [ ) for power
TOK_ASC,"ASC",
TOK_CHR,"CHR$",
TOK_FRE,"FRE",
TOK_INKEY,"INKEY$",
TOK_LEN,"LEN",
TOK_LEFT,"LEFT$",
TOK_MID,"MID$",
TOK_RIGHT,"RIGHT$",
TOK_STR,"STR$",
TOK_STRING,"STRING$",
TOK_VAL,"VAL",
TOK_INSTR,"INSTR",
TOK_ABS,"ABS",
TOK_ATN,"ATN",
TOK_CDBL,"CDBL",
TOK_CINT,"CINT",
TOK_COS,"COS",
TOK_CSNG,"CSGN",
TOK_FEXP,"EXP",
TOK_FIX,"FIX",
TOK_INT,"INT",
TOK_LOG,"LOG",
TOK_RND,"RND",
TOK_SGN,"SGN",
TOK_SIN,"SIN",
TOK_SQR,"SQR",
TOK_TAN,"TAN",
TOK_INPUT,"INPUT",
TOK_CLEAR,"CLEAR",
TOK_CLS,"CLS",
TOK_DEFSTR,"DEFSTR",
TOK_DEFINT,"DEFINT",
TOK_DEFSGN,"DEFSGN",
TOK_DEFDBL,"DEFDBL",
TOK_READ,"READ",
TOK_DATA,"DATA",
TOK_RESTORE,"RESTORE",
TOK_RANDOM,"RANDOM",
TOK_ERROR,"ERROR",
TOK_ON,"ON",
TOK_ERL,"ERL",
TOK_ERR,"ERR",
TOK_RESUME,"RESUME",
TOK_MEM,"MEM",
TOK_TAB,"TAB(",
TOK_USING,"USING",
TOK_POKE,"POKE",
TOK_PEEK,"PEEK",
TOK_SET,"SET",
TOK_RESET,"RESET",
TOK_POINT,"POINT",
TOK_STOP,"STOP",
TOK_POS,"POS",
TOK_RUN,"RUN",
TOK_LPRINT,"LPRINT",
TOK_OUT,"OUT",
TOK_LINE,"LINE",
0x00, ""};
/*******************************************************/
char* TokenToAscii(unsigned char findtoken)
{
for (int i=0; TokenList[i].token; i++)
if (TokenList[i].token==findtoken)
return TokenList[i].word;
return 0;
}
/*******************************************************/
unsigned char GetTokenOrChar(char** source)
{
if (**source == '\n' || **source == '\r' || **source == NULL)
return **source;
for (int i=0; TokenList[i].token; i++)
{
if (0==strnicmp(*source, TokenList[i].word, strlen(TokenList[i].word)))
{
*source += strlen(TokenList[i].word);
return TokenList[i].token;
}
}
(*source)++;
return *(*source-1);
}
/*******************************************************/
bool GetLineNumber(char** source, unsigned short* value)
{
while (**source==' ' || **source=='\r' || **source=='\n')
(*source)++;
if (!isdigit(**source))
return FALSE;
*value = 0;
while (isdigit(**source))
{
*value = *value*10;
*value = *value + **source-'0';
(*source)++;
}
return TRUE;
}
void TokenizeQuote(unsigned char** dest, char** src)
{
while (TRUE)
{
(*dest)++;
**dest = **src;
(*src)++;
if (**dest=='\"' || **dest==NULL || **dest == '\n' || **dest == '\r')
break;
}
}
void TokenizeRem(unsigned char** dest, char** src)
{
while (TRUE)
{
(*dest)++;
**dest = **src;
(*src)++;
if (**dest==NULL || **dest == '\n' || **dest == '\r')
break;
}
}
void TokenizeData(unsigned char** dest, char** src)
{
while (TRUE)
{
(*dest)++;
**dest = **src;
(*src)++;
if (**dest=='\"')
TokenizeQuote(dest, src);
if (**dest==NULL || **dest == '\n' || **dest == '\r' || **dest == ':')
break;
}
}
void TokenizeLine(unsigned char* dest, char* src, int *destlen, int *sourcelen)
{
unsigned char* dest_start = dest;
char* src_start = src;
do {
*dest = GetTokenOrChar(&src);
if (*dest==TOK_REM|| *dest=='\'')
TokenizeRem(&dest, &src);
else if (*dest=='\"')
TokenizeQuote(&dest, &src);
else if (*dest==TOK_DATA)
TokenizeData(&dest, &src);
if (*dest == '\n' || *dest == '\r')
*dest = NULL;
} while (*dest++ != NULL);
*destlen = dest-dest_start;
*sourcelen=src-src_start;
}
bool Tokenize(char* source, struct BasicCode* code)
{
unsigned short line_number, last_line_number;
int image_idx;
int start_idx;
int sourcelen;
int destlen;
image_idx = 0;
/*
* image format is:
* <2 bytes> addr of next statement
* <2 bytes> line number in integer form
* <n bytes> BASIC statment in tokenized form
* <0> End-of-statment marker
* repeat as needed
* <0 0> Two bytes of zero mark end
*/
while (GetLineNumber(&source, &line_number))
{
if (image_idx > 0 && line_number <= last_line_number)
{
io_printf1i(code->std_con, "Line number %d is out of sequence\n", line_number);
return FALSE;
}
last_line_number = line_number;
start_idx = image_idx;
code->image[image_idx++] = 0; // pointer to next statement
code->image[image_idx++] = 0; // pointer to next statement
code->image[image_idx++] = (unsigned char)(line_number&0xFF);
code->image[image_idx++] = 0xFF&(line_number>>8);
TokenizeLine(&code->image[image_idx], source, &destlen, &sourcelen);
image_idx+=destlen;
source+=sourcelen;
code->image[start_idx] = image_idx&0xFF;
code->image[start_idx+1] = 0xFF&(image_idx>>8);
};
code->image[image_idx++] = 0; // mark end
code->image[image_idx++] = 0; // mark end
return TRUE;
}