better ast cleanup
- try to fix block parsing
This commit is contained in:
parent
92a365d908
commit
c2678fe360
1 changed files with 27 additions and 12 deletions
|
@ -175,7 +175,8 @@ def read_start(reader: Reader):
|
|||
token = reader.next()
|
||||
|
||||
if token is None:
|
||||
return []
|
||||
print('eof!')
|
||||
return None
|
||||
|
||||
ast = []
|
||||
res = []
|
||||
|
@ -184,13 +185,23 @@ def read_start(reader: Reader):
|
|||
|
||||
# handle blocks
|
||||
if token.value == '{':
|
||||
while True:
|
||||
token = reader.next()
|
||||
# next can be a whitespace, or a }
|
||||
token = reader.next()
|
||||
|
||||
if token.value == '}':
|
||||
break
|
||||
print('block start!, next:', token)
|
||||
|
||||
res.extend(read_start(reader))
|
||||
if token.type_ == TokenType.whitespace:
|
||||
# keep going on reading
|
||||
while True:
|
||||
token = reader.next()
|
||||
print(token)
|
||||
|
||||
if token.value == '}':
|
||||
break
|
||||
|
||||
res.extend(read_start(reader))
|
||||
elif token.value == '}':
|
||||
res = []
|
||||
|
||||
# import, fn, etc
|
||||
elif token.type_ == TokenType.reserved:
|
||||
|
@ -205,21 +216,25 @@ def read_start(reader: Reader):
|
|||
ast.append(res)
|
||||
return ast
|
||||
|
||||
|
||||
def read_loop(reader: Reader):
|
||||
"""Read the AST."""
|
||||
final_ast = []
|
||||
|
||||
while True:
|
||||
ast = read_start(reader)
|
||||
|
||||
if not ast:
|
||||
# break when eof
|
||||
if ast is None:
|
||||
break
|
||||
|
||||
inner = ast[0]
|
||||
try:
|
||||
inner = ast[0]
|
||||
except IndexError:
|
||||
inner = None
|
||||
|
||||
if not inner:
|
||||
break
|
||||
|
||||
final_ast.append(ast)
|
||||
if ast and not inner:
|
||||
final_ast.append(ast)
|
||||
|
||||
return final_ast
|
||||
|
||||
|
|
Loading…
Reference in a new issue