Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def main():
- task = raw_input("Enter what the program must do: ") #ex: for: x, string, print a, print b; if: x, ==, a, print a
- task += ";"
- getKeyWords(task)
- taskDict = dict()
- if task.find(";"): #if there are multiple commands
- index = 0 #the first command is at the start of the string
- while task.find(";") != -1:
- taskDict[(task[index:task.find(":")].strip())] = task[task.find(":")+2:task.find(";")] #create a dictionary entry of the control type and task
- task = task.replace(task[index:task.find(";")], "", ) #remove the dictionaried structure and task
- index = task.find(";")+1 #update the index
- task = task.replace(";", "", 1)
- else: #if there is a single command, things are simple
- taskDict[(task[:task.find(":")].strip())] = task[task.find(":")+2:]
- task = task.replace(task[:task.find(":")+1], "")
- program = open("programFile.py", "w")
- for entry in taskDict:
- buildStructures(program, entry, taskDict[entry])
- program.close()
- def getKeyWords(task):
- keyWords = ["sum"]
- #keyWords.append("print")
- foundKeyWords = []
- for entry in keyWords:
- pass
- #while task.find(entry) != -1:
- # index = task.find(entry)
- # task = task.replace(entry, "", 1)
- # foundKeyWords.append(entry)
- # task = task.replace(entry, "", 1)
- def getCommands(task): #create the list of commands specified by the task string
- commands = [] #create the list that will hold the commands
- while task: #as long as task still has another command to be processes
- if task.find(",") == -1: #if it is on the final command
- nextCmd = task.strip() #get the next command and remove whitespace
- task = task.replace(task[:], "") #clear task
- else:
- nextCmd = task[:task.find(",")].strip() #get the next command and remove extra whitespace
- commands.append(nextCmd) #place the command in the list of commands
- task = task.replace(task[:task.find(",")+1], "", 1) #remove the command from the original task string
- return commands
- def buildStructures(program, which, task): #create the forLoop commands
- if which == "for": #if a for loop is being made
- iterator = task[:task.find(",")].strip() #find the end of the first item and remove leading and trailing whitespace
- task = task.replace(task[:task.find(",")+1], "", 1) #remove the first item from the provided items
- loopItem = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- commands = getCommands(task)
- forLoop(program, iterator, loopItem, commands) #create the structure using the variables built
- elif which == "while":
- variable1 = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- compare = task[:task.find(",")].strip()
- if compare != any(("==", "!=", "not", "is", "is not", "in", "not in", "is in")): #if there is only 1 variable, pass in compare and variable2 as None
- commands = getCommands(task)
- whileLoop(program, commands, variable1, None, None)
- else:
- task = task.replace(task[:task.find(",")+1], "", 1)
- variable2 = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- commands = getCommands(task)
- whileLoop(program, commands, variable1, compare, variable2)
- elif which == "if": #same as while creation
- variable1 = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- compare = task[:task.find(",")].strip()
- if compare != any(("==", "!=", "not", "is", "is not", "in", "not in", "is in")):
- commands = getCommands(task)
- ifStatement(program, commands, variable1, None, None)
- else:
- task = task.replace(task[:task.find(",")+1], "", 1)
- variable2 = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- commands = getCommands(task)
- ifStatement(program, commands, variable1, compare, variable2)
- elif which == "elif": #same as if/ while
- variable1 = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- compare = task[:task.find(",")].strip()
- if compare != any(("==", "!=", "not", "is", "is not", "in", "not in", "is in")):
- commands = getCommands(task)
- elifStatement(program, commands, variable1, None, None)
- task = task.replace(task[:task.find(",")+1], "", 1)
- variable2 = task[:task.find(",")].strip()
- task = task.replace(task[:task.find(",")+1], "", 1)
- commands = getCommands(task)
- elifStatement(program, commands, variable1, compare, variable2)
- elif which == "else":
- commands = getCommands(task)
- elseStatement(programcommands)
- else:
- print "Error, requested control structure not found."
- def forLoop(program, iterator, loopItem, commands): #write a for loop to the file
- if isinstance(loopItem, basestring): #if the item being looped over is a string*
- program.write("for " + iterator + " in \"" + loopItem + "\":\n") #write it with quotes around it
- else:
- program.write("for " + iterator + " in " + loopItem + ":\n")
- for cmd in commands: #loop over the list of commands and print them to the file
- program.write("\t" + cmd + "\n")
- #*I realise the item can only be a string, as it is created from raw_input. This is here for later when there is no user input
- def whileLoop(program, commands, variable1, compare, variable2): #compare and variable2 are not always set
- if not compare: #if there is only 1 variable (ex: while true)
- if isinstance(variable1, basestring): #if the variable is a string, quotes it
- program.write("while \"" + variable1 + "\":\n")
- else:
- program.write("while " + variable1 + ":\n")
- else:
- if isinstance(variable1, basestring): #same here, just checking two variables
- program.write("while \"" + variable1 + "\" " + compare)
- else:
- program.write("while " + variable1 + " " + compare)
- if isinstance(variable2, basestring):
- program.write(" \"" + variable2 + "\":\n")
- else:
- program.write(variable2 + "\":\n")
- for cmd in commands:
- program.write("\t" + cmd + "\n")
- program.write("\tbreak")
- def ifStatement(program, commands, variable1, compare, variable2):
- if not compare: #if there is only 1 variable (ex: if variable)
- if isinstance(variable1, basestring): #if the variable is a string, put quotes around it
- program.write("if \"" + variable1 + "\":\n")
- else: #otherwise just print the variable
- program.write("if " + variable1 + ":\n")
- else: #if there are two variables (ex: if variable == variable)
- if isinstance(variable1, basestring): #same here, just checking two variables
- program.write("if \"" + variable1 + "\" " + compare)
- else:
- program.write("if " + variable1 + " " + compare)
- if isinstance(variable2, basestring):
- program.write(" \"" + variable2 + "\":\n")
- else:
- program.write(variable2 + "\":\n")
- for cmd in commands:
- program.write("\t" + cmd + "\n")
- def elifStatement(program, commands, variable1, compare, variable2):
- if not compare: #if there is only 1 variable (ex: if variable)
- if isinstance(variable1, basestring): #if the variable is a string, put quotes around it
- program.write("elif \"" + variable1 + "\":\n")
- else: #otherwise just print the variable
- program.write("elif " + variable1 + ":\n")
- else: #if there are two variables (ex: if variable == variable)
- if isinstance(variable1, basestring): #same here, just checking two variables
- program.write("elif \"" + variable1 + "\" " + compare)
- else:
- program.write("elif " + variable1 + " " + compare)
- if isinstance(variable2, basestring):
- program.write(" \"" + variable2 + "\":\n")
- else:
- program.write(variable2 + "\":\n")
- for cmd in commands:
- program.write("\t" + cmd + "\n")
- def elseStatement(program, commands):
- program.write("else:\n")
- for cmd in commands:
- program.write("\t" + cmd + "\n")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement