mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
Rename dir: compiler
-> crates
This commit is contained in:
parent
e1004b538d
commit
a127564b31
221 changed files with 17 additions and 19 deletions
1439
crates/erg_compiler/context/initialize/classes.rs
Normal file
1439
crates/erg_compiler/context/initialize/classes.rs
Normal file
File diff suppressed because it is too large
Load diff
300
crates/erg_compiler/context/initialize/const_func.rs
Normal file
300
crates/erg_compiler/context/initialize/const_func.rs
Normal file
|
@ -0,0 +1,300 @@
|
|||
use std::mem;
|
||||
|
||||
use erg_common::enum_unwrap;
|
||||
|
||||
use crate::context::Context;
|
||||
use crate::feature_error;
|
||||
use crate::ty::constructors::{and, mono};
|
||||
use crate::ty::value::{EvalValueError, EvalValueResult, GenTypeObj, TypeObj, ValueObj};
|
||||
use crate::ty::ValueArgs;
|
||||
use erg_common::error::{ErrorCore, ErrorKind, Location, SubMessage};
|
||||
use erg_common::style::{Color, StyledStr, StyledString, THEME};
|
||||
|
||||
const ERR: Color = THEME.colors.error;
|
||||
const WARN: Color = THEME.colors.warning;
|
||||
|
||||
const SUP_ERR: StyledStr = StyledStr::new("Super", Some(ERR), None);
|
||||
const SUP_WARN: StyledStr = StyledStr::new("Super", Some(WARN), None);
|
||||
const CLASS_ERR: StyledStr = StyledStr::new("Class", Some(ERR), None);
|
||||
const REQ_ERR: StyledStr = StyledStr::new("Requirement", Some(ERR), None);
|
||||
const REQ_WARN: StyledStr = StyledStr::new("Requirement", Some(WARN), None);
|
||||
const BASE_ERR: StyledStr = StyledStr::new("Base", Some(ERR), None);
|
||||
const BASE_WARN: StyledStr = StyledStr::new("Base", Some(WARN), None);
|
||||
|
||||
/// Base := Type or NoneType, Impl := Type -> ClassType
|
||||
pub fn class_func(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let base = args.remove_left_or_key("Base");
|
||||
let impls = args.remove_left_or_key("Impl");
|
||||
let impls = impls.map(|v| v.as_type().unwrap());
|
||||
let t = mono(ctx.name.clone());
|
||||
match base {
|
||||
Some(value) => {
|
||||
if let Some(base) = value.as_type() {
|
||||
Ok(ValueObj::gen_t(GenTypeObj::class(t, Some(base), impls)))
|
||||
} else {
|
||||
let base = StyledString::new(format!("{value}"), Some(ERR), None);
|
||||
Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("non-type object {base} is passed to {BASE_WARN}",),
|
||||
line!() as usize,
|
||||
ErrorKind::TypeError,
|
||||
Location::Unknown,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
None => Ok(ValueObj::gen_t(GenTypeObj::class(t, None, impls))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Super: ClassType, Impl := Type, Additional := Type -> ClassType
|
||||
pub fn inherit_func(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let sup = args.remove_left_or_key("Super").ok_or_else(|| {
|
||||
let sup = StyledStr::new("Super", Some(ERR), None);
|
||||
ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("{sup} is not passed"),
|
||||
line!() as usize,
|
||||
ErrorKind::KeyError,
|
||||
Location::Unknown,
|
||||
)
|
||||
})?;
|
||||
let Some(sup) = sup.as_type() else {
|
||||
let sup_ty = StyledString::new(format!("{sup}"), Some(ERR), None);
|
||||
return Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!(
|
||||
"non-class object {sup_ty} is passed to {SUP_WARN}",
|
||||
),
|
||||
line!() as usize,
|
||||
ErrorKind::TypeError,
|
||||
Location::Unknown,
|
||||
).into());
|
||||
};
|
||||
let impls = args.remove_left_or_key("Impl");
|
||||
let impls = impls.map(|v| v.as_type().unwrap());
|
||||
let additional = args.remove_left_or_key("Additional");
|
||||
let additional = additional.map(|v| v.as_type().unwrap());
|
||||
let t = mono(ctx.name.clone());
|
||||
Ok(ValueObj::gen_t(GenTypeObj::inherited(
|
||||
t, sup, impls, additional,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Class: ClassType -> ClassType (with `InheritableType`)
|
||||
/// This function is used by the compiler to mark a class as inheritable and does nothing in terms of actual operation.
|
||||
pub fn inheritable_func(mut args: ValueArgs, _ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let class = args.remove_left_or_key("Class").ok_or_else(|| {
|
||||
ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("{CLASS_ERR} is not passed"),
|
||||
line!() as usize,
|
||||
ErrorKind::KeyError,
|
||||
Location::Unknown,
|
||||
)
|
||||
})?;
|
||||
match class {
|
||||
ValueObj::Type(TypeObj::Generated(mut gen)) => {
|
||||
if let Some(typ) = gen.impls_mut() {
|
||||
match typ.as_mut().map(|x| x.as_mut()) {
|
||||
Some(TypeObj::Generated(gen)) => {
|
||||
*gen.typ_mut() = and(mem::take(gen.typ_mut()), mono("InheritableType"));
|
||||
}
|
||||
Some(TypeObj::Builtin(t)) => {
|
||||
*t = and(mem::take(t), mono("InheritableType"));
|
||||
}
|
||||
_ => {
|
||||
*typ = Some(Box::new(TypeObj::Builtin(mono("InheritableType"))));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(ValueObj::Type(TypeObj::Generated(gen)))
|
||||
}
|
||||
other => feature_error!(
|
||||
EvalValueError,
|
||||
_ctx,
|
||||
Location::Unknown,
|
||||
&format!("Inheritable {other}")
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Base: Type, Impl := Type -> TraitType
|
||||
pub fn trait_func(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let req = args.remove_left_or_key("Requirement").ok_or_else(|| {
|
||||
ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("{REQ_ERR} is not passed"),
|
||||
line!() as usize,
|
||||
ErrorKind::KeyError,
|
||||
Location::Unknown,
|
||||
)
|
||||
})?;
|
||||
let Some(req) = req.as_type() else {
|
||||
let req = StyledString::new(format!("{req}"), Some(ERR), None);
|
||||
return Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!(
|
||||
"non-type object {req} is passed to {REQ_WARN}",
|
||||
),
|
||||
line!() as usize,
|
||||
ErrorKind::TypeError,
|
||||
Location::Unknown,
|
||||
).into());
|
||||
};
|
||||
let impls = args.remove_left_or_key("Impl");
|
||||
let impls = impls.map(|v| v.as_type().unwrap());
|
||||
let t = mono(ctx.name.clone());
|
||||
Ok(ValueObj::gen_t(GenTypeObj::trait_(t, req, impls)))
|
||||
}
|
||||
|
||||
/// Base: Type, Impl := Type -> Patch
|
||||
pub fn patch_func(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let base = args.remove_left_or_key("Base").ok_or_else(|| {
|
||||
ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("{BASE_ERR} is not passed"),
|
||||
line!() as usize,
|
||||
ErrorKind::KeyError,
|
||||
Location::Unknown,
|
||||
)
|
||||
})?;
|
||||
let Some(base) = base.as_type() else {
|
||||
let base = StyledString::new(format!("{base}"), Some(ERR), None);
|
||||
return Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!(
|
||||
"non-type object {base} is passed to {BASE_WARN}",
|
||||
),
|
||||
line!() as usize,
|
||||
ErrorKind::TypeError,
|
||||
Location::Unknown,
|
||||
).into());
|
||||
};
|
||||
let impls = args.remove_left_or_key("Impl");
|
||||
let impls = impls.map(|v| v.as_type().unwrap());
|
||||
let t = mono(ctx.name.clone());
|
||||
Ok(ValueObj::gen_t(GenTypeObj::patch(t, base, impls)))
|
||||
}
|
||||
|
||||
/// Super: TraitType, Impl := Type, Additional := Type -> TraitType
|
||||
pub fn subsume_func(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let sup = args.remove_left_or_key("Super").ok_or_else(|| {
|
||||
ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("{SUP_ERR} is not passed"),
|
||||
line!() as usize,
|
||||
ErrorKind::KeyError,
|
||||
Location::Unknown,
|
||||
)
|
||||
})?;
|
||||
let Some(sup) = sup.as_type() else {
|
||||
let sup = StyledString::new(format!("{sup}"), Some(ERR), None);
|
||||
return Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!(
|
||||
"non-trait object {sup} is passed to {SUP_WARN}",
|
||||
),
|
||||
line!() as usize,
|
||||
ErrorKind::TypeError,
|
||||
Location::Unknown,
|
||||
).into());
|
||||
};
|
||||
let impls = args.remove_left_or_key("Impl");
|
||||
let impls = impls.map(|v| v.as_type().unwrap());
|
||||
let additional = args.remove_left_or_key("Additional");
|
||||
let additional = additional.map(|v| v.as_type().unwrap());
|
||||
let t = mono(ctx.name.clone());
|
||||
Ok(ValueObj::gen_t(GenTypeObj::subsumed(
|
||||
t, sup, impls, additional,
|
||||
)))
|
||||
}
|
||||
|
||||
pub fn __array_getitem__(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let slf = ctx
|
||||
.convert_value_into_array(args.remove_left_or_key("Self").unwrap())
|
||||
.unwrap();
|
||||
let index = enum_unwrap!(args.remove_left_or_key("Index").unwrap(), ValueObj::Nat);
|
||||
if let Some(v) = slf.get(index as usize) {
|
||||
Ok(v.clone())
|
||||
} else {
|
||||
Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!(
|
||||
"[{}] has {} elements, but accessed {}th element",
|
||||
erg_common::fmt_vec(&slf),
|
||||
slf.len(),
|
||||
index
|
||||
),
|
||||
line!() as usize,
|
||||
ErrorKind::IndexError,
|
||||
Location::Unknown,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn __dict_getitem__(mut args: ValueArgs, ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let slf = args.remove_left_or_key("Self").unwrap();
|
||||
let slf = enum_unwrap!(slf, ValueObj::Dict);
|
||||
let index = args.remove_left_or_key("Index").unwrap();
|
||||
if let Some(v) = slf.get(&index).or_else(|| {
|
||||
for (k, v) in slf.iter() {
|
||||
match (&index, k) {
|
||||
(ValueObj::Type(idx), ValueObj::Type(kt)) => {
|
||||
if ctx.subtype_of(idx.typ(), kt.typ()) {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
(idx, k) => {
|
||||
if idx == k {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}) {
|
||||
Ok(v.clone())
|
||||
} else {
|
||||
let index = if let ValueObj::Type(t) = &index {
|
||||
let derefed = ctx.readable_type(t.typ().clone(), false);
|
||||
ValueObj::builtin_t(derefed)
|
||||
} else {
|
||||
index
|
||||
};
|
||||
Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("{slf} has no key {index}"),
|
||||
line!() as usize,
|
||||
ErrorKind::IndexError,
|
||||
Location::Unknown,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn __range_getitem__(mut args: ValueArgs, _ctx: &Context) -> EvalValueResult<ValueObj> {
|
||||
let (_name, fields) = enum_unwrap!(
|
||||
args.remove_left_or_key("Self").unwrap(),
|
||||
ValueObj::DataClass { name, fields }
|
||||
);
|
||||
let index = enum_unwrap!(args.remove_left_or_key("Index").unwrap(), ValueObj::Nat);
|
||||
let start = fields.get("start").unwrap();
|
||||
let start = *enum_unwrap!(start, ValueObj::Nat);
|
||||
let end = fields.get("end").unwrap();
|
||||
let end = *enum_unwrap!(end, ValueObj::Nat);
|
||||
// FIXME <= if inclusive
|
||||
if start + index < end {
|
||||
Ok(ValueObj::Nat(start + index))
|
||||
} else {
|
||||
Err(ErrorCore::new(
|
||||
vec![SubMessage::only_loc(Location::Unknown)],
|
||||
format!("Index out of range: {}", index),
|
||||
line!() as usize,
|
||||
ErrorKind::IndexError,
|
||||
Location::Unknown,
|
||||
)
|
||||
.into())
|
||||
}
|
||||
}
|
476
crates/erg_compiler/context/initialize/funcs.rs
Normal file
476
crates/erg_compiler/context/initialize/funcs.rs
Normal file
|
@ -0,0 +1,476 @@
|
|||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::vis::Visibility;
|
||||
|
||||
use crate::ty::constructors::*;
|
||||
use crate::ty::typaram::TyParam;
|
||||
use crate::ty::value::ValueObj;
|
||||
use crate::ty::Type;
|
||||
use Type::*;
|
||||
|
||||
use crate::context::initialize::*;
|
||||
use crate::context::Context;
|
||||
use crate::varinfo::Mutability;
|
||||
use Mutability::*;
|
||||
use Visibility::*;
|
||||
|
||||
impl Context {
|
||||
pub(super) fn init_builtin_funcs(&mut self) {
|
||||
let vis = if cfg!(feature = "py_compatible") {
|
||||
Public
|
||||
} else {
|
||||
Private
|
||||
};
|
||||
let T = mono_q("T", instanceof(Type));
|
||||
let U = mono_q("U", instanceof(Type));
|
||||
let Path = mono_q_tp("Path", instanceof(Str));
|
||||
let t_abs = nd_func(vec![kw("n", mono(NUM))], None, Nat);
|
||||
let t_all = func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(Bool)]))],
|
||||
None,
|
||||
vec![],
|
||||
Bool,
|
||||
);
|
||||
let t_any = func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(Bool)]))],
|
||||
None,
|
||||
vec![],
|
||||
Bool,
|
||||
);
|
||||
let t_ascii = nd_func(vec![kw("object", Obj)], None, Str);
|
||||
let t_assert = func(
|
||||
vec![kw("condition", Bool)],
|
||||
None,
|
||||
vec![kw("err_message", Str)],
|
||||
NoneType,
|
||||
);
|
||||
let t_bin = nd_func(vec![kw("n", Int)], None, Str);
|
||||
let t_bytes = nd_func(
|
||||
vec![kw("str", Str), kw("encoding", Str)],
|
||||
None,
|
||||
mono("Bytes"),
|
||||
);
|
||||
let t_chr = nd_func(
|
||||
vec![kw("i", Type::from(value(0usize)..=value(1_114_111usize)))],
|
||||
None,
|
||||
Str,
|
||||
);
|
||||
let t_classof = nd_func(vec![kw("old", Obj)], None, ClassType);
|
||||
let t_compile = nd_func(vec![kw("src", Str)], None, Code);
|
||||
let t_cond = nd_func(
|
||||
vec![
|
||||
kw("condition", Bool),
|
||||
kw("then", T.clone()),
|
||||
kw("else", T.clone()),
|
||||
],
|
||||
None,
|
||||
T.clone(),
|
||||
)
|
||||
.quantify();
|
||||
let t_discard = nd_func(vec![kw("obj", Obj)], None, NoneType);
|
||||
let t_enumerate = func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(T.clone())]))],
|
||||
None,
|
||||
vec![kw("start", Int)],
|
||||
poly("Enumerate", vec![ty_tp(T.clone())]),
|
||||
)
|
||||
.quantify();
|
||||
let t_if = func(
|
||||
vec![
|
||||
kw("cond", Bool),
|
||||
kw("then", nd_func(vec![], None, T.clone())),
|
||||
],
|
||||
None,
|
||||
vec![kw_default(
|
||||
"else",
|
||||
nd_func(vec![], None, U.clone()),
|
||||
nd_func(vec![], None, NoneType),
|
||||
)],
|
||||
or(T.clone(), U.clone()),
|
||||
)
|
||||
.quantify();
|
||||
let t_int = nd_func(vec![kw("obj", Obj)], None, or(Int, NoneType));
|
||||
let t_import = nd_func(
|
||||
vec![anon(tp_enum(Str, set! {Path.clone()}))],
|
||||
None,
|
||||
module(Path.clone()),
|
||||
)
|
||||
.quantify();
|
||||
let t_isinstance = nd_func(
|
||||
vec![
|
||||
kw("object", Obj),
|
||||
kw("classinfo", ClassType), // TODO: => ClassInfo
|
||||
],
|
||||
None,
|
||||
Bool,
|
||||
);
|
||||
let t_issubclass = nd_func(
|
||||
vec![
|
||||
kw("subclass", ClassType),
|
||||
kw("classinfo", ClassType), // TODO: => ClassInfo
|
||||
],
|
||||
None,
|
||||
Bool,
|
||||
);
|
||||
let I = mono_q("I", subtypeof(poly("Iterable", vec![ty_tp(T.clone())])));
|
||||
let t_iter = nd_func(vec![kw("object", I.clone())], None, proj(I, "Iterator")).quantify();
|
||||
let t_len = nd_func(
|
||||
vec![kw("s", poly("Seq", vec![TyParam::erased(Type)]))],
|
||||
None,
|
||||
Nat,
|
||||
);
|
||||
let t_log = func(
|
||||
vec![],
|
||||
Some(kw("objects", ref_(Obj))),
|
||||
vec![
|
||||
kw("sep", Str),
|
||||
kw("end", Str),
|
||||
kw("file", mono("Write")),
|
||||
kw("flush", Bool),
|
||||
],
|
||||
NoneType,
|
||||
);
|
||||
let t_map = nd_func(
|
||||
vec![
|
||||
kw("proc!", nd_proc(vec![anon(T.clone())], None, T.clone())),
|
||||
kw("iterable", poly("Iterable", vec![ty_tp(T.clone())])),
|
||||
],
|
||||
None,
|
||||
poly("Map", vec![ty_tp(T.clone())]),
|
||||
)
|
||||
.quantify();
|
||||
let O = mono_q("O", subtypeof(mono("Ord")));
|
||||
// TODO: iterable should be non-empty
|
||||
let t_max = nd_func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(O.clone())]))],
|
||||
None,
|
||||
O.clone(),
|
||||
)
|
||||
.quantify();
|
||||
let t_min = nd_func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(O.clone())]))],
|
||||
None,
|
||||
O,
|
||||
)
|
||||
.quantify();
|
||||
let t_nat = nd_func(vec![kw("obj", Obj)], None, or(Nat, NoneType));
|
||||
// e.g. not(b: Bool!): Bool!
|
||||
let B = mono_q("B", subtypeof(Bool));
|
||||
let t_not = nd_func(vec![kw("b", B.clone())], None, B).quantify();
|
||||
let t_oct = nd_func(vec![kw("x", Int)], None, Str);
|
||||
let t_ord = nd_func(vec![kw("c", Str)], None, Nat);
|
||||
let t_panic = nd_func(vec![kw("err_message", Str)], None, Never);
|
||||
let M = mono_q("M", Constraint::Uninited);
|
||||
let M = mono_q("M", subtypeof(poly("Mul", vec![ty_tp(M)])));
|
||||
// TODO: mod
|
||||
let t_pow = nd_func(
|
||||
vec![kw("base", M.clone()), kw("exp", M.clone())],
|
||||
None,
|
||||
proj(M, "Output"),
|
||||
)
|
||||
.quantify();
|
||||
let t_pyimport = nd_func(
|
||||
vec![anon(tp_enum(Str, set! {Path.clone()}))],
|
||||
None,
|
||||
py_module(Path),
|
||||
)
|
||||
.quantify();
|
||||
let t_pycompile = nd_func(
|
||||
vec![kw("src", Str), kw("filename", Str), kw("mode", Str)],
|
||||
None,
|
||||
Code,
|
||||
);
|
||||
let t_quit = func(vec![], None, vec![kw("code", Int)], Never);
|
||||
let t_exit = t_quit.clone();
|
||||
let t_repr = nd_func(vec![kw("object", Obj)], None, Str);
|
||||
let t_reversed = nd_func(
|
||||
vec![kw("seq", poly("Seq", vec![ty_tp(T.clone())]))],
|
||||
None,
|
||||
poly("Reversed", vec![ty_tp(T.clone())]),
|
||||
)
|
||||
.quantify();
|
||||
let t_round = nd_func(vec![kw("number", Float)], None, Int);
|
||||
let t_sorted = nd_func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(T.clone())]))],
|
||||
None,
|
||||
array_t(T.clone(), TyParam::erased(Nat)),
|
||||
)
|
||||
.quantify();
|
||||
let t_str = nd_func(vec![kw("object", Obj)], None, Str);
|
||||
let A = mono_q("A", Constraint::Uninited);
|
||||
let A = mono_q("A", subtypeof(poly("Add", vec![ty_tp(A)])));
|
||||
let t_sum = func(
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(A.clone())]))],
|
||||
None,
|
||||
vec![kw_default("start", or(A.clone(), Int), Int)],
|
||||
A,
|
||||
)
|
||||
.quantify();
|
||||
let t_unreachable = nd_func(vec![], None, Never);
|
||||
let t_zip = nd_func(
|
||||
vec![
|
||||
kw("iterable1", poly("Iterable", vec![ty_tp(T.clone())])),
|
||||
kw("iterable2", poly("Iterable", vec![ty_tp(U.clone())])),
|
||||
],
|
||||
None,
|
||||
poly("Zip", vec![ty_tp(T.clone()), ty_tp(U.clone())]),
|
||||
)
|
||||
.quantify();
|
||||
self.register_builtin_py_impl("abs", t_abs, Immutable, vis, Some("abs"));
|
||||
self.register_builtin_py_impl("all", t_all, Immutable, vis, Some("all"));
|
||||
self.register_builtin_py_impl("any", t_any, Immutable, vis, Some("any"));
|
||||
self.register_builtin_py_impl("ascii", t_ascii, Immutable, vis, Some("ascii"));
|
||||
// Leave as `Const`, as it may negatively affect assert casting.
|
||||
self.register_builtin_erg_impl("assert", t_assert, Const, vis);
|
||||
self.register_builtin_py_impl("bin", t_bin, Immutable, vis, Some("bin"));
|
||||
self.register_builtin_py_impl("bytes", t_bytes, Immutable, vis, Some("bytes"));
|
||||
self.register_builtin_py_impl("chr", t_chr, Immutable, vis, Some("chr"));
|
||||
self.register_builtin_py_impl("classof", t_classof, Immutable, vis, Some("type"));
|
||||
self.register_builtin_py_impl("compile", t_compile, Immutable, vis, Some("compile"));
|
||||
self.register_builtin_erg_impl("cond", t_cond, Immutable, vis);
|
||||
self.register_builtin_py_impl("enumerate", t_enumerate, Immutable, vis, Some("enumerate"));
|
||||
self.register_builtin_py_impl("exit", t_exit, Immutable, vis, Some("exit"));
|
||||
self.register_builtin_py_impl(
|
||||
"isinstance",
|
||||
t_isinstance,
|
||||
Immutable,
|
||||
vis,
|
||||
Some("isinstance"),
|
||||
);
|
||||
self.register_builtin_py_impl(
|
||||
"issubclass",
|
||||
t_issubclass,
|
||||
Immutable,
|
||||
vis,
|
||||
Some("issubclass"),
|
||||
);
|
||||
self.register_builtin_py_impl("iter", t_iter, Immutable, vis, Some("iter"));
|
||||
self.register_builtin_py_impl("len", t_len, Immutable, vis, Some("len"));
|
||||
self.register_builtin_py_impl("map", t_map, Immutable, vis, Some("map"));
|
||||
self.register_builtin_py_impl("max", t_max, Immutable, vis, Some("max"));
|
||||
self.register_builtin_py_impl("min", t_min, Immutable, vis, Some("min"));
|
||||
self.register_builtin_py_impl("not", t_not, Immutable, vis, None); // `not` is not a function in Python
|
||||
self.register_builtin_py_impl("oct", t_oct, Immutable, vis, Some("oct"));
|
||||
self.register_builtin_py_impl("ord", t_ord, Immutable, vis, Some("ord"));
|
||||
self.register_builtin_py_impl("pow", t_pow, Immutable, vis, Some("pow"));
|
||||
self.register_builtin_py_impl(
|
||||
"pyimport",
|
||||
t_pyimport.clone(),
|
||||
Immutable,
|
||||
vis,
|
||||
Some("__import__"),
|
||||
);
|
||||
self.register_builtin_py_impl("quit", t_quit, Immutable, vis, Some("quit"));
|
||||
self.register_builtin_py_impl("repr", t_repr, Immutable, vis, Some("repr"));
|
||||
self.register_builtin_py_impl("reversed", t_reversed, Immutable, vis, Some("reversed"));
|
||||
self.register_builtin_py_impl("round", t_round, Immutable, vis, Some("round"));
|
||||
self.register_builtin_py_impl("sorted", t_sorted, Immutable, vis, Some("sorted"));
|
||||
self.register_builtin_py_impl("str", t_str, Immutable, vis, Some("str"));
|
||||
self.register_builtin_py_impl("sum", t_sum, Immutable, vis, Some("sum"));
|
||||
self.register_builtin_py_impl("zip", t_zip, Immutable, vis, Some("zip"));
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
"int"
|
||||
} else {
|
||||
"int__"
|
||||
};
|
||||
self.register_builtin_py_impl("int", t_int, Immutable, vis, Some(name));
|
||||
if !cfg!(feature = "py_compatible") {
|
||||
self.register_builtin_py_impl("if", t_if, Immutable, vis, Some("if__"));
|
||||
self.register_builtin_py_impl("discard", t_discard, Immutable, vis, Some("discard__"));
|
||||
self.register_builtin_py_impl("import", t_import, Immutable, vis, Some("__import__"));
|
||||
self.register_builtin_py_impl("log", t_log, Immutable, vis, Some("print"));
|
||||
self.register_builtin_py_impl("nat", t_nat, Immutable, vis, Some("nat__"));
|
||||
self.register_builtin_py_impl("panic", t_panic, Immutable, vis, Some("quit"));
|
||||
if cfg!(feature = "debug") {
|
||||
self.register_builtin_py_impl("py", t_pyimport, Immutable, vis, Some("__import__"));
|
||||
}
|
||||
self.register_builtin_py_impl(
|
||||
"pycompile",
|
||||
t_pycompile,
|
||||
Immutable,
|
||||
vis,
|
||||
Some("compile"),
|
||||
);
|
||||
// TODO: original implementation
|
||||
self.register_builtin_py_impl(
|
||||
"unreachable",
|
||||
t_unreachable,
|
||||
Immutable,
|
||||
vis,
|
||||
Some("exit"),
|
||||
);
|
||||
} else {
|
||||
let t_range = func(
|
||||
vec![kw("stop", or(Int, NoneType))],
|
||||
None,
|
||||
vec![
|
||||
kw("start", or(Int, NoneType)),
|
||||
kw("step", or(Int, NoneType)),
|
||||
],
|
||||
poly("Range", vec![ty_tp(Int)]),
|
||||
);
|
||||
self.register_builtin_py_impl("range", t_range, Immutable, vis, Some("range"));
|
||||
let t_list = func(
|
||||
vec![],
|
||||
None,
|
||||
vec![kw("iterable", poly("Iterable", vec![ty_tp(T.clone())]))],
|
||||
poly("Array", vec![ty_tp(T.clone()), TyParam::erased(Nat)]),
|
||||
)
|
||||
.quantify();
|
||||
self.register_builtin_py_impl("list", t_list, Immutable, vis, Some("list"));
|
||||
let t_dict = func(
|
||||
vec![],
|
||||
None,
|
||||
vec![kw(
|
||||
"iterable",
|
||||
poly("Iterable", vec![ty_tp(tuple_t(vec![T.clone(), U.clone()]))]),
|
||||
)],
|
||||
dict! { T => U }.into(),
|
||||
)
|
||||
.quantify();
|
||||
self.register_builtin_py_impl("dict", t_dict, Immutable, vis, Some("dict"));
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn init_builtin_const_funcs(&mut self) {
|
||||
let vis = if cfg!(feature = "py_compatible") {
|
||||
Public
|
||||
} else {
|
||||
Private
|
||||
};
|
||||
let class_t = func(
|
||||
vec![],
|
||||
None,
|
||||
vec![kw("Requirement", or(Type, Ellipsis)), kw("Impl", Type)],
|
||||
ClassType,
|
||||
);
|
||||
let class = ConstSubr::Builtin(BuiltinConstSubr::new("Class", class_func, class_t, None));
|
||||
self.register_builtin_const("Class", vis, ValueObj::Subr(class));
|
||||
let inherit_t = func(
|
||||
vec![kw("Super", ClassType)],
|
||||
None,
|
||||
vec![kw("Impl", Type), kw("Additional", Type)],
|
||||
ClassType,
|
||||
);
|
||||
let inherit = ConstSubr::Builtin(BuiltinConstSubr::new(
|
||||
"Inherit",
|
||||
inherit_func,
|
||||
inherit_t,
|
||||
None,
|
||||
));
|
||||
self.register_builtin_const("Inherit", vis, ValueObj::Subr(inherit));
|
||||
let trait_t = func(
|
||||
vec![kw("Requirement", Type)],
|
||||
None,
|
||||
vec![kw("Impl", Type)],
|
||||
TraitType,
|
||||
);
|
||||
let trait_ = ConstSubr::Builtin(BuiltinConstSubr::new("Trait", trait_func, trait_t, None));
|
||||
self.register_builtin_const("Trait", vis, ValueObj::Subr(trait_));
|
||||
let subsume_t = func(
|
||||
vec![kw("Super", TraitType)],
|
||||
None,
|
||||
vec![kw("Impl", Type), kw("Additional", Type)],
|
||||
TraitType,
|
||||
);
|
||||
let subsume = ConstSubr::Builtin(BuiltinConstSubr::new(
|
||||
"Subsume",
|
||||
subsume_func,
|
||||
subsume_t,
|
||||
None,
|
||||
));
|
||||
self.register_builtin_const("Subsume", vis, ValueObj::Subr(subsume));
|
||||
// decorators
|
||||
let inheritable_t = func1(ClassType, ClassType);
|
||||
let inheritable = ConstSubr::Builtin(BuiltinConstSubr::new(
|
||||
"Inheritable",
|
||||
inheritable_func,
|
||||
inheritable_t,
|
||||
None,
|
||||
));
|
||||
self.register_builtin_const("Inheritable", vis, ValueObj::Subr(inheritable));
|
||||
// TODO: register Del function object
|
||||
let t_del = nd_func(vec![kw("obj", Obj)], None, NoneType);
|
||||
self.register_builtin_erg_impl("Del", t_del, Immutable, vis);
|
||||
let patch_t = func(
|
||||
vec![kw("Requirement", Type)],
|
||||
None,
|
||||
vec![kw("Impl", Type)],
|
||||
TraitType,
|
||||
);
|
||||
let patch = ConstSubr::Builtin(BuiltinConstSubr::new("Patch", patch_func, patch_t, None));
|
||||
self.register_builtin_const("Patch", vis, ValueObj::Subr(patch));
|
||||
}
|
||||
|
||||
pub(super) fn init_builtin_operators(&mut self) {
|
||||
/* binary */
|
||||
let R = mono_q("R", instanceof(Type));
|
||||
let params = vec![ty_tp(R.clone())];
|
||||
let L = mono_q("L", subtypeof(poly("Add", params.clone())));
|
||||
let op_t = nd_func(
|
||||
vec![kw("lhs", L.clone()), kw("rhs", R.clone())],
|
||||
None,
|
||||
proj(L, "Output"),
|
||||
)
|
||||
.quantify();
|
||||
self.register_builtin_erg_impl("__add__", op_t, Const, Private);
|
||||
let L = mono_q("L", subtypeof(poly("Sub", params.clone())));
|
||||
let op_t = bin_op(L.clone(), R.clone(), proj(L, "Output")).quantify();
|
||||
self.register_builtin_erg_impl("__sub__", op_t, Const, Private);
|
||||
let L = mono_q("L", subtypeof(poly("Mul", params.clone())));
|
||||
let op_t = bin_op(L.clone(), R.clone(), proj(L, "Output")).quantify();
|
||||
self.register_builtin_erg_impl("__mul__", op_t, Const, Private);
|
||||
let L = mono_q("L", subtypeof(poly("Div", params.clone())));
|
||||
let op_t = bin_op(L.clone(), R.clone(), proj(L, "Output")).quantify();
|
||||
self.register_builtin_erg_impl("__div__", op_t, Const, Private);
|
||||
let L = mono_q("L", subtypeof(poly("FloorDiv", params)));
|
||||
let op_t = bin_op(L.clone(), R, proj(L, "Output")).quantify();
|
||||
self.register_builtin_erg_impl("__floordiv__", op_t, Const, Private);
|
||||
let P = mono_q("P", Constraint::Uninited);
|
||||
let P = mono_q("P", subtypeof(poly("Mul", vec![ty_tp(P)])));
|
||||
let op_t = bin_op(P.clone(), P.clone(), proj(P, "PowOutput")).quantify();
|
||||
// TODO: add bound: M == M.Output
|
||||
self.register_builtin_erg_impl("__pow__", op_t, Const, Private);
|
||||
let M = mono_q("M", Constraint::Uninited);
|
||||
let M = mono_q("M", subtypeof(poly("Div", vec![ty_tp(M)])));
|
||||
let op_t = bin_op(M.clone(), M.clone(), proj(M, "ModOutput")).quantify();
|
||||
self.register_builtin_erg_impl("__mod__", op_t, Const, Private);
|
||||
let op_t = nd_proc(vec![kw("lhs", Obj), kw("rhs", Obj)], None, Bool);
|
||||
self.register_builtin_erg_impl("__is__!", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__isnot__!", op_t, Const, Private);
|
||||
let E = mono_q("E", subtypeof(mono("Eq")));
|
||||
let op_t = bin_op(E.clone(), E, Bool).quantify();
|
||||
self.register_builtin_erg_impl("__eq__", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__ne__", op_t, Const, Private);
|
||||
let O = mono_q("O", subtypeof(mono("Ord")));
|
||||
let op_t = bin_op(O.clone(), O.clone(), Bool).quantify();
|
||||
self.register_builtin_erg_impl("__lt__", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__le__", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__gt__", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__ge__", op_t, Const, Private);
|
||||
let BT = mono_q("BT", subtypeof(or(Bool, Type)));
|
||||
let op_t = bin_op(BT.clone(), BT.clone(), BT).quantify();
|
||||
self.register_builtin_erg_impl("__and__", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__or__", op_t, Const, Private);
|
||||
let op_t = bin_op(O.clone(), O.clone(), range(O)).quantify();
|
||||
self.register_builtin_erg_decl("__rng__", op_t.clone(), Private);
|
||||
self.register_builtin_erg_decl("__lorng__", op_t.clone(), Private);
|
||||
self.register_builtin_erg_decl("__rorng__", op_t.clone(), Private);
|
||||
self.register_builtin_erg_decl("__orng__", op_t, Private);
|
||||
// TODO: use existential type: |T: Type| (T, In(T)) -> Bool
|
||||
let T = mono_q("T", instanceof(Type));
|
||||
let I = mono_q("I", subtypeof(poly("In", vec![ty_tp(T.clone())])));
|
||||
let op_t = bin_op(I, T, Bool).quantify();
|
||||
self.register_builtin_erg_impl("__in__", op_t.clone(), Const, Private);
|
||||
self.register_builtin_erg_impl("__notin__", op_t, Const, Private);
|
||||
/* unary */
|
||||
// TODO: +/- Bool would like to be warned
|
||||
let M = mono_q("M", subtypeof(mono("Mutizable")));
|
||||
let op_t = func1(M.clone(), proj(M, "MutType!")).quantify();
|
||||
self.register_builtin_erg_impl("__mutate__", op_t, Const, Private);
|
||||
let N = mono_q("N", subtypeof(mono(NUM)));
|
||||
let op_t = func1(N.clone(), N).quantify();
|
||||
self.register_builtin_erg_decl("__pos__", op_t.clone(), Private);
|
||||
self.register_builtin_erg_decl("__neg__", op_t, Private);
|
||||
}
|
||||
}
|
784
crates/erg_compiler/context/initialize/mod.rs
Normal file
784
crates/erg_compiler/context/initialize/mod.rs
Normal file
|
@ -0,0 +1,784 @@
|
|||
//! defines type information for builtin objects (in `Context`)
|
||||
//!
|
||||
//! 組み込みオブジェクトの型情報を(Contextに)定義
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
mod classes;
|
||||
pub mod const_func;
|
||||
mod funcs;
|
||||
mod patches;
|
||||
mod procs;
|
||||
mod traits;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use erg_common::config::ErgConfig;
|
||||
use erg_common::dict;
|
||||
use erg_common::env::erg_pystd_path;
|
||||
use erg_common::error::Location;
|
||||
use erg_common::fresh::fresh_varname;
|
||||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::vis::Visibility;
|
||||
use erg_common::Str;
|
||||
use erg_common::{set, unique_in_place};
|
||||
|
||||
use erg_parser::ast::VarName;
|
||||
|
||||
use crate::context::initialize::const_func::*;
|
||||
use crate::context::instantiate::ConstTemplate;
|
||||
use crate::context::{
|
||||
ClassDefType, Context, ContextKind, MethodInfo, ModuleContext, ParamSpec, TraitImpl,
|
||||
};
|
||||
use crate::module::{SharedCompilerResource, SharedModuleCache};
|
||||
use crate::ty::free::Constraint;
|
||||
use crate::ty::value::ValueObj;
|
||||
use crate::ty::Type;
|
||||
use crate::ty::{constructors::*, BuiltinConstSubr, ConstSubr, Predicate};
|
||||
use crate::varinfo::{AbsLocation, Mutability, VarInfo, VarKind};
|
||||
use Mutability::*;
|
||||
use ParamSpec as PS;
|
||||
use Type::*;
|
||||
use VarKind::*;
|
||||
use Visibility::*;
|
||||
|
||||
const NUM: &str = "Num";
|
||||
|
||||
const UNPACK: &str = "Unpack";
|
||||
const INHERITABLE_TYPE: &str = "InheritableType";
|
||||
const NAMED: &str = "Named";
|
||||
const MUTABLE: &str = "Mutable";
|
||||
const SELF: &str = "Self";
|
||||
const IMMUTIZABLE: &str = "Immutizable";
|
||||
const IMMUT_TYPE: &str = "ImmutType";
|
||||
const PROC_UPDATE: &str = "update!";
|
||||
const MUTIZABLE: &str = "Mutizable";
|
||||
const MUTABLE_MUT_TYPE: &str = "MutType!";
|
||||
const PATH_LIKE: &str = "PathLike";
|
||||
const MUTABLE_READABLE: &str = "Readable!";
|
||||
const READABLE: &str = "Readable";
|
||||
const FUNC_READ: &str = "read";
|
||||
const PROC_READ: &str = "read!";
|
||||
const WRITABLE: &str = "Writable";
|
||||
const MUTABLE_WRITABLE: &str = "Writable!";
|
||||
const FUNC_WRITE: &str = "write";
|
||||
const PROC_WRITE: &str = "write!";
|
||||
const FILE_LIKE: &str = "FileLike";
|
||||
const MUTABLE_FILE_LIKE: &str = "FileLike!";
|
||||
const SHOW: &str = "Show";
|
||||
const INPUT: &str = "Input";
|
||||
const OUTPUT: &str = "Output";
|
||||
const POW_OUTPUT: &str = "PowOutput";
|
||||
const MOD_OUTPUT: &str = "ModOutput";
|
||||
const IN: &str = "In";
|
||||
const EQ: &str = "Eq";
|
||||
const ORD: &str = "Ord";
|
||||
const TO_STR: &str = "to_str";
|
||||
const ORDERING: &str = "Ordering";
|
||||
const SEQ: &str = "Seq";
|
||||
const FUNC_LEN: &str = "len";
|
||||
const FUNC_GET: &str = "get";
|
||||
const ITERABLE: &str = "Iterable";
|
||||
const ITERATOR: &str = "Iterator";
|
||||
const STR_ITERATOR: &str = "StrIterator";
|
||||
const FUNC_ITER: &str = "iter";
|
||||
const ITER: &str = "Iter";
|
||||
const ADD: &str = "Add";
|
||||
const SUB: &str = "Sub";
|
||||
const MUL: &str = "Mul";
|
||||
const DIV: &str = "Div";
|
||||
const FLOOR_DIV: &str = "FloorDiv";
|
||||
const NEVER: &str = "Never";
|
||||
const OBJ: &str = "Obj";
|
||||
const MUTABLE_OBJ: &str = "Obj!";
|
||||
const FUNC_CLONE: &str = "clone";
|
||||
const BYTES: &str = "Bytes";
|
||||
const FLOAT: &str = "Float";
|
||||
const MUT_FLOAT: &str = "Float!";
|
||||
const EPSILON: &str = "EPSILON";
|
||||
const REAL: &str = "Real";
|
||||
const FUNC_REAL: &str = "real";
|
||||
const IMAG: &str = "Imag";
|
||||
const FUNC_IMAG: &str = "imag";
|
||||
const FUNC_CONJUGATE: &str = "conjugate";
|
||||
const FUNC_IS_INTEGER: &str = "is_integer";
|
||||
const FUNC_HEX: &str = "hex";
|
||||
const FUNC_FROMHEX: &str = "fromhex";
|
||||
const INT: &str = "Int";
|
||||
const MUT_INT: &str = "Int!";
|
||||
const RATIO: &str = "Ratio";
|
||||
const MUT_RATIO: &str = "Raltio!";
|
||||
const FUNC_ABS: &str = "abs";
|
||||
const FUNC_SUCC: &str = "succ";
|
||||
const FUNC_PRED: &str = "pred";
|
||||
const FUNC_BIT_LENGTH: &str = "bit_length";
|
||||
const FUNC_BIT_COUNT: &str = "bit_count";
|
||||
const FUNC_BYTEORDER: &str = "byteorder";
|
||||
const TOKEN_BIG_ENDIAN: &str = "big";
|
||||
const TOKEN_LITTLE_ENDIAN: &str = "little";
|
||||
const FUNC_FROM_BYTES: &str = "from_bytes";
|
||||
const FUNC_TO_BYTES: &str = "to_bytes";
|
||||
const NAT: &str = "Nat";
|
||||
const MUT_NAT: &str = "Nat!";
|
||||
const PROC_TIMES: &str = "times!";
|
||||
const FUNC_TIMES: &str = "times";
|
||||
const BOOL: &str = "Bool";
|
||||
const MUT_BOOL: &str = "Bool!";
|
||||
const STR: &str = "Str";
|
||||
const MUT_STR: &str = "Str!";
|
||||
const FUNC_REPLACE: &str = "replace";
|
||||
const FUNC_ENCODE: &str = "encode";
|
||||
const FUNC_FORMAT: &str = "format";
|
||||
const FUNC_LOWER: &str = "lower";
|
||||
const FUNC_UPPER: &str = "upper";
|
||||
const FUNC_TO_INT: &str = "to_int";
|
||||
const NONE_TYPE: &str = "NoneType";
|
||||
const TYPE: &str = "Type";
|
||||
const CLASS_TYPE: &str = "ClassType";
|
||||
const TRAIT_TYPE: &str = "TraitType";
|
||||
const CODE: &str = "Code";
|
||||
const FUNC_MRO: &str = "mro";
|
||||
const FUNC_CO_ARGCOUNT: &str = "co_argcount";
|
||||
const FUNC_CO_VARNAMES: &str = "co_varnames";
|
||||
const FUNC_CO_CONSTS: &str = "co_consts";
|
||||
const FUNC_CO_NAMES: &str = "co_names";
|
||||
const FUNC_CO_FREEVARS: &str = "co_freevars";
|
||||
const FUNC_CO_CELLVARS: &str = "co_cellvars";
|
||||
const FUNC_CO_FILENAME: &str = "co_filename";
|
||||
const FUNC_CO_NAME: &str = "co_name";
|
||||
const FUNC_CO_FIRSTLINENO: &str = "co_firstlineno";
|
||||
const FUNC_CO_STACKSIZE: &str = "co_stacksize";
|
||||
const FUNC_CO_FLAGS: &str = "co_flags";
|
||||
const FUNC_CO_CODE: &str = "co_code";
|
||||
const FUNC_CO_LNOTAB: &str = "co_lnotab";
|
||||
const FUNC_CO_NLOCALS: &str = "co_nlocals";
|
||||
const FUNC_CO_KWONLYARGCOUNT: &str = "co_kwonlyargcount";
|
||||
const FUNC_CO_POSONLYARGCOUNT: &str = "co_posonlyargcount";
|
||||
const GENERIC_MODULE: &str = "GenericModule";
|
||||
const PATH: &str = "Path";
|
||||
const MODULE: &str = "Module";
|
||||
const PY_MODULE: &str = "PyModule";
|
||||
const ARRAY: &str = "Array";
|
||||
const MUT_ARRAY: &str = "Array!";
|
||||
const FUNC_CONCAT: &str = "concat";
|
||||
const FUNC_COUNT: &str = "count";
|
||||
const FUNC_PUSH: &str = "push";
|
||||
const PROC_PUSH: &str = "push!";
|
||||
const ARRAY_ITERATOR: &str = "ArrayIterator";
|
||||
const SET: &str = "Set";
|
||||
const MUT_SET: &str = "Set!";
|
||||
const GENERIC_DICT: &str = "GenericDict";
|
||||
const DICT: &str = "Dict";
|
||||
const FUNC_DECODE: &str = "decode";
|
||||
const GENERIC_TUPLE: &str = "GenericTuple";
|
||||
const TUPLE: &str = "Tuple";
|
||||
const RECORD: &str = "Record";
|
||||
const OR: &str = "Or";
|
||||
const RANGE_ITERATOR: &str = "RangeIterator";
|
||||
const ENUMERATE: &str = "Enumerate";
|
||||
const FILTER: &str = "Filter";
|
||||
const MAP: &str = "Map";
|
||||
const REVERSED: &str = "Reversed";
|
||||
const ZIP: &str = "Zip";
|
||||
const FUNC_INC: &str = "inc";
|
||||
const PROC_INC: &str = "inc!";
|
||||
const FUNC_DEC: &str = "dec";
|
||||
const PROC_DEC: &str = "dec!";
|
||||
const MUT_FILE: &str = "File!";
|
||||
const MUT_READABLE: &str = "Readable!";
|
||||
const MUT_WRITABLE: &str = "Writable!";
|
||||
const MUT_FILE_LIKE: &str = "FileLike!";
|
||||
const FUNC_APPEND: &str = "append";
|
||||
const FUNC_EXTEND: &str = "extend";
|
||||
const PROC_EXTEND: &str = "extend!";
|
||||
const FUNC_INSERT: &str = "insert";
|
||||
const PROC_INSERT: &str = "insert!";
|
||||
const FUNC_REMOVE: &str = "remove";
|
||||
const PROC_REMOVE: &str = "remove!";
|
||||
const FUNC_POP: &str = "pop";
|
||||
const PROC_POP: &str = "pop!";
|
||||
const FUNC_CLEAR: &str = "clear";
|
||||
const PROC_CLEAR: &str = "clear!";
|
||||
const FUNC_SORT: &str = "sort";
|
||||
const PROC_SORT: &str = "sort!";
|
||||
const FUNC_REVERSE: &str = "reverse";
|
||||
const PROC_REVERSE: &str = "reverse!";
|
||||
const PROC_STRICT_MAP: &str = "strict_map!";
|
||||
const FUNC_ADD: &str = "add";
|
||||
const PROC_ADD: &str = "add!";
|
||||
const RANGE: &str = "Range";
|
||||
const GENERIC_CALLABLE: &str = "GenericCallable";
|
||||
const GENERIC_GENERATOR: &str = "GenericGenerator";
|
||||
const FUNC_RETURN: &str = "return";
|
||||
const FUNC_YIELD: &str = "yield";
|
||||
const PROC: &str = "Proc";
|
||||
const NAMED_PROC: &str = "NamedProc";
|
||||
const NAMED_FUNC: &str = "NamedFunc";
|
||||
const FUNC: &str = "Func";
|
||||
const QUANTIFIED: &str = "Quantified";
|
||||
const QUANTIFIED_FUNC: &str = "QuantifiedFunc";
|
||||
const FUNC_OBJECT: &str = "object";
|
||||
const FUNC_INT: &str = "int";
|
||||
const FUNC_FLOAT: &str = "float";
|
||||
const FUNC_BOOL: &str = "bool";
|
||||
const FUNC_STR: &str = "str";
|
||||
const FUNC_TYPE: &str = "type";
|
||||
const CODE_TYPE: &str = "CodeType";
|
||||
const MODULE_TYPE: &str = "ModuleType";
|
||||
const FUNC_LIST: &str = "list";
|
||||
const FUNC_SET: &str = "set";
|
||||
const FUNC_DICT: &str = "dict";
|
||||
const FUNC_TUPLE: &str = "tuple";
|
||||
const UNION: &str = "Union";
|
||||
const FUNC_STR_ITERATOR: &str = "str_iterator";
|
||||
const FUNC_ARRAY_ITERATOR: &str = "array_iterator";
|
||||
const FUNC_ENUMERATE: &str = "enumerate";
|
||||
const FUNC_FILTER: &str = "filter";
|
||||
const FUNC_MAP: &str = "map";
|
||||
const FUNC_REVERSED: &str = "reversed";
|
||||
const FUNC_ZIP: &str = "zip";
|
||||
const FILE: &str = "File";
|
||||
const CALLABLE: &str = "Callable";
|
||||
const GENERATOR: &str = "Generator";
|
||||
const FUNC_RANGE: &str = "range";
|
||||
|
||||
const OP_IN: &str = "__in__";
|
||||
const OP_EQ: &str = "__eq__";
|
||||
const OP_CMP: &str = "__cmp__";
|
||||
const OP_ADD: &str = "__add__";
|
||||
const OP_SUB: &str = "__sub__";
|
||||
const OP_MUL: &str = "__mul__";
|
||||
const OP_DIV: &str = "__div__";
|
||||
const OP_FLOOR_DIV: &str = "__floordiv__";
|
||||
const OP_ABS: &str = "__abs__";
|
||||
const OP_PARTIAL_CMP: &str = "__partial_cmp__";
|
||||
const OP_AND: &str = "__and__";
|
||||
const OP_OR: &str = "__or__";
|
||||
|
||||
const FUNDAMENTAL_NAME: &str = "__name__";
|
||||
const FUNDAMENTAL_STR: &str = "__str__";
|
||||
const FUNDAMENTAL_ITER: &str = "__iter__";
|
||||
const FUNDAMENTAL_MODULE: &str = "__module__";
|
||||
const FUNDAMENTAL_SIZEOF: &str = "__sizeof__";
|
||||
const FUNDAMENTAL_REPR: &str = "__repr__";
|
||||
const FUNDAMENTAL_DICT: &str = "__dict__";
|
||||
const FUNDAMENTAL_BYTES: &str = "__bytes__";
|
||||
const FUNDAMENTAL_GETITEM: &str = "__getitem__";
|
||||
const FUNDAMENTAL_TUPLE_GETITEM: &str = "__Tuple_getitem__";
|
||||
|
||||
const LICENSE: &str = "license";
|
||||
const CREDITS: &str = "credits";
|
||||
const COPYRIGHT: &str = "copyright";
|
||||
const TRUE: &str = "True";
|
||||
const FALSE: &str = "False";
|
||||
const NONE: &str = "None";
|
||||
const NOT_IMPLEMENTED: &str = "NotImplemented";
|
||||
const ELLIPSIS: &str = "Ellipsis";
|
||||
const SITEBUILTINS_PRINTER: &str = "_sitebuiltins._Printer";
|
||||
|
||||
const TY_D: &str = "D";
|
||||
const TY_T: &str = "T";
|
||||
const TY_TS: &str = "Ts";
|
||||
const TY_I: &str = "I";
|
||||
const TY_R: &str = "R";
|
||||
const TY_U: &str = "U";
|
||||
const TY_L: &str = "L";
|
||||
const TY_N: &str = "N";
|
||||
const TY_M: &str = "M";
|
||||
|
||||
const KW_OLD: &str = "old";
|
||||
const KW_N: &str = "n";
|
||||
const KW_S: &str = "s";
|
||||
const KW_SELF: &str = "self";
|
||||
const KW_LENGTH: &str = "length";
|
||||
const KW_PROC: &str = "proc!";
|
||||
const KW_PAT: &str = "pat";
|
||||
const KW_INTO: &str = "into";
|
||||
const KW_ENCODING: &str = "encoding";
|
||||
const KW_ERRORS: &str = "errors";
|
||||
const KW_ARGS: &str = "args";
|
||||
const KW_IDX: &str = "idx";
|
||||
const KW_RHS: &str = "rhs";
|
||||
const KW_X: &str = "x";
|
||||
const KW_ELEM: &str = "elem";
|
||||
const KW_FUNC: &str = "func";
|
||||
const KW_ITERABLE: &str = "iterable";
|
||||
const KW_INDEX: &str = "index";
|
||||
const KW_KEY: &str = "key";
|
||||
|
||||
pub fn builtins_path() -> PathBuf {
|
||||
erg_pystd_path().join("builtins.d.er")
|
||||
}
|
||||
|
||||
impl Context {
|
||||
fn register_builtin_decl(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
vis: Visibility,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
if cfg!(feature = "debug") {
|
||||
if let Type::Subr(subr) = &t {
|
||||
if subr.has_qvar() {
|
||||
panic!("not quantified subr: {subr}");
|
||||
}
|
||||
}
|
||||
}
|
||||
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}", self.name);
|
||||
} else {
|
||||
let vi = VarInfo::new(
|
||||
t,
|
||||
Immutable,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
impl_of,
|
||||
py_name.map(Str::ever),
|
||||
AbsLocation::unknown(),
|
||||
);
|
||||
self.decls.insert(name, vi);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_builtin_erg_decl(&mut self, name: &'static str, t: Type, vis: Visibility) {
|
||||
self.register_builtin_decl(name, t, vis, None);
|
||||
}
|
||||
|
||||
fn register_builtin_py_decl(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
vis: Visibility,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
self.register_builtin_decl(name, t, vis, py_name);
|
||||
}
|
||||
|
||||
fn register_builtin_impl(
|
||||
&mut self,
|
||||
name: VarName,
|
||||
t: Type,
|
||||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
py_name: Option<&'static str>,
|
||||
loc: AbsLocation,
|
||||
) {
|
||||
if cfg!(feature = "debug") {
|
||||
if let Type::Subr(subr) = &t {
|
||||
if subr.has_qvar() {
|
||||
panic!("not quantified subr: {subr}");
|
||||
}
|
||||
}
|
||||
}
|
||||
let impl_of = if let ContextKind::MethodDefs(Some(tr)) = &self.kind {
|
||||
Some(tr.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let vi = VarInfo::new(
|
||||
t,
|
||||
muty,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
impl_of,
|
||||
py_name.map(Str::ever),
|
||||
loc,
|
||||
);
|
||||
if let Some(_vi) = self.locals.get(&name) {
|
||||
if _vi != &vi {
|
||||
panic!("already registered: {} {name}", self.name);
|
||||
}
|
||||
} else {
|
||||
self.locals.insert(name, vi);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_builtin_erg_impl(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
) {
|
||||
let name = VarName::from_static(name);
|
||||
self.register_builtin_impl(name, t, muty, vis, None, AbsLocation::unknown());
|
||||
}
|
||||
|
||||
// TODO: replace with `register_builtins`
|
||||
fn register_builtin_py_impl(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
muty: Mutability,
|
||||
vis: Visibility,
|
||||
py_name: Option<&'static str>,
|
||||
) {
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
if let Some(py_name) = py_name {
|
||||
VarName::from_static(py_name)
|
||||
} else {
|
||||
VarName::from_static(name)
|
||||
}
|
||||
} else {
|
||||
VarName::from_static(name)
|
||||
};
|
||||
self.register_builtin_impl(name, t, muty, vis, py_name, AbsLocation::unknown());
|
||||
}
|
||||
|
||||
pub(crate) fn register_py_builtin(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
t: Type,
|
||||
py_name: Option<&'static str>,
|
||||
lineno: u32,
|
||||
) {
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
if let Some(py_name) = py_name {
|
||||
VarName::from_static(py_name)
|
||||
} else {
|
||||
VarName::from_static(name)
|
||||
}
|
||||
} else {
|
||||
VarName::from_static(name)
|
||||
};
|
||||
let vis = if cfg!(feature = "py_compatible") {
|
||||
Public
|
||||
} else {
|
||||
Private
|
||||
};
|
||||
let muty = Immutable;
|
||||
let loc = Location::range(lineno, 0, lineno, name.inspect().len() as u32);
|
||||
let abs_loc = AbsLocation::new(Some(builtins_path()), loc);
|
||||
self.register_builtin_impl(name, t, muty, vis, py_name, abs_loc);
|
||||
}
|
||||
|
||||
fn register_builtin_const(&mut self, name: &str, vis: Visibility, obj: ValueObj) {
|
||||
if self.rec_get_const_obj(name).is_some() {
|
||||
panic!("already registered: {} {name}", self.name);
|
||||
} else {
|
||||
let impl_of = if let ContextKind::MethodDefs(Some(tr)) = &self.kind {
|
||||
Some(tr.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// TODO: not all value objects are comparable
|
||||
let vi = VarInfo::new(
|
||||
v_enum(set! {obj.clone()}),
|
||||
Const,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
impl_of,
|
||||
None,
|
||||
AbsLocation::unknown(),
|
||||
);
|
||||
self.consts.insert(VarName::from_str(Str::rc(name)), obj);
|
||||
self.locals.insert(VarName::from_str(Str::rc(name)), vi);
|
||||
}
|
||||
}
|
||||
|
||||
fn register_const_param_defaults(&mut self, name: &'static str, params: Vec<ConstTemplate>) {
|
||||
if self.const_param_defaults.get(name).is_some() {
|
||||
panic!("already registered: {} {name}", self.name);
|
||||
} else {
|
||||
self.const_param_defaults.insert(Str::ever(name), params);
|
||||
}
|
||||
}
|
||||
|
||||
/// FIXME: トレイトの汎化型を指定するのにも使っているので、この名前は適当でない
|
||||
pub(crate) fn register_superclass(&mut self, sup: Type, sup_ctx: &Context) {
|
||||
self.super_classes.push(sup);
|
||||
self.super_classes.extend(sup_ctx.super_classes.clone());
|
||||
self.super_traits.extend(sup_ctx.super_traits.clone());
|
||||
unique_in_place(&mut self.super_classes);
|
||||
unique_in_place(&mut self.super_traits);
|
||||
}
|
||||
|
||||
pub(crate) fn register_supertrait(&mut self, sup: Type, sup_ctx: &Context) {
|
||||
self.super_traits.push(sup);
|
||||
self.super_traits.extend(sup_ctx.super_traits.clone());
|
||||
unique_in_place(&mut self.super_traits);
|
||||
}
|
||||
|
||||
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, py_name);
|
||||
} else {
|
||||
self.register_poly_type(t, ctx, vis, muty, py_name);
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
panic!("{} has already been registered as const", t.local_name());
|
||||
} else {
|
||||
let name = VarName::from_str(t.local_name());
|
||||
let meta_t = match ctx.kind {
|
||||
ContextKind::Class => Type::ClassType,
|
||||
ContextKind::Trait => Type::TraitType,
|
||||
_ => Type::Type,
|
||||
};
|
||||
self.locals.insert(
|
||||
name.clone(),
|
||||
VarInfo::new(
|
||||
meta_t,
|
||||
muty,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
None,
|
||||
py_name.map(Str::ever),
|
||||
AbsLocation::unknown(),
|
||||
),
|
||||
);
|
||||
self.consts
|
||||
.insert(name.clone(), ValueObj::builtin_t(t.clone()));
|
||||
for impl_trait in ctx.super_traits.iter() {
|
||||
if let Some(impls) = self.trait_impls.get_mut(&impl_trait.qual_name()) {
|
||||
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
|
||||
} else {
|
||||
self.trait_impls.insert(
|
||||
impl_trait.qual_name(),
|
||||
set![TraitImpl::new(t.clone(), impl_trait.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
for (trait_method, vi) in ctx.decls.iter() {
|
||||
if let Some(types) = self.method_to_traits.get_mut(trait_method.inspect()) {
|
||||
types.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_traits.insert(
|
||||
trait_method.inspect().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(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_classes.insert(
|
||||
class_method.inspect().clone(),
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
self.mono_types.insert(name, (t, ctx));
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: MethodDefsと再代入は違う
|
||||
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));
|
||||
} else {
|
||||
let name = VarName::from_str(t.local_name());
|
||||
let meta_t = match ctx.kind {
|
||||
ContextKind::Class => Type::ClassType,
|
||||
ContextKind::Trait => Type::TraitType,
|
||||
_ => Type::Type,
|
||||
};
|
||||
if !cfg!(feature = "py_compatible") {
|
||||
self.locals.insert(
|
||||
name.clone(),
|
||||
VarInfo::new(
|
||||
meta_t,
|
||||
muty,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
None,
|
||||
py_name.map(Str::ever),
|
||||
AbsLocation::unknown(),
|
||||
),
|
||||
);
|
||||
}
|
||||
self.consts
|
||||
.insert(name.clone(), ValueObj::builtin_t(t.clone()));
|
||||
for impl_trait in ctx.super_traits.iter() {
|
||||
if let Some(impls) = self.trait_impls.get_mut(&impl_trait.qual_name()) {
|
||||
impls.insert(TraitImpl::new(t.clone(), impl_trait.clone()));
|
||||
} else {
|
||||
self.trait_impls.insert(
|
||||
impl_trait.qual_name(),
|
||||
set![TraitImpl::new(t.clone(), impl_trait.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
for (trait_method, vi) in ctx.decls.iter() {
|
||||
if let Some(traits) = self.method_to_traits.get_mut(trait_method.inspect()) {
|
||||
traits.push(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_traits.insert(
|
||||
trait_method.inspect().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(MethodInfo::new(t.clone(), vi.clone()));
|
||||
} else {
|
||||
self.method_to_classes.insert(
|
||||
class_method.inspect().clone(),
|
||||
vec![MethodInfo::new(t.clone(), vi.clone())],
|
||||
);
|
||||
}
|
||||
}
|
||||
self.poly_types.insert(name, (t, ctx));
|
||||
}
|
||||
}
|
||||
|
||||
fn register_builtin_patch(
|
||||
&mut self,
|
||||
name: &'static str,
|
||||
ctx: Self,
|
||||
vis: Visibility,
|
||||
muty: Mutability,
|
||||
) {
|
||||
if self.patches.contains_key(name) {
|
||||
panic!("{} has already been registered", name);
|
||||
} else {
|
||||
let name = VarName::from_static(name);
|
||||
let vi = VarInfo::new(
|
||||
Patch,
|
||||
muty,
|
||||
vis,
|
||||
Builtin,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
AbsLocation::unknown(),
|
||||
);
|
||||
self.locals.insert(name.clone(), vi);
|
||||
for method_name in ctx.locals.keys() {
|
||||
if let Some(patches) = self.method_impl_patches.get_mut(method_name) {
|
||||
patches.push(name.clone());
|
||||
} else {
|
||||
self.method_impl_patches
|
||||
.insert(method_name.clone(), vec![name.clone()]);
|
||||
}
|
||||
}
|
||||
if let ContextKind::GluePatch(tr_inst) = &ctx.kind {
|
||||
if let Some(impls) = self.trait_impls.get_mut(&tr_inst.sup_trait.qual_name()) {
|
||||
impls.insert(tr_inst.clone());
|
||||
} else {
|
||||
self.trait_impls
|
||||
.insert(tr_inst.sup_trait.qual_name(), set![tr_inst.clone()]);
|
||||
}
|
||||
}
|
||||
self.patches.insert(name, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
fn init_builtin_consts(&mut self) {
|
||||
let vis = if cfg!(feature = "py_compatible") {
|
||||
Public
|
||||
} else {
|
||||
Private
|
||||
};
|
||||
// TODO: this is not a const, but a special property
|
||||
self.register_builtin_py_impl(
|
||||
FUNDAMENTAL_NAME,
|
||||
Str,
|
||||
Immutable,
|
||||
vis,
|
||||
Some(FUNDAMENTAL_NAME),
|
||||
);
|
||||
self.register_builtin_py_impl(
|
||||
LICENSE,
|
||||
mono(SITEBUILTINS_PRINTER),
|
||||
Immutable,
|
||||
vis,
|
||||
Some(LICENSE),
|
||||
);
|
||||
self.register_builtin_py_impl(
|
||||
CREDITS,
|
||||
mono(SITEBUILTINS_PRINTER),
|
||||
Immutable,
|
||||
vis,
|
||||
Some(CREDITS),
|
||||
);
|
||||
self.register_builtin_py_impl(
|
||||
COPYRIGHT,
|
||||
mono(SITEBUILTINS_PRINTER),
|
||||
Immutable,
|
||||
vis,
|
||||
Some(COPYRIGHT),
|
||||
);
|
||||
self.register_builtin_py_impl(TRUE, Bool, Const, Private, Some(TRUE));
|
||||
self.register_builtin_py_impl(FALSE, Bool, Const, Private, Some(FALSE));
|
||||
self.register_builtin_py_impl(NONE, NoneType, Const, Private, Some(NONE));
|
||||
self.register_builtin_py_impl(
|
||||
NOT_IMPLEMENTED,
|
||||
NotImplementedType,
|
||||
Const,
|
||||
Private,
|
||||
Some(NOT_IMPLEMENTED),
|
||||
);
|
||||
self.register_builtin_py_impl(ELLIPSIS, Ellipsis, Const, Private, Some(ELLIPSIS));
|
||||
}
|
||||
|
||||
pub(crate) fn init_builtins(cfg: ErgConfig, mod_cache: &SharedModuleCache) {
|
||||
let mut ctx = Context::builtin_module("<builtins>", cfg, 100);
|
||||
ctx.init_builtin_consts();
|
||||
ctx.init_builtin_funcs();
|
||||
ctx.init_builtin_const_funcs();
|
||||
ctx.init_builtin_procs();
|
||||
ctx.init_builtin_operators();
|
||||
ctx.init_builtin_traits();
|
||||
ctx.init_builtin_classes();
|
||||
ctx.init_builtin_patches();
|
||||
let module = ModuleContext::new(ctx, dict! {});
|
||||
mod_cache.register(PathBuf::from("<builtins>"), None, module);
|
||||
}
|
||||
|
||||
pub fn new_module<S: Into<Str>>(
|
||||
name: S,
|
||||
cfg: ErgConfig,
|
||||
shared: SharedCompilerResource,
|
||||
) -> Self {
|
||||
Context::new(
|
||||
name.into(),
|
||||
cfg,
|
||||
ContextKind::Module,
|
||||
vec![],
|
||||
None,
|
||||
Some(shared),
|
||||
Context::TOP_LEVEL,
|
||||
)
|
||||
}
|
||||
}
|
80
crates/erg_compiler/context/initialize/patches.rs
Normal file
80
crates/erg_compiler/context/initialize/patches.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::vis::Visibility;
|
||||
|
||||
use crate::ty::constructors::*;
|
||||
use crate::ty::typaram::TyParam;
|
||||
use crate::ty::value::ValueObj;
|
||||
use crate::ty::Type;
|
||||
use Type::*;
|
||||
|
||||
use crate::context::initialize::*;
|
||||
use crate::context::Context;
|
||||
use crate::varinfo::Mutability;
|
||||
use Mutability::*;
|
||||
use Visibility::*;
|
||||
|
||||
impl Context {
|
||||
pub(super) fn init_builtin_patches(&mut self) {
|
||||
let m = mono_q_tp("M", instanceof(Int));
|
||||
let n = mono_q_tp("N", instanceof(Int));
|
||||
let o = mono_q_tp("O", instanceof(Int));
|
||||
let p = mono_q_tp("P", instanceof(Int));
|
||||
let params = vec![
|
||||
PS::named_nd("M", Int),
|
||||
PS::named_nd("N", Int),
|
||||
PS::named_nd("O", Int),
|
||||
PS::named_nd("P", Int),
|
||||
];
|
||||
let class = Type::from(&m..=&n);
|
||||
let impls = poly("Add", vec![TyParam::from(&o..=&p)]);
|
||||
// Interval is a bounding patch connecting M..N and (Add(O..P, M+O..N..P), Sub(O..P, M-P..N-O))
|
||||
let mut interval =
|
||||
Self::builtin_poly_glue_patch("Interval", class.clone(), impls.clone(), params, 2);
|
||||
let op_t = fn1_met(
|
||||
class.clone(),
|
||||
Type::from(&o..=&p),
|
||||
Type::from(m.clone() + o.clone()..=n.clone() + p.clone()),
|
||||
)
|
||||
.quantify();
|
||||
let mut interval_add = Self::builtin_methods(Some(impls), 2);
|
||||
interval_add.register_builtin_erg_impl("__add__", op_t, Const, Public);
|
||||
interval_add.register_builtin_const(
|
||||
"Output",
|
||||
Public,
|
||||
ValueObj::builtin_t(Type::from(m.clone() + o.clone()..=n.clone() + p.clone())),
|
||||
);
|
||||
interval.register_trait(class.clone(), interval_add);
|
||||
let mut interval_sub =
|
||||
Self::builtin_methods(Some(poly("Sub", vec![TyParam::from(&o..=&p)])), 2);
|
||||
let op_t = fn1_met(
|
||||
class.clone(),
|
||||
Type::from(&o..=&p),
|
||||
Type::from(m.clone() - p.clone()..=n.clone() - o.clone()),
|
||||
)
|
||||
.quantify();
|
||||
interval_sub.register_builtin_erg_impl("__sub__", op_t, Const, Public);
|
||||
interval_sub.register_builtin_const(
|
||||
"Output",
|
||||
Public,
|
||||
ValueObj::builtin_t(Type::from(m - p..=n - o)),
|
||||
);
|
||||
interval.register_trait(class, interval_sub);
|
||||
self.register_builtin_patch("Interval", interval, Private, Const);
|
||||
// eq.register_impl("__ne__", op_t, Const, Public);
|
||||
// ord.register_impl("__le__", op_t.clone(), Const, Public);
|
||||
// ord.register_impl("__gt__", op_t.clone(), Const, Public);
|
||||
// ord.register_impl("__ge__", op_t, Const, Public);
|
||||
let E = mono_q("E", subtypeof(mono("Eq")));
|
||||
let base = or(E, NoneType);
|
||||
let impls = mono("Eq");
|
||||
let params = vec![PS::named_nd("E", Type)];
|
||||
let mut option_eq =
|
||||
Self::builtin_poly_glue_patch("OptionEq", base.clone(), impls.clone(), params, 1);
|
||||
let mut option_eq_impl = Self::builtin_methods(Some(impls), 1);
|
||||
let op_t = fn1_met(base.clone(), base.clone(), Bool).quantify();
|
||||
option_eq_impl.register_builtin_erg_impl("__eq__", op_t, Const, Public);
|
||||
option_eq.register_trait(base, option_eq_impl);
|
||||
self.register_builtin_patch("OptionEq", option_eq, Private, Const);
|
||||
}
|
||||
}
|
151
crates/erg_compiler/context/initialize/procs.rs
Normal file
151
crates/erg_compiler/context/initialize/procs.rs
Normal file
|
@ -0,0 +1,151 @@
|
|||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::vis::Visibility;
|
||||
|
||||
use crate::ty::constructors::*;
|
||||
use crate::ty::typaram::TyParam;
|
||||
use crate::ty::Type;
|
||||
use Type::*;
|
||||
|
||||
use crate::context::initialize::*;
|
||||
use crate::context::Context;
|
||||
use crate::varinfo::Mutability;
|
||||
use Mutability::*;
|
||||
use Visibility::*;
|
||||
|
||||
impl Context {
|
||||
pub(super) fn init_builtin_procs(&mut self) {
|
||||
let vis = if cfg!(feature = "py_compatible") {
|
||||
Public
|
||||
} else {
|
||||
Private
|
||||
};
|
||||
let T = mono_q("T", instanceof(Type));
|
||||
let U = mono_q("U", instanceof(Type));
|
||||
let t_dir = proc(
|
||||
vec![kw("obj", ref_(Obj))],
|
||||
None,
|
||||
vec![],
|
||||
array_t(Str, TyParam::erased(Nat)),
|
||||
);
|
||||
let t_print = proc(
|
||||
vec![],
|
||||
Some(kw("objects", ref_(Obj))),
|
||||
vec![
|
||||
kw("sep", Str),
|
||||
kw("end", Str),
|
||||
kw("file", mono("Writable!")),
|
||||
kw("flush", Bool),
|
||||
],
|
||||
NoneType,
|
||||
);
|
||||
let t_id = nd_func(vec![kw("old", Obj)], None, Nat);
|
||||
let t_input = proc(vec![], None, vec![kw("msg", Str)], Str);
|
||||
let t_if = proc(
|
||||
vec![
|
||||
kw("cond", Bool),
|
||||
kw("then", nd_proc(vec![], None, T.clone())),
|
||||
],
|
||||
None,
|
||||
vec![kw_default(
|
||||
"else",
|
||||
nd_proc(vec![], None, U.clone()),
|
||||
nd_proc(vec![], None, NoneType),
|
||||
)],
|
||||
or(T.clone(), U.clone()),
|
||||
)
|
||||
.quantify();
|
||||
let t_for = nd_proc(
|
||||
vec![
|
||||
kw("iterable", poly("Iterable", vec![ty_tp(T.clone())])),
|
||||
kw("proc!", nd_proc(vec![anon(T.clone())], None, NoneType)),
|
||||
],
|
||||
None,
|
||||
NoneType,
|
||||
)
|
||||
.quantify();
|
||||
let t_globals = proc(vec![], None, vec![], dict! { Str => Obj }.into());
|
||||
let t_locals = proc(vec![], None, vec![], dict! { Str => Obj }.into());
|
||||
let t_next = nd_proc(
|
||||
vec![kw(
|
||||
"iterable",
|
||||
ref_mut(poly("Iterable", vec![ty_tp(T.clone())]), None),
|
||||
)],
|
||||
None,
|
||||
T.clone(),
|
||||
)
|
||||
.quantify();
|
||||
let t_cond = if cfg!(feature = "py_compatible") {
|
||||
Bool
|
||||
} else {
|
||||
// not Bool! type because `cond` may be the result of evaluation of a mutable object's method returns Bool.
|
||||
nd_proc(vec![], None, Bool)
|
||||
};
|
||||
let t_while = nd_proc(
|
||||
vec![
|
||||
kw("cond!", t_cond),
|
||||
kw("proc!", nd_proc(vec![], None, NoneType)),
|
||||
],
|
||||
None,
|
||||
NoneType,
|
||||
);
|
||||
let P = mono_q("P", subtypeof(mono("PathLike")));
|
||||
let t_open = proc(
|
||||
vec![kw("file", P)],
|
||||
None,
|
||||
vec![
|
||||
kw("mode", Str),
|
||||
kw("buffering", Int),
|
||||
kw("encoding", or(Str, NoneType)),
|
||||
kw("errors", or(Str, NoneType)),
|
||||
kw("newline", or(Str, NoneType)),
|
||||
kw("closefd", Bool),
|
||||
// param_t("opener", option),
|
||||
],
|
||||
mono("File!"),
|
||||
)
|
||||
.quantify();
|
||||
// TODO: T <: With
|
||||
let t_with = nd_proc(
|
||||
vec![
|
||||
kw("obj", T.clone()),
|
||||
kw("proc!", nd_proc(vec![anon(T)], None, U.clone())),
|
||||
],
|
||||
None,
|
||||
U,
|
||||
)
|
||||
.quantify();
|
||||
self.register_builtin_py_impl("dir!", t_dir, Immutable, vis, Some("dir"));
|
||||
self.register_py_builtin("print!", t_print, Some("print"), 27);
|
||||
self.register_builtin_py_impl("id!", t_id, Immutable, vis, Some("id"));
|
||||
self.register_builtin_py_impl("input!", t_input, Immutable, vis, Some("input"));
|
||||
self.register_builtin_py_impl("globals!", t_globals, Immutable, vis, Some("globals"));
|
||||
self.register_builtin_py_impl("locals!", t_locals, Immutable, vis, Some("locals"));
|
||||
self.register_builtin_py_impl("next!", t_next, Immutable, vis, Some("next"));
|
||||
self.register_py_builtin("open!", t_open, Some("open"), 144);
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
"if"
|
||||
} else {
|
||||
"if__"
|
||||
};
|
||||
self.register_builtin_py_impl("if!", t_if, Immutable, vis, Some(name));
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
"for"
|
||||
} else {
|
||||
"for__"
|
||||
};
|
||||
self.register_builtin_py_impl("for!", t_for, Immutable, vis, Some(name));
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
"while"
|
||||
} else {
|
||||
"while__"
|
||||
};
|
||||
self.register_builtin_py_impl("while!", t_while, Immutable, vis, Some(name));
|
||||
let name = if cfg!(feature = "py_compatible") {
|
||||
"with"
|
||||
} else {
|
||||
"with__"
|
||||
};
|
||||
self.register_builtin_py_impl("with!", t_with, Immutable, vis, Some(name));
|
||||
}
|
||||
}
|
239
crates/erg_compiler/context/initialize/traits.rs
Normal file
239
crates/erg_compiler/context/initialize/traits.rs
Normal file
|
@ -0,0 +1,239 @@
|
|||
#[allow(unused_imports)]
|
||||
use erg_common::log;
|
||||
use erg_common::vis::Visibility;
|
||||
|
||||
use crate::ty::constructors::*;
|
||||
use crate::ty::typaram::TyParam;
|
||||
use crate::ty::value::ValueObj;
|
||||
use crate::ty::Type;
|
||||
use ParamSpec as PS;
|
||||
use Type::*;
|
||||
|
||||
use crate::context::initialize::*;
|
||||
use crate::context::{ConstTemplate, Context, DefaultInfo, ParamSpec};
|
||||
use crate::varinfo::Mutability;
|
||||
use DefaultInfo::*;
|
||||
use Mutability::*;
|
||||
use Visibility::*;
|
||||
|
||||
impl Context {
|
||||
/// see std/prelude.er
|
||||
/// All type boundaries are defined in each subroutine
|
||||
/// `push_subtype_bound`, etc. are used for type boundary determination in user-defined APIs
|
||||
// 型境界はすべて各サブルーチンで定義する
|
||||
// push_subtype_boundなどはユーザー定義APIの型境界決定のために使用する
|
||||
pub(super) fn init_builtin_traits(&mut self) {
|
||||
let vis = if cfg!(feature = "py_compatible") {
|
||||
Public
|
||||
} else {
|
||||
Private
|
||||
};
|
||||
let unpack = Self::builtin_mono_trait(UNPACK, 2);
|
||||
let inheritable_type = Self::builtin_mono_trait(INHERITABLE_TYPE, 2);
|
||||
let named = Self::builtin_mono_trait(NAMED, 2);
|
||||
let mut mutable = Self::builtin_mono_trait(MUTABLE, 2);
|
||||
let Slf = mono_q(SELF, subtypeof(mono(IMMUTIZABLE)));
|
||||
let immut_t = proj(Slf.clone(), IMMUT_TYPE);
|
||||
let f_t = func(vec![kw(KW_OLD, immut_t.clone())], None, vec![], immut_t);
|
||||
let t = pr1_met(ref_mut(Slf, None), f_t, NoneType).quantify();
|
||||
mutable.register_builtin_erg_decl(PROC_UPDATE, t, Public);
|
||||
// REVIEW: Immutatable?
|
||||
let mut immutizable = Self::builtin_mono_trait(IMMUTIZABLE, 2);
|
||||
immutizable.register_superclass(mono(MUTABLE), &mutable);
|
||||
immutizable.register_builtin_erg_decl(IMMUT_TYPE, Type, Public);
|
||||
// REVIEW: Mutatable?
|
||||
let mut mutizable = Self::builtin_mono_trait(MUTIZABLE, 2);
|
||||
mutizable.register_builtin_erg_decl(MUTABLE_MUT_TYPE, Type, Public);
|
||||
let pathlike = Self::builtin_mono_trait(PATH_LIKE, 2);
|
||||
/* Readable */
|
||||
let mut readable = Self::builtin_mono_trait(MUTABLE_READABLE, 2);
|
||||
let Slf = mono_q(SELF, subtypeof(mono(MUTABLE_READABLE)));
|
||||
let t_read = pr_met(ref_mut(Slf, None), vec![], None, vec![kw(KW_N, Int)], Str).quantify();
|
||||
readable.register_builtin_py_decl(PROC_READ, t_read, Public, Some(FUNC_READ));
|
||||
/* Writable */
|
||||
let mut writable = Self::builtin_mono_trait(MUTABLE_WRITABLE, 2);
|
||||
let Slf = mono_q(SELF, subtypeof(mono(MUTABLE_WRITABLE)));
|
||||
let t_write = pr1_kw_met(ref_mut(Slf, None), kw("s", Str), Nat).quantify();
|
||||
writable.register_builtin_py_decl(PROC_WRITE, t_write, Public, Some(FUNC_WRITE));
|
||||
// TODO: Add required methods
|
||||
let mut filelike = Self::builtin_mono_trait(FILE_LIKE, 2);
|
||||
filelike.register_superclass(mono(READABLE), &readable);
|
||||
let mut filelike_mut = Self::builtin_mono_trait(MUTABLE_FILE_LIKE, 2);
|
||||
filelike_mut.register_superclass(mono(FILE_LIKE), &filelike);
|
||||
filelike_mut.register_superclass(mono(MUTABLE_WRITABLE), &writable);
|
||||
/* Show */
|
||||
let mut show = Self::builtin_mono_trait(SHOW, 2);
|
||||
let Slf = mono_q(SELF, subtypeof(mono(SHOW)));
|
||||
let t_show = fn0_met(ref_(Slf), Str).quantify();
|
||||
show.register_builtin_py_decl(TO_STR, t_show, Public, Some(FUNDAMENTAL_STR));
|
||||
/* In */
|
||||
let mut in_ = Self::builtin_poly_trait(IN, vec![PS::t(TY_T, NonDefault)], 2);
|
||||
let params = vec![PS::t(TY_T, NonDefault)];
|
||||
let input = Self::builtin_poly_trait(INPUT, params.clone(), 2);
|
||||
let output = Self::builtin_poly_trait(OUTPUT, params, 2);
|
||||
let T = mono_q(TY_T, instanceof(Type));
|
||||
let I = mono_q(TY_I, subtypeof(poly(IN, vec![ty_tp(T.clone())])));
|
||||
in_.register_superclass(poly(INPUT, vec![ty_tp(T.clone())]), &input);
|
||||
let op_t = fn1_met(T.clone(), I, Bool).quantify();
|
||||
in_.register_builtin_erg_decl(OP_IN, op_t, Public);
|
||||
/* Eq */
|
||||
// Erg does not have a trait equivalent to `PartialEq` in Rust
|
||||
// This means, Erg's `Float` cannot be compared with other `Float`
|
||||
// use `l - r < EPSILON` to check if two floats are almost equal
|
||||
let mut eq = Self::builtin_mono_trait(EQ, 2);
|
||||
let Slf = mono_q(SELF, subtypeof(mono(EQ)));
|
||||
// __eq__: |Self <: Eq| (self: Self, other: Self) -> Bool
|
||||
let op_t = fn1_met(Slf.clone(), Slf, Bool).quantify();
|
||||
eq.register_builtin_erg_decl(OP_EQ, op_t, Public);
|
||||
/* Ord */
|
||||
let mut ord = Self::builtin_mono_trait(ORD, 2);
|
||||
ord.register_superclass(mono(EQ), &eq);
|
||||
let Slf = mono_q(SELF, subtypeof(mono(ORD)));
|
||||
let op_t = fn1_met(Slf.clone(), Slf, or(mono(ORDERING), NoneType)).quantify();
|
||||
ord.register_builtin_erg_decl(OP_CMP, op_t, Public);
|
||||
// FIXME: poly trait
|
||||
/* Num */
|
||||
let num = Self::builtin_mono_trait(NUM, 2);
|
||||
/* vec![
|
||||
poly(ADD, vec![]),
|
||||
poly(SUB, vec![]),
|
||||
poly(MUL, vec![]),
|
||||
], */
|
||||
/* Seq */
|
||||
let mut seq = Self::builtin_poly_trait(SEQ, vec![PS::t(TY_T, NonDefault)], 2);
|
||||
seq.register_superclass(poly(OUTPUT, vec![ty_tp(T.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(SEQ, vec![TyParam::erased(Type)])));
|
||||
let t = fn0_met(Slf.clone(), Nat).quantify();
|
||||
seq.register_builtin_erg_decl(FUNC_LEN, t, Public);
|
||||
let t = fn1_met(Slf, Nat, T.clone()).quantify();
|
||||
// Seq.get: |Self <: Seq(T)| Self.(Nat) -> T
|
||||
seq.register_builtin_erg_decl(FUNC_GET, t, Public);
|
||||
/* Iterable */
|
||||
let mut iterable = Self::builtin_poly_trait(ITERABLE, vec![PS::t(TY_T, NonDefault)], 2);
|
||||
iterable.register_superclass(poly(OUTPUT, vec![ty_tp(T.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(ITERABLE, vec![ty_tp(T.clone())])));
|
||||
let t = fn0_met(Slf.clone(), proj(Slf, ITER)).quantify();
|
||||
iterable.register_builtin_py_decl(FUNC_ITER, t, Public, Some(FUNDAMENTAL_ITER));
|
||||
iterable.register_builtin_erg_decl(ITER, Type, Public);
|
||||
let R = mono_q(TY_R, instanceof(Type));
|
||||
let params = vec![PS::t(TY_R, WithDefault)];
|
||||
let ty_params = vec![ty_tp(R.clone())];
|
||||
/* Num */
|
||||
let mut add = Self::builtin_poly_trait(ADD, params.clone(), 2);
|
||||
// Covariant with `R` (independent of the type of __add__)
|
||||
add.register_superclass(poly(OUTPUT, vec![ty_tp(R.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(ADD, ty_params.clone())));
|
||||
let op_t = fn1_met(Slf.clone(), R.clone(), proj(Slf, OUTPUT)).quantify();
|
||||
add.register_builtin_erg_decl(OP_ADD, op_t, Public);
|
||||
add.register_builtin_erg_decl(OUTPUT, Type, Public);
|
||||
/* Sub */
|
||||
let mut sub = Self::builtin_poly_trait(SUB, params.clone(), 2);
|
||||
sub.register_superclass(poly(OUTPUT, vec![ty_tp(R.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(SUB, ty_params.clone())));
|
||||
let op_t = fn1_met(Slf.clone(), R.clone(), proj(Slf, OUTPUT)).quantify();
|
||||
sub.register_builtin_erg_decl(OP_SUB, op_t, Public);
|
||||
sub.register_builtin_erg_decl(OUTPUT, Type, Public);
|
||||
/* Mul */
|
||||
let mut mul = Self::builtin_poly_trait(MUL, params.clone(), 2);
|
||||
mul.register_superclass(poly(OUTPUT, vec![ty_tp(R.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(MUL, ty_params.clone())));
|
||||
let op_t = fn1_met(Slf.clone(), R.clone(), proj(Slf, OUTPUT)).quantify();
|
||||
mul.register_builtin_erg_decl(OP_MUL, op_t, Public);
|
||||
mul.register_builtin_erg_decl(OUTPUT, Type, Public);
|
||||
/* Div */
|
||||
let mut div = Self::builtin_poly_trait(DIV, params.clone(), 2);
|
||||
div.register_superclass(poly(OUTPUT, vec![ty_tp(R.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(DIV, ty_params.clone())));
|
||||
let op_t = fn1_met(Slf.clone(), R.clone(), proj(Slf, OUTPUT)).quantify();
|
||||
div.register_builtin_erg_decl(OP_DIV, op_t, Public);
|
||||
div.register_builtin_erg_decl(OUTPUT, Type, Public);
|
||||
/* FloorDiv */
|
||||
let mut floor_div = Self::builtin_poly_trait(FLOOR_DIV, params, 2);
|
||||
floor_div.register_superclass(poly(OUTPUT, vec![ty_tp(R.clone())]), &output);
|
||||
let Slf = mono_q(SELF, subtypeof(poly(FLOOR_DIV, ty_params.clone())));
|
||||
let op_t = fn1_met(Slf.clone(), R, proj(Slf.clone(), OUTPUT)).quantify();
|
||||
floor_div.register_builtin_erg_decl(OP_FLOOR_DIV, op_t, Public);
|
||||
floor_div.register_builtin_erg_decl(OUTPUT, Type, Public);
|
||||
self.register_builtin_type(mono(UNPACK), unpack, vis, Const, None);
|
||||
self.register_builtin_type(
|
||||
mono(INHERITABLE_TYPE),
|
||||
inheritable_type,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(mono(NAMED), named, vis, Const, None);
|
||||
self.register_builtin_type(mono(MUTABLE), mutable, vis, Const, None);
|
||||
self.register_builtin_type(mono(IMMUTIZABLE), immutizable, vis, Const, None);
|
||||
self.register_builtin_type(mono(MUTIZABLE), mutizable, vis, Const, None);
|
||||
self.register_builtin_type(mono(PATH_LIKE), pathlike, vis, Const, None);
|
||||
self.register_builtin_type(
|
||||
mono(MUTABLE_READABLE),
|
||||
readable,
|
||||
Private,
|
||||
Const,
|
||||
Some(READABLE),
|
||||
);
|
||||
self.register_builtin_type(
|
||||
mono(MUTABLE_WRITABLE),
|
||||
writable,
|
||||
Private,
|
||||
Const,
|
||||
Some(WRITABLE),
|
||||
);
|
||||
self.register_builtin_type(mono(FILE_LIKE), filelike, vis, Const, None);
|
||||
self.register_builtin_type(mono(MUTABLE_FILE_LIKE), filelike_mut, vis, Const, None);
|
||||
self.register_builtin_type(mono(SHOW), show, vis, Const, None);
|
||||
self.register_builtin_type(
|
||||
poly(INPUT, vec![ty_tp(T.clone())]),
|
||||
input,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(
|
||||
poly(OUTPUT, vec![ty_tp(T.clone())]),
|
||||
output,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(poly(IN, vec![ty_tp(T.clone())]), in_, Private, Const, None);
|
||||
self.register_builtin_type(mono(EQ), eq, vis, Const, None);
|
||||
self.register_builtin_type(mono(ORD), ord, vis, Const, None);
|
||||
self.register_builtin_type(mono(NUM), num, vis, Const, None);
|
||||
self.register_builtin_type(poly(SEQ, vec![ty_tp(T.clone())]), seq, Private, Const, None);
|
||||
self.register_builtin_type(
|
||||
poly(ITERABLE, vec![ty_tp(T)]),
|
||||
iterable,
|
||||
Private,
|
||||
Const,
|
||||
None,
|
||||
);
|
||||
self.register_builtin_type(poly(ADD, ty_params.clone()), add, vis, Const, None);
|
||||
self.register_builtin_type(poly(SUB, ty_params.clone()), sub, vis, Const, None);
|
||||
self.register_builtin_type(poly(MUL, ty_params.clone()), mul, vis, Const, None);
|
||||
self.register_builtin_type(poly(DIV, ty_params.clone()), div, vis, Const, None);
|
||||
self.register_builtin_type(poly(FLOOR_DIV, ty_params), floor_div, vis, Const, None);
|
||||
self.register_const_param_defaults(
|
||||
ADD,
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_t(Slf.clone()))],
|
||||
);
|
||||
self.register_const_param_defaults(
|
||||
SUB,
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_t(Slf.clone()))],
|
||||
);
|
||||
self.register_const_param_defaults(
|
||||
MUL,
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_t(Slf.clone()))],
|
||||
);
|
||||
self.register_const_param_defaults(
|
||||
DIV,
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_t(Slf.clone()))],
|
||||
);
|
||||
self.register_const_param_defaults(
|
||||
FLOOR_DIV,
|
||||
vec![ConstTemplate::Obj(ValueObj::builtin_t(Slf))],
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue