forked from M-Labs/artiq
1
0
Fork 0

Add support for NameConstant.

This commit is contained in:
whitequark 2015-06-06 15:12:40 +03:00
parent eb76f594a0
commit d08598fa0f
2 changed files with 17 additions and 3 deletions

View File

@ -155,6 +155,10 @@ class TValue(Type):
def __ne__(self, other): def __ne__(self, other):
return not (self == other) return not (self == other)
def TNone():
"""The type of None."""
return TMono("NoneType")
def TBool(): def TBool():
"""A boolean type.""" """A boolean type."""
return TMono("bool") return TMono("bool")
@ -193,6 +197,9 @@ class TypePrinter(object):
self.map[typ] = "'%s" % next(self.gen) self.map[typ] = "'%s" % next(self.gen)
return self.map[typ] return self.map[typ]
elif isinstance(typ, TMono): elif isinstance(typ, TMono):
if typ.params == {}:
return typ.name
else:
return "%s(%s)" % (typ.name, ", ".join( return "%s(%s)" % (typ.name, ", ".join(
["%s=%s" % (k, self.name(typ.params[k])) for k in typ.params])) ["%s=%s" % (k, self.name(typ.params[k])) for k in typ.params]))
elif isinstance(typ, TTuple): elif isinstance(typ, TTuple):

View File

@ -215,6 +215,13 @@ class Inferencer(algorithm.Transformer):
return asttyped.NameT(type=self._find_name(node.id, node.loc), return asttyped.NameT(type=self._find_name(node.id, node.loc),
id=node.id, ctx=node.ctx, loc=node.loc) id=node.id, ctx=node.ctx, loc=node.loc)
def visit_NameConstant(self, node):
if node.value is True or node.value is False:
typ = types.TBool()
elif node.value is None:
typ = types.TNone()
return asttyped.NameConstantT(type=typ, value=node.value, loc=node.loc)
def visit_Tuple(self, node): def visit_Tuple(self, node):
node = self.generic_visit(node) node = self.generic_visit(node)
return asttyped.TupleT(type=types.TTuple([x.type for x in node.elts]), return asttyped.TupleT(type=types.TTuple([x.type for x in node.elts]),
@ -303,7 +310,7 @@ class Printer(algorithm.Visitor):
def generic_visit(self, node): def generic_visit(self, node):
if hasattr(node, "type"): if hasattr(node, "type"):
self.rewriter.insert_after(node.loc, self.rewriter.insert_after(node.loc,
":%s".format(self.type_printer.name(node.type))) ":{}".format(self.type_printer.name(node.type)))
super().generic_visit(node) super().generic_visit(node)