aboutsummaryrefslogtreecommitdiff
path: root/interpreter.py
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2014-11-16 19:56:22 +0200
committerJussi Pakkanen <jpakkane@gmail.com>2014-11-16 19:56:22 +0200
commite37424c9e16ebe7bdb374400d52616f9dcfd5074 (patch)
tree9efeb746fceace59951f84d086251b019a46d613 /interpreter.py
parent707e721dd37519eb6cb3256d3a30778ac4bb0340 (diff)
downloadmeson-e37424c9e16ebe7bdb374400d52616f9dcfd5074.zip
meson-e37424c9e16ebe7bdb374400d52616f9dcfd5074.tar.gz
meson-e37424c9e16ebe7bdb374400d52616f9dcfd5074.tar.bz2
More strict type checking for arithmetic operations.
Diffstat (limited to 'interpreter.py')
-rw-r--r--interpreter.py14
1 files changed, 10 insertions, 4 deletions
diff --git a/interpreter.py b/interpreter.py
index 941d6bd..cfd8201 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.')