From c913fb28bdf4ed13be4276288081454e10627349 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 19 Jul 2021 13:34:45 +0800 Subject: [PATCH] use signed integer for TSeq --- nac3core/src/typecheck/typedef.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/nac3core/src/typecheck/typedef.rs b/nac3core/src/typecheck/typedef.rs index e5680feb..9c20f918 100644 --- a/nac3core/src/typecheck/typedef.rs +++ b/nac3core/src/typecheck/typedef.rs @@ -81,7 +81,7 @@ pub enum TypeEnum { id: u32, }, TSeq { - map: VarMap, + map: Mapping, }, TTuple { ty: Vec, @@ -274,16 +274,18 @@ impl Unifier { self.set_a_to_b(a, b); } TypeEnum::TTuple { ty: types } => { - let len = types.len() as u32; + let len = types.len() as i32; for (k, v) in map1.iter() { - if *k >= len { + // handle negative index + let ind = if *k < 0 { len + *k } else { *k }; + if ind >= len || ind < 0 { return Err(format!( "Tuple index out of range. (Length: {}, Index: {})", types.len(), k )); } - self.unify(*v, types[*k as usize])?; + self.unify(*v, types[ind as usize])?; } self.set_a_to_b(a, b); } @@ -516,7 +518,7 @@ impl Unifier { TypeEnum::TVar { .. } => { // TODO: occur check for bounds... } - TypeEnum::TSeq { map } | TypeEnum::TObj { params: map, .. } => { + TypeEnum::TSeq { map } => { for t in map.values() { self.occur_check(a, *t)?; } @@ -534,6 +536,11 @@ impl Unifier { self.occur_check(a, *t)?; } } + TypeEnum::TObj { params: map, .. } => { + for t in map.values() { + self.occur_check(a, *t)?; + } + } TypeEnum::TCall { calls } => { for t in calls .iter()