Add IndexError and ValueError builtins.

This commit is contained in:
whitequark 2015-07-16 14:58:40 +03:00
parent b58fa9067d
commit c1e7a82e97
3 changed files with 36 additions and 2 deletions

View File

@ -43,8 +43,16 @@ class TRange(types.TMono):
]) ])
class TException(types.TMono): class TException(types.TMono):
def __init__(self, name="Exception"):
super().__init__(name)
class TIndexError(types.TMono):
def __init__(self): def __init__(self):
super().__init__("Exception") super().__init__("IndexError")
class TValueError(types.TMono):
def __init__(self):
super().__init__("ValueError")
def fn_bool(): def fn_bool():
return types.TConstructor("bool") return types.TConstructor("bool")
@ -61,6 +69,12 @@ def fn_list():
def fn_Exception(): def fn_Exception():
return types.TExceptionConstructor("Exception") return types.TExceptionConstructor("Exception")
def fn_IndexError():
return types.TExceptionConstructor("IndexError")
def fn_ValueError():
return types.TExceptionConstructor("ValueError")
def fn_range(): def fn_range():
return types.TBuiltinFunction("range") return types.TBuiltinFunction("range")

View File

@ -13,6 +13,8 @@ def globals():
"list": builtins.fn_list(), "list": builtins.fn_list(),
"range": builtins.fn_range(), "range": builtins.fn_range(),
"Exception": builtins.fn_Exception(), "Exception": builtins.fn_Exception(),
"IndexError": builtins.fn_IndexError(),
"ValueError": builtins.fn_ValueError(),
"len": builtins.fn_len(), "len": builtins.fn_len(),
"round": builtins.fn_round(), "round": builtins.fn_round(),
"syscall": builtins.fn_syscall(), "syscall": builtins.fn_syscall(),

View File

@ -432,7 +432,25 @@ 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 types.is_builtin(typ, "bool"): if types.is_exn_constructor(typ):
exns = {
"IndexError": builtins.TIndexError,
"ValueError": builtins.TValueError,
}
for exn in exns:
if types.is_exn_constructor(typ, exn):
valid_forms = lambda: [
valid_form("{exn}() -> {exn}".format(exn=exn))
]
if len(node.args) == 0 and len(node.keywords) == 0:
pass # False
else:
diagnose(valid_forms())
self._unify(node.type, exns[exn](),
node.loc, None)
elif 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")