Skip to content

Commit

Permalink
Fixed a bug with for loops over a send with a block
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Gaynor committed May 11, 2013
1 parent 3757697 commit b3cd7ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_compiler.py
Expand Up @@ -277,6 +277,26 @@ def test_for_loop(self, space):
RETURN RETURN
""") """)


def test_for_loop_over_send_block(self, space):
self.assert_compiles(space, """
for k in f { 1 }
2
end
""", """
LOAD_SELF
LOAD_CONST 0
LOAD_CLOSURE 0
BUILD_BLOCK 1
SEND_BLOCK 1 1
LOAD_CONST 2
LOAD_CLOSURE 0
BUILD_BLOCK 1
SEND_BLOCK 3 1
RETURN
""")

def test_until(self, space): def test_until(self, space):
self.assert_compiles(space, "until false do 5 end", """ self.assert_compiles(space, "until false do 5 end", """
SETUP_LOOP 20 SETUP_LOOP 20
Expand Down
10 changes: 10 additions & 0 deletions topaz/parser.py
Expand Up @@ -12,6 +12,7 @@
class Parser(object): class Parser(object):
def __init__(self, lexer): def __init__(self, lexer):
self.lexer = lexer self.lexer = lexer
self._hidden_scopes = []


def parse(self): def parse(self):
l = LexerWrapper(self.lexer.tokenize()) l = LexerWrapper(self.lexer.tokenize())
Expand All @@ -35,6 +36,13 @@ def save_and_pop_scope(self, node):
child_symtable.parent_symtable.add_subscope(node, child_symtable) child_symtable.parent_symtable.add_subscope(node, child_symtable)
self.lexer.symtable = child_symtable.parent_symtable self.lexer.symtable = child_symtable.parent_symtable


def hide_scope(self):
self._hidden_scopes.append(self.lexer.symtable)
self.lexer.symtable = self.lexer.symtable.parent_symtable

def unhide_scope(self):
self.lexer.symtable = self._hidden_scopes.pop()

def new_token(self, orig, name, value): def new_token(self, orig, name, value):
return Token(name, value, orig.getsourcepos()) return Token(name, value, orig.getsourcepos())


Expand Down Expand Up @@ -1677,10 +1685,12 @@ def for_prod(self, p):
@pg.production("post_for_in : ") @pg.production("post_for_in : ")
def post_for_in(self, p): def post_for_in(self, p):
self.lexer.condition_state.begin() self.lexer.condition_state.begin()
self.hide_scope()


@pg.production("post_for_do : ") @pg.production("post_for_do : ")
def post_for_do(self, p): def post_for_do(self, p):
self.lexer.condition_state.end() self.lexer.condition_state.end()
self.unhide_scope()


@pg.production("primary : CLASS cpath superclass push_local_scope bodystmt END") @pg.production("primary : CLASS cpath superclass push_local_scope bodystmt END")
def primary_class(self, p): def primary_class(self, p):
Expand Down

0 comments on commit b3cd7ae

Please sign in to comment.