-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.cpp
228 lines (207 loc) · 7.05 KB
/
Console.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
#include "Console.h"
#include <QKeyEvent>
#include <QTextLine>
#include <QTextCursor>
#include <QStringList>
Console::Console(QWidget *parent) : QTextEdit(parent)
{
currentLineNumber=0;
inputProgram=nullptr;
programOutput=nullptr;
waitInput=false;
waitQuickInput=false;
write("Minimal BASIC -- Type HELP for help. Type RUN to run your code. Type LIST to print all your code. Type CLEAR to clear the screen and your code. Type QUIT to quit.\n\n");
}
void Console::write(QString msg)
{
++currentLineNumber;
this->append(msg);
}
void Console::keyPressEvent(QKeyEvent *event)
{
if (this->textCursor().hasSelection())
return;
if (event->key() == Qt::Key_Return) {
QTextCursor cursor = this->textCursor();
cursor.movePosition(QTextCursor::End);
cursor.select(QTextCursor::LineUnderCursor);
QString lastLine = cursor.selectedText();
newLineWritten(lastLine);
userText=lastLine;
handle_input();
}
QTextEdit::keyPressEvent(event);
}
void Console::handle_input()
{
Tokenizer tokenizer;
QStringList sList=tokenizer.token(userText);
if(userText.toLower()=="help") {showHelp();return;}
else if(userText.toLower()=="list") {Console::list();return;}
else if(userText.toLower()=="quit") {quit();}
else if(userText.toLower()=="clear") {clear();program.clear();return;}
else if(userText.toLower()=="run")
{
try
{
run();
}
catch(QString error)
{
write(error);
}
return;
}
else if(waitInput)
{
bool *ok= new bool;
int value=userText.toInt(ok);
if(*ok==true)
{
inputProgram=new int;
*inputProgram=value;
waitInput=false;
run();
}
else {
write("Please input an integer");
}
return;
}
else if(waitQuickInput)
{
bool *ok= new bool;
int value=userText.toInt(ok);
if(*ok==true)
{
waitQuickInput=false;
/*Insert the input variable, if it already exists, cover it*/
if(!program.global_Variables->contains(quickInputVariable)) {program.global_Variables->insert(quickInputVariable,value);}
else {
program.global_Variables->remove(quickInputVariable);
program.global_Variables->insert(quickInputVariable,value);
}
quickInputVariable=nullptr;
}
else {
write("Please input an integer");
}
return;
}
else if(waitQuickInput==false && waitInput==false && sList[0].toStdString()[0]>='0' && sList[0].toStdString()[0]<='9')
{
if(!program.insertLine(userText))
{
write("Invalid input");
}
return;
}
else if(sList[0]=="INPUT")
{
if(sList.size()>1)
{
quickInputVariable=sList[1];
write("?");
waitQuickInput=true;
return;
}
else {
write("Invalid input");
}
}
else if(sList[0]=="PRINT")
{
if(sList.size()>1)
{
Parser parser(program.global_Variables);
QStringList expression;
for(int i=1;i<sList.size();++i)
{
expression.append(sList[i]);
}
try
{
int value=parser.evaluate(parser.parse(expression));
write(QString::number(value));
}
catch(QString error)
{
write(error);
}
}
else {
write("Invalid input");
}
}
else if(sList[0]=="LET")
{
if(sList.size()>3)
{
Parser parser(program.global_Variables);
QString variable=sList[1];
QStringList expression;
for(int i=3;i<sList.size();++i)
{
expression.append(sList[i]);
}
try {
int value=parser.evaluate(parser.parse(expression));
if(!program.global_Variables->contains(variable))
{
program.global_Variables->insert(variable,value);
}
else {
program.global_Variables->remove(variable);
program.global_Variables->insert(variable,value);
}
} catch (QString error) {
write(error);
}
}
else {
write("Invalid input");
}
}
else {
write("Invalid input");
}
}
void Console::showHelp()
{
write("\n help.txt \t For Minimal BASIC");
write("1.Every line of statement should begin with a linenumber(except LET statement,PRINT statement and INPUT statement, which will be explained in the following text.)\n\n");
write("2.There are totally 7 types of statements:\n");
write("REM: The statement used for comments. Any text on the line after the keyword REM is ignored.\n");
write("LET: This statement is BASIC’s assignment statement. The LET keyword is followed by a variable name, an equal sign, and an expression.\n ");
write("PRINT: In minimal BASIC, the PRINT statement has the form: PRINT exp The effect of this statement is to print the value of the expression on the console\n");
write("INPUT: In the minimal version of the BASIC interpreter, the INPUT statement has the form: INPUT var The effect of this statement is to print a prompt consisting of the string \"?\" and then to read in a value to be stored in the variable.\n");
write("GOTO: This statement has the syntax GOTO n and forces an unconditional change in the control flow of the program. When the program hits this statement, the program continues from line n instead of continuing with the next statement.\n");
write("IF: This statement provides conditional control. The syntax for this statement is: IF exp1 op exp2 THEN n where exp1 and exp2 are expressions and op is one of the conditional operators =, <, or >. If the condition holds, the program should continue from line n just as in the GOTO statement. If not, the program continues on to the next line. \n");
write("END: Marks the end of the program. Execution halts when this line is reached. \n\n");
write("3.But in the case of LET statement, INPUT statement and PRINT statement, you can omit the linenumber. Then what you have input will be not record as statement,but will be taken as command to directly change the global variables.\n");
}
void Console::list()
{
QString *programContent=nullptr;
int totalLine=program.list(programContent);
for(int i=0;i<totalLine;++i)
{
write(programContent[i]);
}
}
void Console::quit()
{
exit(0);
}
void Console::run()
{
program.run(inputProgram,programOutput);
if(program.endFlag) {program.endFlag=false;return;}
delete inputProgram;
if(programOutput)
{
write(*programOutput);
if(*programOutput=="?") {waitInput=true;}
else {run();}
}
}