From 0667644580a1b5ca3fd2f61b75cd733c88bf7c95 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Mon, 4 Nov 2024 12:48:44 +0900 Subject: [PATCH] feat: add `ctypes` type decl --- .../context/initialize/classes.rs | 2 + .../erg_compiler/context/initialize/funcs.rs | 6 +- .../erg_compiler/context/initialize/procs.rs | 4 +- .../lib/pystd/ctypes.d/__init__.d.er | 108 ++++++++++++++++++ .../pystd/ctypes.d/macholib.d/__init__.d.er | 0 .../erg_compiler/lib/pystd/ctypes.d/util.d.er | 1 + .../lib/pystd/ctypes.d/wintypes.d.er | 0 crates/erg_compiler/lib/pystd/functools.d.er | 2 +- 8 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 crates/erg_compiler/lib/pystd/ctypes.d/__init__.d.er create mode 100644 crates/erg_compiler/lib/pystd/ctypes.d/macholib.d/__init__.d.er create mode 100644 crates/erg_compiler/lib/pystd/ctypes.d/util.d.er create mode 100644 crates/erg_compiler/lib/pystd/ctypes.d/wintypes.d.er diff --git a/crates/erg_compiler/context/initialize/classes.rs b/crates/erg_compiler/context/initialize/classes.rs index 2c176b37f..7bbc18354 100644 --- a/crates/erg_compiler/context/initialize/classes.rs +++ b/crates/erg_compiler/context/initialize/classes.rs @@ -3745,6 +3745,8 @@ impl Context { /* ByteArray! */ let bytearray_mut_t = mono(MUT_BYTEARRAY); let mut bytearray_mut = Self::builtin_mono_class(MUT_BYTEARRAY, 2); + // TODO: different class + bytearray_mut.register_superclass(mono(BYTES), &bytes); let mut bytearray_seq = Self::builtin_methods(Some(poly(SEQUENCE, vec![ty_tp(Int)])), 2); bytearray_seq.register_builtin_erg_impl( FUNDAMENTAL_LEN, diff --git a/crates/erg_compiler/context/initialize/funcs.rs b/crates/erg_compiler/context/initialize/funcs.rs index d6100f430..0cc64198e 100644 --- a/crates/erg_compiler/context/initialize/funcs.rs +++ b/crates/erg_compiler/context/initialize/funcs.rs @@ -419,7 +419,7 @@ impl Context { let t_vars = no_var_func( vec![], vec![kw_default(KW_OBJECT, Obj, Obj)], - dict! { Str => Obj }.into(), + out_dict_t(dict! { Str => Obj }.into()), ); let t_zip = nd_func( vec![ @@ -1003,7 +1003,7 @@ impl Context { None, ); let t_exec = func( - vec![kw(KW_CODE, Str)], + vec![kw(KW_CODE, Str | Code)], None, vec![ kw(KW_GLOBALS, mono(GENERIC_DICT)), @@ -1020,7 +1020,7 @@ impl Context { Some(FUNC_EXEC), ); let t_eval = func( - vec![kw(KW_CODE, Str)], + vec![kw(KW_CODE, Str | Code)], None, vec![ kw(KW_GLOBALS, mono(GENERIC_DICT)), diff --git a/crates/erg_compiler/context/initialize/procs.rs b/crates/erg_compiler/context/initialize/procs.rs index 2dc9e80f3..7c76b258a 100644 --- a/crates/erg_compiler/context/initialize/procs.rs +++ b/crates/erg_compiler/context/initialize/procs.rs @@ -74,9 +74,9 @@ impl Context { NoneType, ) .quantify(); - let t_globals = no_var_proc(vec![], vec![], dict! { Str => Obj }.into()); + let t_globals = no_var_proc(vec![], vec![], out_dict_t(dict! { Str => Obj }.into())); let t_help = nd_proc(vec![kw("object", ref_(Obj))], None, NoneType); - let t_locals = no_var_proc(vec![], vec![], dict! { Str => Obj }.into()); + let t_locals = no_var_proc(vec![], vec![], out_dict_t(dict! { Str => Obj }.into())); let t_next = nd_proc( vec![kw( "iterable", diff --git a/crates/erg_compiler/lib/pystd/ctypes.d/__init__.d.er b/crates/erg_compiler/lib/pystd/ctypes.d/__init__.d.er new file mode 100644 index 000000000..22919ac0d --- /dev/null +++ b/crates/erg_compiler/lib/pystd/ctypes.d/__init__.d.er @@ -0,0 +1,108 @@ +._CData: ClassType +._SimpleCData: ClassType +._SimpleCData <: _CData + +.CBool = 'c_bool': ClassType +.CBool <: _SimpleCData +.CBool. + __call__: (value:= Bool) -> CBool +.CChar = 'c_char': ClassType +.CChar <: _SimpleCData +.CChar. + __call__: (value:= Bytes or ByteArray! or Int) -> CChar +.CWChar = 'c_wchar': ClassType +.CWChar <: _SimpleCData +.CWChar. + __call__: (value:= Str) -> CWChar +.CByte = 'c_byte': ClassType +.CByte <: _SimpleCData +.CByte. + __call__: (value:= Int) -> CByte +.CUByte = 'c_ubyte': ClassType +.CUByte <: _SimpleCData +.CUByte. + __call__: (value:= Int) -> CUByte +.CShort = 'c_short': ClassType +.CShort <: _SimpleCData +.CShort. + __call__: (value:= Int) -> CShort +.CUShort = 'c_ushort': ClassType +.CUShort <: _SimpleCData +.CUShort. + __call__: (value:= Int) -> CUShort +.CInt = 'c_int': ClassType +.CInt <: _SimpleCData +.CInt. + __call__: (value:= Int) -> CInt +.CUInt = 'c_uint': ClassType +.CUInt <: _SimpleCData +.CUInt. + __call__: (value:= Int) -> CUInt +.CLong = 'c_long': ClassType +.CLong <: _SimpleCData +.CLong. + __call__: (value:= Int) -> CLong +.CULong = 'c_ulong': ClassType +.CULong <: _SimpleCData +.CULong. + __call__: (value:= Int) -> CULong +.CLongLong = 'c_longlong': ClassType +.CLongLong <: _SimpleCData +.CLongLong. + __call__: (value:= Int) -> CLongLong +.CULongLong = 'c_ulonglong': ClassType +.CULongLong <: _SimpleCData +.CULongLong. + __call__: (value:= Int) -> CULongLong +.CFloat = 'c_float': ClassType +.CFloat <: _SimpleCData +.CFloat. + __call__: (value:= Float) -> CFloat +.CDouble = 'c_double': ClassType +.CDouble <: _SimpleCData +.CDouble. + __call__: (value:= Float) -> CDouble +.CCharP = 'c_char_p': ClassType +.CCharP <: _SimpleCData +.CCharP. + __call__: (value:= Str or NoneType) -> CCharP +.CWCharP = 'c_wchar_p': ClassType +.CWCharP <: _SimpleCData +.CWCharP. + __call__: (value:= Str or NoneType) -> CWCharP +.CVoidP = 'c_void_p': ClassType +.CVoidP <: _SimpleCData +.CVoidP. + __call__: (value:= Int or NoneType) -> CVoidP + +.CDLL: ClassType +.OleDLL: ClassType +.WinDLL: ClassType +.PyDLL: ClassType + +.Array: ClassType +.ARRAY: ClassType +.Structure: ClassType + +._Pointer: ClassType +._CFuncPtr: ClassType + +.ArgumentError: ClassType +.ArgumentError <: Exception + +.addressof!: (obj:= Obj) => Int +.alignment!: (obj_or_type:= Obj or Type) => Int +.byref!: (obj:= Obj) => Obj +.cast!: (obj:= Obj, typ:= Type) => Obj +.create_string_buffer: (init:= Str or NoneType, size:= Int) -> CCharP +.create_unicode_buffer: (init:= Str or NoneType, size:= Int) -> CWCharP +.get_errno!: () => Int +.get_last_error!: () => Int +.memmove!: (dst:= Obj, src:= Obj, count:= Int) => NoneType +.memset!: (dst:= Obj, c:= Int, count:= Int) => NoneType +.resize!: (obj:= Obj, size:= Int) => NoneType +.set_errno!: (value:= Int) => NoneType +.set_last_error!: (value:= Int) => NoneType +.sizeof!: (obj_or_type:= Obj or Type) => Nat +.string_at: (ptr:= Obj, size:= Int) -> Bytes +.wstring_at: (ptr:= Obj, size:= Int) -> Str diff --git a/crates/erg_compiler/lib/pystd/ctypes.d/macholib.d/__init__.d.er b/crates/erg_compiler/lib/pystd/ctypes.d/macholib.d/__init__.d.er new file mode 100644 index 000000000..e69de29bb diff --git a/crates/erg_compiler/lib/pystd/ctypes.d/util.d.er b/crates/erg_compiler/lib/pystd/ctypes.d/util.d.er new file mode 100644 index 000000000..db914fd9e --- /dev/null +++ b/crates/erg_compiler/lib/pystd/ctypes.d/util.d.er @@ -0,0 +1 @@ +.find_library!: Str => Str diff --git a/crates/erg_compiler/lib/pystd/ctypes.d/wintypes.d.er b/crates/erg_compiler/lib/pystd/ctypes.d/wintypes.d.er new file mode 100644 index 000000000..e69de29bb diff --git a/crates/erg_compiler/lib/pystd/functools.d.er b/crates/erg_compiler/lib/pystd/functools.d.er index 3f5c17253..b9e318a6e 100644 --- a/crates/erg_compiler/lib/pystd/functools.d.er +++ b/crates/erg_compiler/lib/pystd/functools.d.er @@ -1,4 +1,4 @@ .cache: |T <: Proc|(user_function: T) -> T -.lru_cache: |T <: Proc|(user_function: T) -> T +.lru_cache: (|T <: Proc|(user_function: T) -> T) and |T <: Proc|(maxsize := Int, typed := Bool) -> (T -> T) .total_ordering: (cls: ClassType) -> ClassType .reduce: |T: Type|(function!: T => T, iterable: Iterable(T), initializer := T or NoneType) -> T