Skip to content

Commit c89bf6f

Browse files
author
whitequark
committedJun 12, 2015
Add support for UnaryOp.
1 parent df68613 commit c89bf6f

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed
 

‎artiq/py2llvm/types.py

+12
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ def TList(elt=None):
180180
return TMono("list", {"elt": elt})
181181

182182

183+
def is_var(typ):
184+
return isinstance(typ, TVar)
185+
186+
def is_mono(typ, name, **params):
187+
return isinstance(typ, TMono) and \
188+
typ.name == name and typ.params == params
189+
190+
def is_numeric(typ):
191+
return isinstance(typ, TMono) and \
192+
typ.name in ('int', 'float')
193+
194+
183195
class TypePrinter(object):
184196
"""
185197
A class that prints types using Python-like syntax and gives

‎artiq/py2llvm/typing.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,27 @@ def visit_BoolOp(self, node):
284284
op_locs=node.op_locs, loc=node.loc)
285285
return self.visit(node)
286286

287+
def visit_UnaryOp(self, node):
288+
node = self.generic_visit(node)
289+
node = asttyped.UnaryOpT(type=types.TVar(),
290+
op=node.op, operand=node.operand,
291+
loc=node.loc)
292+
return self.visit(node)
293+
294+
def visit_BinOp(self, node):
295+
node = self.generic_visit(node)
296+
node = asttyped.BinOpT(type=types.TVar(),
297+
left=node.left, op=node.op, right=node.right,
298+
loc=node.loc)
299+
return self.visit(node)
300+
301+
def visit_Compare(self, node):
302+
node = self.generic_visit(node)
303+
node = asttyped.CompareT(type=types.TVar(),
304+
left=node.left, ops=node.ops, comparators=node.comparators,
305+
loc=node.loc)
306+
return self.visit(node)
307+
287308
# Visitors that just unify types
288309
#
289310
def visit_ListT(self, node):
@@ -310,6 +331,21 @@ def visit_BoolOpT(self, node):
310331
node.loc, value.loc, self._makenotes_elts(node.values, "an operand"))
311332
return node
312333

334+
def visit_UnaryOpT(self, node):
335+
if isinstance(node.op, ast.Not):
336+
node.type = types.TBool()
337+
else:
338+
operand_type = node.operand.type.find()
339+
if types.is_numeric(operand_type):
340+
node.type = operand_type
341+
elif not types.is_var(operand_type):
342+
diag = diagnostic.Diagnostic("error",
343+
"expected operand to be of numeric type, not {type}",
344+
{"type": types.TypePrinter().name(operand_type)},
345+
node.operand.loc)
346+
self.engine.process(diag)
347+
return node
348+
313349
def visit_Assign(self, node):
314350
node = self.generic_visit(node)
315351
if len(node.targets) > 1:
@@ -375,7 +411,6 @@ def visit_unsupported(self, node):
375411
visit_SetComp = visit_unsupported
376412
visit_Str = visit_unsupported
377413
visit_Starred = visit_unsupported
378-
visit_UnaryOp = visit_unsupported
379414
visit_Yield = visit_unsupported
380415
visit_YieldFrom = visit_unsupported
381416

‎lit-test/py2llvm/typing/error_unify.py

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@
1616
1 and False
1717
# CHECK-L: note: an operand of type int(width='a)
1818
# CHECK-L: note: an operand of type bool
19+
20+
# CHECK-L: ${LINE:+1}: error: expected operand to be of numeric type, not list(elt='a)
21+
~[]

‎lit-test/py2llvm/typing/unify.py

+6
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@
4141

4242
1 and 0
4343
# CHECK-L: 1:int(width='g) and 0:int(width='g):int(width='g)
44+
45+
~1
46+
# CHECK-L: 1:int(width='h):int(width='h)
47+
48+
not 1
49+
# CHECK-L: 1:int(width='i):bool

0 commit comments

Comments
 (0)
Please sign in to comment.