-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatement.cpp
More file actions
323 lines (259 loc) · 8.63 KB
/
Copy pathStatement.cpp
File metadata and controls
323 lines (259 loc) · 8.63 KB
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
#include "Statement.h"
#include <sstream>
#include <stdexcept>
// ========================
// Statement.cpp
// 各种 BASIC 语句节点的语法树文本生成实现。
// ========================
namespace {
// 工具函数:生成指定空格数的缩进,供语法树文本使用。
std::string indent(int value) {
return std::string(value, ' ');
}
}
void Statement::resetRuntimeStats() noexcept {
m_execCount = 0;
}
void Statement::collectVarUses(std::map<std::string, int> &counter) const {
(void)counter;
// 默认实现什么也不做,具体语句自行覆盖
}
std::string Statement::headerWithStats() const {
return header() + " " + std::to_string(m_execCount);
}
// ========================
// REM 语句:注释
// ========================
RemStatement::RemStatement(std::string comment) : m_comment(std::move(comment)) {}
// 返回语句类型头部
std::string RemStatement::header() const {
return "REM";
}
// 语法树内容:输出注释内容
std::string RemStatement::bodyToTreeString(int indentSize,
const std::map<std::string, int>* useCount) const {
(void)useCount;
if (m_comment.empty()) {
return "";
}
return indent(indentSize) + m_comment + "\n";
}
void RemStatement::execute(EvalState &state)
{
(void)state;
markExecuted();
}
// ========================
// LET 语句:赋值
// ========================
LetStatement::LetStatement(std::string variable, std::unique_ptr<Expression> expression)
: m_variable(std::move(variable)), m_expression(std::move(expression)) {}
// 返回语句类型头部
std::string LetStatement::header() const {
return "LET";
}
// 语法树内容:先输出变量名,再递归输出表达式树
std::string LetStatement::bodyToTreeString(int indentSize,
const std::map<std::string, int>* useCount) const {
std::ostringstream oss;
oss << indent(indentSize) << m_variable;
// 如果要输出这个变量用了几次,useCount非null
if (useCount) {
auto it = useCount->find(m_variable);
if (it != useCount->end()) {
oss << " " << it->second;
}
}
oss << "\n";
if (m_expression) {
oss << m_expression->toTreeString(indentSize);
}
return oss.str();
}
void LetStatement::execute(EvalState &state)
{
markExecuted();
if (!m_expression) {
throw std::runtime_error("LET statement missing expression");
}
int value = m_expression->eval(state);
state.setValue(m_variable, value);
}
void LetStatement::collectVarUses(std::map<std::string, int> &counter) const
{
if (m_expression) {
m_expression->collectVarUses(counter);
}
}
const std::string& LetStatement::getVariable() const noexcept
{
return m_variable;
}
const Expression* LetStatement::getExpression() const noexcept
{
return m_expression.get();
}
// ========================
// PRINT 语句:输出
// ========================
PrintStatement::PrintStatement(std::unique_ptr<Expression> expression)
: m_expression(std::move(expression)) {}
// 返回语句类型头部
std::string PrintStatement::header() const {
return "PRINT";
}
// 语法树内容:递归输出表达式树
std::string PrintStatement::bodyToTreeString(int indentSize,
const std::map<std::string, int>* useCount) const {
(void)useCount;
return m_expression ? m_expression->toTreeString(indentSize) : std::string();
// 如果m_expression不存在,返回空字符串 std::string()
}
void PrintStatement::execute(EvalState &state)
{
markExecuted();
if (!m_expression) {
throw std::runtime_error("PRINT statement missing expression");
}
int value = m_expression->eval(state);
throw PrintSignal(std::to_string(value));
}
void PrintStatement::collectVarUses(std::map<std::string, int> &counter) const
{
if (m_expression) {
m_expression->collectVarUses(counter);
}
}
// ========================
// INPUT 语句:输入
// ========================
InputStatement::InputStatement(std::string variable) : m_variable(std::move(variable)) {}
// 返回语句类型头部
std::string InputStatement::header() const {
return "INPUT";
}
// 语法树内容:输出变量名
std::string InputStatement::bodyToTreeString(int indentSize,
const std::map<std::string, int>* useCount) const {
(void)useCount;
return indent(indentSize) + m_variable + "\n";
}
void InputStatement::execute(EvalState &state)
{
(void)state;
markExecuted();
throw InputSignal(m_variable);
}
// ========================
// GOTO 语句:无条件跳转
// ========================
GotoStatement::GotoStatement(int targetLine) : m_targetLine(targetLine) {}
// 返回语句类型头部
std::string GotoStatement::header() const {
return "GOTO";
}
// 语法树内容:输出目标行号
std::string GotoStatement::bodyToTreeString(int indentSize,
const std::map<std::string, int>* useCount) const {
(void)useCount;
return indent(indentSize) + std::to_string(m_targetLine) + "\n";
}
void GotoStatement::execute(EvalState &state)
{
(void)state;
markExecuted();
// 把参数强制转换成 void 是一种常见写法
// 意思是“我知道这个参数存在但有意不使用”,从而消除警告,
// 同时让读者清楚地看到这是刻意的。
throw JumpSignal(m_targetLine);
// INPUT execute 现在只负责抛信号
}
// ========================
// IF 语句:条件跳转
// ========================
IfStatement::IfStatement(std::unique_ptr<Expression> left,
std::string op,
std::unique_ptr<Expression> right,
int targetLine)
: m_left(std::move(left)), m_right(std::move(right)), m_operator(std::move(op)), m_targetLine(targetLine) {}
// 返回语句类型头部
std::string IfStatement::header() const {
return "IF THEN";
}
std::string IfStatement::headerWithStats() const {
return std::string("IF THEN ") + std::to_string(m_trueCount) + " " + std::to_string(m_falseCount);
}
// 语法树内容:先输出比较符,再递归输出左右表达式,最后输出目标行号
std::string IfStatement::bodyToTreeString(int indentSize,
const std::map<std::string, int>* useCount) const {
(void)useCount;
std::ostringstream oss;
if (m_left) {
oss << m_left->toTreeString(indentSize);
}
oss << indent(indentSize) << m_operator << "\n";
if (m_right) {
oss << m_right->toTreeString(indentSize);
}
// 最后一行显示跳转目标行号。
oss << indent(indentSize) << m_targetLine << "\n";
return oss.str();
}
void IfStatement::execute(EvalState &state)
{
markExecuted();
if (!m_left || !m_right) {
throw std::runtime_error("IF statement missing expression");
}
int leftValue = m_left->eval(state);
int rightValue = m_right->eval(state);
bool condition = false;
if (m_operator == "=") {
condition = (leftValue == rightValue);
} else if (m_operator == "<") {
condition = (leftValue < rightValue);
} else if (m_operator == ">") {
condition = (leftValue > rightValue);
} else {
throw std::runtime_error("Unsupported comparison operator in IF");
}
if (condition) {
++m_trueCount;
throw JumpSignal(m_targetLine);
}
++m_falseCount;
}
void IfStatement::resetRuntimeStats() noexcept
{
Statement::resetRuntimeStats();
m_trueCount = 0;
m_falseCount = 0;
}
void IfStatement::collectVarUses(std::map<std::string, int> &counter) const
{
if (m_left) {
m_left->collectVarUses(counter);
}
if (m_right) {
m_right->collectVarUses(counter);
}
}
// ========================
// END 语句:程序结束
// ========================
// 返回语句类型头部
std::string EndStatement::header() const {
return "END";
}
// 语法树内容:END 无内容
std::string EndStatement::bodyToTreeString(int,
const std::map<std::string, int>* useCount) const {
(void)useCount;
return "";
}
void EndStatement::execute(EvalState &state)
{
(void)state;
markExecuted();
throw EndSignal();
}