Skip to content

Loop Body Termination in ThinkPy Parser #35

@lwgray

Description

@lwgray

Issue Description

The current ThinkPy parser incorrectly includes statements outside of loop bodies within the loop's AST. This causes premature loop termination when executing statements that should be outside the loop.

Example of the Problem

# Original Code
objective "Test list operations"
task "Lists":
    step "Process": 
        items = []
        for i in range(3): 
            items = items + [i]
        print(items)
        print(items[1])
run "Lists"

Current Parser Behavior

The parser incorrectly includes both print statements in the loop's body in the AST:

'body': [
    {'type': 'assignment', ...},
    {'type': 'function_call', 'name': 'print', 'arguments': ['items']},
    {'type': 'function_call', 'name': 'print', 'arguments': [{'type': 'index'...}]}
]

Constraints

The solution must:

  1. Clearly define loop body boundaries
  2. Not require additional keywords (like 'end')
  3. Not use curly braces or parentheses
  4. Not use semicolons
  5. Not rely on specific keywords after the loop
  6. Not require additional step definitions
  7. Keep the language simple and Pythonic
  8. Support multiple statements within loop bodies

Desired Solution

  1. Move to a indent/dedent syntax

Temporary Hack

  1. Use END to indicate the end of a loop body
# Original Code
objective "Test list operations"
task "Lists":
    step "Process": 
        items = []
        for i in range(3):
           items = items + [I]
        end
        print(items)
        print(items[1])
run "Lists"

Questions to Consider

  1. How can we unambiguously determine where a loop body ends?
  2. What parsing strategies could maintain simplicity while providing clear boundaries?
  3. Are there existing language designs that solve this elegantly without the above constraints?

Impact

This issue affects:

  • Loop execution
  • Program flow control
  • Statement grouping
  • AST generation accuracy

Labels

  • bug
  • parser
  • needs-discussion
  • syntax-design

Priority

High - This affects basic program flow control and execution accuracy.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions