From a4a9cd884e7ca0227434cf2530311e2089c271d0 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 28 Jun 2015 22:48:15 +0300 Subject: [PATCH] Add exception constructor types. --- artiq/py2llvm/builtins.py | 21 ++++++++++++++++++--- artiq/py2llvm/prelude.py | 17 +++++++++-------- lit-test/py2llvm/typing/exception.py | 5 +++++ 3 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 lit-test/py2llvm/typing/exception.py diff --git a/artiq/py2llvm/builtins.py b/artiq/py2llvm/builtins.py index 7e4e71d76..c1dcfb7ba 100644 --- a/artiq/py2llvm/builtins.py +++ b/artiq/py2llvm/builtins.py @@ -37,6 +37,10 @@ class TRange(types.TMono): elt = types.TVar() super().__init__("range", {"elt": elt}) +class TException(types.TMono): + def __init__(self): + super().__init__("Exception") + def fn_bool(): return types.TConstructor("bool") @@ -49,6 +53,9 @@ def fn_float(): def fn_list(): return types.TConstructor("list") +def fn_Exception(): + return types.TExceptionConstructor("Exception") + def fn_range(): return types.TBuiltinFunction("range") @@ -70,7 +77,7 @@ def is_bool(typ): return types.is_mono(typ, "bool") def is_int(typ, width=None): - if width: + if width is not None: return types.is_mono(typ, "int", {"width": width}) else: return types.is_mono(typ, "int") @@ -88,13 +95,13 @@ def is_numeric(typ): typ.name in ('int', 'float') def is_list(typ, elt=None): - if elt: + if elt is not None: return types.is_mono(typ, "list", {"elt": elt}) else: return types.is_mono(typ, "list") def is_range(typ, elt=None): - if elt: + if elt is not None: return types.is_mono(typ, "range", {"elt": elt}) else: return types.is_mono(typ, "range") @@ -117,3 +124,11 @@ def is_builtin(typ, name): typ = typ.find() return isinstance(typ, types.TBuiltin) and \ typ.name == name + +def is_exception(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) diff --git a/artiq/py2llvm/prelude.py b/artiq/py2llvm/prelude.py index 97a06a082..d024c2423 100644 --- a/artiq/py2llvm/prelude.py +++ b/artiq/py2llvm/prelude.py @@ -7,12 +7,13 @@ from . import builtins def globals(): return { - "bool": builtins.fn_bool(), - "int": builtins.fn_int(), - "float": builtins.fn_float(), - "list": builtins.fn_list(), - "range": builtins.fn_range(), - "len": builtins.fn_len(), - "round": builtins.fn_round(), - "syscall": builtins.fn_syscall(), + "bool": builtins.fn_bool(), + "int": builtins.fn_int(), + "float": builtins.fn_float(), + "list": builtins.fn_list(), + "range": builtins.fn_range(), + "Exception": builtins.fn_Exception(), + "len": builtins.fn_len(), + "round": builtins.fn_round(), + "syscall": builtins.fn_syscall(), } diff --git a/lit-test/py2llvm/typing/exception.py b/lit-test/py2llvm/typing/exception.py new file mode 100644 index 000000000..5500d96a0 --- /dev/null +++ b/lit-test/py2llvm/typing/exception.py @@ -0,0 +1,5 @@ +# RUN: %python -m artiq.py2llvm.typing %s >%t +# RUN: OutputCheck %s --file-to-check=%t + +# CHECK-L: Exception: +Exception