Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/seq.c@15:94ca5c8003e5, 2016-04-14 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 14 22:32:57 2016 +0000
- Revision:
- 15:94ca5c8003e5
- Parent:
- 0:65f1469d6bfb
update Nucleo-F401RE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2006 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #undef __FILE_ID__ |
va009039 | 0:65f1469d6bfb | 10 | #define __FILE_ID__ 0x14 |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Sequence |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * Functions that operate on sequences |
va009039 | 0:65f1469d6bfb | 18 | */ |
va009039 | 0:65f1469d6bfb | 19 | |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 22 | |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | /* |
va009039 | 0:65f1469d6bfb | 25 | * Compares two sequence objects |
va009039 | 0:65f1469d6bfb | 26 | * Assumes both objects are of same type (guaranteed by obj_compare) |
va009039 | 0:65f1469d6bfb | 27 | */ |
va009039 | 0:65f1469d6bfb | 28 | int8_t |
va009039 | 0:65f1469d6bfb | 29 | seq_compare(pPmObj_t pobj1, pPmObj_t pobj2) |
va009039 | 0:65f1469d6bfb | 30 | { |
va009039 | 0:65f1469d6bfb | 31 | int16_t l1; |
va009039 | 0:65f1469d6bfb | 32 | int16_t l2; |
va009039 | 0:65f1469d6bfb | 33 | pPmObj_t pa; |
va009039 | 0:65f1469d6bfb | 34 | pPmObj_t pb; |
va009039 | 0:65f1469d6bfb | 35 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 36 | int8_t retcompare; |
va009039 | 0:65f1469d6bfb | 37 | |
va009039 | 0:65f1469d6bfb | 38 | /* Get the lengths of supported types or return differ */ |
va009039 | 0:65f1469d6bfb | 39 | if (OBJ_GET_TYPE(pobj1) == OBJ_TYPE_TUP) |
va009039 | 0:65f1469d6bfb | 40 | { |
va009039 | 0:65f1469d6bfb | 41 | l1 = ((pPmTuple_t)pobj1)->length; |
va009039 | 0:65f1469d6bfb | 42 | l2 = ((pPmTuple_t)pobj2)->length; |
va009039 | 0:65f1469d6bfb | 43 | } |
va009039 | 0:65f1469d6bfb | 44 | else if (OBJ_GET_TYPE(pobj1) == OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 45 | { |
va009039 | 0:65f1469d6bfb | 46 | l1 = ((pPmList_t)pobj1)->length; |
va009039 | 0:65f1469d6bfb | 47 | l2 = ((pPmList_t)pobj2)->length; |
va009039 | 0:65f1469d6bfb | 48 | } |
va009039 | 0:65f1469d6bfb | 49 | |
va009039 | 0:65f1469d6bfb | 50 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 51 | else if (OBJ_GET_TYPE(pobj1) == OBJ_TYPE_BYA) |
va009039 | 0:65f1469d6bfb | 52 | { |
va009039 | 0:65f1469d6bfb | 53 | /* Return if the lengths differ */ |
va009039 | 0:65f1469d6bfb | 54 | l1 = ((pPmBytearray_t)pobj1)->length; |
va009039 | 0:65f1469d6bfb | 55 | l2 = ((pPmBytearray_t)pobj2)->length; |
va009039 | 0:65f1469d6bfb | 56 | if (l1 != l2) |
va009039 | 0:65f1469d6bfb | 57 | { |
va009039 | 0:65f1469d6bfb | 58 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 59 | } |
va009039 | 0:65f1469d6bfb | 60 | |
va009039 | 0:65f1469d6bfb | 61 | return sli_strncmp((char const *)&(((pPmBytes_t)((pPmBytearray_t)pobj1)->val)->val), |
va009039 | 0:65f1469d6bfb | 62 | (char const *)&(((pPmBytes_t)((pPmBytearray_t)pobj2)->val)->val), |
va009039 | 0:65f1469d6bfb | 63 | l1) |
va009039 | 0:65f1469d6bfb | 64 | ? C_DIFFER : C_SAME; |
va009039 | 0:65f1469d6bfb | 65 | } |
va009039 | 0:65f1469d6bfb | 66 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 67 | |
va009039 | 0:65f1469d6bfb | 68 | else |
va009039 | 0:65f1469d6bfb | 69 | { |
va009039 | 0:65f1469d6bfb | 70 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 71 | } |
va009039 | 0:65f1469d6bfb | 72 | |
va009039 | 0:65f1469d6bfb | 73 | /* Return if the lengths differ */ |
va009039 | 0:65f1469d6bfb | 74 | if (l1 != l2) |
va009039 | 0:65f1469d6bfb | 75 | { |
va009039 | 0:65f1469d6bfb | 76 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 77 | } |
va009039 | 0:65f1469d6bfb | 78 | |
va009039 | 0:65f1469d6bfb | 79 | /* Compare all items in the sequences */ |
va009039 | 0:65f1469d6bfb | 80 | while (--l1 >= 0) |
va009039 | 0:65f1469d6bfb | 81 | { |
va009039 | 0:65f1469d6bfb | 82 | retval = seq_getSubscript(pobj1, l1, &pa); |
va009039 | 0:65f1469d6bfb | 83 | if (retval != PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 84 | { |
va009039 | 0:65f1469d6bfb | 85 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 86 | } |
va009039 | 0:65f1469d6bfb | 87 | retval = seq_getSubscript(pobj2, l1, &pb); |
va009039 | 0:65f1469d6bfb | 88 | if (retval != PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 89 | { |
va009039 | 0:65f1469d6bfb | 90 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 91 | } |
va009039 | 0:65f1469d6bfb | 92 | retcompare = obj_compare(pa, pb); |
va009039 | 0:65f1469d6bfb | 93 | if (retcompare != C_SAME) |
va009039 | 0:65f1469d6bfb | 94 | { |
va009039 | 0:65f1469d6bfb | 95 | return retcompare; |
va009039 | 0:65f1469d6bfb | 96 | } |
va009039 | 0:65f1469d6bfb | 97 | } |
va009039 | 0:65f1469d6bfb | 98 | |
va009039 | 0:65f1469d6bfb | 99 | return C_SAME; |
va009039 | 0:65f1469d6bfb | 100 | } |
va009039 | 0:65f1469d6bfb | 101 | |
va009039 | 0:65f1469d6bfb | 102 | |
va009039 | 0:65f1469d6bfb | 103 | /* Returns the length of the sequence */ |
va009039 | 0:65f1469d6bfb | 104 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 105 | seq_getLength(pPmObj_t pobj, uint16_t *r_index) |
va009039 | 0:65f1469d6bfb | 106 | { |
va009039 | 0:65f1469d6bfb | 107 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 108 | |
va009039 | 0:65f1469d6bfb | 109 | switch (OBJ_GET_TYPE(pobj)) |
va009039 | 0:65f1469d6bfb | 110 | { |
va009039 | 0:65f1469d6bfb | 111 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 112 | *r_index = ((pPmString_t)pobj)->length; |
va009039 | 0:65f1469d6bfb | 113 | break; |
va009039 | 0:65f1469d6bfb | 114 | |
va009039 | 0:65f1469d6bfb | 115 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 116 | *r_index = ((pPmTuple_t)pobj)->length; |
va009039 | 0:65f1469d6bfb | 117 | break; |
va009039 | 0:65f1469d6bfb | 118 | |
va009039 | 0:65f1469d6bfb | 119 | case OBJ_TYPE_LST: |
va009039 | 0:65f1469d6bfb | 120 | *r_index = ((pPmList_t)pobj)->length; |
va009039 | 0:65f1469d6bfb | 121 | break; |
va009039 | 0:65f1469d6bfb | 122 | |
va009039 | 0:65f1469d6bfb | 123 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 124 | case OBJ_TYPE_BYA: |
va009039 | 0:65f1469d6bfb | 125 | *r_index = ((pPmBytearray_t)pobj)->length; |
va009039 | 0:65f1469d6bfb | 126 | break; |
va009039 | 0:65f1469d6bfb | 127 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 128 | |
va009039 | 0:65f1469d6bfb | 129 | case OBJ_TYPE_DIC: |
va009039 | 0:65f1469d6bfb | 130 | *r_index = ((pPmDict_t)pobj)->length; |
va009039 | 0:65f1469d6bfb | 131 | break; |
va009039 | 0:65f1469d6bfb | 132 | |
va009039 | 0:65f1469d6bfb | 133 | default: |
va009039 | 0:65f1469d6bfb | 134 | /* Raise TypeError, non-sequence object */ |
va009039 | 0:65f1469d6bfb | 135 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 136 | break; |
va009039 | 0:65f1469d6bfb | 137 | } |
va009039 | 0:65f1469d6bfb | 138 | |
va009039 | 0:65f1469d6bfb | 139 | return retval; |
va009039 | 0:65f1469d6bfb | 140 | } |
va009039 | 0:65f1469d6bfb | 141 | |
va009039 | 0:65f1469d6bfb | 142 | |
va009039 | 0:65f1469d6bfb | 143 | /* Returns the object sequence[index] */ |
va009039 | 0:65f1469d6bfb | 144 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 145 | seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 146 | { |
va009039 | 0:65f1469d6bfb | 147 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 148 | pSeglist_t pkeys; |
va009039 | 0:65f1469d6bfb | 149 | uint8_t c; |
va009039 | 0:65f1469d6bfb | 150 | |
va009039 | 0:65f1469d6bfb | 151 | switch (OBJ_GET_TYPE(pobj)) |
va009039 | 0:65f1469d6bfb | 152 | { |
va009039 | 0:65f1469d6bfb | 153 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 154 | /* Adjust for negative index */ |
va009039 | 0:65f1469d6bfb | 155 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 156 | { |
va009039 | 0:65f1469d6bfb | 157 | index += ((pPmString_t)pobj)->length; |
va009039 | 0:65f1469d6bfb | 158 | } |
va009039 | 0:65f1469d6bfb | 159 | |
va009039 | 0:65f1469d6bfb | 160 | /* Raise IndexError if index is out of bounds */ |
va009039 | 0:65f1469d6bfb | 161 | if ((index < 0) || (index > ((pPmString_t)pobj)->length)) |
va009039 | 0:65f1469d6bfb | 162 | { |
va009039 | 0:65f1469d6bfb | 163 | PM_RAISE(retval, PM_RET_EX_INDX); |
va009039 | 0:65f1469d6bfb | 164 | break; |
va009039 | 0:65f1469d6bfb | 165 | } |
va009039 | 0:65f1469d6bfb | 166 | |
va009039 | 0:65f1469d6bfb | 167 | /* Get the character from the string */ |
va009039 | 0:65f1469d6bfb | 168 | c = ((pPmString_t)pobj)->val[index]; |
va009039 | 0:65f1469d6bfb | 169 | |
va009039 | 0:65f1469d6bfb | 170 | /* Create a new string from the character */ |
va009039 | 0:65f1469d6bfb | 171 | retval = string_newFromChar(c, r_pobj); |
va009039 | 0:65f1469d6bfb | 172 | break; |
va009039 | 0:65f1469d6bfb | 173 | |
va009039 | 0:65f1469d6bfb | 174 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 175 | /* Get the tuple item */ |
va009039 | 0:65f1469d6bfb | 176 | retval = tuple_getItem(pobj, index, r_pobj); |
va009039 | 0:65f1469d6bfb | 177 | break; |
va009039 | 0:65f1469d6bfb | 178 | |
va009039 | 0:65f1469d6bfb | 179 | case OBJ_TYPE_LST: |
va009039 | 0:65f1469d6bfb | 180 | /* Get the list item */ |
va009039 | 0:65f1469d6bfb | 181 | retval = list_getItem(pobj, index, r_pobj); |
va009039 | 0:65f1469d6bfb | 182 | break; |
va009039 | 0:65f1469d6bfb | 183 | |
va009039 | 0:65f1469d6bfb | 184 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 185 | case OBJ_TYPE_BYA: |
va009039 | 0:65f1469d6bfb | 186 | retval = bytearray_getItem(pobj, index, r_pobj); |
va009039 | 0:65f1469d6bfb | 187 | break; |
va009039 | 0:65f1469d6bfb | 188 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 189 | |
va009039 | 0:65f1469d6bfb | 190 | /* Issue #176 Add support to iterate over keys in a dict */ |
va009039 | 0:65f1469d6bfb | 191 | case OBJ_TYPE_DIC: |
va009039 | 0:65f1469d6bfb | 192 | pkeys = ((pPmDict_t)pobj)->d_keys; |
va009039 | 0:65f1469d6bfb | 193 | retval = seglist_getItem(pkeys, index, r_pobj); |
va009039 | 0:65f1469d6bfb | 194 | break; |
va009039 | 0:65f1469d6bfb | 195 | |
va009039 | 0:65f1469d6bfb | 196 | default: |
va009039 | 0:65f1469d6bfb | 197 | /* Raise TypeError, unsubscriptable object */ |
va009039 | 0:65f1469d6bfb | 198 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 199 | break; |
va009039 | 0:65f1469d6bfb | 200 | } |
va009039 | 0:65f1469d6bfb | 201 | |
va009039 | 0:65f1469d6bfb | 202 | return retval; |
va009039 | 0:65f1469d6bfb | 203 | } |
va009039 | 0:65f1469d6bfb | 204 | |
va009039 | 0:65f1469d6bfb | 205 | |
va009039 | 0:65f1469d6bfb | 206 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 207 | seqiter_getNext(pPmObj_t pobj, pPmObj_t *r_pitem) |
va009039 | 0:65f1469d6bfb | 208 | { |
va009039 | 0:65f1469d6bfb | 209 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 210 | uint16_t length; |
va009039 | 0:65f1469d6bfb | 211 | |
va009039 | 0:65f1469d6bfb | 212 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 213 | C_ASSERT(*r_pitem != C_NULL); |
va009039 | 0:65f1469d6bfb | 214 | C_ASSERT(OBJ_GET_TYPE(pobj) == OBJ_TYPE_SQI); |
va009039 | 0:65f1469d6bfb | 215 | |
va009039 | 0:65f1469d6bfb | 216 | /* |
va009039 | 0:65f1469d6bfb | 217 | * Raise TypeError if sequence iterator's object is not a sequence |
va009039 | 0:65f1469d6bfb | 218 | * otherwise, the get sequence's length |
va009039 | 0:65f1469d6bfb | 219 | */ |
va009039 | 0:65f1469d6bfb | 220 | retval = seq_getLength(((pPmSeqIter_t)pobj)->si_sequence, &length); |
va009039 | 0:65f1469d6bfb | 221 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 222 | |
va009039 | 0:65f1469d6bfb | 223 | /* Raise StopIteration if at the end of the sequence */ |
va009039 | 0:65f1469d6bfb | 224 | if (((pPmSeqIter_t)pobj)->si_index == length) |
va009039 | 0:65f1469d6bfb | 225 | { |
va009039 | 0:65f1469d6bfb | 226 | /* Make null the pointer to the sequence */ |
va009039 | 0:65f1469d6bfb | 227 | ((pPmSeqIter_t)pobj)->si_sequence = C_NULL; |
va009039 | 0:65f1469d6bfb | 228 | PM_RAISE(retval, PM_RET_EX_STOP); |
va009039 | 0:65f1469d6bfb | 229 | return retval; |
va009039 | 0:65f1469d6bfb | 230 | } |
va009039 | 0:65f1469d6bfb | 231 | |
va009039 | 0:65f1469d6bfb | 232 | /* Get the item at the current index */ |
va009039 | 0:65f1469d6bfb | 233 | retval = seq_getSubscript(((pPmSeqIter_t)pobj)->si_sequence, |
va009039 | 0:65f1469d6bfb | 234 | ((pPmSeqIter_t)pobj)->si_index, r_pitem); |
va009039 | 0:65f1469d6bfb | 235 | |
va009039 | 0:65f1469d6bfb | 236 | /* Increment the index */ |
va009039 | 0:65f1469d6bfb | 237 | ((pPmSeqIter_t)pobj)->si_index++; |
va009039 | 0:65f1469d6bfb | 238 | |
va009039 | 0:65f1469d6bfb | 239 | return retval; |
va009039 | 0:65f1469d6bfb | 240 | } |
va009039 | 0:65f1469d6bfb | 241 | |
va009039 | 0:65f1469d6bfb | 242 | |
va009039 | 0:65f1469d6bfb | 243 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 244 | seqiter_new(pPmObj_t pobj, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 245 | { |
va009039 | 0:65f1469d6bfb | 246 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 247 | uint8_t *pchunk; |
va009039 | 0:65f1469d6bfb | 248 | pPmSeqIter_t psi; |
va009039 | 0:65f1469d6bfb | 249 | |
va009039 | 0:65f1469d6bfb | 250 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 251 | C_ASSERT(*r_pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 252 | |
va009039 | 0:65f1469d6bfb | 253 | /* Raise a TypeError if pobj is not a sequence */ |
va009039 | 0:65f1469d6bfb | 254 | if ((OBJ_GET_TYPE(pobj) != OBJ_TYPE_STR) |
va009039 | 0:65f1469d6bfb | 255 | && (OBJ_GET_TYPE(pobj) != OBJ_TYPE_TUP) |
va009039 | 0:65f1469d6bfb | 256 | && (OBJ_GET_TYPE(pobj) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 257 | && (OBJ_GET_TYPE(pobj) != OBJ_TYPE_DIC)) |
va009039 | 0:65f1469d6bfb | 258 | { |
va009039 | 0:65f1469d6bfb | 259 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 260 | return retval; |
va009039 | 0:65f1469d6bfb | 261 | } |
va009039 | 0:65f1469d6bfb | 262 | |
va009039 | 0:65f1469d6bfb | 263 | /* Alloc a chunk for the sequence iterator obj */ |
va009039 | 0:65f1469d6bfb | 264 | retval = heap_getChunk(sizeof(PmSeqIter_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 265 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 266 | |
va009039 | 0:65f1469d6bfb | 267 | /* Set the sequence iterator's fields */ |
va009039 | 0:65f1469d6bfb | 268 | psi = (pPmSeqIter_t)pchunk; |
va009039 | 0:65f1469d6bfb | 269 | OBJ_SET_TYPE(psi, OBJ_TYPE_SQI); |
va009039 | 0:65f1469d6bfb | 270 | psi->si_sequence = pobj; |
va009039 | 0:65f1469d6bfb | 271 | psi->si_index = 0; |
va009039 | 0:65f1469d6bfb | 272 | |
va009039 | 0:65f1469d6bfb | 273 | *r_pobj = (pPmObj_t)psi; |
va009039 | 0:65f1469d6bfb | 274 | return retval; |
va009039 | 0:65f1469d6bfb | 275 | } |