Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CPP.py 4.5KB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from Function import *
  2. def includes():
  3. return """#include <map>
  4. #include <string>
  5. #include <vector>
  6. #include "memory_Context.h"
  7. """
  8. def replaceAnd(s):
  9. return s.replace("and", "&&")
  10. def replaceAppend(s):
  11. return s.replace(".append(", ".push_back(")
  12. def replaceComment(s):
  13. return s.replace("#", "//")
  14. def replaceLen(s):
  15. posLen = s.find("len(")
  16. posEnd = s.find(")", posLen)
  17. if (
  18. posLen == -1 or
  19. posEnd == -1
  20. ):
  21. return s
  22. before = s[:posLen]
  23. name = s[posLen + len("len("):posEnd]
  24. after = s[posEnd + len(")"):]
  25. return f"{before}{name}.size(){after}"
  26. def replaceTrue(s):
  27. return s.replace("True", "true")
  28. def translateParameter(s):
  29. # name: type -> type name
  30. parts = s.split(": ")
  31. indent = len(s) - len(s.lstrip())
  32. name = parts[0].lstrip()
  33. t = translateType(parts[1])
  34. indentation = "".join(" " * indent)
  35. return f"{indentation}{t} {name}"
  36. def translateStatement(s, state):
  37. indent = len(s) - len(s.lstrip())
  38. indentation = "".join(" " * indent)
  39. ss = s.lstrip()
  40. posColon = ss.find(": ")
  41. posComma = ss.find(", ")
  42. posCtx = ss.find("c.")
  43. posEqual = ss.find(" = ")
  44. posFor = ss.find("for ")
  45. posIn = ss.find(" in ")
  46. posRange = ss.find("range(")
  47. posRangeEnd = ss.find("):")
  48. posClosingScope = ss.find("#}")
  49. posOpenSquareBracket = ss.find("[")
  50. # #} -> }
  51. if posClosingScope != -1:
  52. return f"{indentation}}}"
  53. # for name in range(x, y): -> for (auto name = x; name < y; ++name) {
  54. if (
  55. posFor >= 0 and
  56. posIn >= 0 and
  57. posRange >= 0
  58. ):
  59. name = ss[posFor + len("for "):posIn]
  60. x = ss[posRange + len("range("):posComma]
  61. y = ss[posComma + len(", "):posRangeEnd]
  62. return f"{indentation}for (auto {name} = {x}; {name} < {y}; ++{name}) {{"
  63. # name: type = value -> type name = value
  64. if (
  65. posColon >= 0 and
  66. posEqual >= 0
  67. ):
  68. name = ss[:posColon]
  69. type = ss[posColon + len(": "):posEqual]
  70. t = translateType(type)
  71. value = ss[posEqual + len(" = "):]
  72. return f"{indentation}{t} {name} = {value};"
  73. # name = value -> auto name = value
  74. if (
  75. posCtx == -1 and
  76. posColon == -1 and
  77. posOpenSquareBracket == -1 and
  78. posEqual >= 0
  79. ):
  80. name = ss[:posEqual]
  81. # Skip prepending 'auto' each time variable is assigned,
  82. # only do it the first time
  83. if name not in state.varNames:
  84. state.varNames[name] = True
  85. value = ss[posEqual + len(" = "):]
  86. return f"{indentation}auto {name} = {value};"
  87. # Keep "if ("
  88. if ss == "if (":
  89. state.isIf = True
  90. return s
  91. # Keep "if not ("
  92. if ss == "if not (":
  93. state.isIfNot = True
  94. return f"{indentation}if (!("
  95. # ): -> }
  96. if ss == "):":
  97. # if
  98. if state.isIf:
  99. state.isIf = False
  100. return f"{indentation}) {{"
  101. # if not
  102. state.isIfNot = False
  103. return f"{indentation})) {{"
  104. ending = ";"
  105. if state.isIf or state.isIfNot:
  106. ending = ""
  107. # Unknown.
  108. return f"{s}{ending}"
  109. def translateType(s):
  110. # dict[X, Y] -> std::map<X, Y>
  111. if s.startswith("dict["):
  112. kv = s[len("dict["):-len("]")]
  113. parts = kv.split(", ")
  114. return f"std::map<{parts[0]}, {parts[1]}>"
  115. # str -> std::string
  116. if s == "str":
  117. return "std::string"
  118. # Unknown. Return as is.
  119. return s
  120. class CPP:
  121. def __init__(self, fn):
  122. self.fn = fn
  123. self.isIf = False
  124. self.isIfNot = False
  125. self.varNames = {}
  126. def translate(self):
  127. returnType = translateType(self.fn.returnType)
  128. # Parameters.
  129. params = []
  130. for i in range(0, len(self.fn.parameters)):
  131. p = translateParameter(self.fn.parameters[i])
  132. # Make Context passed by reference.
  133. #if "Context" in p:
  134. # p = p.replace("Context", "Context&")
  135. params.append(p)
  136. strparams = "\n".join(params)
  137. if (len(strparams) > 0):
  138. strparams += "\n"
  139. # Statements.
  140. sts = []
  141. for i in range(0, len(self.fn.statements)):
  142. s = translateStatement(self.fn.statements[i], self)
  143. s = replaceAnd(s)
  144. s = replaceAppend(s)
  145. # Replace len twice to account for double invocation.
  146. s = replaceLen(s)
  147. s = replaceLen(s)
  148. s = replaceTrue(s)
  149. sts.append(s)
  150. strstatements = "\n".join(sts)
  151. return f"""{returnType} {self.fn.name}(
  152. {strparams}) {{
  153. {strstatements}
  154. }}
  155. """