More strict type checking for arithmetic operations.

pull/18/merge
Jussi Pakkanen 10 years ago
parent 707e721dd3
commit e37424c9e1
  1. 14
      interpreter.py
  2. 2
      test cases/failing/11 object arithmetic/meson.build
  3. 4
      test cases/failing/12 string arithmetic/meson.build
  4. 4
      test cases/failing/13 array arithmetic/meson.build

@ -1589,17 +1589,23 @@ class Interpreter():
def evaluate_arithmeticstatement(self, cur): def evaluate_arithmeticstatement(self, cur):
l = self.to_native(self.evaluate_statement(cur.left)) l = self.to_native(self.evaluate_statement(cur.left))
r = self.to_native(self.evaluate_statement(cur.right)) r = self.to_native(self.evaluate_statement(cur.right))
if isinstance(l, str) or isinstance(r, str):
l = str(l)
r = str(r)
if cur.operation == 'add': if cur.operation == 'add':
return l + r try:
return l + r
except Exception as e:
raise InvalidCode('Invalid use of addition: ' + str(e))
elif cur.operation == 'sub': elif cur.operation == 'sub':
if not isinstance(l, int) or not isinstance(r, int):
raise InvalidCode('Subtraction works only with integers.')
return l - r return l - r
elif cur.operation == 'mul': elif cur.operation == 'mul':
if not isinstance(l, int) or not isinstance(r, int):
raise InvalidCode('Multiplication works only with integers.')
return l * r return l * r
elif cur.operation == 'div': elif cur.operation == 'div':
if not isinstance(l, int) or not isinstance(r, int):
raise InvalidCode('Division works only with integers.')
return l // r return l // r
else: else:
raise InvalidCode('You broke me.') raise InvalidCode('You broke me.')

@ -1,3 +1,3 @@
project('object arithmetic') project('object arithmetic', 'c')
foo = '5' + meson foo = '5' + meson

@ -1,3 +1,3 @@
project('string arithmetic') project('string arithmetic', 'c')
foo = 'a' / 'b' foo = 'a' + 3

@ -1,3 +1,3 @@
project('array arithmetic') project('array arithmetic', 'c')
foo = ['a', 'b'] * ['6', '4'] foo = ['a', 'b'] * 3

Loading…
Cancel
Save