Skip to content

Commit

Permalink
transforms.inferencer: accept round(width=n) form (#198).
Browse files Browse the repository at this point in the history
whitequark committed Dec 19, 2015
1 parent 1638f0f commit 95af6da
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions artiq/compiler/transforms/inferencer.py
Original file line number Diff line number Diff line change
@@ -708,6 +708,7 @@ def makenotes(printer, typea, typeb, loca, locb):
elif types.is_builtin(typ, "round"):
valid_forms = lambda: [
valid_form("round(x:float) -> int(width='a)"),
valid_form("round(x:float, width='b:<int literal>) -> int(width='b)")
]

self._unify(node.type, builtins.TInt(),
@@ -718,6 +719,19 @@ def makenotes(printer, typea, typeb, loca, locb):

self._unify(arg.type, builtins.TFloat(),
arg.loc, None)
elif len(node.args) == 1 and len(node.keywords) == 1 and \
builtins.is_numeric(node.args[0].type) and \
node.keywords[0].arg == 'width':
width = node.keywords[0].value
if not (isinstance(width, asttyped.NumT) and isinstance(width.n, int)):
diag = diagnostic.Diagnostic("error",
"the width argument of round() must be an integer literal", {},
node.keywords[0].loc)
self.engine.process(diag)
return

self._unify(node.type, builtins.TInt(types.TValue(width.n)),
node.loc, None)
else:
diagnose(valid_forms())
elif types.is_builtin(typ, "print"):
3 changes: 3 additions & 0 deletions lit-test/test/inferencer/builtin_calls.py
Original file line number Diff line number Diff line change
@@ -30,3 +30,6 @@

# CHECK-L: round:<function round>(1.0:float):int(width='h)
round(1.0)

# CHECK-L: round:<function round>(1.0:float, width=64:int(width='i)):int(width=64)
round(1.0, width=64)

0 comments on commit 95af6da

Please sign in to comment.