mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 20:34:44 +00:00
Fix: variable to have VarInfo
instead of Type
This commit is contained in:
parent
119a326ebb
commit
46b87e1ca3
29 changed files with 888 additions and 816 deletions
|
@ -26,7 +26,7 @@ use erg_parser::ast::VarName;
|
|||
use crate::context::initialize::const_func::*;
|
||||
use crate::context::instantiate::ConstTemplate;
|
||||
use crate::context::{
|
||||
ClassDefType, Context, ContextKind, DefaultInfo, MethodType, ParamSpec, TraitInstance,
|
||||
ClassDefType, Context, ContextKind, DefaultInfo, MethodInfo, ParamSpec, TraitInstance,
|
||||
};
|
||||
use crate::mod_cache::SharedModuleCache;
|
||||
use crate::varinfo::{Mutability, VarInfo, VarKind};
|
||||
|
@ -48,7 +48,38 @@ impl Context {
|
|||
} else {
|
||||
self.decls.insert(
|
||||
name,
|
||||
VarInfo::new(t, Immutable, vis, Builtin, None, impl_of),
|
||||
VarInfo::new(t, Immutable, vis, Builtin, None, impl_of, None),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_builtin_py_decl(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
vis: Visibility,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
let impl_of = if let ContextKind::MethodDefs(Some(tr)) = &self.kind {
|
||||
Some(tr.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let name = VarName::from_static(name);
|
||||
if self.decls.get(&name).is_some() {
|
||||
panic!("already registered: {name}");
|
||||
} else {
|
||||
self.decls.insert(
|
||||
name,
|
||||
VarInfo::new(
|
||||
t,
|
||||
Immutable,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
impl_of,
|
||||
py_name.map(Str::ever),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -69,13 +100,39 @@ impl Context {
|
|||
if self.locals.get(&name).is_some() {
|
||||
panic!("already registered: {name}");
|
||||
} else {
|
||||
self.locals
|
||||
.insert(name, VarInfo::new(t, muty, vis, Builtin, None, impl_of));
|
||||
self.locals.insert(
|
||||
name,
|
||||
VarInfo::new(t, muty, vis, Builtin, None, impl_of, None),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_builtin_py_impl(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
let impl_of = if let ContextKind::MethodDefs(Some(tr)) = &self.kind {
|
||||
Some(tr.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let name = VarName::from_static(name);
|
||||
if self.locals.get(&name).is_some() {
|
||||
panic!("already registered: {name}");
|
||||
} else {
|
||||
self.locals.insert(
|
||||
name,
|
||||
VarInfo::new(t, muty, vis, Builtin, None, impl_of, py_name.map(Str::ever)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_builtin_immutable_private_var(&mut self, name: &'static str, t: Type) {
|
||||
self.register_builtin_impl(name, t, Immutable, Private)
|
||||
self.register_builtin_impl(name, t, Immutable, Private);
|
||||
}
|
||||
|
||||
fn register_builtin_const(&mut self, name: &str, vis: Visibility, obj: ValueObj) {
|
||||
|
@ -95,6 +152,7 @@ impl Context {
|
|||
Builtin,
|
||||
None,
|
||||
impl_of,
|
||||
None,
|
||||
);
|
||||
self.consts.insert(VarName::from_str(Str::rc(name)), obj);
|
||||
self.locals.insert(VarName::from_str(Str::rc(name)), vi);
|
||||
|
@ -124,15 +182,29 @@ impl Context {
|
|||
unique_in_place(&mut self.super_traits);
|
||||
}
|
||||
|
||||
fn register_builtin_type(&mut self, t: Type, ctx: Self, vis: Visibility, muty: Mutability) {
|
||||
fn register_builtin_type(
|
||||
&mut self,
|
||||
t: Type,
|
||||
ctx: Self,
|
||||
vis: Visibility,
|
||||
muty: Mutability,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
if t.typarams_len().is_none() {
|
||||
self.register_mono_type(t, ctx, vis, muty);
|
||||
self.register_mono_type(t, ctx, vis, muty, py_name);
|
||||
} else {
|
||||
self.register_poly_type(t, ctx, vis, muty);
|
||||
self.register_poly_type(t, ctx, vis, muty, py_name);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_mono_type(&mut self, t: Type, ctx: Self, vis: Visibility, muty: Mutability) {
|
||||
fn register_mono_type(
|
||||
&mut self,
|
||||
t: Type,
|
||||
ctx: Self,
|
||||
vis: Visibility,
|
||||
muty: Mutability,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
if self.rec_get_mono_type(&t.local_name()).is_some() {
|
||||
panic!("{} has already been registered", t.local_name());
|
||||
} else if self.rec_get_const_obj(&t.local_name()).is_some() {
|
||||
|
@ -146,7 +218,15 @@ impl Context {
|
|||
};
|
||||
self.locals.insert(
|
||||
name.clone(),
|
||||
VarInfo::new(meta_t, muty, vis, Builtin, None, None),
|
||||
VarInfo::new(
|
||||
meta_t,
|
||||
muty,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
None,
|
||||
py_name.map(Str::ever),
|
||||
),
|
||||
);
|
||||
self.consts
|
||||
.insert(name.clone(), ValueObj::builtin_t(t.clone()));
|
||||
|
@ -162,21 +242,21 @@ impl Context {
|
|||
}
|
||||
for (trait_method, vi) in ctx.decls.iter() {
|
||||
if let Some(types) = self.method_to_traits.get_mut(trait_method.inspect()) {
|
||||
types.push(MethodType::new(t.clone(), vi.t.clone()));
|
||||
types.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_traits.insert(
|
||||
trait_method.inspect().clone(),
|
||||
vec![MethodType::new(t.clone(), vi.t.clone())],
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
for (class_method, vi) in ctx.locals.iter() {
|
||||
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
|
||||
types.push(MethodType::new(t.clone(), vi.t.clone()));
|
||||
types.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_classes.insert(
|
||||
class_method.inspect().clone(),
|
||||
vec![MethodType::new(t.clone(), vi.t.clone())],
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -185,7 +265,14 @@ impl Context {
|
|||
}
|
||||
|
||||
// FIXME: MethodDefsと再代入は違う
|
||||
fn register_poly_type(&mut self, t: Type, ctx: Self, vis: Visibility, muty: Mutability) {
|
||||
fn register_poly_type(
|
||||
&mut self,
|
||||
t: Type,
|
||||
ctx: Self,
|
||||
vis: Visibility,
|
||||
muty: Mutability,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
// FIXME: panic
|
||||
if let Some((_, root_ctx)) = self.poly_types.get_mut(&t.local_name()) {
|
||||
root_ctx.methods_list.push((ClassDefType::Simple(t), ctx));
|
||||
|
@ -198,7 +285,15 @@ impl Context {
|
|||
};
|
||||
self.locals.insert(
|
||||
name.clone(),
|
||||
VarInfo::new(meta_t, muty, vis, Builtin, None, None),
|
||||
VarInfo::new(
|
||||
meta_t,
|
||||
muty,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
None,
|
||||
py_name.map(Str::ever),
|
||||
),
|
||||
);
|
||||
self.consts
|
||||
.insert(name.clone(), ValueObj::builtin_t(t.clone()));
|
||||
|
@ -214,21 +309,21 @@ impl Context {
|
|||
}
|
||||
for (trait_method, vi) in ctx.decls.iter() {
|
||||
if let Some(traits) = self.method_to_traits.get_mut(trait_method.inspect()) {
|
||||
traits.push(MethodType::new(t.clone(), vi.t.clone()));
|
||||
traits.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_traits.insert(
|
||||
trait_method.inspect().clone(),
|
||||
vec![MethodType::new(t.clone(), vi.t.clone())],
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
for (class_method, vi) in ctx.locals.iter() {
|
||||
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
|
||||
types.push(MethodType::new(t.clone(), vi.t.clone()));
|
||||
types.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_classes.insert(
|
||||
class_method.inspect().clone(),
|
||||
vec![MethodType::new(t.clone(), vi.t.clone())],
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -249,7 +344,7 @@ impl Context {
|
|||
let name = VarName::from_static(name);
|
||||
self.locals.insert(
|
||||
name.clone(),
|
||||
VarInfo::new(Patch, muty, vis, Builtin, None, None),
|
||||
VarInfo::new(Patch, muty, vis, Builtin, None, None, None),
|
||||
);
|
||||
for method_name in ctx.locals.keys() {
|
||||
if let Some(patches) = self.method_impl_patches.get_mut(method_name) {
|
||||
|
@ -307,7 +402,7 @@ impl Context {
|
|||
t_read,
|
||||
set! { subtypeof(mono_q("Self"), mono("Readable!")) },
|
||||
);
|
||||
readable.register_builtin_decl("read!", t_read, Public);
|
||||
readable.register_builtin_py_decl("read!", t_read, Public, Some("read"));
|
||||
/* Writable */
|
||||
let mut writable = Self::builtin_mono_trait("Writable!", 2);
|
||||
let t_write = pr1_kw_met(ref_mut(mono_q("Self"), None), kw("s", Str), Nat);
|
||||
|
@ -315,12 +410,12 @@ impl Context {
|
|||
t_write,
|
||||
set! { subtypeof(mono_q("Self"), mono("Writable!")) },
|
||||
);
|
||||
writable.register_builtin_decl("write!", t_write, Public);
|
||||
writable.register_builtin_py_decl("write!", t_write, Public, Some("write"));
|
||||
/* Show */
|
||||
let mut show = Self::builtin_mono_trait("Show", 2);
|
||||
let t_show = fn0_met(ref_(mono_q("Self")), Str);
|
||||
let t_show = quant(t_show, set! { subtypeof(mono_q("Self"), mono("Show")) });
|
||||
show.register_builtin_decl("to_str", t_show, Public);
|
||||
show.register_builtin_py_decl("to_str", t_show, Public, Some("__str__"));
|
||||
/* In */
|
||||
let mut in_ = Self::builtin_poly_trait("In", vec![PS::t("T", NonDefault)], 2);
|
||||
let params = vec![PS::t("T", NonDefault)];
|
||||
|
@ -447,50 +542,90 @@ impl Context {
|
|||
let op_t = quant(op_t, set! {r_bound, self_bound});
|
||||
floor_div.register_builtin_decl("__floordiv__", op_t, Public);
|
||||
floor_div.register_builtin_decl("Output", Type, Public);
|
||||
self.register_builtin_type(mono("Unpack"), unpack, Private, Const);
|
||||
self.register_builtin_type(mono("InheritableType"), inheritable_type, Private, Const);
|
||||
self.register_builtin_type(mono("Named"), named, Private, Const);
|
||||
self.register_builtin_type(mono("Mutable"), mutable, Private, Const);
|
||||
self.register_builtin_type(mono("Immutizable"), immutizable, Private, Const);
|
||||
self.register_builtin_type(mono("Mutizable"), mutizable, Private, Const);
|
||||
self.register_builtin_type(mono("PathLike"), pathlike, Private, Const);
|
||||
self.register_builtin_type(mono("Readable!"), readable, Private, Const);
|
||||
self.register_builtin_type(mono("Writable!"), writable, Private, Const);
|
||||
self.register_builtin_type(mono("Show"), show, Private, Const);
|
||||
self.register_builtin_type(mono("Unpack"), unpack, Private, Const, None);
|
||||
self.register_builtin_type(
|
||||
mono("InheritableType"),
|
||||
inheritable_type,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(mono("Named"), named, Private, Const, None);
|
||||
self.register_builtin_type(mono("Mutable"), mutable, Private, Const, None);
|
||||
self.register_builtin_type(mono("Immutizable"), immutizable, Private, Const, None);
|
||||
self.register_builtin_type(mono("Mutizable"), mutizable, Private, Const, None);
|
||||
self.register_builtin_type(mono("PathLike"), pathlike, Private, Const, None);
|
||||
self.register_builtin_type(
|
||||
mono("Readable!"),
|
||||
readable,
|
||||
Private,
|
||||
Const,
|
||||
Some("Readable"),
|
||||
);
|
||||
self.register_builtin_type(
|
||||
mono("Writable!"),
|
||||
writable,
|
||||
Private,
|
||||
Const,
|
||||
Some("Writable"),
|
||||
);
|
||||
self.register_builtin_type(mono("Show"), show, Private, Const, None);
|
||||
self.register_builtin_type(
|
||||
poly("Input", vec![ty_tp(mono_q("T"))]),
|
||||
input,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(
|
||||
poly("Output", vec![ty_tp(mono_q("T"))]),
|
||||
output,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(
|
||||
poly("In", vec![ty_tp(mono_q("T"))]),
|
||||
in_,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(
|
||||
poly("Eq", vec![ty_tp(mono_q("R"))]),
|
||||
eq,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(poly("In", vec![ty_tp(mono_q("T"))]), in_, Private, Const);
|
||||
self.register_builtin_type(poly("Eq", vec![ty_tp(mono_q("R"))]), eq, Private, Const);
|
||||
self.register_builtin_type(
|
||||
poly("PartialOrd", vec![ty_tp(mono_q("R"))]),
|
||||
partial_ord,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(mono("Ord"), ord, Private, Const, None);
|
||||
self.register_builtin_type(mono("Num"), num, Private, Const, None);
|
||||
self.register_builtin_type(
|
||||
poly("Seq", vec![ty_tp(mono_q("T"))]),
|
||||
seq,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(mono("Ord"), ord, Private, Const);
|
||||
self.register_builtin_type(mono("Num"), num, Private, Const);
|
||||
self.register_builtin_type(poly("Seq", vec![ty_tp(mono_q("T"))]), seq, Private, Const);
|
||||
self.register_builtin_type(
|
||||
poly("Iterable", vec![ty_tp(mono_q("T"))]),
|
||||
iterable,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(poly("Add", ty_params.clone()), add, Private, Const);
|
||||
self.register_builtin_type(poly("Sub", ty_params.clone()), sub, Private, Const);
|
||||
self.register_builtin_type(poly("Mul", ty_params.clone()), mul, Private, Const);
|
||||
self.register_builtin_type(poly("Div", ty_params.clone()), div, Private, Const);
|
||||
self.register_builtin_type(poly("FloorDiv", ty_params), floor_div, Private, Const);
|
||||
self.register_builtin_type(poly("Add", ty_params.clone()), add, Private, Const, None);
|
||||
self.register_builtin_type(poly("Sub", ty_params.clone()), sub, Private, Const, None);
|
||||
self.register_builtin_type(poly("Mul", ty_params.clone()), mul, Private, Const, None);
|
||||
self.register_builtin_type(poly("Div", ty_params.clone()), div, Private, Const, None);
|
||||
self.register_builtin_type(poly("FloorDiv", ty_params), floor_div, Private, Const, None);
|
||||
self.register_const_param_defaults(
|
||||
"Eq",
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_t(mono_q("Self")))],
|
||||
|
@ -551,8 +686,8 @@ impl Context {
|
|||
float.register_superclass(Obj, &obj);
|
||||
// TODO: support multi platform
|
||||
float.register_builtin_const("EPSILON", Public, ValueObj::Float(2.220446049250313e-16));
|
||||
float.register_builtin_impl("Real", Float, Const, Public);
|
||||
float.register_builtin_impl("Imag", Float, Const, Public);
|
||||
float.register_builtin_py_impl("Real", Float, Const, Public, Some("real"));
|
||||
float.register_builtin_py_impl("Imag", Float, Const, Public, Some("imag"));
|
||||
float.register_marker_trait(mono("Num"));
|
||||
float.register_marker_trait(mono("Ord"));
|
||||
let mut float_partial_ord =
|
||||
|
@ -598,15 +733,15 @@ impl Context {
|
|||
float.register_trait(Float, float_mutizable);
|
||||
let mut float_show = Self::builtin_methods(Some(mono("Show")), 1);
|
||||
let t = fn0_met(Float, Str);
|
||||
float_show.register_builtin_impl("to_str", t, Immutable, Public);
|
||||
float_show.register_builtin_py_impl("to_str", t, Immutable, Public, Some("__str__"));
|
||||
float.register_trait(Float, float_show);
|
||||
|
||||
/* Ratio */
|
||||
// TODO: Int, Nat, Boolの継承元をRatioにする(今はFloat)
|
||||
let mut ratio = Self::builtin_mono_class("Ratio", 2);
|
||||
ratio.register_superclass(Obj, &obj);
|
||||
ratio.register_builtin_impl("Real", Ratio, Const, Public);
|
||||
ratio.register_builtin_impl("Imag", Ratio, Const, Public);
|
||||
ratio.register_builtin_py_impl("Real", Ratio, Const, Public, Some("real"));
|
||||
ratio.register_builtin_py_impl("Imag", Ratio, Const, Public, Some("imag"));
|
||||
ratio.register_marker_trait(mono("Num"));
|
||||
ratio.register_marker_trait(mono("Ord"));
|
||||
let mut ratio_partial_ord =
|
||||
|
@ -702,17 +837,17 @@ impl Context {
|
|||
int.register_trait(Int, int_mutizable);
|
||||
let mut int_show = Self::builtin_methods(Some(mono("Show")), 1);
|
||||
let t = fn0_met(Int, Str);
|
||||
int_show.register_builtin_impl("to_str", t, Immutable, Public);
|
||||
int_show.register_builtin_py_impl("to_str", t, Immutable, Public, Some("__str__"));
|
||||
int.register_trait(Int, int_show);
|
||||
int.register_builtin_impl("Real", Int, Const, Public);
|
||||
int.register_builtin_impl("Imag", Int, Const, Public);
|
||||
int.register_builtin_py_impl("Real", Int, Const, Public, Some("real"));
|
||||
int.register_builtin_py_impl("Imag", Int, Const, Public, Some("imag"));
|
||||
|
||||
/* Nat */
|
||||
let mut nat = Self::builtin_mono_class("Nat", 10);
|
||||
nat.register_superclass(Int, &int);
|
||||
// class("Rational"),
|
||||
// class("Integral"),
|
||||
nat.register_builtin_impl(
|
||||
nat.register_builtin_py_impl(
|
||||
"times!",
|
||||
pr_met(
|
||||
Nat,
|
||||
|
@ -723,6 +858,7 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("times"),
|
||||
);
|
||||
nat.register_marker_trait(mono("Num"));
|
||||
nat.register_marker_trait(mono("Ord"));
|
||||
|
@ -932,7 +1068,7 @@ impl Context {
|
|||
t,
|
||||
set! {static_instance("T", Type), static_instance("N", Nat), static_instance("M", Nat)},
|
||||
);
|
||||
array_.register_builtin_impl("concat", t, Immutable, Public);
|
||||
array_.register_builtin_py_impl("concat", t, Immutable, Public, Some("__add__"));
|
||||
// Array(T, N)|<: Add(Array(T, M))|.
|
||||
// Output = Array(T, N + M)
|
||||
// __add__: (self: Array(T, N), other: Array(T, M)) -> Array(T, N + M) = Array.concat
|
||||
|
@ -983,7 +1119,13 @@ impl Context {
|
|||
array_.register_marker_trait(mono("Mutizable"));
|
||||
array_.register_marker_trait(poly("Seq", vec![ty_tp(mono_q("T"))]));
|
||||
let mut array_show = Self::builtin_methods(Some(mono("Show")), 1);
|
||||
array_show.register_builtin_impl("to_str", fn0_met(arr_t.clone(), Str), Immutable, Public);
|
||||
array_show.register_builtin_py_impl(
|
||||
"to_str",
|
||||
fn0_met(arr_t.clone(), Str),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("__str__"),
|
||||
);
|
||||
array_.register_trait(arr_t.clone(), array_show);
|
||||
let mut array_iterable =
|
||||
Self::builtin_methods(Some(poly("Iterable", vec![ty_tp(mono_q("T"))])), 2);
|
||||
|
@ -1102,7 +1244,13 @@ impl Context {
|
|||
tuple_getitem_t,
|
||||
set! {static_instance("Ts", array_t(Type, mono_q_tp("N"))), static_instance("N", Nat)},
|
||||
);
|
||||
tuple_.register_builtin_impl("__Tuple_getitem__", tuple_getitem_t, Const, Public);
|
||||
tuple_.register_builtin_py_impl(
|
||||
"__Tuple_getitem__",
|
||||
tuple_getitem_t,
|
||||
Const,
|
||||
Public,
|
||||
Some("__getitem__"),
|
||||
);
|
||||
/* record */
|
||||
let mut record = Self::builtin_mono_class("Record", 2);
|
||||
record.register_superclass(Obj, &obj);
|
||||
|
@ -1211,7 +1359,7 @@ impl Context {
|
|||
/* File_mut */
|
||||
let mut file_mut = Self::builtin_mono_class("File!", 2);
|
||||
let mut file_mut_readable = Self::builtin_methods(Some(mono("Readable!")), 1);
|
||||
file_mut_readable.register_builtin_impl(
|
||||
file_mut_readable.register_builtin_py_impl(
|
||||
"read!",
|
||||
pr_met(
|
||||
ref_mut(mono("File!"), None),
|
||||
|
@ -1222,14 +1370,16 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("read"),
|
||||
);
|
||||
file_mut.register_trait(mono("File!"), file_mut_readable);
|
||||
let mut file_mut_writable = Self::builtin_methods(Some(mono("Writable!")), 1);
|
||||
file_mut_writable.register_builtin_impl(
|
||||
file_mut_writable.register_builtin_py_impl(
|
||||
"write!",
|
||||
pr1_kw_met(ref_mut(mono("File!"), None), kw("s", Str), Nat),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("write"),
|
||||
);
|
||||
file_mut.register_trait(mono("File!"), file_mut_writable);
|
||||
/* Array_mut */
|
||||
|
@ -1257,7 +1407,7 @@ impl Context {
|
|||
t,
|
||||
set! {static_instance("T", Type), static_instance("N", mono("Nat!"))},
|
||||
);
|
||||
array_mut_.register_builtin_impl("push!", t, Immutable, Public);
|
||||
array_mut_.register_builtin_py_impl("push!", t, Immutable, Public, Some("append"));
|
||||
let t = pr_met(
|
||||
array_mut_t.clone(),
|
||||
vec![kw("f", nd_func(vec![anon(mono_q("T"))], None, mono_q("T")))],
|
||||
|
@ -1310,7 +1460,7 @@ impl Context {
|
|||
t,
|
||||
set! {static_instance("T", Type), static_instance("N", mono("Nat!"))},
|
||||
);
|
||||
set_mut_.register_builtin_impl("add!", t, Immutable, Public);
|
||||
set_mut_.register_builtin_py_impl("add!", t, Immutable, Public, Some("add"));
|
||||
let t = pr_met(
|
||||
set_mut_t.clone(),
|
||||
vec![kw("f", nd_func(vec![anon(mono_q("T"))], None, mono_q("T")))],
|
||||
|
@ -1363,53 +1513,99 @@ impl Context {
|
|||
let mut named_func = Self::builtin_mono_class("NamedFunc", 2);
|
||||
named_func.register_superclass(mono("Func"), &func);
|
||||
named_func.register_marker_trait(mono("Named"));
|
||||
let mut quant = Self::builtin_mono_class("Quantified", 2);
|
||||
quant.register_superclass(mono("Proc"), &proc);
|
||||
let mut qfunc = Self::builtin_mono_class("QuantifiedFunc", 2);
|
||||
qfunc.register_superclass(mono("Func"), &func);
|
||||
self.register_builtin_type(Obj, obj, Private, Const);
|
||||
self.register_builtin_type(Obj, obj, Private, Const, Some("object"));
|
||||
// self.register_type(mono("Record"), vec![], record, Private, Const);
|
||||
self.register_builtin_type(Int, int, Private, Const);
|
||||
self.register_builtin_type(Nat, nat, Private, Const);
|
||||
self.register_builtin_type(Float, float, Private, Const);
|
||||
self.register_builtin_type(Ratio, ratio, Private, Const);
|
||||
self.register_builtin_type(Bool, bool_, Private, Const);
|
||||
self.register_builtin_type(Str, str_, Private, Const);
|
||||
self.register_builtin_type(NoneType, nonetype, Private, Const);
|
||||
self.register_builtin_type(Type, type_, Private, Const);
|
||||
self.register_builtin_type(ClassType, class_type, Private, Const);
|
||||
self.register_builtin_type(TraitType, trait_type, Private, Const);
|
||||
self.register_builtin_type(g_module_t, generic_module, Private, Const);
|
||||
self.register_builtin_type(module_t, module, Private, Const);
|
||||
self.register_builtin_type(arr_t, array_, Private, Const);
|
||||
self.register_builtin_type(set_t, set_, Private, Const);
|
||||
self.register_builtin_type(g_dict_t, generic_dict, Private, Const);
|
||||
self.register_builtin_type(dict_t, dict_, Private, Const);
|
||||
self.register_builtin_type(mono("Bytes"), bytes, Private, Const);
|
||||
self.register_builtin_type(mono("GenericTuple"), generic_tuple, Private, Const);
|
||||
self.register_builtin_type(tuple_t, tuple_, Private, Const);
|
||||
self.register_builtin_type(mono("Record"), record, Private, Const);
|
||||
self.register_builtin_type(or_t, or, Private, Const);
|
||||
self.register_builtin_type(mono("StrIterator"), str_iterator, Private, Const);
|
||||
self.register_builtin_type(Int, int, Private, Const, Some("int"));
|
||||
self.register_builtin_type(Nat, nat, Private, Const, Some("Nat"));
|
||||
self.register_builtin_type(Float, float, Private, Const, Some("float"));
|
||||
self.register_builtin_type(Ratio, ratio, Private, Const, Some("Ratio"));
|
||||
self.register_builtin_type(Bool, bool_, Private, Const, Some("Bool"));
|
||||
self.register_builtin_type(Str, str_, Private, Const, Some("Str"));
|
||||
self.register_builtin_type(NoneType, nonetype, Private, Const, Some("NoneType"));
|
||||
self.register_builtin_type(Type, type_, Private, Const, Some("type"));
|
||||
self.register_builtin_type(ClassType, class_type, Private, Const, Some("ClassType"));
|
||||
self.register_builtin_type(TraitType, trait_type, Private, Const, Some("TraitType"));
|
||||
self.register_builtin_type(
|
||||
g_module_t,
|
||||
generic_module,
|
||||
Private,
|
||||
Const,
|
||||
Some("ModuleType"),
|
||||
);
|
||||
self.register_builtin_type(module_t, module, Private, Const, Some("Module"));
|
||||
self.register_builtin_type(arr_t, array_, Private, Const, Some("list"));
|
||||
self.register_builtin_type(set_t, set_, Private, Const, Some("set"));
|
||||
self.register_builtin_type(g_dict_t, generic_dict, Private, Const, Some("dict"));
|
||||
self.register_builtin_type(dict_t, dict_, Private, Const, Some("dict"));
|
||||
self.register_builtin_type(mono("Bytes"), bytes, Private, Const, Some("bytes"));
|
||||
self.register_builtin_type(
|
||||
mono("GenericTuple"),
|
||||
generic_tuple,
|
||||
Private,
|
||||
Const,
|
||||
Some("tuple"),
|
||||
);
|
||||
self.register_builtin_type(tuple_t, tuple_, Private, Const, Some("tuple"));
|
||||
self.register_builtin_type(mono("Record"), record, Private, Const, Some("Record"));
|
||||
self.register_builtin_type(or_t, or, Private, Const, Some("Union"));
|
||||
self.register_builtin_type(
|
||||
mono("StrIterator"),
|
||||
str_iterator,
|
||||
Private,
|
||||
Const,
|
||||
Some("str_iterator"),
|
||||
);
|
||||
self.register_builtin_type(
|
||||
poly("ArrayIterator", vec![ty_tp(mono_q("T"))]),
|
||||
array_iterator,
|
||||
Private,
|
||||
Const,
|
||||
Some("array_iterator"),
|
||||
);
|
||||
self.register_builtin_type(mono("Int!"), int_mut, Private, Const, Some("int"));
|
||||
self.register_builtin_type(mono("Nat!"), nat_mut, Private, Const, Some("Nat"));
|
||||
self.register_builtin_type(mono("Float!"), float_mut, Private, Const, Some("float"));
|
||||
self.register_builtin_type(mono("Ratio!"), ratio_mut, Private, Const, Some("Ratio"));
|
||||
self.register_builtin_type(mono("Bool!"), bool_mut, Private, Const, Some("Bool"));
|
||||
self.register_builtin_type(mono("Str!"), str_mut, Private, Const, Some("Str"));
|
||||
self.register_builtin_type(mono("File!"), file_mut, Private, Const, Some("File"));
|
||||
self.register_builtin_type(array_mut_t, array_mut_, Private, Const, Some("list"));
|
||||
self.register_builtin_type(set_mut_t, set_mut_, Private, Const, Some("set"));
|
||||
self.register_builtin_type(range_t, range, Private, Const, Some("Range"));
|
||||
self.register_builtin_type(mono("Proc"), proc, Private, Const, Some("Proc"));
|
||||
self.register_builtin_type(
|
||||
mono("NamedProc"),
|
||||
named_proc,
|
||||
Private,
|
||||
Const,
|
||||
Some("NamedProc"),
|
||||
);
|
||||
self.register_builtin_type(mono("Func"), func, Private, Const, Some("Func"));
|
||||
self.register_builtin_type(
|
||||
mono("NamedFunc"),
|
||||
named_func,
|
||||
Private,
|
||||
Const,
|
||||
Some("NamedFunc"),
|
||||
);
|
||||
self.register_builtin_type(
|
||||
mono("Quantified"),
|
||||
quant,
|
||||
Private,
|
||||
Const,
|
||||
Some("Quantified"),
|
||||
);
|
||||
self.register_builtin_type(
|
||||
mono("QuantifiedFunc"),
|
||||
qfunc,
|
||||
Private,
|
||||
Const,
|
||||
Some("QuantifiedFunc"),
|
||||
);
|
||||
self.register_builtin_type(mono("Int!"), int_mut, Private, Const);
|
||||
self.register_builtin_type(mono("Nat!"), nat_mut, Private, Const);
|
||||
self.register_builtin_type(mono("Float!"), float_mut, Private, Const);
|
||||
self.register_builtin_type(mono("Ratio!"), ratio_mut, Private, Const);
|
||||
self.register_builtin_type(mono("Bool!"), bool_mut, Private, Const);
|
||||
self.register_builtin_type(mono("Str!"), str_mut, Private, Const);
|
||||
self.register_builtin_type(mono("File!"), file_mut, Private, Const);
|
||||
self.register_builtin_type(array_mut_t, array_mut_, Private, Const);
|
||||
self.register_builtin_type(set_mut_t, set_mut_, Private, Const);
|
||||
self.register_builtin_type(range_t, range, Private, Const);
|
||||
self.register_builtin_type(mono("Proc"), proc, Private, Const);
|
||||
self.register_builtin_type(mono("NamedProc"), named_proc, Private, Const);
|
||||
self.register_builtin_type(mono("Func"), func, Private, Const);
|
||||
self.register_builtin_type(mono("NamedFunc"), named_func, Private, Const);
|
||||
self.register_builtin_type(mono("QuantifiedFunc"), qfunc, Private, Const);
|
||||
}
|
||||
|
||||
fn init_builtin_funcs(&mut self) {
|
||||
|
@ -1519,33 +1715,57 @@ impl Context {
|
|||
let t_exit = t_quit.clone();
|
||||
let t_repr = nd_func(vec![kw("object", Obj)], None, Str);
|
||||
let t_round = nd_func(vec![kw("number", Float)], None, Int);
|
||||
self.register_builtin_impl("abs", t_abs, Immutable, Private);
|
||||
self.register_builtin_impl("ascii", t_ascii, Immutable, Private);
|
||||
self.register_builtin_py_impl("abs", t_abs, Immutable, Private, Some("abs"));
|
||||
self.register_builtin_py_impl("ascii", t_ascii, Immutable, Private, Some("ascii"));
|
||||
self.register_builtin_impl("assert", t_assert, Const, Private); // assert casting に悪影響が出る可能性があるため、Constとしておく
|
||||
self.register_builtin_impl("bin", t_bin, Immutable, Private);
|
||||
self.register_builtin_impl("chr", t_chr, Immutable, Private);
|
||||
self.register_builtin_impl("classof", t_classof, Immutable, Private);
|
||||
self.register_builtin_impl("compile", t_compile, Immutable, Private);
|
||||
self.register_builtin_py_impl("bin", t_bin, Immutable, Private, Some("bin"));
|
||||
self.register_builtin_py_impl("chr", t_chr, Immutable, Private, Some("chr"));
|
||||
self.register_builtin_py_impl("classof", t_classof, Immutable, Private, Some("type"));
|
||||
self.register_builtin_py_impl("compile", t_compile, Immutable, Private, Some("compile"));
|
||||
self.register_builtin_impl("cond", t_cond, Immutable, Private);
|
||||
self.register_builtin_impl("discard", t_discard, Immutable, Private);
|
||||
self.register_builtin_impl("exit", t_exit, Immutable, Private);
|
||||
self.register_builtin_py_impl("exit", t_exit, Immutable, Private, Some("exit"));
|
||||
self.register_builtin_impl("if", t_if, Immutable, Private);
|
||||
self.register_builtin_impl("import", t_import, Immutable, Private);
|
||||
self.register_builtin_impl("isinstance", t_isinstance, Immutable, Private);
|
||||
self.register_builtin_impl("issubclass", t_issubclass, Immutable, Private);
|
||||
self.register_builtin_impl("len", t_len, Immutable, Private);
|
||||
self.register_builtin_impl("log", t_log, Immutable, Private);
|
||||
self.register_builtin_impl("oct", t_oct, Immutable, Private);
|
||||
self.register_builtin_impl("ord", t_ord, Immutable, Private);
|
||||
self.register_builtin_impl("panic", t_panic, Immutable, Private);
|
||||
self.register_builtin_impl("pow", t_pow, Immutable, Private);
|
||||
self.register_builtin_py_impl("import", t_import, Immutable, Private, Some("__import__"));
|
||||
self.register_builtin_py_impl(
|
||||
"isinstance",
|
||||
t_isinstance,
|
||||
Immutable,
|
||||
Private,
|
||||
Some("isinstance"),
|
||||
);
|
||||
self.register_builtin_py_impl(
|
||||
"issubclass",
|
||||
t_issubclass,
|
||||
Immutable,
|
||||
Private,
|
||||
Some("issubclass"),
|
||||
);
|
||||
self.register_builtin_py_impl("len", t_len, Immutable, Private, Some("len"));
|
||||
self.register_builtin_py_impl("log", t_log, Immutable, Private, Some("print"));
|
||||
self.register_builtin_py_impl("oct", t_oct, Immutable, Private, Some("oct"));
|
||||
self.register_builtin_py_impl("ord", t_ord, Immutable, Private, Some("ord"));
|
||||
self.register_builtin_py_impl("panic", t_panic, Immutable, Private, Some("quit"));
|
||||
self.register_builtin_py_impl("pow", t_pow, Immutable, Private, Some("pow"));
|
||||
if cfg!(feature = "debug") {
|
||||
self.register_builtin_impl("py", t_pyimport.clone(), Immutable, Private);
|
||||
self.register_builtin_py_impl(
|
||||
"py",
|
||||
t_pyimport.clone(),
|
||||
Immutable,
|
||||
Private,
|
||||
Some("__import__"),
|
||||
);
|
||||
}
|
||||
self.register_builtin_impl("pyimport", t_pyimport, Immutable, Private);
|
||||
self.register_builtin_impl("quit", t_quit, Immutable, Private);
|
||||
self.register_builtin_impl("repr", t_repr, Immutable, Private);
|
||||
self.register_builtin_impl("round", t_round, Immutable, Private);
|
||||
self.register_builtin_py_impl(
|
||||
"pyimport",
|
||||
t_pyimport,
|
||||
Immutable,
|
||||
Private,
|
||||
Some("__import__"),
|
||||
);
|
||||
self.register_builtin_py_impl("quit", t_quit, Immutable, Private, Some("quit"));
|
||||
self.register_builtin_py_impl("repr", t_repr, Immutable, Private, Some("repr"));
|
||||
self.register_builtin_py_impl("round", t_round, Immutable, Private, Some("round"));
|
||||
}
|
||||
|
||||
fn init_builtin_const_funcs(&mut self) {
|
||||
|
@ -1682,16 +1902,16 @@ impl Context {
|
|||
t_with,
|
||||
set! {static_instance("T", Type), static_instance("U", Type)},
|
||||
);
|
||||
self.register_builtin_impl("dir!", t_dir, Immutable, Private);
|
||||
self.register_builtin_impl("print!", t_print, Immutable, Private);
|
||||
self.register_builtin_impl("id!", t_id, Immutable, Private);
|
||||
self.register_builtin_impl("input!", t_input, Immutable, Private);
|
||||
self.register_builtin_py_impl("dir!", t_dir, Immutable, Private, Some("dir"));
|
||||
self.register_builtin_py_impl("print!", t_print, Immutable, Private, Some("print"));
|
||||
self.register_builtin_py_impl("id!", t_id, Immutable, Private, Some("id"));
|
||||
self.register_builtin_py_impl("input!", t_input, Immutable, Private, Some("input"));
|
||||
self.register_builtin_impl("if!", t_if, Immutable, Private);
|
||||
self.register_builtin_impl("for!", t_for, Immutable, Private);
|
||||
self.register_builtin_impl("globals!", t_globals, Immutable, Private);
|
||||
self.register_builtin_impl("locals!", t_locals, Immutable, Private);
|
||||
self.register_builtin_py_impl("globals!", t_globals, Immutable, Private, Some("globals"));
|
||||
self.register_builtin_py_impl("locals!", t_locals, Immutable, Private, Some("locals"));
|
||||
self.register_builtin_impl("while!", t_while, Immutable, Private);
|
||||
self.register_builtin_impl("open!", t_open, Immutable, Private);
|
||||
self.register_builtin_py_impl("open!", t_open, Immutable, Private, Some("open"));
|
||||
self.register_builtin_impl("with!", t_with, Immutable, Private);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ use Visibility::*;
|
|||
impl Context {
|
||||
pub(crate) fn init_py_glob_mod() -> Self {
|
||||
let mut glob = Context::builtin_module("glob", 10);
|
||||
glob.register_builtin_impl(
|
||||
glob.register_builtin_py_impl(
|
||||
"glob!",
|
||||
proc(
|
||||
vec![kw("pathname", Str)],
|
||||
|
@ -23,6 +23,7 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("glob"),
|
||||
);
|
||||
glob
|
||||
}
|
||||
|
|
|
@ -12,11 +12,12 @@ use Visibility::*;
|
|||
impl Context {
|
||||
pub(crate) fn init_py_importlib_mod() -> Self {
|
||||
let mut importlib = Context::builtin_module("importlib", 15);
|
||||
importlib.register_builtin_impl(
|
||||
importlib.register_builtin_py_impl(
|
||||
"reload!",
|
||||
proc1(mono("GenericModule"), NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("reload"),
|
||||
);
|
||||
importlib
|
||||
}
|
||||
|
|
|
@ -15,13 +15,20 @@ impl Context {
|
|||
let mut string_io = Context::builtin_mono_class("StringIO!", 0);
|
||||
// FIXME: include Obj (pass main_ctx as a param)
|
||||
// string_io.register_superclass(Obj, obj);
|
||||
string_io.register_builtin_impl(
|
||||
string_io.register_builtin_py_impl(
|
||||
"getvalue!",
|
||||
pr0_met(ref_(mono("io.StringIO!")), Str),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("getvalue"),
|
||||
);
|
||||
io.register_builtin_type(
|
||||
mono("io.StringIO!"),
|
||||
string_io,
|
||||
Public,
|
||||
Const,
|
||||
Some("io.StringIO"),
|
||||
);
|
||||
io.register_builtin_type(mono("io.StringIO!"), string_io, Public, Const);
|
||||
io
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,15 +13,22 @@ use Visibility::*;
|
|||
impl Context {
|
||||
pub(crate) fn init_py_os_mod() -> Self {
|
||||
let mut os = Context::builtin_module("os", 15);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"chdir!",
|
||||
nd_proc1(kw("path", mono("PathLike")), NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("chdir"),
|
||||
);
|
||||
os.register_builtin_impl("getcwd!", proc0(Str), Immutable, Public);
|
||||
os.register_builtin_impl("getenv!", nd_proc1(kw("key", Str), Str), Immutable, Public);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl("getcwd!", proc0(Str), Immutable, Public, Some("getcwd"));
|
||||
os.register_builtin_py_impl(
|
||||
"getenv!",
|
||||
nd_proc1(kw("key", Str), Str),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("getenv"),
|
||||
);
|
||||
os.register_builtin_py_impl(
|
||||
"listdir!",
|
||||
proc(
|
||||
vec![],
|
||||
|
@ -31,15 +38,17 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("listdir"),
|
||||
);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"mkdir!",
|
||||
nd_proc1(kw("path", mono("PathLike")), NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("mkdir"),
|
||||
);
|
||||
os.register_builtin_impl("name", Str, Immutable, Public);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"putenv!",
|
||||
proc(
|
||||
vec![kw("key", Str), kw("value", Str)],
|
||||
|
@ -49,20 +58,23 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("putenv"),
|
||||
);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"remove!",
|
||||
nd_proc1(kw("path", mono("PathLike")), NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("remove"),
|
||||
);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"removedirs!",
|
||||
nd_proc1(kw("name", mono("PathLike")), NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("removedirs"),
|
||||
);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"rename!",
|
||||
proc(
|
||||
vec![kw("src", mono("PathLike")), kw("dst", mono("PathLike"))],
|
||||
|
@ -72,19 +84,22 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("rename"),
|
||||
);
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"rmdir!",
|
||||
nd_proc1(kw("path", mono("PathLike")), NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("rmdir"),
|
||||
);
|
||||
if cfg!(unix) {
|
||||
os.register_builtin_impl(
|
||||
os.register_builtin_py_impl(
|
||||
"uname!",
|
||||
proc0(mono("posix.UnameResult")),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("uname"),
|
||||
);
|
||||
}
|
||||
// TODO
|
||||
|
|
|
@ -15,7 +15,7 @@ use Visibility::*;
|
|||
impl Context {
|
||||
pub(crate) fn init_py_random_mod() -> Self {
|
||||
let mut random = Context::builtin_module("random", 10);
|
||||
random.register_builtin_impl(
|
||||
random.register_builtin_py_impl(
|
||||
"seed!",
|
||||
proc(
|
||||
vec![],
|
||||
|
@ -28,12 +28,14 @@ impl Context {
|
|||
),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("seed"),
|
||||
);
|
||||
random.register_builtin_impl(
|
||||
random.register_builtin_py_impl(
|
||||
"randint!",
|
||||
nd_proc(vec![kw("a", Int), kw("b", Int)], None, Int),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("randint"),
|
||||
);
|
||||
let t = nd_proc(
|
||||
vec![kw("seq", poly("Seq", vec![ty_tp(mono_q("T"))]))],
|
||||
|
@ -41,7 +43,7 @@ impl Context {
|
|||
mono_q("T"),
|
||||
);
|
||||
let t = quant(t, set! {static_instance("T", Type)});
|
||||
random.register_builtin_impl("choice!", t, Immutable, Public);
|
||||
random.register_builtin_py_impl("choice!", t, Immutable, Public, Some("choice"));
|
||||
random
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,13 @@ impl Context {
|
|||
Immutable,
|
||||
Public,
|
||||
);
|
||||
socket.register_builtin_type(mono("socket.Socket!"), sock, Public, Const);
|
||||
socket.register_builtin_type(
|
||||
mono("socket.Socket!"),
|
||||
sock,
|
||||
Public,
|
||||
Const,
|
||||
Some("socket.Socket"),
|
||||
);
|
||||
socket
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,14 @@ use Visibility::*;
|
|||
impl Context {
|
||||
pub(crate) fn init_py_time_mod() -> Self {
|
||||
let mut time = Context::builtin_module("time", 15);
|
||||
time.register_builtin_impl("sleep!", proc1(Float, NoneType), Immutable, Public);
|
||||
time.register_builtin_impl("time!", proc0(Float), Immutable, Public);
|
||||
time.register_builtin_py_impl(
|
||||
"sleep!",
|
||||
proc1(Float, NoneType),
|
||||
Immutable,
|
||||
Public,
|
||||
Some("sleep"),
|
||||
);
|
||||
time.register_builtin_py_impl("time!", proc0(Float), Immutable, Public, Some("time"));
|
||||
time
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,13 @@ impl Context {
|
|||
urllib.py_mod_cache = Some(SharedModuleCache::new());
|
||||
let mut request_class = Context::builtin_mono_class("Request", 5);
|
||||
request_class.register_builtin_impl("data", mono("Bytes"), Immutable, Public);
|
||||
urllib.register_builtin_type(mono("urllib.request.Request"), request_class, Public, Const);
|
||||
urllib.register_builtin_type(
|
||||
mono("urllib.request.Request"),
|
||||
request_class,
|
||||
Public,
|
||||
Const,
|
||||
Some("urllib.request.Request"),
|
||||
);
|
||||
urllib.register_builtin_impl("request", module_from_path("request"), Immutable, Public);
|
||||
let mut request = Context::builtin_module("urllib.request", 15);
|
||||
let t = proc(
|
||||
|
@ -30,7 +36,7 @@ impl Context {
|
|||
],
|
||||
mono("http.client.HTTPResponse"),
|
||||
);
|
||||
request.register_builtin_impl("urlopen", t, Immutable, Public);
|
||||
request.register_builtin_py_impl("urlopen!", t, Immutable, Public, Some("urlopen"));
|
||||
urllib.register_builtin_impl("parse", module_from_path("parse"), Immutable, Public);
|
||||
let parse = Context::builtin_module("urllib.parse", 15);
|
||||
urllib
|
||||
|
|
|
@ -3,6 +3,7 @@ use std::option::Option; // conflicting to Type::Option
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use erg_common::config::Input;
|
||||
use erg_common::env::erg_pystd_path;
|
||||
use erg_common::error::{ErrorCore, ErrorKind, Location};
|
||||
use erg_common::levenshtein::get_similar_name;
|
||||
use erg_common::set::Set;
|
||||
|
@ -29,12 +30,12 @@ use crate::error::{
|
|||
TyCheckErrors, TyCheckResult,
|
||||
};
|
||||
use crate::hir;
|
||||
use crate::varinfo::VarInfo;
|
||||
use crate::varinfo::{Mutability, VarInfo, VarKind};
|
||||
use crate::AccessKind;
|
||||
use RegistrationMode::*;
|
||||
use Visibility::*;
|
||||
|
||||
use super::MethodType;
|
||||
use super::MethodInfo;
|
||||
|
||||
impl Context {
|
||||
pub(crate) fn validate_var_sig_t(
|
||||
|
@ -209,7 +210,7 @@ impl Context {
|
|||
&self,
|
||||
pos_args: &[hir::PosArg],
|
||||
kw_args: &[hir::KwArg],
|
||||
) -> TyCheckResult<Type> {
|
||||
) -> TyCheckResult<VarInfo> {
|
||||
if !kw_args.is_empty() {
|
||||
todo!()
|
||||
}
|
||||
|
@ -284,14 +285,17 @@ impl Context {
|
|||
let param_ty = ParamTy::anonymous(match_target_expr_t.clone());
|
||||
let param_ts = [vec![param_ty], branch_ts.to_vec()].concat();
|
||||
let t = func(param_ts, None, vec![], return_t);
|
||||
Ok(t)
|
||||
Ok(VarInfo {
|
||||
t,
|
||||
..VarInfo::default()
|
||||
})
|
||||
}
|
||||
|
||||
fn get_import_call_t(
|
||||
&self,
|
||||
pos_args: &[hir::PosArg],
|
||||
kw_args: &[hir::KwArg],
|
||||
) -> TyCheckResult<Type> {
|
||||
) -> TyCheckResult<VarInfo> {
|
||||
let mod_name = pos_args
|
||||
.get(0)
|
||||
.map(|a| &a.expr)
|
||||
|
@ -337,20 +341,24 @@ impl Context {
|
|||
vec![],
|
||||
module(TyParam::Value(s)),
|
||||
);
|
||||
Ok(import_t)
|
||||
Ok(VarInfo {
|
||||
t: import_t,
|
||||
py_name: Some(Str::ever("__import__")),
|
||||
..VarInfo::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn rec_get_var_t(
|
||||
pub(crate) fn rec_get_var_info(
|
||||
&self,
|
||||
ident: &Identifier,
|
||||
acc_kind: AccessKind,
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> SingleTyCheckResult<Type> {
|
||||
) -> SingleTyCheckResult<VarInfo> {
|
||||
if let Some(vi) = self.get_current_scope_var(&ident.inspect()[..]) {
|
||||
match self.validate_visibility(ident, vi, input, namespace) {
|
||||
Ok(()) => {
|
||||
return Ok(vi.t());
|
||||
return Ok(vi.clone());
|
||||
}
|
||||
Err(err) => {
|
||||
if !acc_kind.is_local() {
|
||||
|
@ -360,7 +368,7 @@ impl Context {
|
|||
}
|
||||
}
|
||||
if let Some(parent) = self.get_outer().or_else(|| self.get_builtins()) {
|
||||
return parent.rec_get_var_t(ident, acc_kind, input, namespace);
|
||||
return parent.rec_get_var_info(ident, acc_kind, input, namespace);
|
||||
}
|
||||
Err(TyCheckError::no_var_error(
|
||||
input.clone(),
|
||||
|
@ -372,17 +380,17 @@ impl Context {
|
|||
))
|
||||
}
|
||||
|
||||
pub(crate) fn rec_get_decl_t(
|
||||
pub(crate) fn rec_get_decl_info(
|
||||
&self,
|
||||
ident: &Identifier,
|
||||
acc_kind: AccessKind,
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> SingleTyCheckResult<Type> {
|
||||
) -> SingleTyCheckResult<VarInfo> {
|
||||
if let Some(vi) = self.decls.get(&ident.inspect()[..]) {
|
||||
match self.validate_visibility(ident, vi, input, namespace) {
|
||||
Ok(()) => {
|
||||
return Ok(vi.t());
|
||||
return Ok(vi.clone());
|
||||
}
|
||||
Err(err) => {
|
||||
if !acc_kind.is_local() {
|
||||
|
@ -392,7 +400,7 @@ impl Context {
|
|||
}
|
||||
}
|
||||
if let Some(parent) = self.get_outer().or_else(|| self.get_builtins()) {
|
||||
return parent.rec_get_decl_t(ident, acc_kind, input, namespace);
|
||||
return parent.rec_get_decl_info(ident, acc_kind, input, namespace);
|
||||
}
|
||||
Err(TyCheckError::no_var_error(
|
||||
input.clone(),
|
||||
|
@ -404,16 +412,16 @@ impl Context {
|
|||
))
|
||||
}
|
||||
|
||||
pub(crate) fn rec_get_attr_t(
|
||||
pub(crate) fn rec_get_attr_info(
|
||||
&self,
|
||||
obj: &hir::Expr,
|
||||
ident: &Identifier,
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> SingleTyCheckResult<Type> {
|
||||
) -> SingleTyCheckResult<VarInfo> {
|
||||
let self_t = obj.t();
|
||||
let name = ident.name.token();
|
||||
match self.get_attr_t_from_attributive(obj, &self_t, ident, namespace) {
|
||||
match self.get_attr_info_from_attributive(obj, &self_t, ident, namespace) {
|
||||
Ok(t) => {
|
||||
return Ok(t);
|
||||
}
|
||||
|
@ -423,9 +431,9 @@ impl Context {
|
|||
}
|
||||
}
|
||||
if let Ok(singular_ctx) = self.get_singular_ctx(obj, namespace) {
|
||||
match singular_ctx.rec_get_var_t(ident, AccessKind::Attr, input, namespace) {
|
||||
Ok(t) => {
|
||||
return Ok(t);
|
||||
match singular_ctx.rec_get_var_info(ident, AccessKind::Attr, input, namespace) {
|
||||
Ok(vi) => {
|
||||
return Ok(vi);
|
||||
}
|
||||
Err(e) if e.core.kind == ErrorKind::NameError => {}
|
||||
Err(e) => {
|
||||
|
@ -443,7 +451,7 @@ impl Context {
|
|||
None, // TODO:
|
||||
)
|
||||
})? {
|
||||
match ctx.rec_get_var_t(ident, AccessKind::Attr, input, namespace) {
|
||||
match ctx.rec_get_var_info(ident, AccessKind::Attr, input, namespace) {
|
||||
Ok(t) => {
|
||||
return Ok(t);
|
||||
}
|
||||
|
@ -455,7 +463,7 @@ impl Context {
|
|||
}
|
||||
// TODO: dependent type widening
|
||||
if let Some(parent) = self.get_outer().or_else(|| self.get_builtins()) {
|
||||
parent.rec_get_attr_t(obj, ident, input, namespace)
|
||||
parent.rec_get_attr_info(obj, ident, input, namespace)
|
||||
} else {
|
||||
Err(TyCheckError::no_attr_error(
|
||||
input.clone(),
|
||||
|
@ -471,32 +479,42 @@ impl Context {
|
|||
|
||||
/// get type from given attributive type (Record).
|
||||
/// not ModuleType or ClassType etc.
|
||||
fn get_attr_t_from_attributive(
|
||||
fn get_attr_info_from_attributive(
|
||||
&self,
|
||||
obj: &hir::Expr,
|
||||
t: &Type,
|
||||
ident: &Identifier,
|
||||
namespace: &Str,
|
||||
) -> SingleTyCheckResult<Type> {
|
||||
) -> SingleTyCheckResult<VarInfo> {
|
||||
match t {
|
||||
Type::FreeVar(fv) if fv.is_linked() => {
|
||||
self.get_attr_t_from_attributive(obj, &fv.crack(), ident, namespace)
|
||||
self.get_attr_info_from_attributive(obj, &fv.crack(), ident, namespace)
|
||||
}
|
||||
Type::FreeVar(fv) => {
|
||||
let sup = fv.get_sup().unwrap();
|
||||
self.get_attr_t_from_attributive(obj, &sup, ident, namespace)
|
||||
self.get_attr_info_from_attributive(obj, &sup, ident, namespace)
|
||||
}
|
||||
Type::Ref(t) => self.get_attr_t_from_attributive(obj, t, ident, namespace),
|
||||
Type::Ref(t) => self.get_attr_info_from_attributive(obj, t, ident, namespace),
|
||||
Type::RefMut { before, .. } => {
|
||||
self.get_attr_t_from_attributive(obj, before, ident, namespace)
|
||||
self.get_attr_info_from_attributive(obj, before, ident, namespace)
|
||||
}
|
||||
Type::Refinement(refine) => {
|
||||
self.get_attr_t_from_attributive(obj, &refine.t, ident, namespace)
|
||||
self.get_attr_info_from_attributive(obj, &refine.t, ident, namespace)
|
||||
}
|
||||
Type::Record(record) => {
|
||||
// REVIEW: `rec.get(name.inspect())` returns None (Borrow<Str> is implemented for Field). Why?
|
||||
if let Some(attr) = record.get(&Field::new(Public, ident.inspect().clone())) {
|
||||
Ok(attr.clone())
|
||||
if let Some(attr_t) = record.get(&Field::new(Public, ident.inspect().clone())) {
|
||||
let muty = Mutability::from(&ident.inspect()[..]);
|
||||
let vi = VarInfo::new(
|
||||
attr_t.clone(),
|
||||
muty,
|
||||
Public,
|
||||
VarKind::Builtin,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
Ok(vi)
|
||||
} else {
|
||||
let t = Type::Record(record.clone());
|
||||
Err(TyCheckError::no_attr_error(
|
||||
|
@ -515,7 +533,18 @@ impl Context {
|
|||
match v {
|
||||
ValueObj::Type(TypeObj::Generated(gen)) => self
|
||||
.get_gen_t_require_attr_t(gen, &ident.inspect()[..])
|
||||
.cloned()
|
||||
.map(|attr_t| {
|
||||
let muty = Mutability::from(&ident.inspect()[..]);
|
||||
VarInfo::new(
|
||||
attr_t.clone(),
|
||||
muty,
|
||||
Public,
|
||||
VarKind::Builtin,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
})
|
||||
.ok_or_else(|| {
|
||||
TyCheckError::dummy(self.cfg.input.clone(), line!() as usize)
|
||||
}),
|
||||
|
@ -539,13 +568,13 @@ impl Context {
|
|||
}
|
||||
|
||||
// returns callee's type, not the return type
|
||||
fn search_callee_t(
|
||||
fn search_callee_info(
|
||||
&self,
|
||||
obj: &hir::Expr,
|
||||
attr_name: &Option<Identifier>,
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> SingleTyCheckResult<Type> {
|
||||
) -> SingleTyCheckResult<VarInfo> {
|
||||
if let Some(attr_name) = attr_name.as_ref() {
|
||||
for ctx in self
|
||||
.get_nominal_super_type_ctxs(obj.ref_t())
|
||||
|
@ -566,7 +595,7 @@ impl Context {
|
|||
.or_else(|| ctx.decls.get(attr_name.inspect()))
|
||||
{
|
||||
self.validate_visibility(attr_name, vi, input, namespace)?;
|
||||
return Ok(vi.t());
|
||||
return Ok(vi.clone());
|
||||
}
|
||||
for (_, methods_ctx) in ctx.methods_list.iter() {
|
||||
if let Some(vi) = methods_ctx
|
||||
|
@ -575,7 +604,7 @@ impl Context {
|
|||
.or_else(|| methods_ctx.decls.get(attr_name.inspect()))
|
||||
{
|
||||
self.validate_visibility(attr_name, vi, input, namespace)?;
|
||||
return Ok(vi.t());
|
||||
return Ok(vi.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -586,7 +615,7 @@ impl Context {
|
|||
.or_else(|| singular_ctx.decls.get(attr_name.inspect()))
|
||||
{
|
||||
self.validate_visibility(attr_name, vi, input, namespace)?;
|
||||
return Ok(vi.t());
|
||||
return Ok(vi.clone());
|
||||
}
|
||||
for (_, method_ctx) in singular_ctx.methods_list.iter() {
|
||||
if let Some(vi) = method_ctx
|
||||
|
@ -595,7 +624,7 @@ impl Context {
|
|||
.or_else(|| method_ctx.decls.get(attr_name.inspect()))
|
||||
{
|
||||
self.validate_visibility(attr_name, vi, input, namespace)?;
|
||||
return Ok(vi.t());
|
||||
return Ok(vi.clone());
|
||||
}
|
||||
}
|
||||
return Err(TyCheckError::singular_no_attr_error(
|
||||
|
@ -610,11 +639,11 @@ impl Context {
|
|||
));
|
||||
}
|
||||
match self.get_method_type_by_name(attr_name) {
|
||||
Ok(t) => {
|
||||
self.sub_unify(obj.ref_t(), &t.definition_type, obj.loc(), None)
|
||||
Ok(method) => {
|
||||
self.sub_unify(obj.ref_t(), &method.definition_type, obj.loc(), None)
|
||||
// HACK: change this func's return type to TyCheckResult<Type>
|
||||
.map_err(|mut errs| errs.remove(0))?;
|
||||
return Ok(t.method_type.clone());
|
||||
return Ok(method.method_type.clone());
|
||||
}
|
||||
Err(err) if err.core.kind == ErrorKind::TypeError => {
|
||||
return Err(err);
|
||||
|
@ -632,7 +661,10 @@ impl Context {
|
|||
self.get_similar_attr(obj.ref_t(), attr_name.inspect()),
|
||||
))
|
||||
} else {
|
||||
Ok(obj.t())
|
||||
Ok(VarInfo {
|
||||
t: obj.t(),
|
||||
..VarInfo::default()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -677,11 +709,11 @@ impl Context {
|
|||
args: &[hir::PosArg],
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> TyCheckResult<Type> {
|
||||
) -> TyCheckResult<VarInfo> {
|
||||
erg_common::debug_power_assert!(args.len() == 2);
|
||||
let cont = binop_to_dname(op.inspect());
|
||||
let symbol = Token::new(op.kind, Str::rc(cont), op.lineno, op.col_begin);
|
||||
let t = self.rec_get_var_t(
|
||||
let t = self.rec_get_var_info(
|
||||
&Identifier::new(None, VarName::new(symbol.clone())),
|
||||
AccessKind::Name,
|
||||
input,
|
||||
|
@ -690,10 +722,11 @@ impl Context {
|
|||
let op = hir::Expr::Accessor(hir::Accessor::private(symbol, t));
|
||||
self.get_call_t(&op, &None, args, &[], input, namespace)
|
||||
.map_err(|errs| {
|
||||
let op = enum_unwrap!(op, hir::Expr::Accessor:(hir::Accessor::Ident:(_)));
|
||||
let op_ident = enum_unwrap!(op, hir::Expr::Accessor:(hir::Accessor::Ident:(_)));
|
||||
let vi = op_ident.vi.clone();
|
||||
let lhs = args[0].expr.clone();
|
||||
let rhs = args[1].expr.clone();
|
||||
let bin = hir::BinOp::new(op.name.into_token(), lhs, rhs, op.t);
|
||||
let bin = hir::BinOp::new(op_ident.name.into_token(), lhs, rhs, vi);
|
||||
TyCheckErrors::new(
|
||||
errs.into_iter()
|
||||
.map(|e| {
|
||||
|
@ -718,22 +751,23 @@ impl Context {
|
|||
args: &[hir::PosArg],
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> TyCheckResult<Type> {
|
||||
) -> TyCheckResult<VarInfo> {
|
||||
erg_common::debug_power_assert!(args.len() == 1);
|
||||
let cont = unaryop_to_dname(op.inspect());
|
||||
let symbol = Token::new(op.kind, Str::rc(cont), op.lineno, op.col_begin);
|
||||
let t = self.rec_get_var_t(
|
||||
let vi = self.rec_get_var_info(
|
||||
&Identifier::new(None, VarName::new(symbol.clone())),
|
||||
AccessKind::Name,
|
||||
input,
|
||||
namespace,
|
||||
)?;
|
||||
let op = hir::Expr::Accessor(hir::Accessor::private(symbol, t));
|
||||
let op = hir::Expr::Accessor(hir::Accessor::private(symbol, vi));
|
||||
self.get_call_t(&op, &None, args, &[], input, namespace)
|
||||
.map_err(|errs| {
|
||||
let op = enum_unwrap!(op, hir::Expr::Accessor:(hir::Accessor::Ident:(_)));
|
||||
let op_ident = enum_unwrap!(op, hir::Expr::Accessor:(hir::Accessor::Ident:(_)));
|
||||
let vi = op_ident.vi.clone();
|
||||
let expr = args[0].expr.clone();
|
||||
let unary = hir::UnaryOp::new(op.name.into_token(), expr, op.t);
|
||||
let unary = hir::UnaryOp::new(op_ident.name.into_token(), expr, vi);
|
||||
TyCheckErrors::new(
|
||||
errs.into_iter()
|
||||
.map(|e| {
|
||||
|
@ -819,7 +853,6 @@ impl Context {
|
|||
let attr = hir::Attribute::new(
|
||||
obj.clone(),
|
||||
hir::Identifier::bare(ident.dot.clone(), ident.name.clone()),
|
||||
Type::Uninited,
|
||||
);
|
||||
hir::Expr::Accessor(hir::Accessor::Attr(attr))
|
||||
}
|
||||
|
@ -1097,7 +1130,7 @@ impl Context {
|
|||
kw_args: &[hir::KwArg],
|
||||
input: &Input,
|
||||
namespace: &Str,
|
||||
) -> TyCheckResult<Type> {
|
||||
) -> TyCheckResult<VarInfo> {
|
||||
if let hir::Expr::Accessor(hir::Accessor::Ident(local)) = obj {
|
||||
if local.vis().is_private() {
|
||||
match &local.inspect()[..] {
|
||||
|
@ -1122,12 +1155,12 @@ impl Context {
|
|||
}
|
||||
}
|
||||
}
|
||||
let found = self.search_callee_t(obj, attr_name, input, namespace)?;
|
||||
let found = self.search_callee_info(obj, attr_name, input, namespace)?;
|
||||
log!(
|
||||
"Found:\ncallee: {obj}{}\nfound: {found}",
|
||||
fmt_option!(pre ".", attr_name.as_ref().map(|ident| &ident.name))
|
||||
);
|
||||
let instance = self.instantiate(found, obj)?;
|
||||
let instance = self.instantiate(found.t, obj)?;
|
||||
log!(
|
||||
"Instantiated:\ninstance: {instance}\npos_args: ({})\nkw_args: ({})",
|
||||
fmt_slice(pos_args),
|
||||
|
@ -1139,6 +1172,7 @@ impl Context {
|
|||
log!(info "Params evaluated:\nres: {res}\n");
|
||||
self.propagate(&res, obj)?;
|
||||
log!(info "Propagated:\nres: {res}\n");
|
||||
let res = VarInfo { t: res, ..found };
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
|
@ -1679,8 +1713,14 @@ impl Context {
|
|||
let path =
|
||||
option_enum_unwrap!(params.remove(0), TyParam::Value:(ValueObj::Str:(_)))?;
|
||||
let path = Path::new(&path[..]);
|
||||
// TODO: erg std
|
||||
let path = if let Ok(path) = self.cfg.input.resolve(path) {
|
||||
path
|
||||
} else if let Ok(path) = erg_pystd_path()
|
||||
.join(format!("{}.d.er", path.display()))
|
||||
.canonicalize()
|
||||
{
|
||||
path
|
||||
} else {
|
||||
PathBuf::from(format!("<builtins>.{}", path.display()))
|
||||
};
|
||||
|
@ -1805,7 +1845,7 @@ impl Context {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_method_type_by_name(&self, name: &Identifier) -> SingleTyCheckResult<&MethodType> {
|
||||
fn get_method_type_by_name(&self, name: &Identifier) -> SingleTyCheckResult<&MethodInfo> {
|
||||
// TODO: min_by
|
||||
if let Some(candidates) = self.method_to_traits.get(name.inspect()) {
|
||||
let first_method_type = &candidates.first().unwrap().method_type;
|
||||
|
|
|
@ -511,9 +511,9 @@ impl Context {
|
|||
) -> TyCheckResult<Type> {
|
||||
// -> Result<Type, (Type, TyCheckErrors)> {
|
||||
let opt_decl_sig_t = self
|
||||
.rec_get_decl_t(&sig.ident, AccessKind::Name, &self.cfg.input, &self.name)
|
||||
.rec_get_decl_info(&sig.ident, AccessKind::Name, &self.cfg.input, &self.name)
|
||||
.ok()
|
||||
.map(|t| enum_unwrap!(t, Type::Subr));
|
||||
.map(|vi| enum_unwrap!(vi.t, Type::Subr));
|
||||
let bounds = self.instantiate_ty_bounds(&sig.bounds, PreRegister)?;
|
||||
let tv_ctx = TyVarInstContext::new(self.level, bounds, self);
|
||||
let mut non_defaults = vec![];
|
||||
|
|
|
@ -299,23 +299,23 @@ pub struct ContextInfo {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MethodType {
|
||||
pub struct MethodInfo {
|
||||
definition_type: Type,
|
||||
method_type: Type,
|
||||
method_type: VarInfo,
|
||||
}
|
||||
|
||||
impl fmt::Display for MethodType {
|
||||
impl fmt::Display for MethodInfo {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{{ def: {} t: {} }}",
|
||||
"{{ def: {} info: {} }}",
|
||||
self.definition_type, self.method_type
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl MethodType {
|
||||
pub const fn new(definition_type: Type, method_type: Type) -> Self {
|
||||
impl MethodInfo {
|
||||
pub const fn new(definition_type: Type, method_type: VarInfo) -> Self {
|
||||
Self {
|
||||
definition_type,
|
||||
method_type,
|
||||
|
@ -345,8 +345,8 @@ pub struct Context {
|
|||
pub(crate) methods_list: Vec<(ClassDefType, Context)>,
|
||||
// K: method name, V: types defines the method
|
||||
// If it is declared in a trait, it takes precedence over the class.
|
||||
pub(crate) method_to_traits: Dict<Str, Vec<MethodType>>,
|
||||
pub(crate) method_to_classes: Dict<Str, Vec<MethodType>>,
|
||||
pub(crate) method_to_traits: Dict<Str, Vec<MethodInfo>>,
|
||||
pub(crate) method_to_classes: Dict<Str, Vec<MethodInfo>>,
|
||||
/// K: method name, V: impl patch
|
||||
/// Provided methods can switch implementations on a scope-by-scope basis
|
||||
/// K: メソッド名, V: それを実装するパッチたち
|
||||
|
@ -462,13 +462,13 @@ impl Context {
|
|||
let idx = ParamIdx::Nth(idx);
|
||||
let kind = VarKind::parameter(id, idx, param.default_info);
|
||||
let muty = Mutability::from(name);
|
||||
let vi = VarInfo::new(param.t, muty, Private, kind, None, None);
|
||||
let vi = VarInfo::new(param.t, muty, Private, kind, None, None, None);
|
||||
params_.push((Some(VarName::new(Token::static_symbol(name))), vi));
|
||||
} else {
|
||||
let idx = ParamIdx::Nth(idx);
|
||||
let kind = VarKind::parameter(id, idx, param.default_info);
|
||||
let muty = Mutability::Immutable;
|
||||
let vi = VarInfo::new(param.t, muty, Private, kind, None, None);
|
||||
let vi = VarInfo::new(param.t, muty, Private, kind, None, None, None);
|
||||
params_.push((None, vi));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ use std::path::PathBuf;
|
|||
|
||||
use crate::ty::free::HasLevel;
|
||||
use erg_common::config::{ErgConfig, Input};
|
||||
use erg_common::env::erg_pystd_path;
|
||||
use erg_common::levenshtein::get_similar_name;
|
||||
use erg_common::python_util::BUILTIN_PYTHON_MODS;
|
||||
use erg_common::set::Set;
|
||||
|
@ -21,7 +22,7 @@ use crate::ty::{HasType, ParamTy, SubrType, Type};
|
|||
|
||||
use crate::build_hir::HIRBuilder;
|
||||
use crate::context::{
|
||||
ClassDefType, Context, ContextKind, DefaultInfo, MethodType, RegistrationMode, TraitInstance,
|
||||
ClassDefType, Context, ContextKind, DefaultInfo, MethodInfo, RegistrationMode, TraitInstance,
|
||||
};
|
||||
use crate::error::readable_name;
|
||||
use crate::error::{
|
||||
|
@ -86,7 +87,7 @@ impl Context {
|
|||
} else {
|
||||
self.decls.insert(
|
||||
ident.name.clone(),
|
||||
VarInfo::new(sig_t, muty, vis, kind, None, self.impl_of()),
|
||||
VarInfo::new(sig_t, muty, vis, kind, None, self.impl_of(), None),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
@ -122,11 +123,20 @@ impl Context {
|
|||
kind.clone(),
|
||||
Some(comptime_decos.clone()),
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
self.decls.insert(sig.ident.name.clone(), vi);
|
||||
e
|
||||
})?;
|
||||
let vi = VarInfo::new(t, muty, vis, kind, Some(comptime_decos), self.impl_of());
|
||||
let vi = VarInfo::new(
|
||||
t,
|
||||
muty,
|
||||
vis,
|
||||
kind,
|
||||
Some(comptime_decos),
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
if let Some(_decl) = self.decls.remove(name) {
|
||||
Err(TyCheckErrors::from(TyCheckError::duplicate_decl_error(
|
||||
self.cfg.input.clone(),
|
||||
|
@ -146,6 +156,7 @@ impl Context {
|
|||
sig: &ast::VarSignature,
|
||||
body_t: &Type,
|
||||
id: DefId,
|
||||
py_name: Option<Str>,
|
||||
) -> TyCheckResult<()> {
|
||||
let ident = match &sig.pat {
|
||||
ast::VarPattern::Ident(ident) => ident,
|
||||
|
@ -169,6 +180,7 @@ impl Context {
|
|||
VarKind::Defined(id),
|
||||
None,
|
||||
self.impl_of(),
|
||||
py_name,
|
||||
);
|
||||
log!(info "Registered {}::{}: {} {:?}", self.name, ident.name, vi.t, vi.impl_of);
|
||||
self.locals.insert(ident.name.clone(), vi);
|
||||
|
@ -220,7 +232,7 @@ impl Context {
|
|||
let muty = Mutability::from(&name.inspect()[..]);
|
||||
self.params.push((
|
||||
Some(name.clone()),
|
||||
VarInfo::new(spec_t, muty, Private, kind, None, None),
|
||||
VarInfo::new(spec_t, muty, Private, kind, None, None, None),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
@ -259,7 +271,7 @@ impl Context {
|
|||
VarKind::parameter(DefId(get_hash(&(&self.name, name))), idx, default);
|
||||
self.params.push((
|
||||
Some(name.clone()),
|
||||
VarInfo::new(spec_t, Immutable, Private, kind, None, None),
|
||||
VarInfo::new(spec_t, Immutable, Private, kind, None, None, None),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
@ -298,7 +310,7 @@ impl Context {
|
|||
VarKind::parameter(DefId(get_hash(&(&self.name, name))), idx, default);
|
||||
self.params.push((
|
||||
Some(name.clone()),
|
||||
VarInfo::new(spec_t, Immutable, Private, kind, None, None),
|
||||
VarInfo::new(spec_t, Immutable, Private, kind, None, None, None),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
@ -461,6 +473,7 @@ impl Context {
|
|||
VarKind::Defined(id),
|
||||
Some(comptime_decos),
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
let t = vi.t.clone();
|
||||
log!(info "Registered {}::{name}: {t}", self.name);
|
||||
|
@ -498,6 +511,7 @@ impl Context {
|
|||
VarKind::DoesNotExist,
|
||||
Some(comptime_decos),
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
log!(info "Registered {}::{name}: {}", self.name, &vi.t);
|
||||
self.locals.insert(name.clone(), vi);
|
||||
|
@ -582,6 +596,7 @@ impl Context {
|
|||
t: Type,
|
||||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
py_name: Option<Str>,
|
||||
) {
|
||||
let name = VarName::from_static(name);
|
||||
if self.locals.get(&name).is_some() {
|
||||
|
@ -589,7 +604,7 @@ impl Context {
|
|||
} else {
|
||||
self.locals.insert(
|
||||
name,
|
||||
VarInfo::new(t, muty, vis, VarKind::Auto, None, self.impl_of()),
|
||||
VarInfo::new(t, muty, vis, VarKind::Auto, None, self.impl_of(), py_name),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -601,6 +616,7 @@ impl Context {
|
|||
t: Type,
|
||||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
py_name: Option<Str>,
|
||||
) {
|
||||
let name = VarName::from_static(name);
|
||||
if self.locals.get(&name).is_some() {
|
||||
|
@ -608,7 +624,15 @@ impl Context {
|
|||
} else {
|
||||
self.locals.insert(
|
||||
name,
|
||||
VarInfo::new(t, muty, vis, VarKind::FixedAuto, None, self.impl_of()),
|
||||
VarInfo::new(
|
||||
t,
|
||||
muty,
|
||||
vis,
|
||||
VarKind::FixedAuto,
|
||||
None,
|
||||
self.impl_of(),
|
||||
py_name,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -619,13 +643,14 @@ impl Context {
|
|||
t: Type,
|
||||
vis: Visibility,
|
||||
impl_of: Option<Type>,
|
||||
py_name: Option<Str>,
|
||||
) {
|
||||
if self.decls.get(&name).is_some() {
|
||||
panic!("already registered: {name}");
|
||||
} else {
|
||||
self.decls.insert(
|
||||
name,
|
||||
VarInfo::new(t, Immutable, vis, VarKind::Declared, None, impl_of),
|
||||
VarInfo::new(t, Immutable, vis, VarKind::Declared, None, impl_of, py_name),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -637,6 +662,7 @@ impl Context {
|
|||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
impl_of: Option<Type>,
|
||||
py_name: Option<Str>,
|
||||
) {
|
||||
if self.locals.get(&name).is_some() {
|
||||
panic!("already registered: {name}");
|
||||
|
@ -644,7 +670,7 @@ impl Context {
|
|||
let id = DefId(get_hash(&(&self.name, &name)));
|
||||
self.locals.insert(
|
||||
name,
|
||||
VarInfo::new(t, muty, vis, VarKind::Defined(id), None, impl_of),
|
||||
VarInfo::new(t, muty, vis, VarKind::Defined(id), None, impl_of, py_name),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -697,6 +723,7 @@ impl Context {
|
|||
VarKind::Defined(id),
|
||||
None,
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
self.decls.insert(ident.name.clone(), vi);
|
||||
self.consts.insert(ident.name.clone(), other);
|
||||
|
@ -729,9 +756,15 @@ impl Context {
|
|||
);
|
||||
let require = gen.require_or_sup.typ().clone();
|
||||
let new_t = func1(require, gen.t.clone());
|
||||
methods.register_fixed_auto_impl("__new__", new_t.clone(), Immutable, Private);
|
||||
methods.register_fixed_auto_impl(
|
||||
"__new__",
|
||||
new_t.clone(),
|
||||
Immutable,
|
||||
Private,
|
||||
Some("__call__".into()),
|
||||
);
|
||||
// 必要なら、ユーザーが独自に上書きする
|
||||
methods.register_auto_impl("new", new_t, Immutable, Public);
|
||||
methods.register_auto_impl("new", new_t, Immutable, Public, None);
|
||||
ctx.methods_list
|
||||
.push((ClassDefType::Simple(gen.t.clone()), methods));
|
||||
self.register_gen_mono_type(ident, gen, ctx, Const);
|
||||
|
@ -786,9 +819,10 @@ impl Context {
|
|||
new_t.clone(),
|
||||
Immutable,
|
||||
Private,
|
||||
Some("__call__".into()),
|
||||
);
|
||||
// 必要なら、ユーザーが独自に上書きする
|
||||
methods.register_auto_impl("new", new_t, Immutable, Public);
|
||||
methods.register_auto_impl("new", new_t, Immutable, Public, None);
|
||||
ctx.methods_list
|
||||
.push((ClassDefType::Simple(gen.t.clone()), methods));
|
||||
self.register_gen_mono_type(ident, gen, ctx, Const);
|
||||
|
@ -823,6 +857,7 @@ impl Context {
|
|||
VarKind::Declared,
|
||||
None,
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
ctx.decls
|
||||
.insert(VarName::from_str(field.symbol.clone()), vi);
|
||||
|
@ -859,6 +894,7 @@ impl Context {
|
|||
VarKind::Declared,
|
||||
None,
|
||||
self.impl_of(),
|
||||
None,
|
||||
);
|
||||
ctx.decls
|
||||
.insert(VarName::from_str(field.symbol.clone()), vi);
|
||||
|
@ -895,6 +931,7 @@ impl Context {
|
|||
VarKind::Defined(id),
|
||||
None,
|
||||
self.impl_of(),
|
||||
None,
|
||||
),
|
||||
);
|
||||
self.consts
|
||||
|
@ -929,6 +966,7 @@ impl Context {
|
|||
VarKind::Defined(id),
|
||||
None,
|
||||
self.impl_of(),
|
||||
None,
|
||||
),
|
||||
);
|
||||
self.consts
|
||||
|
@ -945,21 +983,21 @@ impl Context {
|
|||
}
|
||||
for (trait_method, vi) in ctx.decls.iter() {
|
||||
if let Some(types) = self.method_to_traits.get_mut(trait_method.inspect()) {
|
||||
types.push(MethodType::new(t.clone(), vi.t.clone()));
|
||||
types.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_traits.insert(
|
||||
trait_method.inspect().clone(),
|
||||
vec![MethodType::new(t.clone(), vi.t.clone())],
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
for (class_method, vi) in ctx.locals.iter() {
|
||||
if let Some(types) = self.method_to_classes.get_mut(class_method.inspect()) {
|
||||
types.push(MethodType::new(t.clone(), vi.t.clone()));
|
||||
types.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_classes.insert(
|
||||
class_method.inspect().clone(),
|
||||
vec![MethodType::new(t.clone(), vi.t.clone())],
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -973,24 +1011,24 @@ impl Context {
|
|||
mod_name: &Literal,
|
||||
) -> CompileResult<PathBuf> {
|
||||
if kind.is_erg_import() {
|
||||
self.import_erg_mod(mod_name)
|
||||
self.import_erg_mod_using_cache(mod_name)
|
||||
} else {
|
||||
self.import_py_mod(mod_name)
|
||||
self.import_py_mod_using_cache(mod_name)
|
||||
}
|
||||
}
|
||||
|
||||
fn import_erg_mod(&mut self, mod_name: &Literal) -> CompileResult<PathBuf> {
|
||||
fn import_erg_mod_using_cache(&mut self, mod_name: &Literal) -> CompileResult<PathBuf> {
|
||||
let __name__ = enum_unwrap!(mod_name.value.clone(), ValueObj::Str);
|
||||
let mod_cache = self.mod_cache.as_ref().unwrap();
|
||||
let py_mod_cache = self.py_mod_cache.as_ref().unwrap();
|
||||
#[allow(clippy::match_single_binding)]
|
||||
match &__name__[..] {
|
||||
// TODO: erg builtin modules
|
||||
_ => self.import_user_erg_mod(__name__, mod_name, mod_cache, py_mod_cache),
|
||||
_ => self.import_erg_mod(__name__, mod_name, mod_cache, py_mod_cache),
|
||||
}
|
||||
}
|
||||
|
||||
fn import_user_erg_mod(
|
||||
fn import_erg_mod(
|
||||
&self,
|
||||
__name__: Str,
|
||||
mod_name: &Literal,
|
||||
|
@ -1045,13 +1083,14 @@ impl Context {
|
|||
Ok(path)
|
||||
}
|
||||
|
||||
fn import_py_mod(&mut self, mod_name: &Literal) -> CompileResult<PathBuf> {
|
||||
fn import_py_mod_using_cache(&mut self, mod_name: &Literal) -> CompileResult<PathBuf> {
|
||||
let __name__ = enum_unwrap!(mod_name.value.clone(), ValueObj::Str);
|
||||
let py_mod_cache = self.py_mod_cache.as_ref().unwrap();
|
||||
let builtin_path = PathBuf::from(&format!("<builtins>.{__name__}"));
|
||||
if py_mod_cache.get(&builtin_path).is_some() {
|
||||
return Ok(builtin_path);
|
||||
}
|
||||
// TODO: rewrite all as `d.er`
|
||||
match &__name__[..] {
|
||||
"glob" => {
|
||||
py_mod_cache.register(builtin_path.clone(), None, Self::init_py_glob_mod());
|
||||
|
@ -1097,7 +1136,7 @@ impl Context {
|
|||
py_mod_cache.register(builtin_path.clone(), None, Self::init_py_urllib_mod());
|
||||
Ok(builtin_path)
|
||||
}
|
||||
_ => self.import_user_py_mod(mod_name),
|
||||
_ => self.import_py_mod(mod_name),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1105,8 +1144,15 @@ impl Context {
|
|||
get_similar_name(BUILTIN_PYTHON_MODS.into_iter(), name).map(Str::rc)
|
||||
}
|
||||
|
||||
fn import_user_py_mod(&self, mod_name: &Literal) -> CompileResult<PathBuf> {
|
||||
fn find_decl_in_pystd(__name__: &str) -> std::io::Result<PathBuf> {
|
||||
let mut as_std_path = erg_pystd_path().join(__name__);
|
||||
as_std_path.set_extension("d.er");
|
||||
as_std_path.canonicalize()
|
||||
}
|
||||
|
||||
fn import_py_mod(&self, mod_name: &Literal) -> CompileResult<PathBuf> {
|
||||
let __name__ = enum_unwrap!(mod_name.value.clone(), ValueObj::Str);
|
||||
let mod_cache = self.mod_cache.as_ref().unwrap();
|
||||
let py_mod_cache = self.py_mod_cache.as_ref().unwrap();
|
||||
let mut dir = if let Input::File(mut path) = self.cfg.input.clone() {
|
||||
path.pop();
|
||||
|
@ -1115,7 +1161,10 @@ impl Context {
|
|||
PathBuf::new()
|
||||
};
|
||||
dir.push(format!("{__name__}.d.er"));
|
||||
let path = match dir.canonicalize() {
|
||||
let path = match dir
|
||||
.canonicalize()
|
||||
.or_else(|_| Self::find_decl_in_pystd(&__name__))
|
||||
{
|
||||
Ok(path) => path,
|
||||
Err(err) => {
|
||||
let err = TyCheckError::import_error(
|
||||
|
@ -1137,10 +1186,11 @@ impl Context {
|
|||
let cfg = ErgConfig::with_module_path(path.clone());
|
||||
let src = cfg.input.read();
|
||||
let mut builder =
|
||||
HIRBuilder::new_with_cache(cfg, __name__, py_mod_cache.clone(), py_mod_cache.clone());
|
||||
HIRBuilder::new_with_cache(cfg, __name__, mod_cache.clone(), py_mod_cache.clone());
|
||||
match builder.build(src, "declare") {
|
||||
Ok(hir) => {
|
||||
py_mod_cache.register(path.clone(), Some(hir), builder.pop_mod_ctx());
|
||||
let ctx = builder.pop_mod_ctx();
|
||||
py_mod_cache.register(path.clone(), Some(hir), ctx);
|
||||
}
|
||||
Err((maybe_hir, errs)) => {
|
||||
if let Some(hir) = maybe_hir {
|
||||
|
|
|
@ -731,21 +731,8 @@ impl Context {
|
|||
let loc = acc.loc();
|
||||
let t = acc.ref_mut_t();
|
||||
*t = self.deref_tyvar(mem::take(t), Covariant, loc)?;
|
||||
match acc {
|
||||
hir::Accessor::Attr(attr) => {
|
||||
self.resolve_expr_t(&mut attr.obj)?;
|
||||
}
|
||||
hir::Accessor::TupleAttr(attr) => {
|
||||
self.resolve_expr_t(&mut attr.obj)?;
|
||||
}
|
||||
hir::Accessor::Subscr(subscr) => {
|
||||
self.resolve_expr_t(&mut subscr.obj)?;
|
||||
self.resolve_expr_t(&mut subscr.index)?;
|
||||
}
|
||||
hir::Accessor::Ident(ident) => {
|
||||
ident.t =
|
||||
self.deref_tyvar(mem::take(&mut ident.t), Covariant, ident.loc())?;
|
||||
}
|
||||
if let hir::Accessor::Attr(attr) = acc {
|
||||
self.resolve_expr_t(&mut attr.obj)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -839,8 +826,9 @@ impl Context {
|
|||
}
|
||||
hir::Expr::Call(call) => {
|
||||
let loc = call.loc();
|
||||
let t = call.signature_mut_t().unwrap();
|
||||
*t = self.deref_tyvar(mem::take(t), Covariant, loc)?;
|
||||
if let Some(t) = call.signature_mut_t() {
|
||||
*t = self.deref_tyvar(mem::take(t), Covariant, loc)?;
|
||||
}
|
||||
self.resolve_expr_t(&mut call.obj)?;
|
||||
for arg in call.args.pos_args.iter_mut() {
|
||||
self.resolve_expr_t(&mut arg.expr)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue