diff --git a/interpreter.py b/interpreter.py index 941d6bd95..cfd820171 100644 --- a/interpreter.py +++ b/interpreter.py @@ -1589,17 +1589,23 @@ class Interpreter(): def evaluate_arithmeticstatement(self, cur): l = self.to_native(self.evaluate_statement(cur.left)) 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': - return l + r + try: + return l + r + except Exception as e: + raise InvalidCode('Invalid use of addition: ' + str(e)) elif cur.operation == 'sub': + if not isinstance(l, int) or not isinstance(r, int): + raise InvalidCode('Subtraction works only with integers.') return l - r elif cur.operation == 'mul': + if not isinstance(l, int) or not isinstance(r, int): + raise InvalidCode('Multiplication works only with integers.') return l * r elif cur.operation == 'div': + if not isinstance(l, int) or not isinstance(r, int): + raise InvalidCode('Division works only with integers.') return l // r else: raise InvalidCode('You broke me.') diff --git a/test cases/failing/11 object arithmetic/meson.build b/test cases/failing/11 object arithmetic/meson.build index 34e3a7a5e..9a7a6565c 100644 --- a/test cases/failing/11 object arithmetic/meson.build +++ b/test cases/failing/11 object arithmetic/meson.build @@ -1,3 +1,3 @@ -project('object arithmetic') +project('object arithmetic', 'c') foo = '5' + meson diff --git a/test cases/failing/12 string arithmetic/meson.build b/test cases/failing/12 string arithmetic/meson.build index 753f62b25..c02a865a4 100644 --- a/test cases/failing/12 string arithmetic/meson.build +++ b/test cases/failing/12 string arithmetic/meson.build @@ -1,3 +1,3 @@ -project('string arithmetic') +project('string arithmetic', 'c') -foo = 'a' / 'b' +foo = 'a' + 3 diff --git a/test cases/failing/13 array arithmetic/meson.build b/test cases/failing/13 array arithmetic/meson.build index ceaa8bc08..3ddf06092 100644 --- a/test cases/failing/13 array arithmetic/meson.build +++ b/test cases/failing/13 array arithmetic/meson.build @@ -1,3 +1,3 @@ -project('array arithmetic') +project('array arithmetic', 'c') -foo = ['a', 'b'] * ['6', '4'] +foo = ['a', 'b'] * 3