mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 12:24:45 +00:00
Add importlib
, io
, socket
, sys
, time
This commit is contained in:
parent
8454e079af
commit
a8e4658c67
9 changed files with 196 additions and 0 deletions
|
@ -196,8 +196,13 @@ fn convert_to_python_attr(class: &str, uniq_obj_name: Option<&str>, name: Str) -
|
||||||
("Array!", _, "push!") => Str::ever("append"),
|
("Array!", _, "push!") => Str::ever("append"),
|
||||||
("Complex" | "Real" | "Int" | "Nat" | "Float", _, "Real") => Str::ever("real"),
|
("Complex" | "Real" | "Int" | "Nat" | "Float", _, "Real") => Str::ever("real"),
|
||||||
("Complex" | "Real" | "Int" | "Nat" | "Float", _, "Imag") => Str::ever("imag"),
|
("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"), "randint!") => Str::ever("randint"),
|
||||||
("Module", Some("random"), "choice!") => Str::ever("choice"),
|
("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,
|
_ => name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
18
compiler/erg_compiler/context/initialize/importlib.rs
Normal file
18
compiler/erg_compiler/context/initialize/importlib.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
26
compiler/erg_compiler/context/initialize/io.rs
Normal file
26
compiler/erg_compiler/context/initialize/io.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,13 @@
|
||||||
//! defines type information for builtin objects (in `Context`)
|
//! defines type information for builtin objects (in `Context`)
|
||||||
//!
|
//!
|
||||||
//! 組み込みオブジェクトの型情報を(Contextに)定義
|
//! 組み込みオブジェクトの型情報を(Contextに)定義
|
||||||
|
pub mod importlib;
|
||||||
|
pub mod io;
|
||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod random;
|
pub mod random;
|
||||||
|
pub mod socket;
|
||||||
|
pub mod sys;
|
||||||
|
pub mod time;
|
||||||
|
|
||||||
use erg_common::set;
|
use erg_common::set;
|
||||||
use erg_common::vis::Visibility;
|
use erg_common::vis::Visibility;
|
||||||
|
@ -549,6 +554,18 @@ impl Context {
|
||||||
Immutable,
|
Immutable,
|
||||||
Public,
|
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("AddO", ValueObj::t(Str));
|
||||||
str_.register_const("MulO", ValueObj::t(Str));
|
str_.register_const("MulO", ValueObj::t(Str));
|
||||||
str_.register_const("MutType!", ValueObj::t(class("Str!")));
|
str_.register_const("MutType!", ValueObj::t(class("Str!")));
|
||||||
|
|
36
compiler/erg_compiler/context/initialize/socket.rs
Normal file
36
compiler/erg_compiler/context/initialize/socket.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
50
compiler/erg_compiler/context/initialize/sys.rs
Normal file
50
compiler/erg_compiler/context/initialize/sys.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
19
compiler/erg_compiler/context/initialize/time.rs
Normal file
19
compiler/erg_compiler/context/initialize/time.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -359,6 +359,13 @@ impl Context {
|
||||||
if self.rec_subtype_of(&lit.value.class(), &Str) {
|
if self.rec_subtype_of(&lit.value.class(), &Str) {
|
||||||
let name = enum_unwrap!(lit.value.clone(), ValueObj::Str);
|
let name = enum_unwrap!(lit.value.clone(), ValueObj::Str);
|
||||||
match &name[..] {
|
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" => {
|
"math" => {
|
||||||
self.mods.insert(var_name.clone(), Self::init_py_math_mod());
|
self.mods.insert(var_name.clone(), Self::init_py_math_mod());
|
||||||
}
|
}
|
||||||
|
@ -366,6 +373,16 @@ impl Context {
|
||||||
self.mods
|
self.mods
|
||||||
.insert(var_name.clone(), Self::init_py_random_mod());
|
.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}"),
|
other => todo!("importing {other}"),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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 {
|
pub fn func1(param_t: Type, return_t: Type) -> Type {
|
||||||
func(vec![ParamTy::anonymous(param_t)], None, vec![], return_t)
|
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 {
|
pub fn proc1(param_t: Type, return_t: Type) -> Type {
|
||||||
proc(vec![ParamTy::anonymous(param_t)], None, vec![], return_t)
|
proc(vec![ParamTy::anonymous(param_t)], None, vec![], return_t)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue