From 4711290decf293b0180f509168a94d3a37b647a2 Mon Sep 17 00:00:00 2001 From: Marion Date: Thu, 05 Sep 2013 22:16:22 +0000 Subject: get_type(...) can now guess the return type of binary operators - including boolean operators and comparison operators --- (limited to 'TurtleArt/tatype.py') diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 055fc2f..cf7bb03 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -131,6 +131,19 @@ def get_type(x): # unary operands never change the type of their argument elif isinstance(x, ast.UnaryOp): return get_type(x.operand) + # boolean and comparison operators always return a boolean + if isinstance(x, (ast.BoolOp, ast.Compare)): + return (TYPE_BOOL, True) + # other binary operators + elif isinstance(x, ast.BinOp): + type_left = get_type(x.left)[0] + if type_left == TYPE_STRING: + return (TYPE_STRING, True) + type_right = get_type(x.right)[0] + if type_left == type_right == TYPE_INT: + return (TYPE_INT, True) + else: + return (TYPE_FLOAT, True) return (TYPE_OBJECT, isinstance(x, ast.AST)) -- cgit v0.9.1