/
let.cpp
262 lines (233 loc) · 6.47 KB
/
let.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
// vim:ts=4:sw=4:
// Interactive BASIC Compiler Project
// File: let.cpp - let command functions source file
// Copyright (C) 2013 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:
//
// 2013-07-06 initial version
#include "recreator.h"
#include "table.h"
#include "token.h"
#include "translator.h"
// LET command translate function
void letTranslate(Translator &translator)
{
bool done;
std::stack<TokenPtr> letStack;
int column {translator.token()->column()};
bool hidden;
if (!translator.token()->isCode(Let_Code))
{
hidden = true;
}
else // delete unneeded command token and get another token
{
translator.resetToken();
hidden = false;
}
DataType dataType {DataType::Any};
bool haveSubStr {};
do
{
try
{
// does not return false (throws error for reference)
translator.getOperand(dataType, Translator::Reference::All);
}
catch (TokenError &error)
{
if (error.m_column == column) // at begin command?
{
try
{
// next token determines error
translator.resetToken();
translator.getToken(Status{});
error = translator.token()->isCode(Comma_Code)
|| translator.token()->isCode(Eq_Code)
? Status::ExpAssignItem : Status::ExpCmdOrAssignItem;
}
catch (TokenError)
{
error = Status::ExpCmdOrAssignItem;
}
}
throw;
}
// get and check next token for comma or equal
translator.getToken(Status::ExpEqualOrComma);
if (translator.token()->isCode(Comma_Code))
{
done = false;
}
else if (translator.token()->isCode(Eq_Code))
{
done = true;
}
else // invalid token
{
throw TokenError {Status::ExpEqualOrComma, translator.token()};
}
// check if this is a sub-string assignment
TokenPtr token;
if (translator.table().hasFlag(translator.doneStackTopToken(),
SubStr_Flag))
{
// get sub-string function token from rpn item on top of stack
// (delete rpn item since it was not appended to output)
token = translator.doneStackTopToken();
translator.doneStackPop();
// change to assign sub-string code (first associated code)
translator.table().setToken(token,
translator.table().associatedCode(token->code()));
translator.resetToken(); // don't need comma/equal token
haveSubStr = true;
}
else // reuse comma/equal token
{
token = translator.moveToken();
// change token to appropriate assign code
translator.table().setToken(token, Assign_Code);
translator.processDoneStackTop(token);
}
// get data type for assignment
if (dataType == DataType::Any)
{
dataType = token->dataType();
}
letStack.emplace(std::move(token)); // save token
}
while (!done);
// get expression for value to assign
try
{
translator.getExpression(dataType);
}
catch (TokenError &error)
{
if (error(Status::UnknownToken))
{
error = Status::ExpOpOrEnd;
}
throw;
}
// check terminating token for end-of-statement
if (!translator.table().hasFlag(translator.token(), EndStmt_Flag))
{
throw TokenError {Status::ExpOpOrEnd, translator.token()};
}
TokenPtr token {std::move(letStack.top())};
letStack.pop();
if (!letStack.empty())
{
if (haveSubStr)
{
// add each token saved in let stack except the last
do
{
// change to keep code (second associated code)
translator.table().setToken(token,
translator.table().associatedCode(token->code()));
// append to output and pop next token from let stack
translator.outputAppend(std::move(token));
token = std::move(letStack.top());
letStack.pop();
}
while (!letStack.empty()); // continue until last token
}
else // have a multiple assignment, change to list code
{
translator.table().setToken(token,
translator.table().secondAssociatedCode(token->code()));
}
}
// set hidden LET flag if needed
if (!hidden)
{
token->addSubCode(Option_SubCode);
}
// drop expresion result from done stack, append last assignment token
translator.doneStackPop();
translator.outputAppend(std::move(token));
}
// function to append LET keyword if the option sub-code is set
void letRecreate(Recreator &recreator, TokenPtr token)
{
if (token->hasSubCode(Option_SubCode))
{
recreator.append(recreator.table().optionName(token->code()) + ' ');
}
}
// function to recreate assignment and list assignment statements
void assignRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
std::stack<std::string> stack;
stack.emplace(recreator.popString()); // push value
std::string separator = ' ' + recreator.table().name(rpnItem->token())
+ ' ';
while (!recreator.empty())
{
stack.emplace(recreator.popString() + separator);
separator = ", ";
}
letRecreate(recreator, rpnItem->token());
while (!stack.empty())
{
recreator.append(std::move(stack.top()));
stack.pop();
}
}
// function to recreate string and sub-string assignment statements
void assignStrRecreate(Recreator &recreator, RpnItemPtr &rpnItem)
{
std::string string;
// check if this first assign code
if (!recreator.separatorIsSet())
{
string = ' ' + recreator.table().name(Assign_Code) + ' ';
recreator.setSeparator(',');
}
else // continuation of assignment list
{
string = recreator.separator();
string += ' ';
}
string.append(recreator.popString());
Code code {rpnItem->token()->code()};
if (recreator.table().hasFlag(code, SubStr_Flag))
{
// for sub-string assignments, treat as function to recreate
recreator.pushWithOperands(std::string{recreator.table().name(code)},
recreator.table().operandCount(code));
}
// deterine if assignment is an sub-string assignment keep code
if (recreator.table().hasFlag(code, Keep_Flag))
{
// for keep codes, append string so far to reference string on stack
recreator.topAppend(std::move(string));
}
else
{
// end of statement, append reference and string so far
letRecreate(recreator, rpnItem->token());
recreator.append(recreator.popString());
recreator.append(std::move(string));
recreator.clearSeparator(); // for next command
}
}
// end: let.cpp