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.
This commit is contained in:
Sei Lisa 2014-07-26 04:41:09 +02:00
parent 841413a2cf
commit e03b342f78
2 changed files with 17 additions and 5 deletions

View file

@ -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

View file

@ -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)