Advertisement
jabela

Team C - Python To Pseudocode

Mar 17th, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. # Team C Python to Pseudocode Convertor
  2. import os
  3. linearray = []
  4. pseudocommand = ['IF','THEN','ELSE','ENDCASE','CASE OF','OTHERWISE','ENDCASE','FOR','TO','NEXT','DECLARE','CONSTANT','PROCEDURE','ENDPROCEDURE','CALL','FUNCTION','ENDFUNCTION','OPENFILE','READFILE','CLOSEFILE','INPUT','OUTPUT','PRINT']
  5. pseudoexclusive=['ENDIF','CASE OF','OTHERWISE','ENDCASE','TO','NEXT','DECLARE','CONSTANT','PROCEDURE','ENDPROCEDURE','CALL','FUNCTION','ENDFUNCTION']
  6. conditionalcounter = 0
  7.  
  8. #OUTPUT("Found:", exclusive)
  9.  
  10. def indentchecker(line):
  11. indentcount = 0
  12. for letter in line:
  13. if letter == "":
  14. indentcount += 1
  15. return indentcount
  16.  
  17. def splitline():
  18. file = input("What file would you like to write into?")
  19. writefile = open(file, "w")
  20. for i in linearray:
  21. print(i)
  22. writefile.write(i + "\n")
  23.  
  24. def specialconverter(pyarray): #pyarray is linearray
  25. for pyline2 in pyarray:
  26. wordreplace = pyline2.replace ('print', 'OUTPUT')
  27. for i in pyline2:
  28. thenchecker = 0
  29. if "if" in i:
  30. thenchecker = 1
  31. print("thenchecker:", thenchecker)
  32. #if there is a then, thenchecker = 1, start the new line with then.
  33.  
  34. def lineprint(): #prints out lines into array
  35. file = input("What file would you like to convert? Include .py!")
  36. exists = os.path.isfile(file)
  37. if exists:
  38. openfile = open(file, "r")
  39. readfile = openfile.readlines()
  40. linecounter = 0
  41. for line in readfile:
  42. linecounter += 1
  43. thenchecker = 0
  44. line = line.strip('\n')
  45. # -- IF CHECKER -- #
  46. if "if " in line: #THIS MAKES SURE THAT THE STATEMENT IS INDEED AN IF
  47. indentcount = 0
  48. for letter in line:
  49. if letter == "":
  50. indentcount += 1
  51. thenchecker += 1
  52. print("IF IS detected, then checker incremented")
  53. temporaryline = line.replace("if", "IF")
  54. linearray.append(temporaryline)
  55. space = ""
  56. for i in range(indentcount):
  57. space += ""
  58. linearray.append(space + "THEN")
  59. # -- PRINT CHECKER -- #
  60. elif "print" in line: #APPEND PRINT TO OUTPUT
  61. print("found print")
  62. print(line)
  63. temporaryline = line.replace("print(", "OUTPUT ")
  64. linearray.append(temporaryline)
  65. elif "=" in line:
  66. print("= found")
  67. temporaryline = line.replace("=", "<--")
  68. linearray.append(temporaryline)
  69. elif "for " in line:
  70. print("FOR FOUND")
  71. temporaryline = line.replace("for", "FOR")
  72. linearray.append(temporaryline)
  73. elif "in range" in line:
  74. temporaryline = line.replace("in range(", "TO")
  75. linearray.append(temporaryline)
  76. elif "return" in line:
  77. temporaryline = line.replace("return", "RETURN")
  78. linearray.append(temporaryline)
  79. elif "def " in line:
  80. temporaryline = line.replace("def", "PROCEDURE")
  81. linearray.append(temporaryline)
  82. else:
  83. linearray.append(line)
  84.  
  85. return linearray
  86. else:
  87. print("File invalid")
  88.  
  89. def indent(linearray):
  90. for indarray in linearray:
  91. numspace =' '
  92. indentation=0
  93. n=4
  94. while indarray[:n] == numspace and indarray[n] != '':
  95. numspace += ' '
  96. indentation+=1
  97. n+=4
  98. print("indentation found")
  99. return(indentation)
  100.  
  101. indent(linearray)
  102. lineprint()
  103. splitline()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement