Add Show trait

This commit is contained in:
Shunsuke Shibayama 2022-10-03 01:02:49 +09:00
parent 4f6fbb50d0
commit e55ab8c027
4 changed files with 32 additions and 20 deletions

View file

@ -221,6 +221,7 @@ fn convert_to_python_attr(class: &str, uniq_obj_name: Option<&str>, name: Str) -
("Complex" | "Float" | "Ratio" | "Int" | "Nat" | "Bool", _, "Imag") => Str::ever("imag"),
("File!", _, "read!") => Str::ever("read"),
(_, _, "__new__") => Str::ever("__call__"),
(_, _, "to_str") => Str::ever("__str__"),
("StringIO!", _, "getvalue!") => Str::ever("getvalue"),
("Module", Some("importlib"), "reload!") => Str::ever("reload"),
("Module", Some("random"), "randint!") => Str::ever("randint"),

View file

@ -238,7 +238,7 @@ impl Context {
);
readable.register_builtin_decl("read!", t_read, Public);
let mut show = Self::builtin_mono_trait("Show", 2);
let t_show = pr0_met(ref_(mono_q("Self")), Str);
let t_show = fn0_met(ref_(mono_q("Self")), Str);
let t_show = quant(
t_show,
set! { subtypeof(mono_q("Self"), builtin_mono("Show")) },
@ -485,6 +485,10 @@ impl Context {
float_mutizable
.register_builtin_const("MutType!", ValueObj::builtin_t(builtin_mono("Float!")));
float.register_trait(Float, builtin_mono("Mutizable"), float_mutizable);
let mut float_show = Self::builtin_methods("Show", 1);
let t = fn0_met(Float, Str);
float_show.register_builtin_impl("to_str", t, Immutable, Public);
float.register_trait(Float, builtin_mono("Show"), float_show);
// TODO: Int, Nat, Boolの継承元をRatioにする(今はFloat)
let mut ratio = Self::builtin_mono_class("Ratio", 2);
ratio.register_superclass(Obj, &obj);
@ -530,6 +534,10 @@ impl Context {
ratio_mutizable
.register_builtin_const("MutType!", ValueObj::builtin_t(builtin_mono("Ratio!")));
ratio.register_trait(Ratio, builtin_mono("Mutizable"), ratio_mutizable);
let mut ratio_show = Self::builtin_methods("Show", 1);
let t = fn0_met(Ratio, Str);
ratio_show.register_builtin_impl("to_str", t, Immutable, Public);
ratio.register_trait(Ratio, builtin_mono("Show"), ratio_show);
let mut int = Self::builtin_mono_class("Int", 2);
int.register_superclass(Float, &float); // TODO: Float -> Ratio
int.register_superclass(Obj, &obj);
@ -572,6 +580,10 @@ impl Context {
let mut int_mutizable = Self::builtin_methods("Mutizable", 2);
int_mutizable.register_builtin_const("MutType!", ValueObj::builtin_t(builtin_mono("Int!")));
int.register_trait(Int, builtin_mono("Mutizable"), int_mutizable);
let mut int_show = Self::builtin_methods("Show", 1);
let t = fn0_met(Int, Str);
int_show.register_builtin_impl("to_str", t, Immutable, Public);
int.register_trait(Int, builtin_mono("Show"), int_show);
int.register_builtin_impl("Real", Int, Const, Public);
int.register_builtin_impl("Imag", Int, Const, Public);
let mut nat = Self::builtin_mono_class("Nat", 10);

View file

@ -1242,13 +1242,21 @@ impl Context {
}))
}
/*pub(crate) fn get_nominal_super_type_ctxs<'a>(
pub(crate) fn get_nominal_super_type_ctxs<'a>(
&'a self,
t: &Type,
) -> Option<Vec<(&'a Type, &'a Context)>> {
match t {
Type::FreeVar(fv) if fv.is_linked() => self.get_nominal_super_type_ctxs(&fv.crack()),
Type::FreeVar(fv) => {
let sup = fv.get_sup().unwrap();
self.get_nominal_super_type_ctxs(&sup)
}
Type::And(l, r) => {
match (self.get_nominal_super_type_ctxs(l), self.get_nominal_super_type_ctxs(r)) {
match (
self.get_nominal_super_type_ctxs(l),
self.get_nominal_super_type_ctxs(r),
) {
// TODO: sort
(Some(l), Some(r)) => Some([l, r].concat()),
(Some(l), None) => Some(l),
@ -1258,7 +1266,9 @@ impl Context {
}
// TODO
Type::Or(_l, _r) => None,
_ => self.get_simple_nominal_super_type_ctxs(t).map(|ctxs| ctxs.collect()),
_ => self
.get_simple_nominal_super_type_ctxs(t)
.map(|ctxs| ctxs.collect()),
}
}
@ -1273,19 +1283,6 @@ impl Context {
.chain(ctx.super_traits.iter())
.map(|sup| self.get_nominal_type_ctx(sup).unwrap());
Some(vec![(t, ctx)].into_iter().chain(sups))
}*/
pub(crate) fn get_nominal_super_type_ctxs<'a>(
&'a self,
t: &Type,
) -> Option<impl Iterator<Item = (&'a Type, &'a Context)>> {
let (t, ctx) = self.get_nominal_type_ctx(t)?;
let sups = ctx
.super_classes
.iter()
.chain(ctx.super_traits.iter())
.map(|sup| self.get_nominal_type_ctx(sup).unwrap());
Some(vec![(t, ctx)].into_iter().chain(sups))
}
// TODO: Never

View file

@ -3,10 +3,12 @@ assert id(1) == 1
assert id(True) == True
assert id("hello") == "hello"
const|T: Type, C: Type|(c: C): (T -> C) = (_: T,) -> c
assert const(1)(2) == 1
assert const(True)(2) == True
# const|T: Type, C: Type|(c: C): (T -> C) = (x: T,) -> c
# assert const(1)(2) == 1
# assert const(True)(2) == True
print_to_str!|S <: Show|(s: S): Str =
print! s
s.to_str()
discard print_to_str!([1])