From cb66f1ff4eea101f3219c1e6f3f363463c21c3ca Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Thu, 31 Jul 2014 05:41:15 +0200 Subject: [PATCH] Make if ... else if ... chains iterative, rather than recursive. Should save some stack if the chain is long. --- lslopt/lslparse.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lslopt/lslparse.py b/lslopt/lslparse.py index 9522446..7b54187 100644 --- a/lslopt/lslparse.py +++ b/lslopt/lslparse.py @@ -1198,21 +1198,25 @@ class parser(object): if value is None: return {'nt':'RETURN', 't':None} 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 - self.NextToken() - self.expect('(') - self.NextToken() - condition = self.Parse_expression() - self.expect(')') - self.NextToken() - then_branch = self.Parse_statement(ReturnType) - else_branch = None - if self.tok[0] == 'ELSE': + if tok0 == 'IF': + ret = {'nt':'IF', 't':None, 'ch':[]} + cur = ret + while True: self.NextToken() - else_branch = self.Parse_statement(ReturnType) - if else_branch is not None: - return {'nt':'IF', 't':None, 'ch':[condition, then_branch, else_branch]} - return {'nt':'IF', 't':None, 'ch':[condition, then_branch]} + self.expect('(') + self.NextToken() + cur['ch'].append(self.Parse_expression()) + 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': self.NextToken()