[pypy-svn] r13719 - in pypy/dist/pypy/rpython: . test
mwh at codespeak.net
mwh at codespeak.net
Thu Jun 23 16:39:24 CEST 2005
Author: mwh
Date: Thu Jun 23 16:39:21 2005
New Revision: 13719
Modified:
pypy/dist/pypy/rpython/llinterp.py
pypy/dist/pypy/rpython/rint.py
pypy/dist/pypy/rpython/test/test_rint.py
Log:
implementation of str for ints.
also, a little more llinterp.
(mwh, pedronis)
Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py (original)
+++ pypy/dist/pypy/rpython/llinterp.py Thu Jun 23 16:39:21 2005
@@ -259,7 +259,7 @@
for typ in (float, int):
typname = typ.__name__
- optup = ('add', 'sub', 'mul', 'div', 'gt', 'lt', 'ge', 'ne', 'le', 'eq')
+ optup = ('add', 'sub', 'mul', 'div', 'mod', 'gt', 'lt', 'ge', 'ne', 'le', 'eq')
if typ is int:
optup += 'truediv', 'floordiv'
for opname in optup:
@@ -271,7 +271,7 @@
func = opimpls[%(opname)r]
return func(x, y)
""" % locals()).compile()
- for opname in 'is_true',:
+ for opname in 'is_true', 'neg':
assert opname in opimpls
exec py.code.Source("""
def %(typname)s_%(opname)s(x):
Modified: pypy/dist/pypy/rpython/rint.py
==============================================================================
--- pypy/dist/pypy/rpython/rint.py (original)
+++ pypy/dist/pypy/rpython/rint.py Thu Jun 23 16:39:21 2005
@@ -1,7 +1,8 @@
from pypy.annotation.pairtype import pairtype
from pypy.annotation import model as annmodel
from pypy.objspace.flow.objspace import op_appendices
-from pypy.rpython.lltype import Signed, Unsigned, Bool, Float, Void, Char
+from pypy.rpython.lltype import Signed, Unsigned, Bool, Float, Void, Char, \
+ GcArray, malloc
from pypy.rpython.rmodel import Repr, TyperError, IntegerRepr, CharRepr
from pypy.rpython.robject import PyObjRepr, pyobj_repr
@@ -264,6 +265,40 @@
vlist = hop.inputargs(Float)
return vlist[0]
+ def rtype_str(_, hop):
+ varg = hop.inputarg(hop.args_r[0], 0)
+ return hop.gendirectcall(ll_int2str, varg)
+
+CHAR_ARRAY = GcArray(Char)
+
+def ll_int2str(i):
+ from pypy.rpython.rstr import STR
+ temp = malloc(CHAR_ARRAY, 20)
+ len = 0
+ sign = 0
+ if i < 0:
+ sign = 1
+ i = -i
+ if i == 0:
+ len = 1
+ temp[0] = '0'
+ else:
+ while i:
+ temp[len] = chr(i%10+ord('0'))
+ i /= 10
+ len += 1
+ len += sign
+ result = malloc(STR, len)
+ if sign:
+ result.chars[0] = '-'
+ j = 1
+ else:
+ j = 0
+ while j < len:
+ result.chars[j] = temp[len-j-1]
+ j += 1
+ return result
+
#
# _________________________ Conversions _________________________
Modified: pypy/dist/pypy/rpython/test/test_rint.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rint.py (original)
+++ pypy/dist/pypy/rpython/test/test_rint.py Thu Jun 23 16:39:21 2005
@@ -2,7 +2,7 @@
from pypy.rpython.rtyper import RPythonTyper
from pypy.annotation import model as annmodel
from pypy.rpython.test import snippet
-from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.test.test_llinterp import interpret, make_interpreter
class TestSnippet(object):
@@ -49,3 +49,18 @@
res = interpret(dummyfn, [ord('a')])
assert res == 'a'
+def test_str_of_int():
+ def dummy(i):
+ return str(i)
+
+ ev_fun = make_interpreter(dummy, [0])
+
+ res = ev_fun(0)
+ assert ''.join(res.chars) == '0'
+
+ res = ev_fun(1034)
+ assert ''.join(res.chars) == '1034'
+
+ res = ev_fun(-123)
+ assert ''.join(res.chars) == '-123'
+
More information about the Pypy-commit
mailing list