Commits

Anonymous committed a852cb3

#26: mainlining directly. Partial checkin for chr() and ord().

Comments (0)

Files changed (3)

 # LOG
 # ---
 #
+# 2006/11/24    #26: Implement more builtin functions
 # 2006/08/21    #28: Adapt native libs to use the changed func calls
 # 2002/12/20    Added globals, locals, range.
 # 2002/12/19    Created.
 
 #### FUNCS
 
+def chr(n):
+    """__NATIVE__
+    pPmObj_t ps;
+    pPmObj_t pn;
+    int32_t n;
+    PmReturn_t retval;
+
+    /* If wrong number of args, raise TypeError */
+    if (NATIVE_GET_NUM_ARGS() != 1)
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    pn = NATIVE_GET_LOCAL(0);
+    n = ((pPmInt_t)pn)->val;
+
+    /* Raise ValueError if arg is not int within range(256) */
+    if ((OBJ_GET_TYPE(*pn) != OBJ_TYPE_INT) || (n < 0) || (n > 255))
+
+    {
+        PM_RAISE(retval, PM_RET_EX_VAL);
+        return retval;
+    }
+
+    /* Create char string from  integer value */
+    retval = string_newFromChar((uint8_t)n, &ps);
+    NATIVE_SET_TOS(ps);
+    return retval;
+    """
+    pass
+
+
 def globals():
     """__NATIVE__
     pPmObj_t pr = C_NULL;
     pass
 
 
+def ord(s):
+    """__NATIVE__
+    pPmObj_t ps;
+    pPmObj_t pn;
+    int32_t n;
+    PmReturn_t retval;
+
+    /* If wrong number of args, raise TypeError */
+    if (NATIVE_GET_NUM_ARGS() != 1)
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    ps = NATIVE_GET_LOCAL(0);
+
+    /* Raise TypeError if arg is not string of length 1 */
+    if ((OBJ_GET_TYPE(*ps) != OBJ_TYPE_STR)
+        || (((pPmString_t)ps)->length != 1))
+
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    /* Get integer value of character */
+    n = ((pPmString_t)ps)->val[0];
+    retval = int_new(n, &pn);
+    NATIVE_SET_TOS(pn);
+    return retval;
+    """
+    pass
+
+
 def pow(x, y):
     """__NATIVE__
     pPmObj_t px;

src/tests/system/t026.c

+/*
+ * PyMite - A flyweight Python interpreter for 8-bit microcontrollers and more.
+ * Copyright 2002 Dean Hall
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+/**
+ * System Test 026
+ * Tests new builtin functions
+ *
+ * Log
+ * ---
+ *
+ * 2006/11/24   First.
+ */
+
+#include "pm.h"
+
+
+extern unsigned char usrlib_img[];
+
+
+int main(void)
+{
+    PmReturn_t retval;
+
+    retval = pm_init(MEMSPACE_FLASH, usrlib_img);
+    PM_RETURN_IF_ERROR(retval);
+
+    retval = pm_run((uint8_t *)"t026");
+    pm_reportResult(retval);
+    return (int)retval;
+}

src/tests/system/t026.py

+# PyMite - A flyweight Python interpreter for 8-bit microcontrollers and more.
+# Copyright 2002 Dean Hall
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program 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.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+
+#
+# System Test 026
+# Tests new builtin functions
+#
+
+c = ord("t")
+assert c == 116
+
+c = ord("\xff")
+assert c == 0xff
+
+c = ord("\0")
+assert c == 0
+
+n = chr(0)
+assert n == "\0"
+
+n = chr(255)
+assert n == "\xff"
+
+n = chr(116)
+assert n == "t"
+
+i = 32
+while i >= 0:
+    assert ord(chr(i)) == i
+    i -= 1