Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: List Literals Not Working in For Loops and Enumerate #37

Open
lwgray opened this issue Nov 24, 2024 · 1 comment
Open

Bug: List Literals Not Working in For Loops and Enumerate #37

lwgray opened this issue Nov 24, 2024 · 1 comment
Assignees
Labels

Comments

@lwgray
Copy link
Owner

lwgray commented Nov 24, 2024

Current Behavior

Basic for loops and enumerate loops fail when trying to iterate over list literals.

Error Examples

# This currently fails
%%thinkpy --explain
objective "Test For Loop"
task "loop":
    step "simple range loop":
        for index, value in enumerate([1,2,3]):
            print(index, value)
        end
run "loop"

# This also fails
%%thinkpy --explain
objective "Test For Loop"
task "loop":
    step "simple range loop":
        for item in [1,2,3]:
            print(item)
        end
run "loop"

Expected Behavior

Both patterns above should work:

  • Direct iteration over list literals for item in [1,2,3]
  • Enumeration over list literals for index, value in enumerate([1,2,3])

Test Cases

class TestListLiterals:
    def test_basic_list_iteration(self, parser, interpreter):
        """Test simple iteration over list literal"""
        code = '''
        objective "Test Basic List Iteration"
        task "Basic List":
            step "Iterate":
                sum = 0
                for item in [1, 2, 3]:
                    sum = sum + item
                end
        run "Basic List"'''
        
        ast = parser.parse(code)
        interpreter.execute(ast)
        assert interpreter.state["sum"] == 6

    def test_enumerate_list_literal(self, parser, interpreter):
        """Test enumeration over list literal"""
        code = '''
        objective "Test Enumerate List"
        task "Enumerate List":
            step "Iterate":
                indices = []
                values = []
                for idx, val in enumerate([10, 20, 30]):
                    indices = indices + [idx]
                    values = values + [val]
                end
        run "Enumerate List"'''
        
        ast = parser.parse(code)
        interpreter.execute(ast)
        assert interpreter.state["indices"] == [0, 1, 2]
        assert interpreter.state["values"] == [10, 20, 30]

    def test_nested_list_literals(self, parser, interpreter):
        """Test iteration over nested list literals"""
        code = '''
        objective "Test Nested Lists"
        task "Nested Lists":
            step "Iterate":
                flattened = []
                for sublist in [[1, 2], [3, 4]]:
                    for item in sublist:
                        flattened = flattened + [item]
                    end
                end
        run "Nested Lists"'''
        
        ast = parser.parse(code)
        interpreter.execute(ast)
        assert interpreter.state["flattened"] == [1, 2, 3, 4]

    def test_mixed_variable_and_literal(self, parser, interpreter):
        """Test mixing variable references and list literals in loops"""
        code = '''
        objective "Test Mixed Variables and Literals"
        task "Mixed":
            step "Setup":
                nums = [1, 2]
            step "Iterate":
                results = []
                for x in nums:
                    for y in [10, 20]:
                        results = results + [x * y]
                    end
                end
        run "Mixed"'''
        
        ast = parser.parse(code)
        interpreter.execute(ast)
        assert interpreter.state["results"] == [10, 20, 20, 40]

    def test_enumerate_nested_lists(self, parser, interpreter):
        """Test enumeration over nested list literals"""
        code = '''
        objective "Test Enumerate Nested Lists"
        task "Enumerate Nested":
            step "Iterate":
                positions = []
                sums = []
                for pos, pair in enumerate([[1, 2], [3, 4]]):
                    positions = positions + [pos]
                    sums = sums + [pair[0] + pair[1]]
                end
        run "Enumerate Nested"'''
        
        ast = parser.parse(code)
        interpreter.execute(ast)
        assert interpreter.state["positions"] == [0, 1]
        assert interpreter.state["sums"] == [3, 7]
@lwgray lwgray added bug Something isn't working parser complexity : Master labels Nov 24, 2024
@lwgray lwgray self-assigned this Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants
@lwgray and others