@@ -62,6 +62,61 @@ static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
6262 return NULL ;
6363}
6464
65+ // Don't pass RT_COMPARE_OP_NOT_EQUAL here
66+ static bool list_cmp_helper (int op , mp_obj_t self_in , mp_obj_t another_in ) {
67+ assert (MP_OBJ_IS_TYPE (self_in , & list_type ));
68+ if (!MP_OBJ_IS_TYPE (another_in , & list_type )) {
69+ return false;
70+ }
71+ mp_obj_list_t * self = self_in ;
72+ mp_obj_list_t * another = another_in ;
73+ if (op == RT_COMPARE_OP_EQUAL && self -> len != another -> len ) {
74+ return false;
75+ }
76+
77+ // Let's deal only with > & >=
78+ if (op == RT_COMPARE_OP_LESS || op == RT_COMPARE_OP_LESS_EQUAL ) {
79+ mp_obj_t t = self ;
80+ self = another ;
81+ another = t ;
82+ if (op == RT_COMPARE_OP_LESS ) {
83+ op = RT_COMPARE_OP_MORE ;
84+ } else {
85+ op = RT_COMPARE_OP_MORE_EQUAL ;
86+ }
87+ }
88+
89+ int len = self -> len < another -> len ? self -> len : another -> len ;
90+ bool eq_status = true; // empty lists are equal
91+ bool rel_status ;
92+ for (int i = 0 ; i < len ; i ++ ) {
93+ eq_status = mp_obj_equal (self -> items [i ], another -> items [i ]);
94+ if (op == RT_COMPARE_OP_EQUAL && !eq_status ) {
95+ return false;
96+ }
97+ rel_status = (rt_binary_op (op , self -> items [i ], another -> items [i ]) == mp_const_true );
98+ if (!eq_status && !rel_status ) {
99+ return false;
100+ }
101+ }
102+
103+ // If we had tie in the last element...
104+ if (eq_status ) {
105+ // ... and we have lists of different lengths...
106+ if (self -> len != another -> len ) {
107+ if (self -> len < another -> len ) {
108+ // ... then longer list length wins (we deal only with >)
109+ return false;
110+ }
111+ } else if (op == RT_COMPARE_OP_MORE ) {
112+ // Otherwise, if we have strict relation, equality means failure
113+ return false;
114+ }
115+ }
116+
117+ return true;
118+ }
119+
65120static mp_obj_t list_binary_op (int op , mp_obj_t lhs , mp_obj_t rhs ) {
66121 mp_obj_list_t * o = lhs ;
67122 switch (op ) {
@@ -105,6 +160,15 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
105160 }
106161 return s ;
107162 }
163+ case RT_COMPARE_OP_EQUAL :
164+ case RT_COMPARE_OP_LESS :
165+ case RT_COMPARE_OP_LESS_EQUAL :
166+ case RT_COMPARE_OP_MORE :
167+ case RT_COMPARE_OP_MORE_EQUAL :
168+ return MP_BOOL (list_cmp_helper (op , lhs , rhs ));
169+ case RT_COMPARE_OP_NOT_EQUAL :
170+ return MP_BOOL (!list_cmp_helper (RT_COMPARE_OP_EQUAL , lhs , rhs ));
171+
108172 default :
109173 // op not supported
110174 return NULL ;
0 commit comments