質問編集履歴
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
以下のプログラムをエラーなく,コンパイルすることができました。が,ofstreamのオブジェクトofsに<<を用いて文字列を出力しても,開いたファイルには何も書きこまれませんでした。全く原因が分かりません。
|
2
2
|
|
3
|
+
何故か、Main.cppの下部にあるwhile(1)以降のところをコメントアウトしたら、Code Writerクラスのコンストラクタで定義した、デバッグ用の ofs<<"Open Wow”が実行されて、出力ファイルに上手くか囲まれました。
|
3
4
|
#Main.cpp
|
4
5
|
```C++
|
5
6
|
#include <iostream>
|
1
cppファイルを一つ上げ忘れたのでそれを追加した。
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,33 +5,21 @@
|
|
5
5
|
#include <iostream>
|
6
6
|
#include "Parser.h"
|
7
7
|
#include "CodeWriter.h"
|
8
|
-
|
9
8
|
using namespace std;
|
10
|
-
|
11
9
|
int main()
|
12
10
|
{
|
13
11
|
Parser parser;
|
14
12
|
CodeWriter codewriter(parser);
|
15
|
-
cout << "1" << endl;
|
16
13
|
parser.advance();
|
17
|
-
cout << "2" << endl;
|
18
|
-
cout << parser.commandType() << endl;
|
19
14
|
while (1) {
|
20
|
-
cout << "3" << endl;
|
21
15
|
if (parser.commandType() == C_ARITHMATIC) {
|
22
16
|
codewriter.writeArithmatic(parser.arg1());
|
23
|
-
cout << "4" << endl;
|
24
17
|
}
|
25
18
|
else if (parser.commandType() == C_POP) {
|
26
19
|
codewriter.writePushPop(C_POP, parser.arg1(), parser.arg2());
|
27
|
-
cout << "5" << endl;
|
28
20
|
}
|
29
21
|
else if (parser.commandType() == C_PUSH) {
|
30
|
-
cout << "check_push" << endl;
|
31
|
-
cout << "arg1: " << parser.arg1() << endl;
|
32
|
-
cout << "arg2: " << parser.arg2() << endl;
|
33
22
|
codewriter.writePushPop(C_PUSH, parser.arg1(), parser.arg2());
|
34
|
-
cout << "6" << endl;
|
35
23
|
}
|
36
24
|
parser.advance();
|
37
25
|
}
|
@@ -45,8 +33,6 @@
|
|
45
33
|
#include <sstream>
|
46
34
|
#include <string>
|
47
35
|
#include <iostream>
|
48
|
-
|
49
|
-
|
50
36
|
enum comType{
|
51
37
|
C_ERROR,
|
52
38
|
C_ARITHMATIC,
|
@@ -82,7 +68,6 @@
|
|
82
68
|
cin >> infilename;
|
83
69
|
ifs.open(infilename);
|
84
70
|
while(ifs.fail()) {
|
85
|
-
cout << "そんな名前のファイルねぇよぼけ" << endl;
|
86
71
|
cin >> infilename;
|
87
72
|
ifs.open(infilename);
|
88
73
|
}
|
@@ -105,10 +90,7 @@
|
|
105
90
|
|
106
91
|
comType Parser::commandType()
|
107
92
|
{
|
108
|
-
|
109
|
-
//cout << curcmd << endl;
|
110
|
-
|
111
|
-
|
93
|
+
string curcmdope = curcmd;
|
112
94
|
curcmdope = curcmdope.substr(0, curcmd.find(" "));
|
113
95
|
if (curcmdope == "add" || curcmdope == "sub" || curcmdope == "neg" ||
|
114
96
|
curcmdope == "eq" || curcmdope == "qt" || curcmdope == "lt" ||
|
@@ -151,9 +133,6 @@
|
|
151
133
|
|
152
134
|
return curcmdarg1;
|
153
135
|
}
|
154
|
-
else {
|
155
|
-
cout << "そのようなコマンドは存在しない" << endl;
|
156
|
-
}
|
157
136
|
}
|
158
137
|
|
159
138
|
int Parser::arg2()
|
@@ -204,113 +183,237 @@
|
|
204
183
|
//std::string* curcmd; //現在コマンド
|
205
184
|
};
|
206
185
|
```
|
207
|
-
#
|
186
|
+
#CodeWriter.cpp
|
208
187
|
```C++
|
209
188
|
#include "Parser.h"
|
189
|
+
#include "CodeWriter.h"
|
190
|
+
|
210
191
|
using namespace std;
|
211
|
-
|
192
|
+
|
212
|
-
|
193
|
+
CodeWriter::CodeWriter(Parser& parser)
|
213
194
|
{
|
195
|
+
label_num = 0;
|
196
|
+
//curcmd = &(parser.curcmd);
|
214
|
-
|
197
|
+
outfilename = parser.infilename;
|
198
|
+
outfilename.erase(outfilename.find(".") + 1, outfilename.find(".") + 3);
|
199
|
+
outfilename += "hack";
|
215
|
-
|
200
|
+
ofs.open(outfilename);
|
216
|
-
while(ifs.fail()) {
|
217
|
-
|
201
|
+
ofs << "open WOW";
|
218
|
-
ifs.open(infilename);
|
219
|
-
}
|
220
202
|
}
|
221
203
|
|
222
|
-
void
|
204
|
+
void CodeWriter::setFileName(string fileName)
|
223
205
|
{
|
224
|
-
|
206
|
+
|
225
|
-
|
226
|
-
while (curcmd[0] == ' ') {
|
227
|
-
curcmd.erase(curcmd.begin()+0);
|
228
|
-
}
|
229
|
-
while (curcmd[0] == '/') {
|
230
|
-
advance();
|
231
|
-
}
|
232
|
-
while (curcmd[0] == '\0') {
|
233
|
-
advance();
|
234
|
-
}
|
235
207
|
}
|
236
208
|
|
209
|
+
|
210
|
+
|
237
|
-
|
211
|
+
void CodeWriter::writePushPop(comType command, string segment, int index)
|
238
212
|
{
|
213
|
+
string idx = to_string(index);
|
214
|
+
string outfilename_ex_hack = outfilename;
|
215
|
+
outfilename_ex_hack=outfilename_ex_hack.erase(outfilename.find("."), outfilename.find(".") + 5);
|
216
|
+
cout << "outfilename(-hack): " << outfilename_ex_hack << endl;
|
239
217
|
|
218
|
+
if (command == C_PUSH) {
|
219
|
+
if (segment == "static") {
|
220
|
+
ofs << "@" << outfilename_ex_hack << "." << idx << "\n";
|
221
|
+
ofs << "D=M\n";
|
222
|
+
//write push from D-register
|
223
|
+
ofs << "@SP\n";
|
224
|
+
ofs << "A=M\n";
|
225
|
+
ofs << "M=D\n";
|
226
|
+
ofs << "@SP\n";
|
227
|
+
ofs << "M=M+1\n";
|
228
|
+
}
|
229
|
+
else if (segment == "constant") {
|
230
|
+
ofs << "@" << idx << "\n";
|
231
|
+
ofs << "D=A\n";
|
232
|
+
//write push from D-register
|
233
|
+
ofs << "@SP\n";
|
234
|
+
ofs << "A=M\n";
|
235
|
+
ofs << "M=D\n";
|
236
|
+
ofs << "@SP\n";
|
237
|
+
ofs << "M=M+1\n";
|
238
|
+
}
|
239
|
+
else if (segment == "local" || segment == "argument" ||
|
240
|
+
segment == "this" || segment == "that") {
|
241
|
+
string reg_name;
|
240
|
-
|
242
|
+
//write push from virtual segment
|
243
|
+
if (segment == "local") reg_name = "LCL";
|
244
|
+
else if (segment == "argument")reg_name = "ARG";
|
245
|
+
else if (segment == "this")reg_name = "THIS";
|
246
|
+
else if (segment == "that")reg_name = "THAT";
|
247
|
+
ofs << "@" << reg_name << "\n";
|
248
|
+
ofs << "A=M\n";
|
249
|
+
for (int i(0); i < index; i++) {
|
250
|
+
ofs << "A=A+1\n";
|
251
|
+
}
|
252
|
+
ofs << "D=M\n";
|
253
|
+
//write push from D-register
|
254
|
+
ofs << "@SP\n";
|
255
|
+
ofs << "A=M\n";
|
256
|
+
ofs << "M=D\n";
|
257
|
+
ofs << "@SP\n";
|
258
|
+
ofs << "M=M+1\n";
|
259
|
+
}
|
260
|
+
else if (segment == "pointer" || segment == "temp") {
|
261
|
+
ofs << "@0\n";
|
262
|
+
if (segment == "pointer") {
|
263
|
+
for (int i(0); i < 3 + index; i++) {
|
264
|
+
ofs << "A = A + 1\n";
|
265
|
+
}
|
266
|
+
ofs << "D=M\n";
|
267
|
+
//write push from D-register
|
268
|
+
ofs << "@SP\n";
|
269
|
+
ofs << "A=M\n";
|
270
|
+
ofs << "M=D\n";
|
271
|
+
ofs << "@SP\n";
|
272
|
+
ofs << "M=M+1\n";
|
273
|
+
}
|
274
|
+
else if (segment == "temp") {
|
275
|
+
for (int i(0); i < 5 + index; i++) {
|
276
|
+
ofs << "A = A + 1\n";
|
277
|
+
}
|
278
|
+
ofs << "D=M\n";
|
279
|
+
//write push from D-register
|
280
|
+
ofs << "@SP\n";
|
281
|
+
ofs << "A=M\n";
|
282
|
+
ofs << "M=D\n";
|
283
|
+
ofs << "@SP\n";
|
284
|
+
ofs << "M=M+1\n";
|
285
|
+
}
|
286
|
+
}
|
241
287
|
|
288
|
+
}
|
242
|
-
|
289
|
+
else if (command == C_POP) {
|
243
|
-
|
290
|
+
if (segment == "static") {
|
291
|
+
//write pop to m_register
|
292
|
+
ofs << "@SP\n";
|
293
|
+
ofs << "M=M-1\n";
|
294
|
+
ofs << "A=M\n";
|
295
|
+
ofs << "D=M\n";
|
296
|
+
ofs << "@" << outfilename_ex_hack << "." << idx << "\n";
|
297
|
+
ofs << "M=D\n";
|
298
|
+
}
|
244
|
-
|
299
|
+
else if (segment == "local" || segment == "argument" ||
|
245
|
-
|
300
|
+
segment == "this" || segment == "that") {
|
301
|
+
string reg_name;
|
302
|
+
//write pop to virtual segment
|
303
|
+
if (segment == "local") reg_name = "LCL";
|
304
|
+
else if (segment == "argument")reg_name = "ARG";
|
305
|
+
else if (segment == "this")reg_name = "THIS";
|
306
|
+
else if (segment == "that")reg_name = "THAT";
|
307
|
+
//write pop to m_register
|
308
|
+
ofs << "@SP\n";
|
309
|
+
ofs << "M=M-1\n";
|
310
|
+
ofs << "A=M\n";
|
311
|
+
ofs << "D=M\n";
|
312
|
+
ofs << "@" << reg_name << "\n";
|
313
|
+
ofs << "M=D\n";
|
314
|
+
}
|
246
|
-
|
315
|
+
else if (segment == "temp" || segment == "pointer") {
|
316
|
+
//write pop to static segment
|
317
|
+
if (segment == "temp") {
|
247
318
|
|
319
|
+
//write pop to m_register
|
320
|
+
ofs << "@SP\n";
|
321
|
+
ofs << "M=M-1\n";
|
248
|
-
|
322
|
+
ofs << "A=M\n";
|
323
|
+
ofs << "D=M\n";
|
324
|
+
ofs << "@0\n";
|
325
|
+
for (int i(0); i < 3 + index; i++) {
|
326
|
+
ofs << "A=A+1\n";
|
327
|
+
}
|
328
|
+
ofs << "M=D";
|
329
|
+
|
330
|
+
}
|
331
|
+
else if (segment == "pointer") {
|
332
|
+
//write pop to m_register
|
333
|
+
ofs << "@SP\n";
|
334
|
+
ofs << "M=M-1\n";
|
335
|
+
ofs << "A=M\n";
|
336
|
+
ofs << "D=M\n";
|
337
|
+
ofs << "@0\n";
|
338
|
+
for (int i(0); i < 5 + index; i++) {
|
339
|
+
ofs << "A=A+1\n";
|
340
|
+
}
|
341
|
+
ofs << "M=D";
|
342
|
+
}
|
343
|
+
}
|
249
344
|
}
|
250
|
-
else if (curcmdope == "push") {
|
251
|
-
return C_PUSH;
|
252
|
-
}
|
253
|
-
else if (curcmdope == "pop") {
|
254
|
-
return C_POP;
|
255
|
-
}
|
256
|
-
else {
|
257
|
-
return C_ERROR;
|
258
|
-
}
|
259
345
|
}
|
260
346
|
|
261
|
-
string
|
347
|
+
string CodeWriter::get_new_label() {
|
348
|
+
label_num += 1;
|
349
|
+
return "LABEL"+to_string(label_num);
|
350
|
+
}
|
351
|
+
void CodeWriter::writeArithmatic(string command)
|
262
352
|
{
|
353
|
+
if (command == "add") {
|
354
|
+
//write pop to m_register
|
355
|
+
ofs << "@SP\n";
|
263
|
-
|
356
|
+
ofs << "M=M-1\n";
|
357
|
+
ofs << "A=M\n";
|
358
|
+
ofs << "D=M\n";
|
359
|
+
//write pop to m_register
|
360
|
+
ofs << "@SP\n";
|
361
|
+
ofs << "M=M-1\n";
|
362
|
+
ofs << "A=M\n";
|
264
363
|
|
265
|
-
if (commandType() == C_RETURN) {
|
266
|
-
|
364
|
+
ofs << "D=D+M\n";
|
365
|
+
|
366
|
+
//write push from D_register
|
267
|
-
|
367
|
+
ofs << "@SP\n";
|
368
|
+
ofs << "A=M\n";
|
369
|
+
ofs << "M=D\n";
|
370
|
+
ofs << "@SP\n";
|
371
|
+
ofs << "M=M+1\n";
|
268
372
|
}
|
269
|
-
else if (
|
373
|
+
else if (command == "sub") {
|
374
|
+
//write pop to m_register
|
270
|
-
|
375
|
+
ofs << "@SP\n";
|
376
|
+
ofs << "M=M-1\n";
|
377
|
+
ofs << "A=M\n";
|
378
|
+
ofs << "D=M\n";
|
379
|
+
//write pop to m_register
|
380
|
+
ofs << "@SP\n";
|
381
|
+
ofs << "M=M-1\n";
|
382
|
+
ofs << "A=M\n";
|
383
|
+
ofs << "D=M-D\n";
|
384
|
+
|
385
|
+
//write push from D_register
|
386
|
+
ofs << "@SP\n";
|
387
|
+
ofs << "A=M\n";
|
388
|
+
ofs << "M=D\n";
|
389
|
+
ofs << "@SP\n";
|
390
|
+
ofs << "M=M+1\n";
|
271
391
|
}
|
272
|
-
else if (
|
392
|
+
else if (command == "and") {
|
273
|
-
|
274
|
-
while (curcmdarg1[0] != ' ') {
|
275
|
-
|
393
|
+
//write pop to m_register
|
276
|
-
|
394
|
+
ofs << "@SP\n";
|
277
|
-
|
395
|
+
ofs << "M=M-1\n";
|
396
|
+
ofs << "A=M\n";
|
397
|
+
ofs << "D=M\n";
|
278
|
-
|
398
|
+
//write pop to m_register
|
279
|
-
|
399
|
+
ofs << "@SP\n";
|
400
|
+
ofs << "M=M-1\n";
|
401
|
+
ofs << "A=M\n";
|
280
402
|
|
281
|
-
|
403
|
+
ofs << "D=D&M\n";
|
282
404
|
|
405
|
+
//write push from D_register
|
406
|
+
ofs << "@SP\n";
|
407
|
+
ofs << "A=M\n";
|
408
|
+
ofs << "M=D\n";
|
409
|
+
ofs << "@SP\n";
|
283
|
-
|
410
|
+
ofs << "M=M+1\n";
|
284
411
|
}
|
285
|
-
|
412
|
+
|
286
|
-
cout << "そのようなコマンドは存在しない" << endl;
|
287
|
-
}
|
288
413
|
}
|
289
414
|
|
290
|
-
|
415
|
+
void CodeWriter::close()
|
291
416
|
{
|
292
|
-
string curcmdarg2 = curcmd;
|
293
|
-
|
294
|
-
if (commandType() == C_PUSH || commandType() == C_POP ||
|
295
|
-
commandType() == C_FUNCTION || commandType() == C_CALL) {
|
296
|
-
|
297
|
-
while (curcmdarg2[0] != ' ') {
|
298
|
-
curcmdarg2.erase(curcmdarg2.begin() + 0);
|
299
|
-
}
|
300
|
-
while (curcmdarg2[0] == ' ') {
|
301
|
-
curcmdarg2.erase(curcmdarg2.begin() + 0);
|
302
|
-
}
|
303
|
-
while (curcmdarg2[0] != ' ') {
|
304
|
-
curcmdarg2.erase(curcmdarg2.begin() + 0);
|
305
|
-
}
|
306
|
-
while (curcmdarg2[0] == ' ') {
|
307
|
-
curcmdarg2.erase(curcmdarg2.begin() + 0);
|
308
|
-
}
|
309
|
-
curcmdarg2 = curcmdarg2.substr(0, curcmdarg2.find(" "));
|
310
|
-
//cout << "curcmdarg2 " << curcmdarg2 << endl;
|
311
|
-
|
417
|
+
ofs.close();
|
312
|
-
}
|
313
418
|
}
|
314
|
-
|
315
|
-
コード
|
316
419
|
```
|