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':
self.NextToken() ret = {'nt':'IF', 't':None, 'ch':[]}
self.expect('(') cur = ret
self.NextToken() while True:
condition = self.Parse_expression()
self.expect(')')
self.NextToken()
then_branch = self.Parse_statement(ReturnType)
else_branch = None
if self.tok[0] == 'ELSE':
self.NextToken() self.NextToken()
else_branch = self.Parse_statement(ReturnType) self.expect('(')
if else_branch is not None: self.NextToken()
return {'nt':'IF', 't':None, 'ch':[condition, then_branch, else_branch]} cur['ch'].append(self.Parse_expression())
return {'nt':'IF', 't':None, 'ch':[condition, then_branch]} self.expect(')')
self.NextToken()
cur['ch'].append(self.Parse_statement(ReturnType))
if self.tok[0] == 'ELSE':
self.NextToken()
if self.tok[0] == 'IF':
cur['ch'].append({'nt':'IF', 't':None, 'ch':[]})
cur = cur['ch'][2]
continue
cur['ch'].append(self.Parse_statement(ReturnType))
return ret
if tok0 == 'WHILE': if tok0 == 'WHILE':
self.NextToken() self.NextToken()