code.js 964 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var Class = require("rtl.js").Class;
  2. var NullCodeGenerator = Class.extend({
  3. init: function NullCodeGenerator(){},
  4. write: function(){}
  5. });
  6. exports.Generator = Class.extend({
  7. init: function CodeGenerator(){
  8. this.__result = "";
  9. this.__indent = "";
  10. },
  11. write: function(s){
  12. this.__result += s.replace(/\n/g, "\n" + this.__indent);
  13. },
  14. openScope: function(){
  15. this.__indent += "\t";
  16. this.__result += "{\n" + this.__indent;
  17. },
  18. closeScope: function(ending){
  19. this.__indent = this.__indent.substr(1);
  20. this.__result = this.__result.substr(0, this.__result.length - 1) + "}";
  21. if (ending)
  22. this.write(ending);
  23. else
  24. this.write("\n");
  25. },
  26. getResult: function(){return this.__result;}
  27. });
  28. exports.SimpleGenerator = Class.extend({
  29. init: function SimpleCodeGenerator(code){this.__result = code ? code : "";},
  30. write: function(s){this.__result += s;},
  31. result: function(){return this.__result;}
  32. });
  33. exports.nullGenerator = new NullCodeGenerator();