atabac / python-on-a-chip

Automatically exported from code.google.com/p/python-on-a-chip
Other
0 stars 0 forks source link

Create int() builtin #154

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Oscar Linberg via maillist on 2010/10/04:

It could be improved by also accepting ints (just copy) and strings
(should use string.atoi). But this is all I needed today:

def int(v):
   """__NATIVE__

   PmReturn_t retval = PM_RET_OK;

   int func_retval;
   pPmObj_t p_func_retval = C_NULL;

   int int_val;
   pPmObj_t p_float = C_NULL;

   /* If wrong number of args, raise TypeError */
   if (NATIVE_GET_NUM_ARGS() != 1)
   {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
   }

   /* Raise TypeError if arg is not correct type */
   p_float = NATIVE_GET_LOCAL(0);
   if (OBJ_GET_TYPE(p_float) != OBJ_TYPE_FLT)
   {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
   }

   int_val = (int)(((pPmFloat_t)p_float)->val);

   retval = int_new(int_val, &p_func_retval);

   NATIVE_SET_TOS(p_func_retval);

   return retval;
   """
   pass

Original issue reported on code.google.com by dwhall...@gmail.com on 4 Oct 2010 at 9:34

GoogleCodeExporter commented 9 years ago
Could also create float() at the same time:

def float(n):
    return n * 1.0

It only accepts Int/Bool arguments (string's absence is notable), but it's a 
start.

Original comment by dwhall...@gmail.com on 16 Oct 2010 at 7:36

GoogleCodeExporter commented 9 years ago

Original comment by dwhall...@gmail.com on 22 Oct 2010 at 3:28

GoogleCodeExporter commented 9 years ago
I created an implementation of int() and float() that accepts all types, 
converts ints floats and strings, and raises a TypeError for other types.

However, I can't put this in the default branch because it requires HAVE_FLOAT 
and not all platforms support that.  I checked in this code on a branch 
(issue_0154_dwhall_int_float_builtins) so people can use it.  I won't be able 
to put this in the default branch until I can figure out how to conditionally 
compile parts of python code (such as in __bi.py).

Original comment by dwhall...@gmail.com on 26 Oct 2010 at 9:50

GoogleCodeExporter commented 9 years ago
r2b92d3dd03
Implemented int() and float() in lib/__bi.py.
Also updated abs() with one that doesn't create a tuple.
Checked in on branch: issue_0154_dwhall_int_float_builtins.

Original comment by dwhall...@gmail.com on 26 Oct 2010 at 10:06

GoogleCodeExporter commented 9 years ago

Original comment by dwhall...@gmail.com on 16 Nov 2010 at 2:41

GoogleCodeExporter commented 9 years ago
If you contain the conditional compiles within the boundaries of the __NATIVE 
portion of the code both float and int can be made to compile with or without 
HAVE_FLOAT.
Obviously float() on a non-float platform is made to always raise a type error!

def float(v):
    """__NATIVE__
    pPmObj_t pv;
    pPmObj_t pn;
    PmReturn_t retval = PM_RET_OK;

#ifdef HAVE_FLOAT
    float n;

    /* If wrong number of args, raise TypeError */
    if (NATIVE_GET_NUM_ARGS() != 1)
    {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
    }

    pv = NATIVE_GET_LOCAL(0);

    /* Convert from int */
    if (OBJ_GET_TYPE(pv) == OBJ_TYPE_INT)
    {
        n = (float)(((pPmInt_t)pv)->val);
        retval = float_new(n, &pn);
    }

    /* Convert from string */
    else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_STR)
    {
        n = (float)atof((const char *)((pPmString_t)pv)->val);
        retval = float_new(n, &pn);
    }

    /* Raise TypeError if arg is not correct type */
    else

#endif

    /* turn this function in a dummy that always raise type error */
    {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
    }

    NATIVE_SET_TOS(pn);
    return retval;
    """
    pass

def int(v):
    """__NATIVE__
    int32_t n;
    pPmObj_t pv;
    pPmObj_t pn;
    PmReturn_t retval = PM_RET_OK;

    /* If wrong number of args, raise TypeError */
    if (NATIVE_GET_NUM_ARGS() != 1)
    {
        PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
    }

    pv = NATIVE_GET_LOCAL(0);

    /* No conversion required if already an integer */
    if (OBJ_GET_TYPE(pv) == OBJ_TYPE_INT)
    {
        n = ((pPmInt_t)pv)->val;
    }

    /* Convert from string */
    else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_STR)
    {
        n = (int)atoi((const char *)((pPmString_t)pv)->val);
    }

#ifdef HAVE_FLOAT
    /* Convert from float */
    else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_FLT)
    {
        n = (int)(((pPmFloat_t)pv)->val);
    }
#endif

    /* Raise TypeError if arg is not correct type */
    else
    {
       PM_RAISE(retval, PM_RET_EX_TYPE);
       return retval;
    }

    retval = int_new(n, &pn);
    NATIVE_SET_TOS(pn);
    return retval;
    """
    pass

Original comment by lucio.di...@googlemail.com on 11 Jul 2013 at 12:21