-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandhandlers.cpp
370 lines (323 loc) · 11 KB
/
commandhandlers.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// vim:ts=4:sw=4:
// Interactive BASIC Compiler Project
// File: commandhandlers.cpp - contains code for translator command handlers
// Copyright (C) 2012 Thunder422
//
// This program 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 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2012-11-02 initial version (parts removed from translator.cpp)
#include "translator.h"
#include "table.h"
////////////////////////////////////////////////////////////////////////////////
// //
// COMMAND HANDLERS //
// //
////////////////////////////////////////////////////////////////////////////////
// command handler functions are called to process command tokens on the
// command stack at the end of a statements
//
// - these functions are friends of the Translator class so that
// pointers to these functions can be put into table entries
// - there is a reference to the Translator instance so the functions
// have access to all the variables of the instance
// - the other argument is a pointer to the command stack item being handled
// (in case of an error, the original token will be deleted and the token
// pointer changed to point to the token with the error)
// - returns Good if successful, else an error
// - command handlers must handle the following tokens:
// Comma, SemiColon, and codes with EndStmt_Flag
//**************************************
//** ASSIGNMENT COMMAND HANDLER **
//**************************************
TokenStatus Assign_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
{
TokenStatus status;
switch (token->code())
{
case EOL_Code:
// check done stack is empty before calling process_final_operand()
// to avoid bug error check for empty done stack in find_code()
if (t.m_doneStack.empty())
{
// save expected type
DataType datatype = cmd_item->token->dataType();
cmd_item->token = token; // point error to end-of-statement token
return Translator::expectedErrStatus(datatype);
}
// turnoff reference flag of assign token, no longer needed
cmd_item->token->setReference(false);
return t.processFinalOperand(cmd_item->token, NULL, 1);
default: // token was not expected command
switch (t.m_mode)
{
case AssignmentList_TokenMode:
// point error to unexpected token
cmd_item->token = token;
return ExpEqualOrComma_TokenStatus;
case Expression_TokenMode:
if (t.m_state == Translator::BinOp_State)
{
// point error to unexpected token
cmd_item->token = token;
return ExpOpOrEnd_TokenStatus;
}
status = Translator::expectedErrStatus(t.m_cmdStack.top().token
->dataType());
// point error to unexpected token after setting error
cmd_item->token = token;
return status;
default:
return BUG_InvalidMode;
}
}
}
//*********************************
//** PRINT COMMAND HANDLER **
//*********************************
TokenStatus Print_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
{
TokenStatus status;
switch (token->code())
{
case Comma_Code:
// make sure the expression before comma is complete
status = t.expressionEnd();
if (status != Good_TokenStatus)
{
return status;
}
status = t.addPrintCode();
if (status > Good_TokenStatus)
{
return status;
}
// append comma (advance to next column) token to output
t.m_output->append(new RpnItem(token));
// set PRINT command item flag in case last item in statement
// (resets PrintFunc_CmdFlag if set)
t.m_cmdStack.top().flag = PrintStay_CmdFlag;
// switch back to operand state (expecting operand next)
t.m_state = Translator::OperandOrEnd_State;
return Good_TokenStatus;
case SemiColon_Code:
status = t.addPrintCode();
if (status == Good_TokenStatus)
{
// print code added, delete semicolon token
delete token;
}
else if (status == Null_TokenStatus)
{
// check if last token added was a print function
if (t.m_cmdStack.top().flag & PrintFunc_CmdFlag)
{
// set semicolon subcode flag on print function
t.m_output->last()->token()->setSubCodeMask(SemiColon_SubCode);
// sub-code set, delete semicolon token
delete token;
}
else // no expression, add dummy semicolon token
{
t.m_output->append(new RpnItem(token));
}
}
else // an error occurred
{
return status;
}
// set PRINT command item flag in case last item in statement
// (resets PrintFunc_CmdFlag if set)
t.m_cmdStack.top().flag = PrintStay_CmdFlag;
// switch back to operand state (expecting operand next)
t.m_state = Translator::OperandOrEnd_State;
return Good_TokenStatus;
case EOL_Code:
status = t.addPrintCode();
if (status == Good_TokenStatus // data type specific code added?
|| status == Null_TokenStatus // or not stay on line?
&& !(cmd_item->flag & PrintStay_CmdFlag))
{
// append the print token to go to newline at runtime
t.m_output->append(new RpnItem(cmd_item->token));
// set good status; could be set to null
status = Good_TokenStatus;
}
// check if print token was not used
else if (status == Null_TokenStatus)
{
delete cmd_item->token; // print token not used
status = Good_TokenStatus;
}
return status;
default: // token was not expected command
// TODO may need a proper error here
return BUG_UnexpToken;
}
}
//*******************************
//** LET COMMAND HANDLER **
//*******************************
TokenStatus Let_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
{
// this function should not be called for correct statements
// if it is then it means that the LET command was not completed
cmd_item->token = token; // point to end-of-statement token
// make sure done stack is not empty
if (t.m_doneStack.empty())
{
return ExpAssignItem_TokenStatus;
}
return ExpEqualOrComma_TokenStatus;
}
//*********************************
//** INPUT COMMAND HANDLER **
//*********************************
TokenStatus Input_CmdHandler(Translator &t, CmdItem *cmd_item, Token *token)
{
TokenStatus status;
Code code = token->code();
// TODO check for invalid token, report correct error
// PROCESS BEGIN/PROMPT
// used input begin command flag instead of element pointer
if (!(t.m_cmdStack.top().flag & InputBegin_CmdFlag)) // no begin code yet?
{
t.m_cmdStack.top().flag |= InputBegin_CmdFlag; // begin processed
if (cmd_item->token->isCode(InputPrompt_Code))
{
if (t.m_doneStack.empty())
{
// report error against token
cmd_item->token = token; // set error token
return ExpStrExpr_TokenStatus;
}
if (Translator::equivalentDataType(t.m_doneStack.top().rpnItem
->token()->dataType()) != String_DataType)
{
// don't delete token, caller will delete it
// report error against first token of expression on done stack
token = t.m_doneStack.top().first
->setThrough(t.m_doneStack.top().last);
// delete last token if close paren, remove from done stack
t.deleteCloseParen(t.m_doneStack.pop().rpnItem->token());
cmd_item->token = token; // set error token
return ExpStrExpr_TokenStatus;
}
if (t.m_table.flags(code) & EndStmt_Flag)
{
cmd_item->token = token; // set error token
return ExpOpSemiOrComma_TokenStatus;
}
// change token to InputBeginStr and set sub-code
t.m_table.setToken(token, InputBeginStr_Code);
switch (code)
{
case Comma_Code:
token->setSubCode(Question_SubCode);
break;
case SemiColon_Code:
token->setSubCode(None_SubCode);
break;
default: // unexpected token
cmd_item->token = token; // set error token
return ExpSemiCommaOrEnd_TokenStatus;
}
status = t.processFinalOperand(token, NULL, 0);
if (status == Good_TokenStatus)
{
// set pointer to new end of output
t.m_cmdStack.top().index = t.m_output->size();
}
else
{
;//cmd_item->token = token; // set error token (2011-03-24)
}
t.m_mode = Reference_TokenMode; // change mode
// set state for first variable
t.m_state = Translator::Operand_State;
return status;
}
// Input_Code
if (!t.m_doneStack.empty())
{
// create new token for InputBegin
// insert at begin of command before first variable
t.m_output->insert(t.m_cmdStack.top().index++,
new RpnItem(t.m_table.newToken(InputBegin_Code)));
}
// if no variable on done stack, error will be reported below
// now continue with input variable
}
// PROCESS INPUT VARIABLE
if (t.m_state != Translator::EndStmt_State)
{
if (t.m_doneStack.empty())
{
cmd_item->token = token; // set error token
return ExpVar_TokenStatus;
}
// set token for data type specific input assign token
t.m_table.setToken(token, InputAssign_Code);
// set reference flag to be handled correctly in find_code
token->setReference();
// find appropriate input assign code and append to output
status = t.processFinalOperand(token, NULL, 0);
if (status != Good_TokenStatus)
{
cmd_item->token = token; // set error token
return status;
}
token->setReference(false); // now reset reference flag
// create new token for input parse code
// (which is first associated 2 code of input assign code)
// insert input parse token into list at current index (update index)
t.m_output->insert(t.m_cmdStack.top().index++,
new RpnItem(t.m_table.newToken(
t.m_table.assoc2Code(token->code()))));
// now process code
if (code == Comma_Code)
{
t.m_state = Translator::Operand_State; // for next variable
return Good_TokenStatus; // done now, but expecting more variables
}
if (code == SemiColon_Code)
{
t.m_cmdStack.top().flag |= InputStay_CmdFlag; // stay on line
t.m_state = Translator::EndStmt_State; // expecting end-of-statment
return Good_TokenStatus; // done now, wait for end-of-statment
}
if (!(t.m_table.flags(code) & EndStmt_Flag))
{
// unexpected token
cmd_item->token = token; // set error token
return ExpSemiCommaOrEnd_TokenStatus;
}
// else fall thru to end processing
}
// else leave end stmt token for caller to deal with (delete if not needed)
// PROCESS END OF INPUT
// mark last parse code with end subcode
(*t.m_output)[t.m_cmdStack.top().index - 1]->token()
->setSubCode(End_SubCode);
// append the input token to go to newline at runtime
if (t.m_cmdStack.top().flag & InputStay_CmdFlag)
{
cmd_item->token->setSubCode(Keep_SubCode);
}
t.m_output->append(new RpnItem(cmd_item->token));
return Good_TokenStatus;
}
// end: commandhandlers.cpp