Make if ... else if ... chains iterative, rather than recursive.

Should save some stack if the chain is long.
This commit is contained in:
Sei Lisa 2014-07-31 05:41:15 +02:00
parent 5bedc17b73
commit cb66f1ff4e

View file

@ -1198,21 +1198,25 @@ class parser(object):
if value is None: if value is None:
return {'nt':'RETURN', 't':None} return {'nt':'RETURN', 't':None}
return {'nt':'RETURN', 't':None, 'ch':[self.autocastcheck(value, ReturnType)]} return {'nt':'RETURN', 't':None, 'ch':[self.autocastcheck(value, ReturnType)]}
if tok0 == 'IF': # TODO: Convert this into a loop for ELSE IF's, saving recursion if tok0 == 'IF':
ret = {'nt':'IF', 't':None, 'ch':[]}
cur = ret
while True:
self.NextToken() self.NextToken()
self.expect('(') self.expect('(')
self.NextToken() self.NextToken()
condition = self.Parse_expression() cur['ch'].append(self.Parse_expression())
self.expect(')') self.expect(')')
self.NextToken() self.NextToken()
then_branch = self.Parse_statement(ReturnType) cur['ch'].append(self.Parse_statement(ReturnType))
else_branch = None
if self.tok[0] == 'ELSE': if self.tok[0] == 'ELSE':
self.NextToken() self.NextToken()
else_branch = self.Parse_statement(ReturnType) if self.tok[0] == 'IF':
if else_branch is not None: cur['ch'].append({'nt':'IF', 't':None, 'ch':[]})
return {'nt':'IF', 't':None, 'ch':[condition, then_branch, else_branch]} cur = cur['ch'][2]
return {'nt':'IF', 't':None, 'ch':[condition, then_branch]} continue
cur['ch'].append(self.Parse_statement(ReturnType))
return ret
if tok0 == 'WHILE': if tok0 == 'WHILE':
self.NextToken() self.NextToken()