master
Voussoir 2015-08-20 23:09:50 -07:00
parent 79ebb44c65
commit 53202191ee
1 changed files with 24 additions and 12 deletions

View File

@ -23,7 +23,7 @@ def join(text=None, spacer=None, *trash):
return return
return spacer.join(text.split(' ')) return spacer.join(text.split(' '))
def default_function(*trash): def whatever(*trash):
return 'Look what I found: ' + str(trash) return 'Look what I found: ' + str(trash)
@ -33,6 +33,8 @@ FUNCTION_MAP = {
'exponent': exponent, 'exponent': exponent,
'join': join 'join': join
} }
DEFAULT_FUNCTION = whatever
FUNCTION_MAP = {c.lower():FUNCTION_MAP[c] for c in FUNCTION_MAP}
COMMAND_IDENTIFIERS = ['robot!'] COMMAND_IDENTIFIERS = ['robot!']
@ -48,21 +50,22 @@ SAMPLE_COMMENTS = [
'robot! add 1 1\nrobot! multiply 1 1', 'robot! add 1 1\nrobot! multiply 1 1',
' do ROBOT! add boom box', ' do ROBOT! add boom box',
'robot! robot!', 'robot! robot!',
'robot! add'
''] '']
COMMAND_IDENTIFIERS = [c.lower() for c in COMMAND_IDENTIFIERS] COMMAND_IDENTIFIERS = [c.lower() for c in COMMAND_IDENTIFIERS]
def handle_line(text): def functionmap_line(text):
print('User said:', text) print('User said:', text)
elements = shlex.split(text) elements = shlex.split(text)
print('Broken into:', elements) print('Broken into:', elements)
results = []
for element_index, element in enumerate(elements): for element_index, element in enumerate(elements):
if element.lower() not in COMMAND_IDENTIFIERS: if element.lower() not in COMMAND_IDENTIFIERS:
continue continue
arguments = elements[element_index:] arguments = elements[element_index:]
assert arguments.pop(0).lower() in COMMAND_IDENTIFIERS assert arguments.pop(0).lower() in COMMAND_IDENTIFIERS
#arguments = arguments[1:]
# If the user has multiple command calls on one line # If the user has multiple command calls on one line
# (Which is stupid but they might do it anyway) # (Which is stupid but they might do it anyway)
@ -77,26 +80,35 @@ def handle_line(text):
print('Did nothing') print('Did nothing')
continue continue
function = FUNCTION_MAP.get(arguments[0], default_function) command = arguments[0].lower()
actual_function = command in FUNCTION_MAP
function = FUNCTION_MAP.get(command, DEFAULT_FUNCTION)
print('Using function:', function.__name__) print('Using function:', function.__name__)
if function is not default_function: if actual_function:
# When using the default function, we want to pass in the # Currently, the first argument is the name of the command
# first word because it might be important # If we found an actual function, we can remove that
# For other commands, we don't need it because add() # (because add() doesn't need "add" as the first arg)
# doesn't need the string "add" for anything. # If we're using the default, let's keep that first arg
# because it might be important.
arguments = arguments[1:] arguments = arguments[1:]
result = function(*arguments) result = function(*arguments)
print('Output: %s' % result) print('Output: %s' % result)
results.append(results)
return results
def handle_comment(comment): def functionmap_comment(comment):
lines = comment.split('\n') lines = comment.split('\n')
results = []
for line in lines: for line in lines:
handle_line(line) result = functionmap_line(line)
if result is not None:
results += result
return results
for comment in SAMPLE_COMMENTS: for comment in SAMPLE_COMMENTS:
print() print()
handle_comment(comment) functionmap_comment(comment)
''' '''
User said: robot! add 1 2 User said: robot! add 1 2