PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ast/While.h

https://bitbucket.org/timcerexhe/jolog
C Header | 53 lines | 32 code | 12 blank | 9 comment | 0 complexity | a750551590642aa4f216d06f5c32af0a MD5 | raw file
  1. /*
  2. * While.h
  3. *
  4. * Created on: Aug 19, 2009
  5. * Author: timothyc
  6. */
  7. #ifndef WHILE
  8. #define WHILE
  9. #include "JologNode.h"
  10. #include "opcodes.h"
  11. class While : public JologNode {
  12. public:
  13. While(JologNode* condition, JologNode* body) :
  14. JologNode("while statement"), condition(condition), body(body) {}
  15. virtual ~While() {
  16. delete condition;
  17. delete body;
  18. }
  19. virtual JologType getType() const {
  20. return JologType::Void();
  21. }
  22. virtual void emit(ostream& out, StackDepth& stack) const {
  23. int initial = stack.final();
  24. string startPoint = newLabel();
  25. string endPoint = newLabel();
  26. //start
  27. out << opcodes::label(startPoint);
  28. //condition
  29. condition->emit(out, stack);
  30. out << opcodes::branchIfTrue(endPoint, true, stack);
  31. //body
  32. body->emit(out, stack);
  33. out << opcodes::gotoLabel(startPoint);
  34. out << opcodes::label(endPoint);
  35. checkStack(stack.final(), initial, "while loop");
  36. }
  37. private:
  38. JologNode* condition;
  39. JologNode* body;
  40. };
  41. #endif