feat: add ByteArray!

This commit is contained in:
Shunsuke Shibayama 2023-09-11 21:55:46 +09:00
parent 79b891c32b
commit 68acaf00fb
4 changed files with 120 additions and 28 deletions

View file

@ -6,7 +6,7 @@ use erg_common::log;
use crate::ty::constructors::*;
use crate::ty::typaram::TyParam;
use crate::ty::value::ValueObj;
use crate::ty::{Type, Visibility};
use crate::ty::{IntervalOp, Type, Visibility};
use ParamSpec as PS;
use Type::*;
@ -2463,6 +2463,8 @@ impl Context {
)
.quantify();
array_mut_.register_py_builtin(PROC_PUSH, t, Some(FUNC_APPEND), 15);
let t_copy = pr0_met(ref_(array_mut_t.clone()), array_mut_t.clone()).quantify();
array_mut_.register_py_builtin(FUNC_COPY, t_copy, Some(FUNC_COPY), 116);
let t_extend = pr_met(
ref_mut(
array_mut_t.clone(),
@ -2584,6 +2586,87 @@ impl Context {
Visibility::BUILTIN_PUBLIC,
);
array_mut_.register_trait(array_mut_t.clone(), array_mut_mutable);
/* ByteArray! */
let bytearray_mut_t = mono(BYTEARRAY);
let mut bytearray_mut = Self::builtin_mono_class(BYTEARRAY, 2);
let t_append = pr_met(
ref_mut(bytearray_mut_t.clone(), None),
vec![kw(KW_ELEM, int_interval(IntervalOp::Closed, 0, 255))],
None,
vec![],
NoneType,
);
bytearray_mut.register_builtin_py_impl(
PROC_PUSH,
t_append,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_APPEND),
);
let t_copy = pr0_met(bytearray_mut_t.clone(), bytearray_mut_t.clone());
bytearray_mut.register_builtin_py_impl(
FUNC_COPY,
t_copy,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_COPY),
);
let t_extend = pr_met(
ref_mut(bytearray_mut_t.clone(), None),
vec![kw(
KW_ITERABLE,
poly(
ITERABLE,
vec![ty_tp(int_interval(IntervalOp::Closed, 0, 255))],
),
)],
None,
vec![],
NoneType,
);
bytearray_mut.register_builtin_py_impl(
PROC_EXTEND,
t_extend,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_EXTEND),
);
let t_insert = pr_met(
ref_mut(bytearray_mut_t.clone(), None),
vec![
kw(KW_INDEX, Nat),
kw(KW_ELEM, int_interval(IntervalOp::Closed, 0, 255)),
],
None,
vec![],
NoneType,
);
bytearray_mut.register_builtin_py_impl(
PROC_INSERT,
t_insert,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_INSERT),
);
let t_pop = pr0_met(
ref_mut(bytearray_mut_t.clone(), None),
int_interval(IntervalOp::Closed, 0, 255),
);
bytearray_mut.register_builtin_py_impl(
PROC_POP,
t_pop,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_POP),
);
let t_reverse = pr0_met(ref_mut(bytearray_mut_t.clone(), None), NoneType);
bytearray_mut.register_builtin_py_impl(
PROC_REVERSE,
t_reverse,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_REVERSE),
);
/* Dict! */
let dict_mut_t = poly(MUT_DICT, vec![D.clone()]);
let mut dict_mut =
@ -2918,6 +3001,13 @@ impl Context {
);
self.register_builtin_type(mono(MUT_FILE), file_mut, vis.clone(), Const, Some(FILE));
self.register_builtin_type(array_mut_t, array_mut_, vis.clone(), Const, Some(ARRAY));
self.register_builtin_type(
bytearray_mut_t,
bytearray_mut,
vis.clone(),
Const,
Some(BYTEARRAY),
);
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(

View file

@ -52,11 +52,18 @@ impl Context {
);
let t_bin = nd_func(vec![kw(KW_N, Int)], None, Str);
// TODO: overload: Iterable(Int) -> Bytes
let t_bytes = nd_func(
vec![kw(KW_STR, Str), kw(KW_ENCODING, Str)],
let t_bytes = func(
vec![],
None,
vec![kw(KW_STR, Str), kw(KW_ENCODING, Str)],
mono(BYTES),
);
let t_bytes_array = func(
vec![],
None,
vec![kw(KW_ITERABLE, poly(ITERABLE, vec![ty_tp(Int)]))],
mono(BYTEARRAY),
);
let t_chr = nd_func(
vec![kw(KW_I, Type::from(value(0usize)..=value(1_114_111usize)))],
None,
@ -292,6 +299,13 @@ impl Context {
vis.clone(),
Some(FUNC_BYTES),
);
self.register_builtin_py_impl(
FUNC_BYTEARRAY,
t_bytes_array,
Immutable,
vis.clone(),
Some(FUNC_BYTEARRAY),
);
self.register_builtin_py_impl(FUNC_CHR, t_chr, Immutable, vis.clone(), Some(FUNC_CHR));
self.register_builtin_py_impl(
FUNC_CLASSOF,
@ -516,31 +530,6 @@ impl Context {
Some(FUNC_EXIT),
);
} else {
let t_list = func(
vec![],
None,
vec![kw(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(
FUNC_LIST,
t_list,
Immutable,
vis.clone(),
Some(FUNC_LIST),
);
let t_dict = func(
vec![],
None,
vec![kw(
KW_ITERABLE,
poly(ITERABLE, vec![ty_tp(tuple_t(vec![T.clone(), U.clone()]))]),
)],
dict! { T => U }.into(),
)
.quantify();
self.register_builtin_py_impl(FUNC_DICT, t_dict, Immutable, vis, Some(FUNC_DICT));
self.register_builtin_py_impl(
PYIMPORT,
t_pyimport,

View file

@ -285,6 +285,7 @@ const FUNC_REMOVE: &str = "remove";
const PROC_REMOVE: &str = "remove!";
const FUNC_POP: &str = "pop";
const PROC_POP: &str = "pop!";
const FUNC_COPY: &str = "copy";
const FUNC_CLEAR: &str = "clear";
const PROC_CLEAR: &str = "clear!";
const FUNC_SORT: &str = "sort";
@ -346,6 +347,7 @@ const FUNC_ASCII: &str = "ascii";
const FUNC_ASSERT: &str = "assert";
const FUNC_BIN: &str = "bin";
const FUNC_BYTES: &str = "bytes";
const FUNC_BYTEARRAY: &str = "bytearray";
const FUNC_CHR: &str = "chr";
const FUNC_CLASSOF: &str = "classof";
const FUNC_COMPILE: &str = "compile";