From e03b342f78a5bf9dd831c8d3886eb48c2c318b02 Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Sat, 26 Jul 2014 04:41:09 +0200 Subject: [PATCH] Allow concatenation of key+string and string+key producing string. Also add corresponding test cases, making some mostly cosmetic changes to the test program while on it. --- lslopt/lslparse.py | 12 ++++++++++-- testparser.py | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lslopt/lslparse.py b/lslopt/lslparse.py index b86369a..f1a0a43 100644 --- a/lslopt/lslparse.py +++ b/lslopt/lslparse.py @@ -886,6 +886,7 @@ class parser(object): #or op == '-' and type2 not in ('integer', 'float', # 'vector', 'rotation'): raise EParseTypeMismatch(self) + # Isolate the additions where the types match to make our life easier later if op == '+' and (type1 == type2 or type1 == 'list' or type2 == 'list'): if type1 == type2 == 'key': # key + key is the only disallowed combo of equals @@ -902,6 +903,13 @@ class parser(object): # list + (list)nonlist and same goes for nonlist + list, they # don't compile to the same thing, but the optimizer should deal # with typecast removal anyway. + elif self.allowkeyconcat and op == '+' \ + and type1 in ('key', 'string') and type2 in ('key', 'string'): + # Allow key addition (but add explicit cast) + if type1 == 'key': + term = [S[op], S[type2], [S['CAST'], S[type2], term], value] + else: + term = [S[op], S[type1], term, [S['CAST'], S[type1], value]] elif type1 == 'key' or type2 == 'key': # Only list + key or key + list is allowed, otherwise keys can't # be added or subtracted with anything. @@ -1663,8 +1671,8 @@ class parser(object): # the correctness of the output) self.explicitcast = 'explicitcast' in options - # TODO: Allow string + key - #self.allowkeyconcat = 'allowkeyconcat' in options + # Allow string + key = string and key + string = string + self.allowkeyconcat = 'allowkeyconcat' in options # Allow C style string composition of strings: "blah" "blah" = "blahblah" self.allowmultistrings = 'allowmultistrings' in options diff --git a/testparser.py b/testparser.py index 12978e7..5999358 100644 --- a/testparser.py +++ b/testparser.py @@ -81,7 +81,7 @@ class Test02Compiler(UnitTestCase): <0,0,0.>0>0>*<0,0,0==0>2,3>>3>3.>%<3,4,5>; f -= TRUE-(integer)-1; f *= !FALSE; - V %= ZERO_VECTOR*ZERO_ROTATION; + V %= (ZERO_VECTOR+-ZERO_VECTOR)*(ZERO_ROTATION+-ZERO_ROTATION); 1e37;1.1e22;1.; print(V *= 3); fwd("","",""); @@ -147,6 +147,8 @@ class Test02Compiler(UnitTestCase): self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){""-(key)"";}''') self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){""+f();}''') self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){""+(key)"";}''') + self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){(key)""+"";}''') + self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){(key)""+(key)"";}''') self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){key k;k+k;}''') self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){3/<1,2,3>;}''') self.assertRaises(EParseTypeMismatch, self.parser.parse, '''f(){3/<1,2,3,4>;}''') @@ -193,10 +195,12 @@ class Test02Compiler(UnitTestCase): integer i; i |= i; "a" "b" "c"; + "a"+(key)"b"; (key)"a" + "b"; i>>=i; }}''', - set(('explicitcast','extendedtypecast','extendedassignment', - 'extendedglobalexpr', 'allowmultistrings')))) + ['explicitcast','extendedtypecast','extendedassignment', + 'extendedglobalexpr', 'allowmultistrings', 'allowkeyconcat'] + )) print self.parser.scopeindex self.assertRaises(EParseUnexpected, self.parser.PopScope)