Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Team C Python to Pseudocode Convertor
- import os
- linearray = []
- 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']
- pseudoexclusive=['ENDIF','CASE OF','OTHERWISE','ENDCASE','TO','NEXT','DECLARE','CONSTANT','PROCEDURE','ENDPROCEDURE','CALL','FUNCTION','ENDFUNCTION']
- conditionalcounter = 0
- #OUTPUT("Found:", exclusive)
- def indentchecker(line):
- indentcount = 0
- for letter in line:
- if letter == "":
- indentcount += 1
- return indentcount
- def splitline():
- file = input("What file would you like to write into?")
- writefile = open(file, "w")
- for i in linearray:
- print(i)
- writefile.write(i + "\n")
- def specialconverter(pyarray): #pyarray is linearray
- for pyline2 in pyarray:
- wordreplace = pyline2.replace ('print', 'OUTPUT')
- for i in pyline2:
- thenchecker = 0
- if "if" in i:
- thenchecker = 1
- print("thenchecker:", thenchecker)
- #if there is a then, thenchecker = 1, start the new line with then.
- def lineprint(): #prints out lines into array
- file = input("What file would you like to convert? Include .py!")
- exists = os.path.isfile(file)
- if exists:
- openfile = open(file, "r")
- readfile = openfile.readlines()
- linecounter = 0
- for line in readfile:
- linecounter += 1
- thenchecker = 0
- line = line.strip('\n')
- # -- IF CHECKER -- #
- if "if " in line: #THIS MAKES SURE THAT THE STATEMENT IS INDEED AN IF
- indentcount = 0
- for letter in line:
- if letter == "":
- indentcount += 1
- thenchecker += 1
- print("IF IS detected, then checker incremented")
- temporaryline = line.replace("if", "IF")
- linearray.append(temporaryline)
- space = ""
- for i in range(indentcount):
- space += ""
- linearray.append(space + "THEN")
- # -- PRINT CHECKER -- #
- elif "print" in line: #APPEND PRINT TO OUTPUT
- print("found print")
- print(line)
- temporaryline = line.replace("print(", "OUTPUT ")
- linearray.append(temporaryline)
- elif "=" in line:
- print("= found")
- temporaryline = line.replace("=", "<--")
- linearray.append(temporaryline)
- elif "for " in line:
- print("FOR FOUND")
- temporaryline = line.replace("for", "FOR")
- linearray.append(temporaryline)
- elif "in range" in line:
- temporaryline = line.replace("in range(", "TO")
- linearray.append(temporaryline)
- elif "return" in line:
- temporaryline = line.replace("return", "RETURN")
- linearray.append(temporaryline)
- elif "def " in line:
- temporaryline = line.replace("def", "PROCEDURE")
- linearray.append(temporaryline)
- else:
- linearray.append(line)
- return linearray
- else:
- print("File invalid")
- def indent(linearray):
- for indarray in linearray:
- numspace =' '
- indentation=0
- n=4
- while indarray[:n] == numspace and indarray[n] != '':
- numspace += ' '
- indentation+=1
- n+=4
- print("indentation found")
- return(indentation)
- indent(linearray)
- lineprint()
- splitline()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement