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.

translate 1.6KB

6 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
  5. # Demand file as input
  6. if len(sys.argv) < 2:
  7. print("Usage: /path/to/translate CPP_FILE")
  8. sys.exit(1)
  9. FILE_IN = sys.argv[1]
  10. # Read file.
  11. lines_in = []
  12. with open(FILE_IN) as file:
  13. for line in file:
  14. lines_in.append(line.rstrip())
  15. # Translate.
  16. lines_out = []
  17. isDeclaration = False
  18. isDeclarationLine = False
  19. isBody = False
  20. functionName = None
  21. returnType = None
  22. argumentNames = []
  23. argumentTypes = []
  24. for ln in lines_in:
  25. ln = ln.lstrip()
  26. # Ignore includes.
  27. if "#include" in ln:
  28. continue
  29. parts = ln.split(" ")
  30. count = len(parts)
  31. lastPart = parts[count - 1]
  32. if not isBody and lastPart.endswith("("):
  33. isDeclaration = True
  34. isDeclarationLine = True
  35. isBody = False
  36. # End of function arguments.
  37. if not isDeclarationLine and not isBody and ln.endswith(") {"):
  38. isDeclaration = False
  39. isBody = True
  40. # Function argument.
  41. if (
  42. isDeclaration and
  43. not isDeclarationLine and
  44. not isBody and
  45. not ln.endswith(") {")
  46. ):
  47. print(f"TODO argument: '{ln}'")
  48. # Function return type and function name.
  49. if isDeclaration and lastPart.endswith("("):
  50. functionName = lastPart[:-1]
  51. returnType = ln[:-len(lastPart) - 1] # -1 for space before function name
  52. print(f"Function name: '{functionName}'")
  53. print(f"Return type: '{returnType}'")
  54. isDeclarationLine = False
  55. lines_out.append(ln)
  56. # Output result.
  57. print(lines_out)