Skip to content

Commit

Permalink
feat: add ByteArray!
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 11, 2023
1 parent 79b891c commit 68acaf0
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 28 deletions.
92 changes: 91 additions & 1 deletion crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down
43 changes: 16 additions & 27 deletions crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions crates/erg_compiler/context/initialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
11 changes: 11 additions & 0 deletions crates/erg_compiler/lib/std.d/Array!.d.er
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,14 @@ array = pyimport "Array"
assert arr == [2, 2]
'''
update_nth!: |T, N: Nat|(self: Array!(T, N), index: Nat, f: T -> T) => NoneType
'''
Return a (deep) copy of the array.
'''
'''erg
arr = ![1, 2]
arr_copy = arr.copy()
arr_copy.push! 3
assert arr_copy == [1, 2, 3]
assert arr == [1, 2]
'''
copy: |T, N: Nat|(self: Ref(Array!(T, N))) => Array!(T, N)

0 comments on commit 68acaf0

Please sign in to comment.