Skip to content

Commit

Permalink
feat: impl Hash for Array
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 18, 2023
1 parent 53a4396 commit 57d3a23
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 19 deletions.
19 changes: 19 additions & 0 deletions crates/erg_compiler/context/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,28 @@ impl Context {
}
}

fn get_value_from_tv_cache(&self, ident: &Identifier) -> Option<ValueObj> {
if let Some(val) = self.tv_cache.as_ref().and_then(|tv| {
tv.get_tyvar(ident.inspect())
.map(|t| ValueObj::builtin_type(t.clone()))
}) {
Some(val)
} else if let Some(TyParam::Value(val)) = self
.tv_cache
.as_ref()
.and_then(|tv| tv.get_typaram(ident.inspect()))
{
Some(val.clone())
} else {
None
}
}

fn eval_const_ident(&self, ident: &Identifier) -> EvalResult<ValueObj> {
if let Some(val) = self.rec_get_const_obj(ident.inspect()) {
Ok(val.clone())
} else if let Some(val) = self.get_value_from_tv_cache(ident) {
Ok(val)
} else if self.kind.is_subr() {
feature_error!(self, ident.loc(), "const parameters")
} else if ident.is_const() {
Expand Down
8 changes: 8 additions & 0 deletions crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,14 @@ impl Context {
Immutable,
Visibility::BUILTIN_PUBLIC,
);
let mut array_hash = Self::builtin_methods(Some(mono(HASH)), 1);
array_hash.register_builtin_erg_impl(
OP_HASH,
fn0_met(mono(GENERIC_ARRAY), Int),
Const,
Visibility::BUILTIN_PUBLIC,
);
generic_array.register_trait(mono(GENERIC_ARRAY), array_hash);
/* Array */
let mut array_ =
Self::builtin_poly_class(ARRAY, vec![PS::t_nd(TY_T), PS::default(TY_N, Nat)], 10);
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Context {
O,
)
.quantify();
let t_nat = nd_func(vec![kw(KW_OBJ, Obj)], None, or(Nat, NoneType));
let t_nat = nd_func(vec![kw(KW_OBJ, Obj)], None, Nat);
// e.g. not(b: Bool!): Bool!
let B = mono_q(TY_B, subtypeof(Bool));
let t_not = nd_func(vec![kw(KW_B, B.clone())], None, B).quantify();
Expand Down
27 changes: 27 additions & 0 deletions crates/erg_compiler/context/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2614,6 +2614,33 @@ impl Context {
}
res
}
ast::Expr::BinOp(bin) => {
self.inc_ref_expr(&bin.args[0], namespace, tmp_tv_cache)
|| self.inc_ref_expr(&bin.args[1], namespace, tmp_tv_cache)
}
ast::Expr::Array(ast::Array::Normal(arr)) => {
let mut res = false;
for val in arr.elems.pos_args().iter() {
if self.inc_ref_expr(&val.expr, namespace, tmp_tv_cache) {
res = true;
}
}
res
}
ast::Expr::Set(ast::Set::Comprehension(comp)) => {
let mut res = false;
for (_, gen) in comp.generators.iter() {
if self.inc_ref_expr(gen, namespace, tmp_tv_cache) {
res = true;
}
}
if let Some(guard) = &comp.guard {
if self.inc_ref_expr(guard, namespace, tmp_tv_cache) {
res = true;
}
}
res
}
other => {
log!(err "inc_ref_expr: {other}");
false
Expand Down
5 changes: 5 additions & 0 deletions crates/erg_compiler/lib/std/_erg_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def __getitem__(self, index_or_slice):
else:
return list.__getitem__(self, index_or_slice)

def __hash__(self):
return hash(tuple(self))

def type_check(self, t: type) -> bool:
if isinstance(t, list):
if len(t) < len(self):
Expand All @@ -59,6 +62,8 @@ def type_check(self, t: type) -> bool:
if not contains_operator(inner_t, elem):
return False
return True
elif isinstance(t, set):
return self in t
elif not hasattr(t, "__args__"):
return isinstance(self, t)
elem_t = t.__args__[0]
Expand Down
20 changes: 4 additions & 16 deletions crates/erg_compiler/lib/std/_erg_convertors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,16 @@


def int__(i):
try:
return Int(i)
except:
return None
return Int(i)


def nat__(i):
try:
return Nat(i)
except:
return None
return Nat(i)


def float__(f):
try:
return Float(f)
except:
return None
return Float(f)


def str__(s):
try:
return Str(s)
except:
return None
return Str(s)
9 changes: 7 additions & 2 deletions crates/erg_compiler/lib/std/_erg_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from _erg_int import IntMut # don't unify with the above line
from _erg_control import then__


class Nat(Int):
def __init__(self, i):
if int(i) < 0:
raise ValueError("Nat can't be negative: {}".format(i))

def try_new(i): # -> Result[Nat]
if i >= 0:
return Nat(i)
else:
return Error("Nat can't be negative")
return Error("Nat can't be negative: {}".format(i))

def times(self, f):
for _ in range(self):
Expand Down Expand Up @@ -38,6 +41,8 @@ class NatMut(IntMut): # and Nat
value: Nat

def __init__(self, n: Nat):
if int(n) < 0:
raise ValueError("Nat can't be negative: {}".format(n))
self.value = n

def __int__(self):
Expand Down

0 comments on commit 57d3a23

Please sign in to comment.