Optimizer.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "Optimizer.h"
  2. #include "Parser.h"
  3. #include <iostream>
  4. #include <vector>
  5. using namespace zp;
  6. using namespace std;
  7. unique_ptr<BlockInstruction> AstVisitor::visit(Block& block)
  8. {
  9. return block.accept(*this);
  10. }
  11. unique_ptr<BlockInstruction> Optimizer::visitInstructionBlock(InstructionBlock& ib)
  12. {
  13. std::cout << "InstructionBlock" << std::endl;
  14. unique_ptr<SimpleBlockInstruction> sbi = make_unique<SimpleBlockInstruction>();
  15. for (auto instr : ib.instructions) {
  16. switch(instr) {
  17. case Instruction::LEFT:
  18. sbi->pointerMoved--;
  19. break;
  20. case Instruction::RIGHT:
  21. sbi->pointerMoved++;
  22. break;
  23. case Instruction::PLUS:
  24. sbi->added[sbi->pointerMoved]++;
  25. break;
  26. case Instruction::MINUS:
  27. sbi->added[sbi->pointerMoved]--;
  28. break;
  29. default:
  30. break;
  31. }
  32. }
  33. return sbi;
  34. }
  35. unique_ptr<BlockInstruction> Optimizer::visitIOBlock(IOBlock& io)
  36. {
  37. cout << "IOBlock" << std::endl;
  38. return make_unique<SimpleBlockInstruction>();
  39. }
  40. unique_ptr<BlockInstruction> Optimizer::visitUnionBlock(UnionBlock& ub)
  41. {
  42. cout << "UnionBlock" << std::endl;
  43. for (auto& block : ub.instructions) {
  44. visit(*block.get());
  45. }
  46. return make_unique<SimpleBlockInstruction>();
  47. }
  48. unique_ptr<BlockInstruction> Optimizer::visitLoop(Loop& loop)
  49. {
  50. cout << "Loop" << std::endl;
  51. for (auto& block : loop.instructions) {
  52. visit(*block.get());
  53. }
  54. return make_unique<SimpleBlockInstruction>();
  55. }