Commits

Anonymous committed 2b92d3d

Updates issue 154
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.

Comments (0)

Files changed (3)

 #
 #  The builtins are loaded by the interpreter.
 #  The user SHOULD NOT import this module directly.
+"""__NATIVE__
+/* Needed for atoi(), atof() in the int() and float() funcs */
+#include <stdlib.h>
+"""
 
 
 def abs(n):
 #    return n > 0 and n or -n
-    return (n, -n)[n < 0]
+#    return (n, -n)[n < 0]
+    return n if n >= 0 else -n
 
 
 def chr(n):
     return [x for x in s if f(x)]
 
 
+def float(v):
+    if type(v) == type(1.0):
+        return v
+    else:
+        return _float(v)
+
+
+def _float(v):
+    """__NATIVE__
+    float 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);
+
+    /* Convert from int */
+    if (OBJ_GET_TYPE(pv) == OBJ_TYPE_INT)
+    {
+        n = (float)(((pPmInt_t)pv)->val);
+    }
+
+    /* Convert from string */
+    else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_STR)
+    {
+        n = (float)atof((const char *)((pPmString_t)pv)->val);
+    }
+
+    /* Raise TypeError if arg is not correct type */
+    else
+    {
+       PM_RAISE(retval, PM_RET_EX_TYPE);
+       return retval;
+    }
+
+
+    retval = float_new(n, &pn);
+    NATIVE_SET_TOS(pn);
+    return retval;
+    """
+    pass
+
+
 def globals():
     """__NATIVE__
     pPmObj_t pr = C_NULL;
     pass
 
 
+def int(v):
+    if type(v) == type(0):
+        return v
+    else:
+        return _int(v)
+
+
+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);
+
+    /* Convert from float */
+    if (OBJ_GET_TYPE(pv) == OBJ_TYPE_FLT)
+    {
+        n = (int)(((pPmFloat_t)pv)->val);
+    }
+
+    /* Convert from string */
+    else if (OBJ_GET_TYPE(pv) == OBJ_TYPE_STR)
+    {
+        n = (int)atoi((const char *)((pPmString_t)pv)->val);
+    }
+
+    /* 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
+
+
 # Yields every (i)tem in the (s)equence; requires HAVE_GENERATORS
 #def iter(s):
 #    for i in s:

src/tests/system/t354.c

+/*
+# This file is Copyright 2010 Dean Hall.
+#
+# This file is part of the Python-on-a-Chip program.
+# Python-on-a-Chip is free software: you can redistribute it and/or modify
+# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
+#
+# Python-on-a-Chip is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
+# is seen in the file COPYING up one directory from this.
+*/
+
+/**
+ * System Test 354
+ */
+
+#include "pm.h"
+
+
+extern unsigned char usrlib_img[];
+
+
+int main(void)
+{
+    PmReturn_t retval;
+
+    retval = pm_init(MEMSPACE_PROG, usrlib_img);
+    PM_RETURN_IF_ERROR(retval);
+
+    retval = pm_run((uint8_t *)"t354");
+    return (int)retval;
+}

src/tests/system/t354.py

+#
+# PyMite - A flyweight Python interpreter for 8-bit and larger microcontrollers.
+# Copyright 2010 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.
+#
+
+#
+# System Test 354
+#
+
+assert int(1.0) == 1
+assert int(1.5) == 1
+assert int(1.9) == 1
+
+assert int(-1.0) == -1
+assert int(-1.5) == -1
+assert int(-1.9) == -1
+
+assert int("1.0") == 1
+assert int("1.5") == 1
+assert int("1.9") == 1
+
+assert int("-1.0") == -1
+assert int("-1.5") == -1
+assert int("-1.9") == -1
+
+
+assert float(1) == 1.0
+assert float(-1) == -1.0
+assert float("1.0") == 1.0
+assert float("1.5") == 1.5
+assert float("-1.5") == -1.5