Commits

Anonymous committed 6dc3354

issue #11 mainlined directly

Comments (0)

Files changed (2)

src/lib/sizeof.py

+#
+# PyMite - A flyweight Python interpreter for 8-bit and larger microcontrollers.
+# Copyright 2002 Dean Hall.  All rights reserved.
+# PyMite is offered through one of two licenses: commercial or open-source.
+# See the LICENSE file at the root of this package for licensing details.
+#
+
+#
+# Provides PyMite's sizeof module.
+#
+# USAGE
+# -----
+#
+# sizeof.sizeof(obj)
+#
+# Prints the size of the given object.  If obj is an integer from 0..31,
+# the size of the object type represented by that integer will be returned.
+#
+"""__NATIVE__
+#include "pm.h"
+"""
+
+
+def sizeof(obj):
+    """__NATIVE__
+    pPmObj_t pobj;
+    pPmObj_t psize;
+    int32_t n;
+    PmReturn_t retval = PM_RET_OK;
+    int32_t static size[] = {
+        sizeof(PmObj_t), /* None type */
+        sizeof(PmInt_t),
+        sizeof(PmFloat_t),
+        sizeof(PmString_t),
+        sizeof(PmTuple_t),
+        sizeof(PmCo_t),
+        sizeof(PmFunc_t), /* Module Obj uses func struct */
+        sizeof(PmClass_t),
+        sizeof(PmFunc_t),
+        sizeof(PmClass_t), /* Class instance */
+        0, /* CIM */
+        0, /* NIM */
+        sizeof(PmCo_t), /* NOB */
+        sizeof(PmThread_t),
+        sizeof(PmClass_t), /* Exception instance */
+        sizeof(PmBoolean_t),
+        sizeof(PmCodeImgObj_t),
+        sizeof(PmList_t),
+        sizeof(PmDict_t),
+        0,
+        0,
+        0,
+        0,
+        0,
+        0,
+        sizeof(PmFrame_t),
+        sizeof(PmBlock_t),
+        sizeof(Segment_t),
+        sizeof(Seglist_t),
+        sizeof(PmSeqIter_t),
+        sizeof(PmNativeFrame_t),
+    };
+
+    /* If wrong number of args, raise TypeError */
+    if (NATIVE_GET_NUM_ARGS() != 1)
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    pobj = NATIVE_GET_LOCAL(0);
+    if (OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT)
+    {
+        n = ((pPmInt_t)pobj)->val;
+        if ((n >= 0) && (n < 32))
+        {
+            /* Return the size of the type represented by the integer */
+            retval = int_new(size[n], &psize);
+        }
+        else
+        {
+            /* Return the size of an integer object */
+            retval = int_new(OBJ_GET_SIZE(pobj), &psize);
+        }
+    }
+    else
+    {
+        /* Return the size of the given non-integer object */
+        retval = int_new(OBJ_GET_SIZE(pobj), &psize);
+    }
+
+    NATIVE_SET_TOS(psize);
+    return retval;
+    """
+    pass
+
+
+def print_sizes():
+    types = (
+        'NON',
+        'INT',
+        'FLT',
+        'STR',
+        'TUP',
+        'COB',
+        'MOD',
+        'CLO',
+        'FXN',
+        'CLI',
+        'CIM',
+        'NIM',
+        'NOB',
+        'THR',
+        'EXN',
+        'BOL',
+        'CIO',
+        'LST',
+        'DIC',
+        0, 0, 0, 0, 0, 0,
+        'FRM',
+        'BLK',
+        'SEG',
+        'SGL',
+        'SQI',
+        'NFM',
+        0,
+    )
+    for i in range(32):
+        if types[i] != 0:
+            print "sizeof(", types[i], ") = ", sizeof(i)
+    
+#:mode=c:
  */
 
 
-#ifdef HAVE_FLOAT
-
 /**
  * Float obj
  *
 } PmFloat_t, *pPmFloat_t;
 
 
+#ifdef HAVE_FLOAT
+
 /**
  * Creates a new Float object
  *