chore: add Subroutine & GenericCallable is now a trait

This commit is contained in:
Shunsuke Shibayama 2024-02-07 13:23:09 +09:00
parent 53172d5132
commit 58514c5890
7 changed files with 47 additions and 19 deletions

View file

@ -191,7 +191,7 @@ impl Context {
(Absolutely, true)
}
(Mono(l), Mono(r))
if &l[..] == "GenericCallable"
if &l[..] == "Subroutine"
&& (&r[..] == "GenericFunc"
|| &r[..] == "GenericProc"
|| &r[..] == "GenericFuncMethod"
@ -210,7 +210,7 @@ impl Context {
Some((Type::Never, Type::Obj)) => (Absolutely, true),
_ => (Maybe, false),
},
(Mono(n), Subr(_) | Quantified(_)) if &n[..] == "GenericCallable" => (Absolutely, true),
(Mono(n), Subr(_) | Quantified(_)) if &n[..] == "Subroutine" => (Absolutely, true),
(lhs, rhs) if lhs.is_mono_value_class() && rhs.is_mono_value_class() => {
(Absolutely, false)
}

View file

@ -3183,17 +3183,17 @@ impl Context {
None,
get_item,
);
let mut g_callable = Self::builtin_mono_class(GENERIC_CALLABLE, 2);
g_callable.register_superclass(Obj, &obj);
let t_return = fn1_met(mono(GENERIC_CALLABLE), Obj, Never).quantify();
g_callable.register_builtin_erg_impl(
let mut subr = Self::builtin_mono_class(SUBROUTINE, 2);
subr.register_superclass(Obj, &obj);
let t_return = fn1_met(mono(SUBROUTINE), Obj, Never).quantify();
subr.register_builtin_erg_impl(
FUNC_RETURN,
t_return,
Immutable,
Visibility::BUILTIN_PRIVATE,
);
let mut g_generator = Self::builtin_mono_class(GENERIC_GENERATOR, 2);
g_generator.register_superclass(mono(GENERIC_CALLABLE), &g_callable);
g_generator.register_superclass(mono(SUBROUTINE), &subr);
let t_yield = fn1_met(mono(GENERIC_GENERATOR), Obj, Never).quantify();
g_generator.register_builtin_erg_impl(
FUNC_YIELD,
@ -3203,7 +3203,7 @@ impl Context {
);
/* Proc */
let mut proc = Self::builtin_mono_class(PROC, 2);
proc.register_superclass(mono(GENERIC_CALLABLE), &g_callable);
proc.register_superclass(mono(SUBROUTINE), &subr);
let mut named_proc = Self::builtin_mono_class(NAMED_PROC, 2);
named_proc.register_superclass(mono(PROC), &proc);
named_proc.register_marker_trait(self, mono(NAMED)).unwrap();
@ -3433,13 +3433,7 @@ impl Context {
);
self.register_builtin_type(dict_mut_t, dict_mut, vis.clone(), Const, Some(DICT));
self.register_builtin_type(set_mut_t, set_mut_, vis.clone(), Const, Some(SET));
self.register_builtin_type(
mono(GENERIC_CALLABLE),
g_callable,
vis.clone(),
Const,
Some(CALLABLE),
);
self.register_builtin_type(mono(SUBROUTINE), subr, vis.clone(), Const, Some(SUBROUTINE));
self.register_builtin_type(
mono(GENERIC_GENERATOR),
g_generator,

View file

@ -309,6 +309,7 @@ const FUNC_INVERT: &str = "invert";
const PROC_INVERT: &str = "invert!";
const RANGE: &str = "Range";
const GENERIC_CALLABLE: &str = "GenericCallable";
const SUBROUTINE: &str = "Subroutine";
const GENERIC_GENERATOR: &str = "GenericGenerator";
const FUNC_RETURN: &str = "return";
const FUNC_YIELD: &str = "yield";

View file

@ -356,6 +356,18 @@ impl Context {
Visibility::BUILTIN_PUBLIC,
Some(FUNDAMENTAL_EXIT),
);
let mut g_callable = Self::builtin_mono_trait(GENERIC_CALLABLE, 1);
g_callable.register_builtin_erg_decl(
FUNDAMENTAL_CALL,
func(
vec![pos(mono(GENERIC_CALLABLE))],
Some(pos(Obj)),
vec![],
Some(pos(Obj)),
Obj,
),
Visibility::BUILTIN_PUBLIC,
);
/* HasShape */
let S = mono_q_tp(TY_S, instanceof(unknown_len_array_t(Nat)));
let params = vec![PS::named_nd("S", unknown_len_array_t(Nat))];
@ -551,6 +563,13 @@ impl Context {
Const,
None,
);
self.register_builtin_type(
mono(GENERIC_CALLABLE),
g_callable,
vis.clone(),
Const,
Some(CALLABLE),
);
self.register_builtin_type(
poly(HAS_SHAPE, vec![S]),
has_shape,

View file

@ -1529,7 +1529,7 @@ impl<'c, 'l, 'u, L: Locational> Unifier<'c, 'l, 'u, L> {
) => {
self.nominal_sub_unify(maybe_sub, maybe_sup, sup_params)?;
}
(Subr(_), Mono(name)) if &name[..] == "GenericCallable" => {}
(Subr(_), Mono(name)) if &name[..] == "Subroutine" => {}
_ => {
return type_feature_error!(
self.ctx,