diff --git a/compiler/erg_compiler/codegen.rs b/compiler/erg_compiler/codegen.rs index 2d9ac450..39c1fc7f 100644 --- a/compiler/erg_compiler/codegen.rs +++ b/compiler/erg_compiler/codegen.rs @@ -196,8 +196,13 @@ fn convert_to_python_attr(class: &str, uniq_obj_name: Option<&str>, name: Str) - ("Array!", _, "push!") => Str::ever("append"), ("Complex" | "Real" | "Int" | "Nat" | "Float", _, "Real") => Str::ever("real"), ("Complex" | "Real" | "Int" | "Nat" | "Float", _, "Imag") => Str::ever("imag"), + ("StringIO!", _, "getvalue!") => Str::ever("getvalue"), + ("Module", Some("importlib"), "reload!") => Str::ever("reload"), ("Module", Some("random"), "randint!") => Str::ever("randint"), ("Module", Some("random"), "choice!") => Str::ever("choice"), + ("Module", Some("sys"), "setrecurtionlimit!") => Str::ever("setrecurtionlimit"), + ("Module", Some("time"), "sleep!") => Str::ever("sleep"), + ("Module", Some("time"), "time!") => Str::ever("time"), _ => name, } } diff --git a/compiler/erg_compiler/context/initialize/importlib.rs b/compiler/erg_compiler/context/initialize/importlib.rs new file mode 100644 index 00000000..66e7e4b8 --- /dev/null +++ b/compiler/erg_compiler/context/initialize/importlib.rs @@ -0,0 +1,18 @@ +use erg_common::vis::Visibility; + +use erg_type::constructors::proc1; +use erg_type::Type; +use Type::*; + +use crate::context::Context; +use crate::varinfo::Mutability; +use Mutability::*; +use Visibility::*; + +impl Context { + pub(crate) fn init_py_importlib_mod() -> Self { + let mut importlib = Context::module("importlib".into(), 15); + importlib.register_impl("reload!", proc1(Module, NoneType), Immutable, Public); + importlib + } +} diff --git a/compiler/erg_compiler/context/initialize/io.rs b/compiler/erg_compiler/context/initialize/io.rs new file mode 100644 index 00000000..00451fbe --- /dev/null +++ b/compiler/erg_compiler/context/initialize/io.rs @@ -0,0 +1,26 @@ +use erg_common::vis::Visibility; +use erg_common::Str; + +use erg_type::constructors::{class, pr0_met}; +use erg_type::Type; +use Type::*; + +use crate::context::Context; +use crate::varinfo::Mutability; +use Mutability::*; +use Visibility::*; + +impl Context { + pub(crate) fn init_py_io_mod() -> Self { + let mut io = Context::module("io".into(), 15); + let mut string_io = Context::mono_class(Str::ever("StringIO!"), vec![Obj], vec![], 0); + string_io.register_impl( + "getvalue!", + pr0_met(class("StringIO!"), None, Str), + Immutable, + Public, + ); + io.register_type(class("StringIO!"), string_io, Const); + io + } +} diff --git a/compiler/erg_compiler/context/initialize/mod.rs b/compiler/erg_compiler/context/initialize/mod.rs index 9fd2b8ee..fc145e71 100644 --- a/compiler/erg_compiler/context/initialize/mod.rs +++ b/compiler/erg_compiler/context/initialize/mod.rs @@ -1,8 +1,13 @@ //! defines type information for builtin objects (in `Context`) //! //! 組み込みオブジェクトの型情報を(Contextに)定義 +pub mod importlib; +pub mod io; pub mod math; pub mod random; +pub mod socket; +pub mod sys; +pub mod time; use erg_common::set; use erg_common::vis::Visibility; @@ -549,6 +554,18 @@ impl Context { Immutable, Public, ); + str_.register_impl( + "encode", + fn_met( + Str, + vec![], + None, + vec![param_t("encoding", Str), param_t("errors", Str)], + class("Bytes"), + ), + Immutable, + Public, + ); str_.register_const("AddO", ValueObj::t(Str)); str_.register_const("MulO", ValueObj::t(Str)); str_.register_const("MutType!", ValueObj::t(class("Str!"))); diff --git a/compiler/erg_compiler/context/initialize/socket.rs b/compiler/erg_compiler/context/initialize/socket.rs new file mode 100644 index 00000000..42ec4c32 --- /dev/null +++ b/compiler/erg_compiler/context/initialize/socket.rs @@ -0,0 +1,36 @@ +use erg_common::vis::Visibility; +use erg_common::Str; + +use erg_type::constructors::{class, func, option, param_t}; +use erg_type::Type; +use Type::*; + +use crate::context::Context; +use crate::varinfo::Mutability; +use Mutability::*; +use Visibility::*; + +impl Context { + pub(crate) fn init_py_socket_mod() -> Self { + let mut socket = Context::module("socket".into(), 15); + let mut sock = Context::mono_class(Str::ever("Socket!"), vec![Obj], vec![], 0); + sock.register_impl( + "new", + func( + vec![], + None, + vec![ + param_t("family", Int), + param_t("type", Int), + param_t("proto", Int), + param_t("fileno", option(Int)), + ], + class("Socket!"), + ), + Immutable, + Public, + ); + socket.register_type(class("Socket!"), sock, Const); + socket + } +} diff --git a/compiler/erg_compiler/context/initialize/sys.rs b/compiler/erg_compiler/context/initialize/sys.rs new file mode 100644 index 00000000..1ad02502 --- /dev/null +++ b/compiler/erg_compiler/context/initialize/sys.rs @@ -0,0 +1,50 @@ +use erg_common::vis::Visibility; + +use erg_type::constructors::{array, array_mut, class, func0, func1, proc1}; +use erg_type::typaram::TyParam; +use erg_type::Type; +use Type::*; + +use crate::context::Context; +use crate::varinfo::Mutability; +use Mutability::*; +use Visibility::*; + +impl Context { + pub(crate) fn init_py_sys_mod() -> Self { + let mut sys = Context::module("sys".into(), 15); + sys.register_impl("argv", array(Str, TyParam::erased(Nat)), Immutable, Public); + sys.register_impl("byteorder", Str, Immutable, Public); + sys.register_impl( + "builtin_module_names", + array(Str, TyParam::erased(Nat)), + Immutable, + Public, + ); + sys.register_impl("copyright", Str, Immutable, Public); + sys.register_impl("executable", Str, Immutable, Public); + sys.register_impl("exit", func1(Int, Never), Immutable, Public); + sys.register_impl("getdefaultencoding", func0(Str), Immutable, Public); + sys.register_impl( + "path", + array_mut(Str, TyParam::erased(Nat)), + Immutable, + Public, + ); + sys.register_impl("platform", Str, Immutable, Public); + sys.register_impl("prefix", Str, Immutable, Public); + sys.register_impl("ps1", class("Str!"), Immutable, Public); + sys.register_impl("ps2", class("Str!"), Immutable, Public); + sys.register_impl( + "setrecursionlimit!", + proc1(Int, NoneType), + Immutable, + Public, + ); + sys.register_impl("stderr", class("TextIOWrapper!"), Immutable, Public); + sys.register_impl("stdin", class("TextIOWrapper!"), Immutable, Public); + sys.register_impl("stdout", class("TextIOWrapper!"), Immutable, Public); + sys.register_impl("version", Str, Immutable, Public); + sys + } +} diff --git a/compiler/erg_compiler/context/initialize/time.rs b/compiler/erg_compiler/context/initialize/time.rs new file mode 100644 index 00000000..4609eb0d --- /dev/null +++ b/compiler/erg_compiler/context/initialize/time.rs @@ -0,0 +1,19 @@ +use erg_common::vis::Visibility; + +use erg_type::constructors::{proc0, proc1}; +use erg_type::Type; +use Type::*; + +use crate::context::Context; +use crate::varinfo::Mutability; +use Mutability::*; +use Visibility::*; + +impl Context { + pub(crate) fn init_py_time_mod() -> Self { + let mut time = Context::module("time".into(), 15); + time.register_impl("sleep!", proc1(Float, NoneType), Immutable, Public); + time.register_impl("time!", proc0(Float), Immutable, Public); + time + } +} diff --git a/compiler/erg_compiler/context/register.rs b/compiler/erg_compiler/context/register.rs index 28723cb5..2ff67b7d 100644 --- a/compiler/erg_compiler/context/register.rs +++ b/compiler/erg_compiler/context/register.rs @@ -359,6 +359,13 @@ impl Context { if self.rec_subtype_of(&lit.value.class(), &Str) { let name = enum_unwrap!(lit.value.clone(), ValueObj::Str); match &name[..] { + "importlib" => { + self.mods + .insert(var_name.clone(), Self::init_py_importlib_mod()); + } + "io" => { + self.mods.insert(var_name.clone(), Self::init_py_io_mod()); + } "math" => { self.mods.insert(var_name.clone(), Self::init_py_math_mod()); } @@ -366,6 +373,16 @@ impl Context { self.mods .insert(var_name.clone(), Self::init_py_random_mod()); } + "socket" => { + self.mods + .insert(var_name.clone(), Self::init_py_socket_mod()); + } + "sys" => { + self.mods.insert(var_name.clone(), Self::init_py_sys_mod()); + } + "time" => { + self.mods.insert(var_name.clone(), Self::init_py_time_mod()); + } other => todo!("importing {other}"), } } else { diff --git a/compiler/erg_type/constructors.rs b/compiler/erg_type/constructors.rs index 982389fd..756df604 100644 --- a/compiler/erg_type/constructors.rs +++ b/compiler/erg_type/constructors.rs @@ -141,6 +141,10 @@ pub fn func( )) } +pub fn func0(return_t: Type) -> Type { + func(vec![], None, vec![], return_t) +} + pub fn func1(param_t: Type, return_t: Type) -> Type { func(vec![ParamTy::anonymous(param_t)], None, vec![], return_t) } @@ -184,6 +188,10 @@ pub fn proc( )) } +pub fn proc0(return_t: Type) -> Type { + proc(vec![], None, vec![], return_t) +} + pub fn proc1(param_t: Type, return_t: Type) -> Type { proc(vec![ParamTy::anonymous(param_t)], None, vec![], return_t) }