diff --git a/crates/erg_compiler/context/initialize/classes.rs b/crates/erg_compiler/context/initialize/classes.rs index 05a31611..725a5fa2 100644 --- a/crates/erg_compiler/context/initialize/classes.rs +++ b/crates/erg_compiler/context/initialize/classes.rs @@ -4023,6 +4023,12 @@ impl Context { Immutable, Visibility::BUILTIN_PRIVATE, ); + let coro_t = poly(COROUTINE, vec![ty_tp(T.clone())]); + let mut coroutine = Self::builtin_poly_class(COROUTINE, vec![PS::t_nd(TY_T)], 2); + coroutine.register_superclass(mono(SUBROUTINE), &subr); + coroutine + .register_trait(self, poly(OUTPUT, vec![ty_tp(T.clone())])) + .unwrap(); // TODO: non-builtin /* Dimension */ let Ty = type_q("Ty"); @@ -4644,6 +4650,7 @@ impl Context { self.register_builtin_type(set_mut_t, set_mut_, vis.clone(), Const, Some(SET)); self.register_builtin_type(mono(SUBROUTINE), subr, vis.clone(), Const, Some(SUBROUTINE)); self.register_builtin_type(generator_t, generator, vis.clone(), Const, Some(GENERATOR)); + self.register_builtin_type(coro_t, coroutine, vis.clone(), Const, Some(FUNC_COROUTINE)); self.register_builtin_type(dimension_t, dimension, vis.clone(), Const, Some(DIMENSION)); self.register_builtin_type( mono(BASE_EXCEPTION), diff --git a/crates/erg_compiler/context/initialize/mod.rs b/crates/erg_compiler/context/initialize/mod.rs index 55e2c6df..04180c38 100644 --- a/crates/erg_compiler/context/initialize/mod.rs +++ b/crates/erg_compiler/context/initialize/mod.rs @@ -553,6 +553,8 @@ const FUNC_NEARLY_EQ: &str = "nearly_eq"; const FUNC_RESOLVE_PATH: &str = "ResolvePath"; const FUNC_RESOLVE_DECL_PATH: &str = "ResolveDeclPath"; const FUNC_PROD: &str = "prod"; +const COROUTINE: &str = "Coroutine"; +const FUNC_COROUTINE: &str = "coroutine"; const OP_EQ: &str = "__eq__"; const OP_HASH: &str = "__hash__"; diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/__init__.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/__init__.d.er new file mode 100644 index 00000000..7db45402 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/__init__.d.er @@ -0,0 +1,19 @@ +{.BaseEventLoop!; .Server!} = import "base_events" +{.iscoroutine;. iscoroutinefunction;} = pyimport "coroutines" +{ + .AbstractEventLoop!; + .AbstractEventLoopPolicy!; + .AbstractServer!; + .Handle!; + .TimerHandle!; +} = pyimport "events" +{ + .LifoQueue!; + .PriorityQueue!; + .Queue!; + .QueueEmpty; + .QueueFull; +} = pyimport "queues" +{.Runner!; .run!;} = pyimport "runners" +{.Task!; .current_task!; .gather!; .sleep!;} = pyimport "tasks" +{.to_thread!;} = pyimport "threads" diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/base_events.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/base_events.d.er new file mode 100644 index 00000000..f162e5e5 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/base_events.d.er @@ -0,0 +1,3 @@ +.BaseEventLoop!: ClassType + +.Server!: ClassType diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/coroutines.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/coroutines.d.er new file mode 100644 index 00000000..dc1ac432 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/coroutines.d.er @@ -0,0 +1,2 @@ +.iscoroutinefunction: (obj: Obj) -> Bool +.iscoroutine: (obj: Obj) -> Bool diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/events.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/events.d.er new file mode 100644 index 00000000..86d50965 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/events.d.er @@ -0,0 +1,8 @@ +.Handle!: ClassType + +.TimerHandle!: ClassType +.TimerHandle! <: Handle! + +.AbstractServer!: ClassType +.AbstractEventLoop!: ClassType +.AbstractEventLoopPolicy!: ClassType diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/futures.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/futures.d.er new file mode 100644 index 00000000..f7848d17 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/futures.d.er @@ -0,0 +1,11 @@ +.Future!: ClassType +.Future!. + result: (self: Ref Future!) -> Obj + exception: (self: Ref Future!) -> Exception + done: (self: Ref Future!) -> Bool + cancelled: (self: Ref Future!) -> Bool + set_result!: (self: RefMut Future!, result: Obj) => NoneType + set_exception!: (self: RefMut Future!, exception: Exception) => NoneType + cancel!: (self: RefMut Future!, msg := Str) => Bool + +.isfuture: (obj: Obj) -> Bool diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/queues.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/queues.d.er new file mode 100644 index 00000000..79018b21 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/queues.d.er @@ -0,0 +1,13 @@ +.QueueEmpty: ClassType +.QueueEmpty <: BaseException + +.QueueFull: ClassType +.QueueFull <: BaseException + +.Queue!: ClassType + +.PriorityQueue!: ClassType +.PriorityQueue! <: Queue! + +.LifoQueue!: ClassType +.LifoQueue! <: Queue! diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/runners.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/runners.d.er new file mode 100644 index 00000000..804809de --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/runners.d.er @@ -0,0 +1,8 @@ +.Runner!: ClassType +.Runner!. + run!: (self: Runner!, coro: GenericCallable, debug := Bool) => NoneType + close!: (self: Runner!) => NoneType + get_loop!: (self: Runner!) => NoneType + +# TODO: return value +.run!: (coro: GenericCallable, debug := Bool) => NoneType diff --git a/crates/erg_compiler/lib/pystd/asyncio.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/tasks.d.er similarity index 51% rename from crates/erg_compiler/lib/pystd/asyncio.d.er rename to crates/erg_compiler/lib/pystd/asyncio.d/tasks.d.er index 4844fdba..1e176943 100644 --- a/crates/erg_compiler/lib/pystd/asyncio.d.er +++ b/crates/erg_compiler/lib/pystd/asyncio.d/tasks.d.er @@ -1,4 +1,5 @@ -# TODO: return value -.run!: (coro: GenericCallable, debug := Bool) => NoneType -.sleep!: (delay: Float) => NoneType +.Task!: ClassType + +.current_task!: (loop := Obj) => Task! .gather!: (*coros: GenericCallable) => NoneType +.sleep!: (delay: Float) => NoneType diff --git a/crates/erg_compiler/lib/pystd/asyncio.d/threads.d.er b/crates/erg_compiler/lib/pystd/asyncio.d/threads.d.er new file mode 100644 index 00000000..393a9167 --- /dev/null +++ b/crates/erg_compiler/lib/pystd/asyncio.d/threads.d.er @@ -0,0 +1 @@ +.to_thread!: (func: GenericCallable) => Obj