Advertisement
asqapro

ProtoSelfGen

Jun 28th, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.57 KB | None | 0 0
  1. def main():
  2.     task = raw_input("Enter what the program must do: ") #ex: for: x, string, print a, print b; if: x, ==, a, print a
  3.     task += ";"
  4.     getKeyWords(task)
  5.     taskDict = dict()
  6.     if task.find(";"): #if there are multiple commands
  7.         index = 0 #the first command is at the start of the string
  8.         while task.find(";") != -1:
  9.             taskDict[(task[index:task.find(":")].strip())] = task[task.find(":")+2:task.find(";")] #create a dictionary entry of the control type and task
  10.             task = task.replace(task[index:task.find(";")], "", ) #remove the dictionaried structure and task
  11.             index = task.find(";")+1 #update the index
  12.             task = task.replace(";", "", 1)
  13.     else: #if there is a single command, things are simple
  14.         taskDict[(task[:task.find(":")].strip())] = task[task.find(":")+2:]
  15.         task = task.replace(task[:task.find(":")+1], "")
  16.     program = open("programFile.py", "w")
  17.     for entry in taskDict:
  18.         buildStructures(program, entry, taskDict[entry])
  19.     program.close()
  20.  
  21. def getKeyWords(task):
  22.     keyWords = ["sum"]
  23.     #keyWords.append("print")
  24.     foundKeyWords = []
  25.     for entry in keyWords:
  26.         pass
  27.         #while task.find(entry) != -1:
  28.         #    index = task.find(entry)
  29.         #    task = task.replace(entry, "", 1)
  30.         #    foundKeyWords.append(entry)
  31.         #    task = task.replace(entry, "", 1)
  32.    
  33.  
  34. def getCommands(task): #create the list of commands specified by the task string
  35.     commands = [] #create the list that will hold the commands
  36.     while task: #as long as task still has another command to be processes
  37.         if task.find(",") == -1: #if it is on the final command
  38.             nextCmd = task.strip() #get the next command and remove whitespace
  39.             task = task.replace(task[:], "") #clear task
  40.         else:
  41.             nextCmd = task[:task.find(",")].strip() #get the next command and remove extra whitespace
  42.         commands.append(nextCmd) #place the command in the list of commands
  43.         task = task.replace(task[:task.find(",")+1], "", 1) #remove the command from the original task string
  44.     return commands
  45.  
  46. def buildStructures(program, which, task): #create the forLoop commands
  47.     if which == "for": #if a for loop is being made
  48.         iterator = task[:task.find(",")].strip() #find the end of the first item and remove leading and trailing whitespace
  49.         task = task.replace(task[:task.find(",")+1], "", 1) #remove the first item from the provided items
  50.  
  51.         loopItem = task[:task.find(",")].strip()
  52.         task = task.replace(task[:task.find(",")+1], "", 1)
  53.  
  54.         commands = getCommands(task)
  55.         forLoop(program, iterator, loopItem, commands) #create the structure using the variables built
  56.     elif which == "while":
  57.         variable1 = task[:task.find(",")].strip()
  58.         task = task.replace(task[:task.find(",")+1], "", 1)
  59.  
  60.         compare = task[:task.find(",")].strip()
  61.         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
  62.             commands = getCommands(task)
  63.             whileLoop(program, commands, variable1, None, None)
  64.         else:
  65.             task = task.replace(task[:task.find(",")+1], "", 1)
  66.            
  67.             variable2 = task[:task.find(",")].strip()
  68.             task = task.replace(task[:task.find(",")+1], "", 1)
  69.  
  70.             commands = getCommands(task)
  71.             whileLoop(program, commands, variable1, compare, variable2)
  72.     elif which == "if": #same as while creation
  73.         variable1 = task[:task.find(",")].strip()
  74.         task = task.replace(task[:task.find(",")+1], "", 1)
  75.        
  76.         compare = task[:task.find(",")].strip()
  77.         if compare != any(("==", "!=", "not", "is", "is not", "in", "not in", "is in")):
  78.             commands = getCommands(task)
  79.             ifStatement(program, commands, variable1, None, None)
  80.         else:
  81.             task = task.replace(task[:task.find(",")+1], "", 1)
  82.            
  83.             variable2 = task[:task.find(",")].strip()
  84.             task = task.replace(task[:task.find(",")+1], "", 1)
  85.             commands = getCommands(task)
  86.             ifStatement(program, commands, variable1, compare, variable2)
  87.     elif which == "elif": #same as if/ while
  88.         variable1 = task[:task.find(",")].strip()
  89.         task = task.replace(task[:task.find(",")+1], "", 1)
  90.  
  91.         compare = task[:task.find(",")].strip()
  92.         if compare != any(("==", "!=", "not", "is", "is not", "in", "not in", "is in")):
  93.             commands = getCommands(task)
  94.             elifStatement(program, commands, variable1, None, None)
  95.         task = task.replace(task[:task.find(",")+1], "", 1)
  96.        
  97.         variable2 = task[:task.find(",")].strip()
  98.         task = task.replace(task[:task.find(",")+1], "", 1)
  99.  
  100.         commands = getCommands(task)
  101.         elifStatement(program, commands, variable1, compare, variable2)
  102.     elif which == "else":
  103.         commands = getCommands(task)
  104.         elseStatement(programcommands)
  105.     else:
  106.         print "Error, requested control structure not found."
  107.  
  108.  
  109. def forLoop(program, iterator, loopItem, commands): #write a for loop to the file
  110.     if isinstance(loopItem, basestring): #if the item being looped over is a string*
  111.         program.write("for " + iterator + " in \"" + loopItem + "\":\n") #write it with quotes around it
  112.     else:
  113.         program.write("for " + iterator + " in " + loopItem + ":\n")
  114.     for cmd in commands: #loop over the list of commands and print them to the file
  115.         program.write("\t" + cmd + "\n")
  116.     #*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
  117.  
  118. def whileLoop(program, commands, variable1, compare, variable2): #compare and variable2 are not always set
  119.     if not compare: #if there is only 1 variable (ex: while true)
  120.         if isinstance(variable1, basestring): #if the variable is a string, quotes it
  121.             program.write("while \"" + variable1 + "\":\n")
  122.         else:
  123.             program.write("while " + variable1 + ":\n")
  124.     else:
  125.         if isinstance(variable1, basestring): #same here, just checking two variables
  126.             program.write("while \"" + variable1 + "\" " + compare)
  127.         else:
  128.             program.write("while " + variable1 + " " + compare)
  129.         if isinstance(variable2, basestring):
  130.             program.write(" \"" + variable2 + "\":\n")
  131.         else:
  132.             program.write(variable2 + "\":\n")
  133.     for cmd in commands:
  134.         program.write("\t" + cmd + "\n")
  135.     program.write("\tbreak")
  136.  
  137. def ifStatement(program, commands, variable1, compare, variable2):
  138.     if not compare: #if there is only 1 variable (ex: if variable)
  139.         if isinstance(variable1, basestring): #if the variable is a string, put quotes around it
  140.             program.write("if \"" + variable1 + "\":\n")
  141.         else: #otherwise just print the variable
  142.             program.write("if " + variable1 + ":\n")
  143.     else: #if there are two variables (ex: if variable == variable)
  144.         if isinstance(variable1, basestring): #same here, just checking two variables
  145.             program.write("if \"" + variable1 + "\" " + compare)
  146.         else:
  147.             program.write("if " + variable1 + " " + compare)
  148.         if isinstance(variable2, basestring):
  149.             program.write(" \"" + variable2 + "\":\n")
  150.         else:
  151.             program.write(variable2 + "\":\n")
  152.     for cmd in commands:
  153.         program.write("\t" + cmd + "\n")
  154.  
  155. def elifStatement(program, commands, variable1, compare, variable2):
  156.     if not compare: #if there is only 1 variable (ex: if variable)
  157.         if isinstance(variable1, basestring): #if the variable is a string, put quotes around it
  158.             program.write("elif \"" + variable1 + "\":\n")
  159.         else: #otherwise just print the variable
  160.             program.write("elif " + variable1 + ":\n")
  161.     else: #if there are two variables (ex: if variable == variable)
  162.         if isinstance(variable1, basestring): #same here, just checking two variables
  163.             program.write("elif \"" + variable1 + "\" " + compare)
  164.         else:
  165.             program.write("elif " + variable1 + " " + compare)
  166.         if isinstance(variable2, basestring):
  167.             program.write(" \"" + variable2 + "\":\n")
  168.         else:
  169.             program.write(variable2 + "\":\n")
  170.     for cmd in commands:
  171.         program.write("\t" + cmd + "\n")
  172.  
  173. def elseStatement(program, commands):
  174.     program.write("else:\n")
  175.     for cmd in commands:
  176.         program.write("\t" + cmd + "\n")
  177. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement