Add support for BoolOp.

This commit is contained in:
whitequark 2015-06-11 04:20:33 +03:00
parent b8ce3f85bd
commit 4b7d4c2425
3 changed files with 24 additions and 10 deletions

View File

@ -301,18 +301,24 @@ class Inferencer(algorithm.Transformer):
def visit_BoolOp(self, node): def visit_BoolOp(self, node):
node = self.generic_visit(node) node = self.generic_visit(node)
for value, op_loc in zip(node.values, node.op_locs): node = asttyped.BoolOpT(type=types.TVar(),
def makenotes(printer, typea, typeb, loca, locb):
return [
diagnostic.Diagnostic("note",
"py2llvm requires boolean operations to have boolean operands", {},
op_loc)
]
self._unify(value.type, types.TBool(),
value.loc, None, makenotes)
return asttyped.BoolOpT(type=types.TBool(),
op=node.op, values=node.values, op=node.op, values=node.values,
op_locs=node.op_locs, loc=node.loc) op_locs=node.op_locs, loc=node.loc)
def makenotes(printer, typea, typeb, loca, locb):
return [
diagnostic.Diagnostic("note",
"an operand of type {typea}",
{"typea": printer.name(node.values[0].type)},
node.values[0].loc),
diagnostic.Diagnostic("note",
"an operand of type {typeb}",
{"typeb": printer.name(typeb)},
locb)
]
for value in node.values:
self._unify(node.type, value.type,
node.loc, value.loc, makenotes)
return node
# Visitors that just unify types # Visitors that just unify types
# #

View File

@ -11,3 +11,8 @@ a = b
[1, []] [1, []]
# CHECK-L: note: a list of type list(elt=int(width='a)) # CHECK-L: note: a list of type list(elt=int(width='a))
# CHECK-L: note: a list element of type list(elt='b) # CHECK-L: note: a list element of type list(elt='b)
# CHECK-L: ${LINE:+1}: error: cannot unify int(width='a) with bool
1 and False
# CHECK-L: note: an operand of type int(width='a)
# CHECK-L: note: an operand of type bool

View File

@ -38,3 +38,6 @@ j += [1.0]
True and False True and False
# CHECK-L: True:bool and False:bool:bool # CHECK-L: True:bool and False:bool:bool
1 and 0
# CHECK-L: 1:int(width='g) and 0:int(width='g):int(width='g)