forked from M-Labs/artiq
1
0
Fork 0

Move builtin.is_{builtin,exn_constructor} to types.

This commit is contained in:
whitequark 2015-07-16 14:56:39 +03:00
parent 5000f87dfc
commit a6950bf11d
3 changed files with 24 additions and 24 deletions

View File

@ -120,19 +120,6 @@ def is_collection(typ):
return isinstance(typ, types.TTuple) or \ return isinstance(typ, types.TTuple) or \
types.is_mono(typ, "list") types.is_mono(typ, "list")
def is_builtin(typ, name):
typ = typ.find()
return isinstance(typ, types.TBuiltin) and \
typ.name == name
def is_exn_constructor(typ, name=None):
typ = typ.find()
if name is not None:
return isinstance(typ, types.TExceptionConstructor) and \
typ.name == name
else:
return isinstance(typ, types.TExceptionConstructor)
def is_mutable(typ): def is_mutable(typ):
return typ.fold(False, lambda accum, typ: return typ.fold(False, lambda accum, typ:
is_list(typ) or types.is_function(typ)) is_list(typ) or types.is_function(typ))

View File

@ -432,7 +432,7 @@ class Inferencer(algorithm.Visitor):
node.func.loc, notes=valid_forms) node.func.loc, notes=valid_forms)
self.engine.process(diag) self.engine.process(diag)
if builtins.is_builtin(typ, "bool"): if types.is_builtin(typ, "bool"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("bool() -> bool"), valid_form("bool() -> bool"),
valid_form("bool(x:'a) -> bool") valid_form("bool(x:'a) -> bool")
@ -448,7 +448,7 @@ class Inferencer(algorithm.Visitor):
self._unify(node.type, builtins.TBool(), self._unify(node.type, builtins.TBool(),
node.loc, None) node.loc, None)
elif builtins.is_builtin(typ, "int"): elif types.is_builtin(typ, "int"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("int() -> int(width='a)"), valid_form("int() -> int(width='a)"),
valid_form("int(x:'a) -> int(width='b) where 'a is numeric"), valid_form("int(x:'a) -> int(width='b) where 'a is numeric"),
@ -482,7 +482,7 @@ class Inferencer(algorithm.Visitor):
node.loc, None) node.loc, None)
else: else:
diagnose(valid_forms()) diagnose(valid_forms())
elif builtins.is_builtin(typ, "float"): elif types.is_builtin(typ, "float"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("float() -> float"), valid_form("float() -> float"),
valid_form("float(x:'a) -> float where 'a is numeric") valid_form("float(x:'a) -> float where 'a is numeric")
@ -501,7 +501,7 @@ class Inferencer(algorithm.Visitor):
pass pass
else: else:
diagnose(valid_forms()) diagnose(valid_forms())
elif builtins.is_builtin(typ, "list"): elif types.is_builtin(typ, "list"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("list() -> list(elt='a)"), valid_form("list() -> list(elt='a)"),
valid_form("list(x:'a) -> list(elt='b) where 'a is iterable") valid_form("list(x:'a) -> list(elt='b) where 'a is iterable")
@ -530,7 +530,7 @@ class Inferencer(algorithm.Visitor):
self.engine.process(diag) self.engine.process(diag)
else: else:
diagnose(valid_forms()) diagnose(valid_forms())
elif builtins.is_builtin(typ, "range"): elif types.is_builtin(typ, "range"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("range(max:'a) -> range(elt='a)"), valid_form("range(max:'a) -> range(elt='a)"),
valid_form("range(min:'a, max:'a) -> range(elt='a)"), valid_form("range(min:'a, max:'a) -> range(elt='a)"),
@ -561,7 +561,7 @@ class Inferencer(algorithm.Visitor):
self.engine.process(diag) self.engine.process(diag)
else: else:
diagnose(valid_forms()) diagnose(valid_forms())
elif builtins.is_builtin(typ, "len"): elif types.is_builtin(typ, "len"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("len(x:'a) -> int(width='b) where 'a is iterable"), valid_form("len(x:'a) -> int(width='b) where 'a is iterable"),
] ]
@ -588,7 +588,7 @@ class Inferencer(algorithm.Visitor):
self.engine.process(diag) self.engine.process(diag)
else: else:
diagnose(valid_forms()) diagnose(valid_forms())
elif builtins.is_builtin(typ, "round"): elif types.is_builtin(typ, "round"):
valid_forms = lambda: [ valid_forms = lambda: [
valid_form("round(x:float) -> int(width='a)"), valid_form("round(x:float) -> int(width='a)"),
] ]
@ -604,7 +604,7 @@ class Inferencer(algorithm.Visitor):
else: else:
diagnose(valid_forms()) diagnose(valid_forms())
# TODO: add when it is clear what interface syscall() has # TODO: add when it is clear what interface syscall() has
# elif builtins.is_builtin(typ, "syscall"): # elif types.is_builtin(typ, "syscall"):
# valid_Forms = lambda: [ # valid_Forms = lambda: [
# ] # ]
@ -778,7 +778,7 @@ class Inferencer(algorithm.Visitor):
def visit_ExceptHandlerT(self, node): def visit_ExceptHandlerT(self, node):
self.generic_visit(node) self.generic_visit(node)
if not builtins.is_exn_constructor(node.filter.type): if not types.is_exn_constructor(node.filter.type):
diag = diagnostic.Diagnostic("error", diag = diagnostic.Diagnostic("error",
"this expression must refer to an exception constructor", "this expression must refer to an exception constructor",
{"type": types.TypePrinter().name(node.filter.type)}, {"type": types.TypePrinter().name(node.filter.type)},

View File

@ -322,8 +322,21 @@ def is_tuple(typ, elts=None):
def is_function(typ): def is_function(typ):
return isinstance(typ.find(), TFunction) return isinstance(typ.find(), TFunction)
def is_builtin(typ): def is_builtin(typ, name=None):
return isinstance(typ.find(), TBuiltin) typ = typ.find()
if name is None:
return isinstance(typ, TBuiltin)
else:
return isinstance(typ, TBuiltin) and \
typ.name == name
def is_exn_constructor(typ, name=None):
typ = typ.find()
if name is not None:
return isinstance(typ, TExceptionConstructor) and \
typ.name == name
else:
return isinstance(typ, TExceptionConstructor)
def get_value(typ): def get_value(typ):
typ = typ.find() typ = typ.find()