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/list.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 2002 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__ 0x0B |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief List Object Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * List object type operations. |
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 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 25 | list_append(pPmObj_t plist, pPmObj_t pobj) |
va009039 | 0:65f1469d6bfb | 26 | { |
va009039 | 0:65f1469d6bfb | 27 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 28 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 29 | |
va009039 | 0:65f1469d6bfb | 30 | C_ASSERT(plist != C_NULL); |
va009039 | 0:65f1469d6bfb | 31 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 32 | |
va009039 | 0:65f1469d6bfb | 33 | /* If plist is not a list, raise a TypeError exception */ |
va009039 | 0:65f1469d6bfb | 34 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 35 | { |
va009039 | 0:65f1469d6bfb | 36 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 37 | return retval; |
va009039 | 0:65f1469d6bfb | 38 | } |
va009039 | 0:65f1469d6bfb | 39 | |
va009039 | 0:65f1469d6bfb | 40 | /* Create new seglist if needed */ |
va009039 | 0:65f1469d6bfb | 41 | if (((pPmList_t)plist)->length == 0) |
va009039 | 0:65f1469d6bfb | 42 | { |
va009039 | 0:65f1469d6bfb | 43 | retval = seglist_new(&((pPmList_t)plist)->val); |
va009039 | 0:65f1469d6bfb | 44 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 45 | } |
va009039 | 0:65f1469d6bfb | 46 | |
va009039 | 0:65f1469d6bfb | 47 | /* Append object to list */ |
va009039 | 0:65f1469d6bfb | 48 | heap_gcPushTempRoot((pPmObj_t)((pPmList_t)plist)->val, &objid); |
va009039 | 0:65f1469d6bfb | 49 | retval = seglist_appendItem(((pPmList_t)plist)->val, pobj); |
va009039 | 0:65f1469d6bfb | 50 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 51 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 52 | |
va009039 | 0:65f1469d6bfb | 53 | /* Increment list length */ |
va009039 | 0:65f1469d6bfb | 54 | ((pPmList_t)plist)->length++; |
va009039 | 0:65f1469d6bfb | 55 | |
va009039 | 0:65f1469d6bfb | 56 | return retval; |
va009039 | 0:65f1469d6bfb | 57 | } |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | |
va009039 | 0:65f1469d6bfb | 60 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 61 | list_getItem(pPmObj_t plist, int16_t index, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 62 | { |
va009039 | 0:65f1469d6bfb | 63 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 64 | |
va009039 | 0:65f1469d6bfb | 65 | /* If it's not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 66 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 67 | { |
va009039 | 0:65f1469d6bfb | 68 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 69 | return retval; |
va009039 | 0:65f1469d6bfb | 70 | } |
va009039 | 0:65f1469d6bfb | 71 | |
va009039 | 0:65f1469d6bfb | 72 | /* Adjust the index */ |
va009039 | 0:65f1469d6bfb | 73 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 74 | { |
va009039 | 0:65f1469d6bfb | 75 | index += ((pPmList_t)plist)->length; |
va009039 | 0:65f1469d6bfb | 76 | } |
va009039 | 0:65f1469d6bfb | 77 | |
va009039 | 0:65f1469d6bfb | 78 | /* Check the bounds of the index */ |
va009039 | 0:65f1469d6bfb | 79 | if ((index < 0) || (index >= ((pPmList_t)plist)->length)) |
va009039 | 0:65f1469d6bfb | 80 | { |
va009039 | 0:65f1469d6bfb | 81 | PM_RAISE(retval, PM_RET_EX_INDX); |
va009039 | 0:65f1469d6bfb | 82 | return retval; |
va009039 | 0:65f1469d6bfb | 83 | } |
va009039 | 0:65f1469d6bfb | 84 | |
va009039 | 0:65f1469d6bfb | 85 | /* Get item from seglist */ |
va009039 | 0:65f1469d6bfb | 86 | retval = seglist_getItem(((pPmList_t)plist)->val, index, r_pobj); |
va009039 | 0:65f1469d6bfb | 87 | return retval; |
va009039 | 0:65f1469d6bfb | 88 | } |
va009039 | 0:65f1469d6bfb | 89 | |
va009039 | 0:65f1469d6bfb | 90 | |
va009039 | 0:65f1469d6bfb | 91 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 92 | list_insert(pPmObj_t plist, int16_t index, pPmObj_t pobj) |
va009039 | 0:65f1469d6bfb | 93 | { |
va009039 | 0:65f1469d6bfb | 94 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 95 | int16_t len; |
va009039 | 0:65f1469d6bfb | 96 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 97 | |
va009039 | 0:65f1469d6bfb | 98 | C_ASSERT(plist != C_NULL); |
va009039 | 0:65f1469d6bfb | 99 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 100 | |
va009039 | 0:65f1469d6bfb | 101 | /* Raise a TypeError if plist is not a List */ |
va009039 | 0:65f1469d6bfb | 102 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 103 | { |
va009039 | 0:65f1469d6bfb | 104 | retval = PM_RET_EX_TYPE; |
va009039 | 0:65f1469d6bfb | 105 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 106 | } |
va009039 | 0:65f1469d6bfb | 107 | |
va009039 | 0:65f1469d6bfb | 108 | /* Adjust an out-of-bounds index value */ |
va009039 | 0:65f1469d6bfb | 109 | len = ((pPmList_t)plist)->length; |
va009039 | 0:65f1469d6bfb | 110 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 111 | { |
va009039 | 0:65f1469d6bfb | 112 | index += len; |
va009039 | 0:65f1469d6bfb | 113 | } |
va009039 | 0:65f1469d6bfb | 114 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 115 | { |
va009039 | 0:65f1469d6bfb | 116 | index = 0; |
va009039 | 0:65f1469d6bfb | 117 | } |
va009039 | 0:65f1469d6bfb | 118 | if (index > len) |
va009039 | 0:65f1469d6bfb | 119 | { |
va009039 | 0:65f1469d6bfb | 120 | index = len; |
va009039 | 0:65f1469d6bfb | 121 | } |
va009039 | 0:65f1469d6bfb | 122 | |
va009039 | 0:65f1469d6bfb | 123 | /* Create new seglist if needed */ |
va009039 | 0:65f1469d6bfb | 124 | if (((pPmList_t)plist)->length == 0) |
va009039 | 0:65f1469d6bfb | 125 | { |
va009039 | 0:65f1469d6bfb | 126 | retval = seglist_new(&((pPmList_t)plist)->val); |
va009039 | 0:65f1469d6bfb | 127 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 128 | } |
va009039 | 0:65f1469d6bfb | 129 | |
va009039 | 0:65f1469d6bfb | 130 | /* Insert the item in the container */ |
va009039 | 0:65f1469d6bfb | 131 | heap_gcPushTempRoot((pPmObj_t)((pPmList_t)plist)->val, &objid); |
va009039 | 0:65f1469d6bfb | 132 | retval = seglist_insertItem(((pPmList_t)plist)->val, pobj, index); |
va009039 | 0:65f1469d6bfb | 133 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 134 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 135 | |
va009039 | 0:65f1469d6bfb | 136 | /* Increment list length */ |
va009039 | 0:65f1469d6bfb | 137 | ((pPmList_t)plist)->length++; |
va009039 | 0:65f1469d6bfb | 138 | return retval; |
va009039 | 0:65f1469d6bfb | 139 | } |
va009039 | 0:65f1469d6bfb | 140 | |
va009039 | 0:65f1469d6bfb | 141 | |
va009039 | 0:65f1469d6bfb | 142 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 143 | list_new(pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 144 | { |
va009039 | 0:65f1469d6bfb | 145 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 146 | pPmList_t plist = C_NULL; |
va009039 | 0:65f1469d6bfb | 147 | |
va009039 | 0:65f1469d6bfb | 148 | /* Allocate a list */ |
va009039 | 0:65f1469d6bfb | 149 | retval = heap_getChunk(sizeof(PmList_t), (uint8_t **)r_pobj); |
va009039 | 0:65f1469d6bfb | 150 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 151 | |
va009039 | 0:65f1469d6bfb | 152 | /* Set list type, empty the contents */ |
va009039 | 0:65f1469d6bfb | 153 | plist = (pPmList_t)*r_pobj; |
va009039 | 0:65f1469d6bfb | 154 | OBJ_SET_TYPE(plist, OBJ_TYPE_LST); |
va009039 | 0:65f1469d6bfb | 155 | plist->length = 0; |
va009039 | 0:65f1469d6bfb | 156 | plist->val = C_NULL; |
va009039 | 0:65f1469d6bfb | 157 | |
va009039 | 0:65f1469d6bfb | 158 | return retval; |
va009039 | 0:65f1469d6bfb | 159 | } |
va009039 | 0:65f1469d6bfb | 160 | |
va009039 | 0:65f1469d6bfb | 161 | |
va009039 | 0:65f1469d6bfb | 162 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 163 | list_copy(pPmObj_t pobj, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 164 | { |
va009039 | 0:65f1469d6bfb | 165 | return list_replicate(pobj, 1, r_pobj); |
va009039 | 0:65f1469d6bfb | 166 | } |
va009039 | 0:65f1469d6bfb | 167 | |
va009039 | 0:65f1469d6bfb | 168 | |
va009039 | 0:65f1469d6bfb | 169 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 170 | list_replicate(pPmObj_t psrclist, int16_t n, pPmObj_t *r_pnewlist) |
va009039 | 0:65f1469d6bfb | 171 | { |
va009039 | 0:65f1469d6bfb | 172 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 173 | int16_t i = 0; |
va009039 | 0:65f1469d6bfb | 174 | int16_t j = 0; |
va009039 | 0:65f1469d6bfb | 175 | int16_t length = 0; |
va009039 | 0:65f1469d6bfb | 176 | pPmObj_t pitem = C_NULL; |
va009039 | 0:65f1469d6bfb | 177 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 178 | |
va009039 | 0:65f1469d6bfb | 179 | C_ASSERT(psrclist != C_NULL); |
va009039 | 0:65f1469d6bfb | 180 | C_ASSERT(r_pnewlist != C_NULL); |
va009039 | 0:65f1469d6bfb | 181 | |
va009039 | 0:65f1469d6bfb | 182 | /* If first arg is not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 183 | if (OBJ_GET_TYPE(psrclist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 184 | { |
va009039 | 0:65f1469d6bfb | 185 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 186 | return retval; |
va009039 | 0:65f1469d6bfb | 187 | } |
va009039 | 0:65f1469d6bfb | 188 | length = ((pPmList_t)psrclist)->length; |
va009039 | 0:65f1469d6bfb | 189 | |
va009039 | 0:65f1469d6bfb | 190 | /* Allocate new list */ |
va009039 | 0:65f1469d6bfb | 191 | retval = list_new(r_pnewlist); |
va009039 | 0:65f1469d6bfb | 192 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 193 | |
va009039 | 0:65f1469d6bfb | 194 | /* Copy srclist the designated number of times */ |
va009039 | 0:65f1469d6bfb | 195 | for (i = n; i > 0; i--) |
va009039 | 0:65f1469d6bfb | 196 | { |
va009039 | 0:65f1469d6bfb | 197 | /* Iterate over the length of srclist */ |
va009039 | 0:65f1469d6bfb | 198 | for (j = 0; j < length; j++) |
va009039 | 0:65f1469d6bfb | 199 | { |
va009039 | 0:65f1469d6bfb | 200 | retval = list_getItem(psrclist, j, &pitem); |
va009039 | 0:65f1469d6bfb | 201 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 202 | heap_gcPushTempRoot(*r_pnewlist, &objid); |
va009039 | 0:65f1469d6bfb | 203 | retval = list_append(*r_pnewlist, pitem); |
va009039 | 0:65f1469d6bfb | 204 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 205 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 206 | } |
va009039 | 0:65f1469d6bfb | 207 | } |
va009039 | 0:65f1469d6bfb | 208 | return retval; |
va009039 | 0:65f1469d6bfb | 209 | } |
va009039 | 0:65f1469d6bfb | 210 | |
va009039 | 0:65f1469d6bfb | 211 | |
va009039 | 0:65f1469d6bfb | 212 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 213 | list_setItem(pPmObj_t plist, int16_t index, pPmObj_t pobj) |
va009039 | 0:65f1469d6bfb | 214 | { |
va009039 | 0:65f1469d6bfb | 215 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 216 | |
va009039 | 0:65f1469d6bfb | 217 | /* If it's not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 218 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 219 | { |
va009039 | 0:65f1469d6bfb | 220 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 221 | return retval; |
va009039 | 0:65f1469d6bfb | 222 | } |
va009039 | 0:65f1469d6bfb | 223 | |
va009039 | 0:65f1469d6bfb | 224 | /* Adjust the index */ |
va009039 | 0:65f1469d6bfb | 225 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 226 | { |
va009039 | 0:65f1469d6bfb | 227 | index += ((pPmList_t)plist)->length; |
va009039 | 0:65f1469d6bfb | 228 | } |
va009039 | 0:65f1469d6bfb | 229 | |
va009039 | 0:65f1469d6bfb | 230 | /* Check the bounds of the index */ |
va009039 | 0:65f1469d6bfb | 231 | if ((index < 0) || (index >= ((pPmList_t)plist)->length)) |
va009039 | 0:65f1469d6bfb | 232 | { |
va009039 | 0:65f1469d6bfb | 233 | PM_RAISE(retval, PM_RET_EX_INDX); |
va009039 | 0:65f1469d6bfb | 234 | return retval; |
va009039 | 0:65f1469d6bfb | 235 | } |
va009039 | 0:65f1469d6bfb | 236 | |
va009039 | 0:65f1469d6bfb | 237 | /* Set the item */ |
va009039 | 0:65f1469d6bfb | 238 | retval = seglist_setItem(((pPmList_t)plist)->val, pobj, index); |
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 | list_remove(pPmObj_t plist, pPmObj_t item) |
va009039 | 0:65f1469d6bfb | 245 | { |
va009039 | 0:65f1469d6bfb | 246 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 247 | uint16_t index; |
va009039 | 0:65f1469d6bfb | 248 | |
va009039 | 0:65f1469d6bfb | 249 | /* If it's not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 250 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 251 | { |
va009039 | 0:65f1469d6bfb | 252 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 253 | return retval; |
va009039 | 0:65f1469d6bfb | 254 | } |
va009039 | 0:65f1469d6bfb | 255 | |
va009039 | 0:65f1469d6bfb | 256 | /* Locate the item to remove */ |
va009039 | 0:65f1469d6bfb | 257 | retval = list_index(plist, item, &index); |
va009039 | 0:65f1469d6bfb | 258 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 259 | |
va009039 | 0:65f1469d6bfb | 260 | /* Remove the item and decrement the list length */ |
va009039 | 0:65f1469d6bfb | 261 | retval = seglist_removeItem(((pPmList_t)plist)->val, index); |
va009039 | 0:65f1469d6bfb | 262 | ((pPmList_t)plist)->length--; |
va009039 | 0:65f1469d6bfb | 263 | |
va009039 | 0:65f1469d6bfb | 264 | /* Unlink seglist if there are no contents */ |
va009039 | 0:65f1469d6bfb | 265 | if (((pPmList_t)plist)->length == 0) |
va009039 | 0:65f1469d6bfb | 266 | { |
va009039 | 0:65f1469d6bfb | 267 | ((pPmList_t)plist)->val = C_NULL; |
va009039 | 0:65f1469d6bfb | 268 | } |
va009039 | 0:65f1469d6bfb | 269 | |
va009039 | 0:65f1469d6bfb | 270 | return retval; |
va009039 | 0:65f1469d6bfb | 271 | } |
va009039 | 0:65f1469d6bfb | 272 | |
va009039 | 0:65f1469d6bfb | 273 | |
va009039 | 0:65f1469d6bfb | 274 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 275 | list_index(pPmObj_t plist, pPmObj_t pitem, uint16_t *r_index) |
va009039 | 0:65f1469d6bfb | 276 | { |
va009039 | 0:65f1469d6bfb | 277 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 278 | pSeglist_t pseglist; |
va009039 | 0:65f1469d6bfb | 279 | pPmObj_t pobj; |
va009039 | 0:65f1469d6bfb | 280 | uint16_t index; |
va009039 | 0:65f1469d6bfb | 281 | |
va009039 | 0:65f1469d6bfb | 282 | /* If it's not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 283 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 284 | { |
va009039 | 0:65f1469d6bfb | 285 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 286 | return retval; |
va009039 | 0:65f1469d6bfb | 287 | } |
va009039 | 0:65f1469d6bfb | 288 | |
va009039 | 0:65f1469d6bfb | 289 | /* Raise a ValueError if the list is empty */ |
va009039 | 0:65f1469d6bfb | 290 | if (((pPmList_t)plist)->length == 0) |
va009039 | 0:65f1469d6bfb | 291 | { |
va009039 | 0:65f1469d6bfb | 292 | PM_RAISE(retval, PM_RET_EX_VAL); |
va009039 | 0:65f1469d6bfb | 293 | return retval; |
va009039 | 0:65f1469d6bfb | 294 | } |
va009039 | 0:65f1469d6bfb | 295 | |
va009039 | 0:65f1469d6bfb | 296 | pseglist = ((pPmList_t)plist)->val; |
va009039 | 0:65f1469d6bfb | 297 | |
va009039 | 0:65f1469d6bfb | 298 | /* Iterate over the list's contents */ |
va009039 | 0:65f1469d6bfb | 299 | for (index = 0; index < pseglist->sl_length; index++) |
va009039 | 0:65f1469d6bfb | 300 | { |
va009039 | 0:65f1469d6bfb | 301 | retval = seglist_getItem(pseglist, index, &pobj); |
va009039 | 0:65f1469d6bfb | 302 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 303 | |
va009039 | 0:65f1469d6bfb | 304 | /* If the list item matches the given item, return the index */ |
va009039 | 0:65f1469d6bfb | 305 | if (obj_compare(pobj, pitem) == C_SAME) |
va009039 | 0:65f1469d6bfb | 306 | { |
va009039 | 0:65f1469d6bfb | 307 | *r_index = index; |
va009039 | 0:65f1469d6bfb | 308 | return PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 309 | } |
va009039 | 0:65f1469d6bfb | 310 | } |
va009039 | 0:65f1469d6bfb | 311 | |
va009039 | 0:65f1469d6bfb | 312 | return PM_RET_EX_VAL; |
va009039 | 0:65f1469d6bfb | 313 | } |
va009039 | 0:65f1469d6bfb | 314 | |
va009039 | 0:65f1469d6bfb | 315 | |
va009039 | 0:65f1469d6bfb | 316 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 317 | list_delItem(pPmObj_t plist, int16_t index) |
va009039 | 0:65f1469d6bfb | 318 | { |
va009039 | 0:65f1469d6bfb | 319 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 320 | |
va009039 | 0:65f1469d6bfb | 321 | /* If it's not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 322 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 323 | { |
va009039 | 0:65f1469d6bfb | 324 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 325 | return retval; |
va009039 | 0:65f1469d6bfb | 326 | } |
va009039 | 0:65f1469d6bfb | 327 | |
va009039 | 0:65f1469d6bfb | 328 | /* Adjust the index */ |
va009039 | 0:65f1469d6bfb | 329 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 330 | { |
va009039 | 0:65f1469d6bfb | 331 | index += ((pPmList_t)plist)->length; |
va009039 | 0:65f1469d6bfb | 332 | } |
va009039 | 0:65f1469d6bfb | 333 | |
va009039 | 0:65f1469d6bfb | 334 | /* Check the bounds of the index */ |
va009039 | 0:65f1469d6bfb | 335 | if ((index < 0) || (index >= ((pPmList_t)plist)->length)) |
va009039 | 0:65f1469d6bfb | 336 | { |
va009039 | 0:65f1469d6bfb | 337 | PM_RAISE(retval, PM_RET_EX_INDX); |
va009039 | 0:65f1469d6bfb | 338 | return retval; |
va009039 | 0:65f1469d6bfb | 339 | } |
va009039 | 0:65f1469d6bfb | 340 | |
va009039 | 0:65f1469d6bfb | 341 | /* Remove the item and decrement the list length */ |
va009039 | 0:65f1469d6bfb | 342 | retval = seglist_removeItem(((pPmList_t)plist)->val, index); |
va009039 | 0:65f1469d6bfb | 343 | ((pPmList_t)plist)->length--; |
va009039 | 0:65f1469d6bfb | 344 | |
va009039 | 0:65f1469d6bfb | 345 | /* Unlink seglist if there are no contents */ |
va009039 | 0:65f1469d6bfb | 346 | if (((pPmList_t)plist)->length == 0) |
va009039 | 0:65f1469d6bfb | 347 | { |
va009039 | 0:65f1469d6bfb | 348 | ((pPmList_t)plist)->val = C_NULL; |
va009039 | 0:65f1469d6bfb | 349 | } |
va009039 | 0:65f1469d6bfb | 350 | |
va009039 | 0:65f1469d6bfb | 351 | return retval; |
va009039 | 0:65f1469d6bfb | 352 | } |
va009039 | 0:65f1469d6bfb | 353 | |
va009039 | 0:65f1469d6bfb | 354 | |
va009039 | 0:65f1469d6bfb | 355 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 356 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 357 | list_print(pPmObj_t plist) |
va009039 | 0:65f1469d6bfb | 358 | { |
va009039 | 0:65f1469d6bfb | 359 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 360 | int16_t index; |
va009039 | 0:65f1469d6bfb | 361 | pSeglist_t vals; |
va009039 | 0:65f1469d6bfb | 362 | pPmObj_t pobj1; |
va009039 | 0:65f1469d6bfb | 363 | |
va009039 | 0:65f1469d6bfb | 364 | C_ASSERT(plist != C_NULL); |
va009039 | 0:65f1469d6bfb | 365 | |
va009039 | 0:65f1469d6bfb | 366 | /* If it's not a list, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 367 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 368 | { |
va009039 | 0:65f1469d6bfb | 369 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 370 | return retval; |
va009039 | 0:65f1469d6bfb | 371 | } |
va009039 | 0:65f1469d6bfb | 372 | |
va009039 | 0:65f1469d6bfb | 373 | plat_putByte('['); |
va009039 | 0:65f1469d6bfb | 374 | |
va009039 | 0:65f1469d6bfb | 375 | vals = ((pPmList_t)plist)->val; |
va009039 | 0:65f1469d6bfb | 376 | |
va009039 | 0:65f1469d6bfb | 377 | /* Iterate over the list's contents */ |
va009039 | 0:65f1469d6bfb | 378 | for (index = 0; index < ((pPmList_t)plist)->length; index++) |
va009039 | 0:65f1469d6bfb | 379 | { |
va009039 | 0:65f1469d6bfb | 380 | if (index != 0) |
va009039 | 0:65f1469d6bfb | 381 | { |
va009039 | 0:65f1469d6bfb | 382 | plat_putByte(','); |
va009039 | 0:65f1469d6bfb | 383 | plat_putByte(' '); |
va009039 | 0:65f1469d6bfb | 384 | } |
va009039 | 0:65f1469d6bfb | 385 | |
va009039 | 0:65f1469d6bfb | 386 | /* Print each item */ |
va009039 | 0:65f1469d6bfb | 387 | retval = seglist_getItem(vals, index, &pobj1); |
va009039 | 0:65f1469d6bfb | 388 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 389 | retval = obj_print(pobj1, C_FALSE, C_TRUE); |
va009039 | 0:65f1469d6bfb | 390 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 391 | } |
va009039 | 0:65f1469d6bfb | 392 | |
va009039 | 0:65f1469d6bfb | 393 | return plat_putByte(']'); |
va009039 | 0:65f1469d6bfb | 394 | } |
va009039 | 0:65f1469d6bfb | 395 | #endif /* HAVE_PRINT */ |
va009039 | 0:65f1469d6bfb | 396 | |
va009039 | 0:65f1469d6bfb | 397 | |
va009039 | 0:65f1469d6bfb | 398 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 399 | list_clear(pPmObj_t plist) |
va009039 | 0:65f1469d6bfb | 400 | { |
va009039 | 0:65f1469d6bfb | 401 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 402 | |
va009039 | 0:65f1469d6bfb | 403 | C_ASSERT(plist != C_NULL); |
va009039 | 0:65f1469d6bfb | 404 | |
va009039 | 0:65f1469d6bfb | 405 | /* Raise TypeError if arg is not a dict */ |
va009039 | 0:65f1469d6bfb | 406 | if (OBJ_GET_TYPE(plist) != OBJ_TYPE_LST) |
va009039 | 0:65f1469d6bfb | 407 | { |
va009039 | 0:65f1469d6bfb | 408 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 409 | return retval; |
va009039 | 0:65f1469d6bfb | 410 | } |
va009039 | 0:65f1469d6bfb | 411 | |
va009039 | 0:65f1469d6bfb | 412 | /* Clear length and unlink seglist */ |
va009039 | 0:65f1469d6bfb | 413 | ((pPmList_t)plist)->length = 0; |
va009039 | 0:65f1469d6bfb | 414 | ((pPmList_t)plist)->val = C_NULL; |
va009039 | 0:65f1469d6bfb | 415 | |
va009039 | 0:65f1469d6bfb | 416 | return retval; |
va009039 | 0:65f1469d6bfb | 417 | } |