lwgray / think

MIT License
0 stars 1 forks source link

Smart Subtask Name Handling Fix #16 #17

Closed lwgray closed 1 week ago

lwgray commented 1 week ago

Description

This PR implements smart subtask name handling in the ThinkPy interpreter, allowing users to define subtasks with readable names (spaces and proper capitalization) while calling them using standard programming conventions (lowercase with underscores).

Changes

  1. Updated execute_function_call in interpreter.py:

    def execute_function_call(self, func_call):
    """Execute a function call"""
    func_name = func_call['name']
    args = [self.evaluate_expression(arg) for arg in func_call['arguments']]
    
    # Check for built-in functions
    if func_name in self.builtins:
        return self.builtins[func_name](*args)
    
    # Try exact match first
    if func_name in self.subtasks:
        return self.execute_subtask(func_name)
    
    # Try converting the function name to possible subtask names
    converted_name = func_name.replace('_', ' ').title()
    if converted_name in self.subtasks:
        return self.execute_subtask(converted_name)
    
    raise RuntimeError(f"Unknown function: {func_name}")

Testing

Example program that now works:

objective "Test subtask naming"

task "Test" {
    subtask "Calculate Total" {
        return 42
    }

    step "Use It" {
        result = calculate_total()  # Works now!
        print(result)
    }
}

run "Test"

Related Issues

Closes #123

Documentation Updates

Will need to update the documentation to explain:

Screenshots

Before:

ThinkPy Error: Unknown function: calculate_total

After:

[OUTPUT] 42

Testing Instructions

  1. Run the example program above
  2. Try variations of subtask names:
    • "Calculate Total" -> calculate_total()
    • "Process Data" -> process_data()
    • "Validate Input" -> validate_input()
  3. Verify that exact matches still work
  4. Verify helpful error messages for truly unknown functions

Checklist