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/obj.c@0:65f1469d6bfb, 2013-03-02 (annotated)
- Committer:
- va009039
- Date:
- Sat Mar 02 11:54:20 2013 +0000
- Revision:
- 0:65f1469d6bfb
first commit
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__ 0x0F |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Object Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * 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 | obj_loadFromImg(PmMemSpace_t memspace, |
va009039 | 0:65f1469d6bfb | 26 | uint8_t const **paddr, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 27 | { |
va009039 | 0:65f1469d6bfb | 28 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 29 | PmObj_t obj; |
va009039 | 0:65f1469d6bfb | 30 | |
va009039 | 0:65f1469d6bfb | 31 | |
va009039 | 0:65f1469d6bfb | 32 | /* Get the object descriptor */ |
va009039 | 0:65f1469d6bfb | 33 | obj.od = (PmObjDesc_t)0x0000; |
va009039 | 0:65f1469d6bfb | 34 | OBJ_SET_TYPE(&obj, mem_getByte(memspace, paddr)); |
va009039 | 0:65f1469d6bfb | 35 | |
va009039 | 0:65f1469d6bfb | 36 | switch (OBJ_GET_TYPE(&obj)) |
va009039 | 0:65f1469d6bfb | 37 | { |
va009039 | 0:65f1469d6bfb | 38 | case OBJ_TYPE_NON: |
va009039 | 0:65f1469d6bfb | 39 | /* If it's the None object, return global None */ |
va009039 | 0:65f1469d6bfb | 40 | *r_pobj = PM_NONE; |
va009039 | 0:65f1469d6bfb | 41 | break; |
va009039 | 0:65f1469d6bfb | 42 | |
va009039 | 0:65f1469d6bfb | 43 | case OBJ_TYPE_INT: |
va009039 | 0:65f1469d6bfb | 44 | /* Read an integer and create an integer object with the value */ |
va009039 | 0:65f1469d6bfb | 45 | retval = int_new(mem_getInt(memspace, paddr), r_pobj); |
va009039 | 0:65f1469d6bfb | 46 | break; |
va009039 | 0:65f1469d6bfb | 47 | |
va009039 | 0:65f1469d6bfb | 48 | #ifdef HAVE_FLOAT |
va009039 | 0:65f1469d6bfb | 49 | case OBJ_TYPE_FLT: |
va009039 | 0:65f1469d6bfb | 50 | /* Read a float and create an float object with the value */ |
va009039 | 0:65f1469d6bfb | 51 | retval = float_new(mem_getFloat(memspace, paddr), r_pobj); |
va009039 | 0:65f1469d6bfb | 52 | break; |
va009039 | 0:65f1469d6bfb | 53 | #endif /* HAVE_FLOAT */ |
va009039 | 0:65f1469d6bfb | 54 | |
va009039 | 0:65f1469d6bfb | 55 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 56 | retval = string_loadFromImg(memspace, paddr, r_pobj); |
va009039 | 0:65f1469d6bfb | 57 | break; |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 60 | retval = tuple_loadFromImg(memspace, paddr, r_pobj); |
va009039 | 0:65f1469d6bfb | 61 | break; |
va009039 | 0:65f1469d6bfb | 62 | |
va009039 | 0:65f1469d6bfb | 63 | case OBJ_TYPE_NIM: |
va009039 | 0:65f1469d6bfb | 64 | /* If it's a native code img, load into a code obj */ |
va009039 | 0:65f1469d6bfb | 65 | retval = no_loadFromImg(memspace, paddr, r_pobj); |
va009039 | 0:65f1469d6bfb | 66 | break; |
va009039 | 0:65f1469d6bfb | 67 | |
va009039 | 0:65f1469d6bfb | 68 | case OBJ_TYPE_CIM: |
va009039 | 0:65f1469d6bfb | 69 | /* If it's a code img, load into a code obj */ |
va009039 | 0:65f1469d6bfb | 70 | retval = co_loadFromImg(memspace, paddr, r_pobj); |
va009039 | 0:65f1469d6bfb | 71 | break; |
va009039 | 0:65f1469d6bfb | 72 | |
va009039 | 0:65f1469d6bfb | 73 | default: |
va009039 | 0:65f1469d6bfb | 74 | /* All other types should not be in an img obj */ |
va009039 | 0:65f1469d6bfb | 75 | PM_RAISE(retval, PM_RET_EX_SYS); |
va009039 | 0:65f1469d6bfb | 76 | break; |
va009039 | 0:65f1469d6bfb | 77 | } |
va009039 | 0:65f1469d6bfb | 78 | return retval; |
va009039 | 0:65f1469d6bfb | 79 | } |
va009039 | 0:65f1469d6bfb | 80 | |
va009039 | 0:65f1469d6bfb | 81 | |
va009039 | 0:65f1469d6bfb | 82 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 83 | obj_loadFromImgObj(pPmObj_t pimg, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 84 | { |
va009039 | 0:65f1469d6bfb | 85 | uint8_t const *imgaddr; |
va009039 | 0:65f1469d6bfb | 86 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 87 | |
va009039 | 0:65f1469d6bfb | 88 | C_ASSERT(OBJ_GET_TYPE(pimg) == OBJ_TYPE_CIO); |
va009039 | 0:65f1469d6bfb | 89 | imgaddr = (uint8_t const *)&(((pPmCodeImgObj_t)pimg)->val); |
va009039 | 0:65f1469d6bfb | 90 | |
va009039 | 0:65f1469d6bfb | 91 | retval = obj_loadFromImg(MEMSPACE_RAM, &imgaddr, r_pobj); |
va009039 | 0:65f1469d6bfb | 92 | C_ASSERT(OBJ_GET_TYPE(*r_pobj) == OBJ_TYPE_COB); |
va009039 | 0:65f1469d6bfb | 93 | |
va009039 | 0:65f1469d6bfb | 94 | /* All COs must reference the top of the code img obj |
va009039 | 0:65f1469d6bfb | 95 | * so the image is marked and prevented from being reclaimed */ |
va009039 | 0:65f1469d6bfb | 96 | co_rSetCodeImgAddr((pPmCo_t)*r_pobj, (uint8_t const *)pimg); |
va009039 | 0:65f1469d6bfb | 97 | |
va009039 | 0:65f1469d6bfb | 98 | return retval; |
va009039 | 0:65f1469d6bfb | 99 | } |
va009039 | 0:65f1469d6bfb | 100 | |
va009039 | 0:65f1469d6bfb | 101 | |
va009039 | 0:65f1469d6bfb | 102 | /* Returns true if the obj is false */ |
va009039 | 0:65f1469d6bfb | 103 | int8_t |
va009039 | 0:65f1469d6bfb | 104 | obj_isFalse(pPmObj_t pobj) |
va009039 | 0:65f1469d6bfb | 105 | { |
va009039 | 0:65f1469d6bfb | 106 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 107 | |
va009039 | 0:65f1469d6bfb | 108 | switch (OBJ_GET_TYPE(pobj)) |
va009039 | 0:65f1469d6bfb | 109 | { |
va009039 | 0:65f1469d6bfb | 110 | case OBJ_TYPE_NON: |
va009039 | 0:65f1469d6bfb | 111 | /* None evaluates to false, so return true */ |
va009039 | 0:65f1469d6bfb | 112 | return C_TRUE; |
va009039 | 0:65f1469d6bfb | 113 | |
va009039 | 0:65f1469d6bfb | 114 | case OBJ_TYPE_INT: |
va009039 | 0:65f1469d6bfb | 115 | /* Only the integer zero is false */ |
va009039 | 0:65f1469d6bfb | 116 | return ((pPmInt_t)pobj)->val == 0; |
va009039 | 0:65f1469d6bfb | 117 | |
va009039 | 0:65f1469d6bfb | 118 | #ifdef HAVE_FLOAT |
va009039 | 0:65f1469d6bfb | 119 | case OBJ_TYPE_FLT: |
va009039 | 0:65f1469d6bfb | 120 | /* The floats 0.0 and -0.0 are false */ |
va009039 | 0:65f1469d6bfb | 121 | return (((pPmFloat_t) pobj)->val == 0.0) |
va009039 | 0:65f1469d6bfb | 122 | || (((pPmFloat_t) pobj)->val == -0.0); |
va009039 | 0:65f1469d6bfb | 123 | #endif /* HAVE_FLOAT */ |
va009039 | 0:65f1469d6bfb | 124 | |
va009039 | 0:65f1469d6bfb | 125 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 126 | /* An empty string is false */ |
va009039 | 0:65f1469d6bfb | 127 | return ((pPmString_t)pobj)->length == 0; |
va009039 | 0:65f1469d6bfb | 128 | |
va009039 | 0:65f1469d6bfb | 129 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 130 | /* An empty tuple is false */ |
va009039 | 0:65f1469d6bfb | 131 | return ((pPmTuple_t)pobj)->length == 0; |
va009039 | 0:65f1469d6bfb | 132 | |
va009039 | 0:65f1469d6bfb | 133 | case OBJ_TYPE_LST: |
va009039 | 0:65f1469d6bfb | 134 | /* An empty list is false */ |
va009039 | 0:65f1469d6bfb | 135 | return ((pPmList_t)pobj)->length == 0; |
va009039 | 0:65f1469d6bfb | 136 | |
va009039 | 0:65f1469d6bfb | 137 | case OBJ_TYPE_DIC: |
va009039 | 0:65f1469d6bfb | 138 | /* An empty dict is false */ |
va009039 | 0:65f1469d6bfb | 139 | return ((pPmDict_t)pobj)->length == 0; |
va009039 | 0:65f1469d6bfb | 140 | |
va009039 | 0:65f1469d6bfb | 141 | case OBJ_TYPE_BOOL: |
va009039 | 0:65f1469d6bfb | 142 | /* C int zero means false */ |
va009039 | 0:65f1469d6bfb | 143 | return ((pPmBoolean_t) pobj)->val == 0; |
va009039 | 0:65f1469d6bfb | 144 | |
va009039 | 0:65f1469d6bfb | 145 | default: |
va009039 | 0:65f1469d6bfb | 146 | /* |
va009039 | 0:65f1469d6bfb | 147 | * The following types are always not false: |
va009039 | 0:65f1469d6bfb | 148 | * CodeObj, Function, Module, Class, ClassInstance. |
va009039 | 0:65f1469d6bfb | 149 | */ |
va009039 | 0:65f1469d6bfb | 150 | return C_FALSE; |
va009039 | 0:65f1469d6bfb | 151 | } |
va009039 | 0:65f1469d6bfb | 152 | } |
va009039 | 0:65f1469d6bfb | 153 | |
va009039 | 0:65f1469d6bfb | 154 | |
va009039 | 0:65f1469d6bfb | 155 | /* Returns true if the item is in the container object */ |
va009039 | 0:65f1469d6bfb | 156 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 157 | obj_isIn(pPmObj_t pobj, pPmObj_t pitem) |
va009039 | 0:65f1469d6bfb | 158 | { |
va009039 | 0:65f1469d6bfb | 159 | PmReturn_t retval = PM_RET_NO; |
va009039 | 0:65f1469d6bfb | 160 | pPmObj_t ptestItem; |
va009039 | 0:65f1469d6bfb | 161 | int16_t i; |
va009039 | 0:65f1469d6bfb | 162 | uint8_t c; |
va009039 | 0:65f1469d6bfb | 163 | |
va009039 | 0:65f1469d6bfb | 164 | switch (OBJ_GET_TYPE(pobj)) |
va009039 | 0:65f1469d6bfb | 165 | { |
va009039 | 0:65f1469d6bfb | 166 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 167 | /* Iterate over tuple to find item */ |
va009039 | 0:65f1469d6bfb | 168 | for (i = 0; i < ((pPmTuple_t)pobj)->length; i++) |
va009039 | 0:65f1469d6bfb | 169 | { |
va009039 | 0:65f1469d6bfb | 170 | PM_RETURN_IF_ERROR(tuple_getItem(pobj, i, &ptestItem)); |
va009039 | 0:65f1469d6bfb | 171 | |
va009039 | 0:65f1469d6bfb | 172 | if (obj_compare(pitem, ptestItem) == C_SAME) |
va009039 | 0:65f1469d6bfb | 173 | { |
va009039 | 0:65f1469d6bfb | 174 | retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 175 | break; |
va009039 | 0:65f1469d6bfb | 176 | } |
va009039 | 0:65f1469d6bfb | 177 | } |
va009039 | 0:65f1469d6bfb | 178 | break; |
va009039 | 0:65f1469d6bfb | 179 | |
va009039 | 0:65f1469d6bfb | 180 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 181 | /* Raise a TypeError if item is not a string */ |
va009039 | 0:65f1469d6bfb | 182 | if ((OBJ_GET_TYPE(pitem) != OBJ_TYPE_STR)) |
va009039 | 0:65f1469d6bfb | 183 | { |
va009039 | 0:65f1469d6bfb | 184 | retval = PM_RET_EX_TYPE; |
va009039 | 0:65f1469d6bfb | 185 | break; |
va009039 | 0:65f1469d6bfb | 186 | } |
va009039 | 0:65f1469d6bfb | 187 | |
va009039 | 0:65f1469d6bfb | 188 | /* Empty string is alway present */ |
va009039 | 0:65f1469d6bfb | 189 | if (((pPmString_t)pitem)->length == 0) |
va009039 | 0:65f1469d6bfb | 190 | { |
va009039 | 0:65f1469d6bfb | 191 | retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 192 | break; |
va009039 | 0:65f1469d6bfb | 193 | } |
va009039 | 0:65f1469d6bfb | 194 | |
va009039 | 0:65f1469d6bfb | 195 | /* Raise a ValueError if the string is more than 1 char */ |
va009039 | 0:65f1469d6bfb | 196 | else if (((pPmString_t)pitem)->length != 1) |
va009039 | 0:65f1469d6bfb | 197 | { |
va009039 | 0:65f1469d6bfb | 198 | retval = PM_RET_EX_VAL; |
va009039 | 0:65f1469d6bfb | 199 | break; |
va009039 | 0:65f1469d6bfb | 200 | } |
va009039 | 0:65f1469d6bfb | 201 | |
va009039 | 0:65f1469d6bfb | 202 | /* Iterate over string to find char */ |
va009039 | 0:65f1469d6bfb | 203 | c = ((pPmString_t)pitem)->val[0]; |
va009039 | 0:65f1469d6bfb | 204 | for (i = 0; i < ((pPmString_t)pobj)->length; i++) |
va009039 | 0:65f1469d6bfb | 205 | { |
va009039 | 0:65f1469d6bfb | 206 | if (c == ((pPmString_t)pobj)->val[i]) |
va009039 | 0:65f1469d6bfb | 207 | { |
va009039 | 0:65f1469d6bfb | 208 | retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 209 | break; |
va009039 | 0:65f1469d6bfb | 210 | } |
va009039 | 0:65f1469d6bfb | 211 | } |
va009039 | 0:65f1469d6bfb | 212 | break; |
va009039 | 0:65f1469d6bfb | 213 | |
va009039 | 0:65f1469d6bfb | 214 | case OBJ_TYPE_LST: |
va009039 | 0:65f1469d6bfb | 215 | /* Iterate over list to find item */ |
va009039 | 0:65f1469d6bfb | 216 | for (i = 0; i < ((pPmList_t)pobj)->length; i++) |
va009039 | 0:65f1469d6bfb | 217 | { |
va009039 | 0:65f1469d6bfb | 218 | PM_RETURN_IF_ERROR(list_getItem(pobj, i, &ptestItem)); |
va009039 | 0:65f1469d6bfb | 219 | |
va009039 | 0:65f1469d6bfb | 220 | if (obj_compare(pitem, ptestItem) == C_SAME) |
va009039 | 0:65f1469d6bfb | 221 | { |
va009039 | 0:65f1469d6bfb | 222 | retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 223 | break; |
va009039 | 0:65f1469d6bfb | 224 | } |
va009039 | 0:65f1469d6bfb | 225 | } |
va009039 | 0:65f1469d6bfb | 226 | break; |
va009039 | 0:65f1469d6bfb | 227 | |
va009039 | 0:65f1469d6bfb | 228 | case OBJ_TYPE_DIC: |
va009039 | 0:65f1469d6bfb | 229 | /* Check if the item is one of the keys of the dict */ |
va009039 | 0:65f1469d6bfb | 230 | retval = dict_getItem(pobj, pitem, &ptestItem); |
va009039 | 0:65f1469d6bfb | 231 | if (retval == PM_RET_EX_KEY) |
va009039 | 0:65f1469d6bfb | 232 | { |
va009039 | 0:65f1469d6bfb | 233 | retval = PM_RET_NO; |
va009039 | 0:65f1469d6bfb | 234 | } |
va009039 | 0:65f1469d6bfb | 235 | break; |
va009039 | 0:65f1469d6bfb | 236 | |
va009039 | 0:65f1469d6bfb | 237 | default: |
va009039 | 0:65f1469d6bfb | 238 | retval = PM_RET_EX_TYPE; |
va009039 | 0:65f1469d6bfb | 239 | break; |
va009039 | 0:65f1469d6bfb | 240 | } |
va009039 | 0:65f1469d6bfb | 241 | |
va009039 | 0:65f1469d6bfb | 242 | return retval; |
va009039 | 0:65f1469d6bfb | 243 | } |
va009039 | 0:65f1469d6bfb | 244 | |
va009039 | 0:65f1469d6bfb | 245 | |
va009039 | 0:65f1469d6bfb | 246 | int8_t |
va009039 | 0:65f1469d6bfb | 247 | obj_compare(pPmObj_t pobj1, pPmObj_t pobj2) |
va009039 | 0:65f1469d6bfb | 248 | { |
va009039 | 0:65f1469d6bfb | 249 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 250 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 251 | pPmObj_t pobj; |
va009039 | 0:65f1469d6bfb | 252 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 253 | |
va009039 | 0:65f1469d6bfb | 254 | C_ASSERT(pobj1 != C_NULL); |
va009039 | 0:65f1469d6bfb | 255 | C_ASSERT(pobj2 != C_NULL); |
va009039 | 0:65f1469d6bfb | 256 | |
va009039 | 0:65f1469d6bfb | 257 | /* Check if pointers are same */ |
va009039 | 0:65f1469d6bfb | 258 | if (pobj1 == pobj2) |
va009039 | 0:65f1469d6bfb | 259 | { |
va009039 | 0:65f1469d6bfb | 260 | return C_SAME; |
va009039 | 0:65f1469d6bfb | 261 | } |
va009039 | 0:65f1469d6bfb | 262 | |
va009039 | 0:65f1469d6bfb | 263 | /* If types are different, objs must differ */ |
va009039 | 0:65f1469d6bfb | 264 | if (OBJ_GET_TYPE(pobj1) != OBJ_GET_TYPE(pobj2)) |
va009039 | 0:65f1469d6bfb | 265 | { |
va009039 | 0:65f1469d6bfb | 266 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 267 | } |
va009039 | 0:65f1469d6bfb | 268 | |
va009039 | 0:65f1469d6bfb | 269 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 270 | /* If object is an instance, get the thing it contains */ |
va009039 | 0:65f1469d6bfb | 271 | if (OBJ_GET_TYPE(pobj1) == OBJ_TYPE_CLI) |
va009039 | 0:65f1469d6bfb | 272 | { |
va009039 | 0:65f1469d6bfb | 273 | retval = dict_getItem((pPmObj_t)((pPmInstance_t)pobj1)->cli_attrs, |
va009039 | 0:65f1469d6bfb | 274 | PM_NONE, |
va009039 | 0:65f1469d6bfb | 275 | &pobj); |
va009039 | 0:65f1469d6bfb | 276 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 277 | pobj1 = pobj; |
va009039 | 0:65f1469d6bfb | 278 | } |
va009039 | 0:65f1469d6bfb | 279 | if (OBJ_GET_TYPE(pobj2) == OBJ_TYPE_CLI) |
va009039 | 0:65f1469d6bfb | 280 | { |
va009039 | 0:65f1469d6bfb | 281 | retval = dict_getItem((pPmObj_t)((pPmInstance_t)pobj2)->cli_attrs, |
va009039 | 0:65f1469d6bfb | 282 | PM_NONE, |
va009039 | 0:65f1469d6bfb | 283 | &pobj); |
va009039 | 0:65f1469d6bfb | 284 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 285 | pobj2 = pobj; |
va009039 | 0:65f1469d6bfb | 286 | } |
va009039 | 0:65f1469d6bfb | 287 | |
va009039 | 0:65f1469d6bfb | 288 | /* If types are different, objs must differ */ |
va009039 | 0:65f1469d6bfb | 289 | if (OBJ_GET_TYPE(pobj1) != OBJ_GET_TYPE(pobj2)) |
va009039 | 0:65f1469d6bfb | 290 | { |
va009039 | 0:65f1469d6bfb | 291 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 292 | } |
va009039 | 0:65f1469d6bfb | 293 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 294 | |
va009039 | 0:65f1469d6bfb | 295 | /* Otherwise handle types individually */ |
va009039 | 0:65f1469d6bfb | 296 | switch (OBJ_GET_TYPE(pobj1)) |
va009039 | 0:65f1469d6bfb | 297 | { |
va009039 | 0:65f1469d6bfb | 298 | case OBJ_TYPE_NON: |
va009039 | 0:65f1469d6bfb | 299 | return C_SAME; |
va009039 | 0:65f1469d6bfb | 300 | |
va009039 | 0:65f1469d6bfb | 301 | case OBJ_TYPE_INT: |
va009039 | 0:65f1469d6bfb | 302 | return ((pPmInt_t)pobj1)->val == |
va009039 | 0:65f1469d6bfb | 303 | ((pPmInt_t)pobj2)->val ? C_SAME : C_DIFFER; |
va009039 | 0:65f1469d6bfb | 304 | |
va009039 | 0:65f1469d6bfb | 305 | #ifdef HAVE_FLOAT |
va009039 | 0:65f1469d6bfb | 306 | case OBJ_TYPE_FLT: |
va009039 | 0:65f1469d6bfb | 307 | { |
va009039 | 0:65f1469d6bfb | 308 | pPmObj_t r_pobj; |
va009039 | 0:65f1469d6bfb | 309 | |
va009039 | 0:65f1469d6bfb | 310 | float_compare(pobj1, pobj2, &r_pobj, COMP_EQ); |
va009039 | 0:65f1469d6bfb | 311 | return (r_pobj == PM_TRUE) ? C_SAME : C_DIFFER; |
va009039 | 0:65f1469d6bfb | 312 | } |
va009039 | 0:65f1469d6bfb | 313 | #endif /* HAVE_FLOAT */ |
va009039 | 0:65f1469d6bfb | 314 | |
va009039 | 0:65f1469d6bfb | 315 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 316 | return string_compare((pPmString_t)pobj1, (pPmString_t)pobj2); |
va009039 | 0:65f1469d6bfb | 317 | |
va009039 | 0:65f1469d6bfb | 318 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 319 | case OBJ_TYPE_LST: |
va009039 | 0:65f1469d6bfb | 320 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 321 | case OBJ_TYPE_BYA: |
va009039 | 0:65f1469d6bfb | 322 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 323 | return seq_compare(pobj1, pobj2); |
va009039 | 0:65f1469d6bfb | 324 | |
va009039 | 0:65f1469d6bfb | 325 | case OBJ_TYPE_DIC: |
va009039 | 0:65f1469d6bfb | 326 | return dict_compare(pobj1, pobj2); |
va009039 | 0:65f1469d6bfb | 327 | |
va009039 | 0:65f1469d6bfb | 328 | default: |
va009039 | 0:65f1469d6bfb | 329 | break; |
va009039 | 0:65f1469d6bfb | 330 | } |
va009039 | 0:65f1469d6bfb | 331 | |
va009039 | 0:65f1469d6bfb | 332 | /* All other types would need same pointer to be true */ |
va009039 | 0:65f1469d6bfb | 333 | return C_DIFFER; |
va009039 | 0:65f1469d6bfb | 334 | } |
va009039 | 0:65f1469d6bfb | 335 | |
va009039 | 0:65f1469d6bfb | 336 | |
va009039 | 0:65f1469d6bfb | 337 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 338 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 339 | obj_print(pPmObj_t pobj, uint8_t is_expr_repr, uint8_t is_nested) |
va009039 | 0:65f1469d6bfb | 340 | { |
va009039 | 0:65f1469d6bfb | 341 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 342 | |
va009039 | 0:65f1469d6bfb | 343 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 344 | |
va009039 | 0:65f1469d6bfb | 345 | /* Something gets printed unless it's None in an unnested expression */ |
va009039 | 0:65f1469d6bfb | 346 | if (!((OBJ_GET_TYPE(pobj) == OBJ_TYPE_NON) && is_expr_repr && !is_nested)) |
va009039 | 0:65f1469d6bfb | 347 | { |
va009039 | 0:65f1469d6bfb | 348 | gVmGlobal.somethingPrinted = C_TRUE; |
va009039 | 0:65f1469d6bfb | 349 | } |
va009039 | 0:65f1469d6bfb | 350 | |
va009039 | 0:65f1469d6bfb | 351 | switch (OBJ_GET_TYPE(pobj)) |
va009039 | 0:65f1469d6bfb | 352 | { |
va009039 | 0:65f1469d6bfb | 353 | case OBJ_TYPE_NON: |
va009039 | 0:65f1469d6bfb | 354 | if (!is_expr_repr || is_nested) |
va009039 | 0:65f1469d6bfb | 355 | { |
va009039 | 0:65f1469d6bfb | 356 | sli_puts((uint8_t *)"None"); |
va009039 | 0:65f1469d6bfb | 357 | } |
va009039 | 0:65f1469d6bfb | 358 | break; |
va009039 | 0:65f1469d6bfb | 359 | case OBJ_TYPE_INT: |
va009039 | 0:65f1469d6bfb | 360 | retval = int_print(pobj); |
va009039 | 0:65f1469d6bfb | 361 | break; |
va009039 | 0:65f1469d6bfb | 362 | #ifdef HAVE_FLOAT |
va009039 | 0:65f1469d6bfb | 363 | case OBJ_TYPE_FLT: |
va009039 | 0:65f1469d6bfb | 364 | retval = float_print(pobj); |
va009039 | 0:65f1469d6bfb | 365 | break; |
va009039 | 0:65f1469d6bfb | 366 | #endif /* HAVE_FLOAT */ |
va009039 | 0:65f1469d6bfb | 367 | case OBJ_TYPE_STR: |
va009039 | 0:65f1469d6bfb | 368 | retval = string_print(pobj, (is_expr_repr || is_nested)); |
va009039 | 0:65f1469d6bfb | 369 | break; |
va009039 | 0:65f1469d6bfb | 370 | case OBJ_TYPE_TUP: |
va009039 | 0:65f1469d6bfb | 371 | retval = tuple_print(pobj); |
va009039 | 0:65f1469d6bfb | 372 | break; |
va009039 | 0:65f1469d6bfb | 373 | case OBJ_TYPE_LST: |
va009039 | 0:65f1469d6bfb | 374 | retval = list_print(pobj); |
va009039 | 0:65f1469d6bfb | 375 | break; |
va009039 | 0:65f1469d6bfb | 376 | case OBJ_TYPE_DIC: |
va009039 | 0:65f1469d6bfb | 377 | retval = dict_print(pobj); |
va009039 | 0:65f1469d6bfb | 378 | break; |
va009039 | 0:65f1469d6bfb | 379 | case OBJ_TYPE_BOOL: |
va009039 | 0:65f1469d6bfb | 380 | sli_puts( |
va009039 | 0:65f1469d6bfb | 381 | (((pPmBoolean_t) pobj)->val == C_TRUE) |
va009039 | 0:65f1469d6bfb | 382 | ? (uint8_t *)"True" |
va009039 | 0:65f1469d6bfb | 383 | : (uint8_t *)"False"); |
va009039 | 0:65f1469d6bfb | 384 | break; |
va009039 | 0:65f1469d6bfb | 385 | |
va009039 | 0:65f1469d6bfb | 386 | case OBJ_TYPE_CLI: |
va009039 | 0:65f1469d6bfb | 387 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 388 | { |
va009039 | 0:65f1469d6bfb | 389 | pPmObj_t pobj2; |
va009039 | 0:65f1469d6bfb | 390 | |
va009039 | 0:65f1469d6bfb | 391 | retval = dict_getItem((pPmObj_t)((pPmInstance_t)pobj)->cli_attrs, |
va009039 | 0:65f1469d6bfb | 392 | PM_NONE, |
va009039 | 0:65f1469d6bfb | 393 | (pPmObj_t *)&pobj2); |
va009039 | 0:65f1469d6bfb | 394 | if ((retval == PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 395 | && (OBJ_GET_TYPE(pobj2) == OBJ_TYPE_BYA)) |
va009039 | 0:65f1469d6bfb | 396 | { |
va009039 | 0:65f1469d6bfb | 397 | retval = bytearray_print(pobj2); |
va009039 | 0:65f1469d6bfb | 398 | break; |
va009039 | 0:65f1469d6bfb | 399 | } |
va009039 | 0:65f1469d6bfb | 400 | } |
va009039 | 0:65f1469d6bfb | 401 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 402 | |
va009039 | 0:65f1469d6bfb | 403 | case OBJ_TYPE_COB: |
va009039 | 0:65f1469d6bfb | 404 | case OBJ_TYPE_MOD: |
va009039 | 0:65f1469d6bfb | 405 | case OBJ_TYPE_CLO: |
va009039 | 0:65f1469d6bfb | 406 | case OBJ_TYPE_FXN: |
va009039 | 0:65f1469d6bfb | 407 | case OBJ_TYPE_CIM: |
va009039 | 0:65f1469d6bfb | 408 | case OBJ_TYPE_NIM: |
va009039 | 0:65f1469d6bfb | 409 | case OBJ_TYPE_NOB: |
va009039 | 0:65f1469d6bfb | 410 | case OBJ_TYPE_THR: |
va009039 | 0:65f1469d6bfb | 411 | case OBJ_TYPE_CIO: |
va009039 | 0:65f1469d6bfb | 412 | case OBJ_TYPE_MTH: |
va009039 | 0:65f1469d6bfb | 413 | case OBJ_TYPE_SQI: |
va009039 | 0:65f1469d6bfb | 414 | { |
va009039 | 0:65f1469d6bfb | 415 | uint8_t buf[17]; |
va009039 | 0:65f1469d6bfb | 416 | sli_puts((uint8_t *)"<obj type 0x"); |
va009039 | 0:65f1469d6bfb | 417 | sli_btoa16(OBJ_GET_TYPE(pobj), buf, sizeof(buf), C_TRUE); |
va009039 | 0:65f1469d6bfb | 418 | sli_puts(buf); |
va009039 | 0:65f1469d6bfb | 419 | sli_puts((uint8_t *)" @ 0x"); |
va009039 | 0:65f1469d6bfb | 420 | sli_ptoa16((intptr_t)pobj, buf, sizeof(buf), C_TRUE); |
va009039 | 0:65f1469d6bfb | 421 | sli_puts(buf); |
va009039 | 0:65f1469d6bfb | 422 | retval = plat_putByte('>'); |
va009039 | 0:65f1469d6bfb | 423 | break; |
va009039 | 0:65f1469d6bfb | 424 | } |
va009039 | 0:65f1469d6bfb | 425 | |
va009039 | 0:65f1469d6bfb | 426 | default: |
va009039 | 0:65f1469d6bfb | 427 | /* Otherwise raise a TypeError */ |
va009039 | 0:65f1469d6bfb | 428 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 429 | break; |
va009039 | 0:65f1469d6bfb | 430 | } |
va009039 | 0:65f1469d6bfb | 431 | return retval; |
va009039 | 0:65f1469d6bfb | 432 | } |
va009039 | 0:65f1469d6bfb | 433 | #endif /* HAVE_PRINT */ |
va009039 | 0:65f1469d6bfb | 434 | |
va009039 | 0:65f1469d6bfb | 435 | |
va009039 | 0:65f1469d6bfb | 436 | #ifdef HAVE_BACKTICK |
va009039 | 0:65f1469d6bfb | 437 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 438 | obj_repr(pPmObj_t pobj, pPmObj_t *r_pstr) |
va009039 | 0:65f1469d6bfb | 439 | { |
va009039 | 0:65f1469d6bfb | 440 | uint8_t tBuffer[32]; |
va009039 | 0:65f1469d6bfb | 441 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 442 | uint8_t const *pcstr = (uint8_t *)tBuffer;; |
va009039 | 0:65f1469d6bfb | 443 | |
va009039 | 0:65f1469d6bfb | 444 | C_ASSERT(pobj != C_NULL); |
va009039 | 0:65f1469d6bfb | 445 | |
va009039 | 0:65f1469d6bfb | 446 | switch (OBJ_GET_TYPE(pobj)) |
va009039 | 0:65f1469d6bfb | 447 | { |
va009039 | 0:65f1469d6bfb | 448 | case OBJ_TYPE_INT: |
va009039 | 0:65f1469d6bfb | 449 | retval = sli_ltoa10(((pPmInt_t)pobj)->val, tBuffer, sizeof(tBuffer)); |
va009039 | 0:65f1469d6bfb | 450 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 451 | retval = string_new(&pcstr, r_pstr); |
va009039 | 0:65f1469d6bfb | 452 | break; |
va009039 | 0:65f1469d6bfb | 453 | |
va009039 | 0:65f1469d6bfb | 454 | #ifdef HAVE_FLOAT |
va009039 | 0:65f1469d6bfb | 455 | case OBJ_TYPE_FLT: |
va009039 | 0:65f1469d6bfb | 456 | /* #212: Use homebrew float formatter */ |
va009039 | 0:65f1469d6bfb | 457 | retval = sli_ftoa(((pPmFloat_t)pobj)->val, tBuffer, sizeof(tBuffer)); |
va009039 | 0:65f1469d6bfb | 458 | sli_strlen((char *)tBuffer); |
va009039 | 0:65f1469d6bfb | 459 | retval = string_new(&pcstr, r_pstr); |
va009039 | 0:65f1469d6bfb | 460 | break; |
va009039 | 0:65f1469d6bfb | 461 | #endif /* HAVE_FLOAT */ |
va009039 | 0:65f1469d6bfb | 462 | |
va009039 | 0:65f1469d6bfb | 463 | default: |
va009039 | 0:65f1469d6bfb | 464 | /* Otherwise raise a TypeError */ |
va009039 | 0:65f1469d6bfb | 465 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 466 | break; |
va009039 | 0:65f1469d6bfb | 467 | } |
va009039 | 0:65f1469d6bfb | 468 | |
va009039 | 0:65f1469d6bfb | 469 | return retval; |
va009039 | 0:65f1469d6bfb | 470 | } |
va009039 | 0:65f1469d6bfb | 471 | #endif /* HAVE_BACKTICK */ |