-
Notifications
You must be signed in to change notification settings - Fork 3
/
commandsOn.cpp
344 lines (271 loc) · 7.15 KB
/
commandsOn.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "stdafx.h"
#include <vector>
#ifdef __amigaos4__
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/retroMode.h>
#endif
#ifdef __linux__
#include <stdint.h>
#include "os/linux/stuff.h"
#endif
#include <amosKittens.h>
#include <stack.h>
#include "commands.h"
#include "engine.h"
#include "debug.h"
#include "kittyErrors.h"
#include "var_helper.h"
#include "label.h"
extern struct globalVar globalVars[];
extern struct nativeCommand nativeCommands[];
extern struct globalVar globalVars[1000];
extern std::vector<struct label> labels;
extern struct stackFrame *currentFrame;
extern int nativeCommandsSize;
extern unsigned short token_not_found;
extern int tokenMode;
static unsigned int is_token_cmd = 0;
extern const char *TokenName( unsigned short token );
extern char *_gosub_return( struct glueCommands *data, int nextToken );
extern char *dupRef( struct reference *ref );
extern void stack_frame_up(int varIndex);
char *executeOnToken(char *ptr, unsigned short token)
{
struct nativeCommand *cmd;
char *ret;
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
// we are at end of line, we need to find the next data command.
switch (token)
{
case token_goto:
case token_gosub:
case token_proc:
is_token_cmd = token;
return ptr;
case 0x0000:
return NULL;
}
// find the token
for (cmd = nativeCommands ; cmd < nativeCommands + nativeCommandsSize ; cmd++ )
{
if (token == cmd->id )
{
#ifdef show_token_numbers_yes
getLineFromPointer(ptr);
printf("ON READ %08d %08X %20s:%08d stack is %d cmd stack is %d token %04x -- name %s\n",
lineFromPtr.line, ptr +2,__FUNCTION__,__LINE__, instance.stack, instance.cmdStack, token , TokenName(token));
#endif
ret = cmd -> fn( cmd, ptr ) ;
if (ret) ret += cmd -> size;
return ret;
}
}
token_not_found = token;
setError(33, ptr); // Out of Data
return NULL;
}
static char *collect_data(char *ptr)
{
unsigned short token = *((short *) ptr );
setStackNum(0);
ptr+=2;
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
while ( (ptr = executeOnToken( ptr, token )) && (is_token_cmd == 0) )
{
token = *((short *) ptr );
ptr += 2; // next token.
}
flushCmdParaStack( 0x0000 );
return ptr;
}
char *execute_on( int num, char *tokenBuffer, char *returnTokenBuffer, unsigned short token );
char *cmdOn(struct nativeCommand *cmd, char *tokenBuffer )
{
unsigned int flags;
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
is_token_cmd = 0;
tokenBuffer += 4; // skip ON data, maybe we don't need it.
tokenBuffer = collect_data(tokenBuffer);
while (instance.cmdStack)
{
flags = cmdTmp[instance.cmdStack-1].flag;
if ( flags & cmd_para )
{
cmdTmp[--instance.cmdStack].cmd(&cmdTmp[instance.cmdStack], 0);
}
else break;
}
tokenBuffer = execute_on( getStackNum(__stack), tokenBuffer, NULL, is_token_cmd );
return tokenBuffer-4; // on function exit +4 is added.
}
struct label *GetLabel( unsigned int is_token, int ref_num )
{
struct label *label = NULL;
switch (is_token)
{
case 0x0006: label = findLabel(globalVars[ref_num-1].varName, currentFrame -> id );
break;
case 0x0018: label = &labels[ref_num-1];
break;
case 0x003E: label = &labels[ref_num-1];
break;
}
return label;
}
char *do_ON_command(uint16_t token_cmd, uint16_t token_item, int ref_num, char *tokenBuffer )
{
char *ret = NULL;
if (ref_num>0)
{
struct label *label = NULL;
switch (token_cmd)
{
case token_goto:
dprintf("--GOTO--\n");
label = GetLabel( token_item, ref_num );
if (label)
{
dropProgStackAllFlag( cmd_true | cmd_false ); // just kill the if condition, if any.
ret = label -> tokenLocation -2;
}
break;
case token_gosub:
dprintf("--GOSUB--\n");
label = GetLabel( token_item, ref_num );
if (label)
{
stackCmdLoop( _gosub_return, tokenBuffer+2 );
ret = label -> tokenLocation -2;
}
break;
case token_proc:
dprintf("--PROC--\n");
{
int idx = ref_num - 1;
int oldStack = __stack;
stackCmdProc( _procedure, tokenBuffer);
cmdTmp[instance.cmdStack-1].stack = oldStack; // carry stack.
tokenMode = mode_store;
stack_frame_up(idx);
ret = globalVars[idx].var.tokenBufferPos-2;
}
break;
}
}
return ret;
}
char *execute_on( int num, char *tokenBuffer, char *returnTokenBuffer, unsigned short token_cmd )
{
unsigned short ref_num = 0;
unsigned short is_token = 0;
unsigned short next_token = 0;
char *ret = NULL;
struct reference *ref = NULL;
proc_names_printf("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
if ( (token_cmd==token_proc) || (token_cmd==token_goto) || (token_cmd==token_gosub) )
{
for(;;)
{
next_token = NEXT_TOKEN(tokenBuffer);
switch (next_token)
{
case 0x0006:
is_token = 0x0006;
tokenBuffer +=2;
ref = (struct reference *) (tokenBuffer);
num--;
if (num == 0)
{
if (ref -> flags & type_proc )
{
ref_num = ref -> ref;
}
else
{
ref -> flags |= type_proc;
ref -> ref = var_find_proc_ref( ref );
ref_num = ref -> ref;
}
if (ref_num == 0) setError(22,tokenBuffer);
}
tokenBuffer += sizeof(struct reference) + ref -> length;
break;
case 0x0012:
is_token = 0x0012;
tokenBuffer +=2;
ref = (struct reference *) (tokenBuffer);
num--;
if (num == 0)
{
if (ref -> flags & type_proc )
{
ref_num = ref -> ref;
}
else
{
ref -> ref = var_find_proc_ref( ref );
ref_num = ref -> ref;
}
if (ref_num == 0) setError(22,tokenBuffer);
}
tokenBuffer += sizeof(struct reference) + ref -> length;
break;
case 0x0018:
is_token = 0x0018;
tokenBuffer +=2;
ref = (struct reference *) (tokenBuffer);
num--;
if (num == 0)
{
if (ref->ref == -1) // Because line start with 0x0018 is labels.
{
char *name = dupRef( ref ) ;
if (name)
{
dprintf("name: %s\n",name);
ref_num = ref->ref = findLabelRef( name, currentFrame -> id );
free(name);
}
if (ref_num == 0)
{
printf("set errror\n");
setError(22,tokenBuffer);
}
}
else ref_num = ref->ref;
}
tokenBuffer += sizeof(struct reference) + ref -> length;
break;
case 0x001E:
case 0x0036:
case 0x003E:
is_token = 0x003E;
tokenBuffer +=2;
num--;
if (num == 0)
{
char num[50];
sprintf(num,"%d", *((int *) tokenBuffer));
ref_num = findLabelRef( num, currentFrame -> id );
}
tokenBuffer += 4;
break;
case 0x005C:
tokenBuffer +=2;
break;
case 0x0000: // exit at end of list..
case 0x0054:
ret = tokenBuffer +2;
default:
goto exit_on_for_loop;
}
}
exit_on_for_loop:
ret = do_ON_command( token_cmd, is_token, ref_num, returnTokenBuffer ? returnTokenBuffer : tokenBuffer );
}
// if this function is used some where else then in normal ON ... PROC.
if (returnTokenBuffer) tokenBuffer = NULL;
if (ret) tokenBuffer = ret;
return tokenBuffer;
}