我有一个简化的程序,如下所示:
def listAssign(lst,index,item):
"""Assigns item item to list lst at index index, returns modified list."""
lst[index] = item
return lst
def listInsert(lst,index,item):
"""Inserts item item to list lst at index index, returns modified list."""
lst.insert(index.item)
return lst
# ...
def listSurgery(lst,indices,f,*extraArgs):
"""Performs operation f on list lst at depth at indices indices, returns modified list."""
parent = lst
for index in indices[:-1]:
parent = parent[index]
parent = f(parent,indices[-1],*extraArgs)
return listSurgery(lst,indices[:-1],listAssign,parent)
# ...
def parseStringToList(s):
"""Takes in a user-input string, and converts it into a list to be passed into parseListToExpr."""
for c in s[:]: # Removes extra spaces from beginning of string
if c == ' ':
s = s[1:]
else:
break
for c in s[::-1]: # Removes spaces from end of string
if c == ' ':
s = s[:-1]
else:
break
l = [] # List to build from string; built by serially appending stuff as it comes up
b = True # Bool for whether the parser is experiencing spaces (supposed to be True if last character processed was a space)
t = False # Bool for whether the parser is experiencing a string of non-alphanumeric characters (supposed to be True if last character was a non-alphanumeric character)
lvl = 0 # Keeps track of depth at which operations are supposed to be occurring
for c in s:
if c == ' ': # If the character is a space, do nothing, but make sure that it's documented that the last character processed was a space
b = True
elif c == '(' or c == '[': # If the character is an opening parenthesis, add a new level of depth to the list, by appending another list to the lowest level and incrementing lvl
l = listSurgery(l,[-1]*(lvl+1),listInsert,[])
lvl += 1
if c == '[': # ] (left here for overzealous parsers) If the bracket is square, also append a comma as the first element of the appended list
l = listSurgery(l,[-1]*(lvl+1),listInsert,',')
elif c == ')' or c == ']': # If it's a closing parenthesis, make it stop paying attention to the list it's working on
lvl -= 1
elif c == ',': # If it's a comma, then append it to the list as a separate element and start a new string to append characters to
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),[',',''])
elif not c.alnum(): # If it's non-alphanumeric and not any of the above, then append it to string that it's working on if it's a non-alphanumeric string
if not t: # If the string that it's working on isn't non-alphanumeric, then finish working on that string and append a new string to work on
l = listSurgery(l,[-1]*(lvl+1),listInsert,'')
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),c)
t = True
else: # If the character is alphanumeric, append it to the string it's working on
assert c.isalnum()
if b or t: # If the string it's working on isn't alphanumeric or doesn't exist, append a new string
l = listSurgery(l,[-1]*(lvl+1),listInsert,'')
b, t = False, False
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),c)
return l
# ...
cmds = {
'help':"List the available functions.",
'quit':"Quit this application.",
'exit':"Exit this application (exactly the same thing)."
}
print "MAT (Michael's Analysis Tool), version 0.0.0"
op = '' # Declare string to use for input
while op != 'exit' and op != 'quit': # Keep a REPL unless the user types "exit" or "quit", in which case exit
op = raw_input("> ")
if op == 'help':
print "The commands available in addition to free evaluation are:"
for item in cmds:
print ' ', item + ":", cmds[item]
elif op == 'quit' or 'exit':
pass
else:
print str(parseStringToList(op))
当我使用help
, quit
, orexit
作为命令时,效果很好。但是当我给它任何其他类型的输入时,例如1 + 1 = 2
(which should elicit ['1','+','1','=','2']
) 或This is a sentence.
(which should elicit ['This','is','a','sentence','.']
,它不会打印任何内容,只是要求更多输入。(也就是说,它在功能上等同于该程序的最后一行替换为continue
。)而且我不明白为什么。parseStringToList
以非常简单的方式定义。它至少应该返回[]
它返回的变量的原始值。但是它不打印任何东西。为什么?(我会明天尝试更彻底的调试,但到目前为止我还没有努力。)