質問編集履歴

2

2019/05/08 15:32

投稿

robomoco
robomoco

スコア31

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
 
4
4
 
5
+ 何故か、Main.cppの下部にあるwhile(1)以降のところをコメントアウトしたら、Code Writerクラスのコンストラクタで定義した、デバッグ用の ofs<<"Open Wow”が実行されて、出力ファイルに上手くか囲まれました。
6
+
5
7
  #Main.cpp
6
8
 
7
9
  ```C++

1

cppファイルを一つ上げ忘れたのでそれを追加した。

2019/05/08 15:32

投稿

robomoco
robomoco

スコア31

test CHANGED
File without changes
test CHANGED
@@ -12,12 +12,8 @@
12
12
 
13
13
  #include "CodeWriter.h"
14
14
 
15
-
16
-
17
15
  using namespace std;
18
16
 
19
-
20
-
21
17
  int main()
22
18
 
23
19
  {
@@ -26,46 +22,26 @@
26
22
 
27
23
  CodeWriter codewriter(parser);
28
24
 
29
- cout << "1" << endl;
30
-
31
25
  parser.advance();
32
26
 
33
- cout << "2" << endl;
34
-
35
- cout << parser.commandType() << endl;
36
-
37
27
  while (1) {
38
28
 
39
- cout << "3" << endl;
40
-
41
29
  if (parser.commandType() == C_ARITHMATIC) {
42
30
 
43
31
  codewriter.writeArithmatic(parser.arg1());
44
32
 
45
- cout << "4" << endl;
46
-
47
33
  }
48
34
 
49
35
  else if (parser.commandType() == C_POP) {
50
36
 
51
37
  codewriter.writePushPop(C_POP, parser.arg1(), parser.arg2());
52
38
 
53
- cout << "5" << endl;
54
-
55
39
  }
56
40
 
57
41
  else if (parser.commandType() == C_PUSH) {
58
42
 
59
- cout << "check_push" << endl;
60
-
61
- cout << "arg1: " << parser.arg1() << endl;
62
-
63
- cout << "arg2: " << parser.arg2() << endl;
64
-
65
43
  codewriter.writePushPop(C_PUSH, parser.arg1(), parser.arg2());
66
44
 
67
- cout << "6" << endl;
68
-
69
45
  }
70
46
 
71
47
  parser.advance();
@@ -92,10 +68,6 @@
92
68
 
93
69
  #include <iostream>
94
70
 
95
-
96
-
97
-
98
-
99
71
  enum comType{
100
72
 
101
73
  C_ERROR,
@@ -166,8 +138,6 @@
166
138
 
167
139
  while(ifs.fail()) {
168
140
 
169
- cout << "そんな名前のファイルねぇよぼけ" << endl;
170
-
171
141
  cin >> infilename;
172
142
 
173
143
  ifs.open(infilename);
@@ -212,13 +182,7 @@
212
182
 
213
183
  {
214
184
 
215
-
216
-
217
- //cout << curcmd << endl;
218
-
219
-
220
-
221
- string curcmdope = curcmd;
185
+ string curcmdope = curcmd;
222
186
 
223
187
  curcmdope = curcmdope.substr(0, curcmd.find(" "));
224
188
 
@@ -304,12 +268,6 @@
304
268
 
305
269
  }
306
270
 
307
- else {
308
-
309
- cout << "そのようなコマンドは存在しない" << endl;
310
-
311
- }
312
-
313
271
  }
314
272
 
315
273
 
@@ -410,222 +368,470 @@
410
368
 
411
369
  ```
412
370
 
413
- #Parser.cpp
371
+ #CodeWriter.cpp
414
372
 
415
373
  ```C++
416
374
 
417
375
  #include "Parser.h"
418
376
 
377
+ #include "CodeWriter.h"
378
+
379
+
380
+
419
381
  using namespace std;
420
382
 
421
- //クラスParserの実装
422
-
423
- Parser::Parser()
424
-
425
- {
426
-
427
- cin >> infilename;
428
-
429
- ifs.open(infilename);
430
-
431
- while(ifs.fail()) {
432
-
433
- cin >> infilename;
434
-
435
- ifs.open(infilename);
436
-
437
- }
438
-
439
- }
440
-
441
-
442
-
443
- void Parser::advance()
444
-
445
- {
446
-
447
- getline(ifs, curcmd);
448
-
449
-
450
-
451
- while (curcmd[0] == ' ') {
452
-
453
- curcmd.erase(curcmd.begin()+0);
454
-
455
- }
456
-
457
- while (curcmd[0] == '/') {
458
-
459
- advance();
460
-
461
- }
462
-
463
- while (curcmd[0] == '\0') {
464
-
465
- advance();
466
-
467
- }
468
-
469
- }
470
-
471
-
472
-
473
- comType Parser::commandType()
474
-
475
- {
476
-
477
-
478
-
479
- //cout << curcmd << endl;
480
-
481
-
482
-
483
- string curcmdope = curcmd;
484
-
485
- curcmdope = curcmdope.substr(0, curcmd.find(" "));
486
-
487
- if (curcmdope == "add" || curcmdope == "sub" || curcmdope == "neg" ||
488
-
489
- curcmdope == "eq" || curcmdope == "qt" || curcmdope == "lt" ||
490
-
491
- curcmdope == "and" || curcmdope == "or" || curcmdope == "not") {
383
+
384
+
385
+ CodeWriter::CodeWriter(Parser& parser)
386
+
387
+ {
388
+
389
+ label_num = 0;
390
+
391
+ //curcmd = &(parser.curcmd);
392
+
393
+ outfilename = parser.infilename;
394
+
395
+ outfilename.erase(outfilename.find(".") + 1, outfilename.find(".") + 3);
396
+
397
+ outfilename += "hack";
398
+
399
+ ofs.open(outfilename);
400
+
401
+ ofs << "open WOW";
402
+
403
+ }
404
+
405
+
406
+
407
+ void CodeWriter::setFileName(string fileName)
408
+
409
+ {
410
+
411
+
412
+
413
+ }
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+ void CodeWriter::writePushPop(comType command, string segment, int index)
422
+
423
+ {
424
+
425
+ string idx = to_string(index);
426
+
427
+ string outfilename_ex_hack = outfilename;
428
+
429
+ outfilename_ex_hack=outfilename_ex_hack.erase(outfilename.find("."), outfilename.find(".") + 5);
430
+
431
+ cout << "outfilename(-hack): " << outfilename_ex_hack << endl;
432
+
433
+
434
+
435
+ if (command == C_PUSH) {
436
+
437
+ if (segment == "static") {
438
+
439
+ ofs << "@" << outfilename_ex_hack << "." << idx << "\n";
440
+
441
+ ofs << "D=M\n";
442
+
443
+ //write push from D-register
444
+
445
+ ofs << "@SP\n";
446
+
447
+ ofs << "A=M\n";
448
+
449
+ ofs << "M=D\n";
450
+
451
+ ofs << "@SP\n";
452
+
453
+ ofs << "M=M+1\n";
454
+
455
+ }
456
+
457
+ else if (segment == "constant") {
458
+
459
+ ofs << "@" << idx << "\n";
460
+
461
+ ofs << "D=A\n";
462
+
463
+ //write push from D-register
464
+
465
+ ofs << "@SP\n";
466
+
467
+ ofs << "A=M\n";
468
+
469
+ ofs << "M=D\n";
470
+
471
+ ofs << "@SP\n";
472
+
473
+ ofs << "M=M+1\n";
474
+
475
+ }
476
+
477
+ else if (segment == "local" || segment == "argument" ||
478
+
479
+ segment == "this" || segment == "that") {
480
+
481
+ string reg_name;
482
+
483
+ //write push from virtual segment
484
+
485
+ if (segment == "local") reg_name = "LCL";
486
+
487
+ else if (segment == "argument")reg_name = "ARG";
488
+
489
+ else if (segment == "this")reg_name = "THIS";
490
+
491
+ else if (segment == "that")reg_name = "THAT";
492
+
493
+ ofs << "@" << reg_name << "\n";
494
+
495
+ ofs << "A=M\n";
496
+
497
+ for (int i(0); i < index; i++) {
498
+
499
+ ofs << "A=A+1\n";
500
+
501
+ }
502
+
503
+ ofs << "D=M\n";
504
+
505
+ //write push from D-register
506
+
507
+ ofs << "@SP\n";
508
+
509
+ ofs << "A=M\n";
510
+
511
+ ofs << "M=D\n";
512
+
513
+ ofs << "@SP\n";
514
+
515
+ ofs << "M=M+1\n";
516
+
517
+ }
518
+
519
+ else if (segment == "pointer" || segment == "temp") {
520
+
521
+ ofs << "@0\n";
522
+
523
+ if (segment == "pointer") {
524
+
525
+ for (int i(0); i < 3 + index; i++) {
526
+
527
+ ofs << "A = A + 1\n";
528
+
529
+ }
530
+
531
+ ofs << "D=M\n";
532
+
533
+ //write push from D-register
534
+
535
+ ofs << "@SP\n";
536
+
537
+ ofs << "A=M\n";
538
+
539
+ ofs << "M=D\n";
540
+
541
+ ofs << "@SP\n";
542
+
543
+ ofs << "M=M+1\n";
544
+
545
+ }
546
+
547
+ else if (segment == "temp") {
548
+
549
+ for (int i(0); i < 5 + index; i++) {
550
+
551
+ ofs << "A = A + 1\n";
552
+
553
+ }
554
+
555
+ ofs << "D=M\n";
556
+
557
+ //write push from D-register
558
+
559
+ ofs << "@SP\n";
560
+
561
+ ofs << "A=M\n";
562
+
563
+ ofs << "M=D\n";
564
+
565
+ ofs << "@SP\n";
566
+
567
+ ofs << "M=M+1\n";
568
+
569
+ }
570
+
571
+ }
572
+
573
+
574
+
575
+ }
576
+
577
+ else if (command == C_POP) {
578
+
579
+ if (segment == "static") {
580
+
581
+ //write pop to m_register
582
+
583
+ ofs << "@SP\n";
584
+
585
+ ofs << "M=M-1\n";
586
+
587
+ ofs << "A=M\n";
588
+
589
+ ofs << "D=M\n";
590
+
591
+ ofs << "@" << outfilename_ex_hack << "." << idx << "\n";
592
+
593
+ ofs << "M=D\n";
594
+
595
+ }
596
+
597
+ else if (segment == "local" || segment == "argument" ||
598
+
599
+ segment == "this" || segment == "that") {
600
+
601
+ string reg_name;
602
+
603
+ //write pop to virtual segment
604
+
605
+ if (segment == "local") reg_name = "LCL";
606
+
607
+ else if (segment == "argument")reg_name = "ARG";
608
+
609
+ else if (segment == "this")reg_name = "THIS";
610
+
611
+ else if (segment == "that")reg_name = "THAT";
612
+
613
+ //write pop to m_register
614
+
615
+ ofs << "@SP\n";
616
+
617
+ ofs << "M=M-1\n";
618
+
619
+ ofs << "A=M\n";
620
+
621
+ ofs << "D=M\n";
622
+
623
+ ofs << "@" << reg_name << "\n";
624
+
625
+ ofs << "M=D\n";
626
+
627
+ }
628
+
629
+ else if (segment == "temp" || segment == "pointer") {
630
+
631
+ //write pop to static segment
632
+
633
+ if (segment == "temp") {
492
634
 
493
635
 
494
636
 
495
- return C_ARITHMATIC;
496
-
497
- }
498
-
499
- else if (curcmdope == "push") {
500
-
501
- return C_PUSH;
502
-
503
- }
504
-
505
- else if (curcmdope == "pop") {
506
-
507
- return C_POP;
508
-
509
- }
510
-
511
- else {
512
-
513
- return C_ERROR;
514
-
515
- }
516
-
517
- }
518
-
519
-
520
-
521
- string Parser::arg1()
522
-
523
- {
524
-
525
- string curcmdarg1 = curcmd;
526
-
527
-
528
-
529
- if (commandType() == C_RETURN) {
530
-
531
- cout << "C_RETURNは呼ぶな" << endl;
532
-
533
- return curcmd;
534
-
535
- }
536
-
537
- else if (commandType() == C_ARITHMATIC) {
538
-
539
- return curcmd;
540
-
541
- }
542
-
543
- else if (commandType() == C_PUSH || commandType() == C_POP) {
637
+ //write pop to m_register
638
+
639
+ ofs << "@SP\n";
640
+
641
+ ofs << "M=M-1\n";
642
+
643
+ ofs << "A=M\n";
644
+
645
+ ofs << "D=M\n";
646
+
647
+ ofs << "@0\n";
648
+
649
+ for (int i(0); i < 3 + index; i++) {
650
+
651
+ ofs << "A=A+1\n";
652
+
653
+ }
654
+
655
+ ofs << "M=D";
656
+
657
+
658
+
659
+ }
660
+
661
+ else if (segment == "pointer") {
662
+
663
+ //write pop to m_register
664
+
665
+ ofs << "@SP\n";
666
+
667
+ ofs << "M=M-1\n";
668
+
669
+ ofs << "A=M\n";
670
+
671
+ ofs << "D=M\n";
672
+
673
+ ofs << "@0\n";
674
+
675
+ for (int i(0); i < 5 + index; i++) {
676
+
677
+ ofs << "A=A+1\n";
678
+
679
+ }
680
+
681
+ ofs << "M=D";
682
+
683
+ }
684
+
685
+ }
686
+
687
+ }
688
+
689
+ }
690
+
691
+
692
+
693
+ string CodeWriter::get_new_label() {
694
+
695
+ label_num += 1;
696
+
697
+ return "LABEL"+to_string(label_num);
698
+
699
+ }
700
+
701
+ void CodeWriter::writeArithmatic(string command)
702
+
703
+ {
704
+
705
+ if (command == "add") {
706
+
707
+ //write pop to m_register
708
+
709
+ ofs << "@SP\n";
710
+
711
+ ofs << "M=M-1\n";
712
+
713
+ ofs << "A=M\n";
714
+
715
+ ofs << "D=M\n";
716
+
717
+ //write pop to m_register
718
+
719
+ ofs << "@SP\n";
720
+
721
+ ofs << "M=M-1\n";
722
+
723
+ ofs << "A=M\n";
724
+
725
+
726
+
727
+ ofs << "D=D+M\n";
728
+
729
+
730
+
731
+ //write push from D_register
732
+
733
+ ofs << "@SP\n";
734
+
735
+ ofs << "A=M\n";
736
+
737
+ ofs << "M=D\n";
738
+
739
+ ofs << "@SP\n";
740
+
741
+ ofs << "M=M+1\n";
742
+
743
+ }
744
+
745
+ else if (command == "sub") {
746
+
747
+ //write pop to m_register
748
+
749
+ ofs << "@SP\n";
750
+
751
+ ofs << "M=M-1\n";
752
+
753
+ ofs << "A=M\n";
754
+
755
+ ofs << "D=M\n";
756
+
757
+ //write pop to m_register
758
+
759
+ ofs << "@SP\n";
760
+
761
+ ofs << "M=M-1\n";
762
+
763
+ ofs << "A=M\n";
764
+
765
+ ofs << "D=M-D\n";
766
+
767
+
768
+
769
+ //write push from D_register
770
+
771
+ ofs << "@SP\n";
772
+
773
+ ofs << "A=M\n";
774
+
775
+ ofs << "M=D\n";
776
+
777
+ ofs << "@SP\n";
778
+
779
+ ofs << "M=M+1\n";
780
+
781
+ }
782
+
783
+ else if (command == "and") {
784
+
785
+ //write pop to m_register
786
+
787
+ ofs << "@SP\n";
788
+
789
+ ofs << "M=M-1\n";
790
+
791
+ ofs << "A=M\n";
792
+
793
+ ofs << "D=M\n";
794
+
795
+ //write pop to m_register
796
+
797
+ ofs << "@SP\n";
798
+
799
+ ofs << "M=M-1\n";
800
+
801
+ ofs << "A=M\n";
802
+
803
+
804
+
805
+ ofs << "D=D&M\n";
806
+
807
+
808
+
809
+ //write push from D_register
810
+
811
+ ofs << "@SP\n";
812
+
813
+ ofs << "A=M\n";
814
+
815
+ ofs << "M=D\n";
816
+
817
+ ofs << "@SP\n";
818
+
819
+ ofs << "M=M+1\n";
820
+
821
+ }
544
822
 
545
823
 
546
824
 
547
- while (curcmdarg1[0] != ' ') {
548
-
549
- curcmdarg1.erase(curcmdarg1.begin() + 0);
550
-
551
- }
825
+ }
552
-
553
- while (curcmdarg1[0] == ' ') {
826
+
554
-
827
+
828
+
555
- curcmdarg1.erase(curcmdarg1.begin() + 0);
829
+ void CodeWriter::close()
830
+
556
-
831
+ {
832
+
833
+ ofs.close();
834
+
557
- }
835
+ }
558
-
559
-
560
-
561
- curcmdarg1 = curcmdarg1.substr(0, curcmdarg1.find(" "));
562
-
563
-
564
-
565
- return curcmdarg1;
566
-
567
- }
568
-
569
- else {
570
-
571
- cout << "そのようなコマンドは存在しない" << endl;
572
-
573
- }
574
-
575
- }
576
-
577
-
578
-
579
- int Parser::arg2()
580
-
581
- {
582
-
583
- string curcmdarg2 = curcmd;
584
-
585
-
586
-
587
- if (commandType() == C_PUSH || commandType() == C_POP ||
588
-
589
- commandType() == C_FUNCTION || commandType() == C_CALL) {
590
-
591
-
592
-
593
- while (curcmdarg2[0] != ' ') {
594
-
595
- curcmdarg2.erase(curcmdarg2.begin() + 0);
596
-
597
- }
598
-
599
- while (curcmdarg2[0] == ' ') {
600
-
601
- curcmdarg2.erase(curcmdarg2.begin() + 0);
602
-
603
- }
604
-
605
- while (curcmdarg2[0] != ' ') {
606
-
607
- curcmdarg2.erase(curcmdarg2.begin() + 0);
608
-
609
- }
610
-
611
- while (curcmdarg2[0] == ' ') {
612
-
613
- curcmdarg2.erase(curcmdarg2.begin() + 0);
614
-
615
- }
616
-
617
- curcmdarg2 = curcmdarg2.substr(0, curcmdarg2.find(" "));
618
-
619
- //cout << "curcmdarg2 " << curcmdarg2 << endl;
620
-
621
- return stoi(curcmdarg2);
622
-
623
- }
624
-
625
- }
626
-
627
-
628
-
629
- コード
630
836
 
631
837
  ```