code_generation.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from visitor import Visitor
  2. from utils import FileOutputer
  3. from utils import Enum
  4. from utils import Logger
  5. from compiler_exceptions import CodeBlockException
  6. NOT_SET = 0
  7. SPACES_USED = 1
  8. TABS_USED = 2
  9. Languages = Enum("Python","CSharp","Javascript")
  10. Platforms = Enum("Threads","GameLoop")
  11. class CodeGenerator(Visitor):
  12. def __init__(self):
  13. self.supported_platforms = []
  14. def generate(self, class_diagram, output_file, platform):
  15. self.platform = platform
  16. if self.platform not in self.supported_platforms :
  17. Logger.showError("Unsupported platform.")
  18. return False
  19. try :
  20. self.fOut = FileOutputer(output_file)
  21. class_diagram.accept(self)
  22. finally :
  23. self.fOut.close()
  24. return True
  25. def writeCodeCorrectIndent(self, body):
  26. lines = body.split('\n')
  27. while( len(lines) > 0 and lines[-1].strip() == "") :
  28. del(lines[-1])
  29. index = 0;
  30. while( len(lines) > index and lines[index].strip() == "") :
  31. index += 1
  32. if index >= len(lines) :
  33. return
  34. #first index where valid code is present
  35. to_strip_index = len(lines[index].rstrip()) - len(lines[index].strip())
  36. indent_type = NOT_SET;
  37. while index < len(lines):
  38. strip_part = lines[index][:to_strip_index]
  39. if( ('\t' in strip_part and ' ' in strip_part) or
  40. (indent_type == SPACES_USED and '\t' in strip_part) or
  41. (indent_type == TABS_USED and ' ' in strip_part)
  42. ) :
  43. raise CodeBlockException("Mixed tab and space indentation!")
  44. if indent_type == NOT_SET :
  45. if ' ' in strip_part :
  46. indent_type = SPACES_USED
  47. elif '\t' in strip_part :
  48. indent_type = TABS_USED
  49. self.fOut.write(lines[index][to_strip_index:])
  50. index += 1