[pypy-svn] r47919 - in pypy/dist/pypy/lang/smalltalk: . test

akuhn at codespeak.net akuhn at codespeak.net
Thu Oct 25 14:17:26 CEST 2007


Author: akuhn
Date: Thu Oct 25 14:17:25 2007
New Revision: 47919

Modified:
   pypy/dist/pypy/lang/smalltalk/constants.py
   pypy/dist/pypy/lang/smalltalk/primitives.py
   pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
Log:
(akuhn, oscar)
implemented three missing float primitives


Modified: pypy/dist/pypy/lang/smalltalk/constants.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/constants.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/constants.py	Thu Oct 25 14:17:25 2007
@@ -52,7 +52,9 @@
 SO_CHARACTER_CLASS = 19
 SO_DOES_NOT_UNDERSTAND = 20
 SO_CANNOT_RETURN = 21
-# no clue what 22 is doing
+
+# XXX no clue what 22 is doing, lookup in Squeak: ObjectMemory >> initializeSpecialObjectIndices
+
 SO_SPECIAL_SELECTORS_ARRAY = 23
 SO_CHARACTER_TABLE_ARRAY = 24
 SO_MUST_BE_BOOLEAN = 25

Modified: pypy/dist/pypy/lang/smalltalk/primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/primitives.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/primitives.py	Thu Oct 25 14:17:25 2007
@@ -179,18 +179,32 @@
 _FLOAT_OFFSET = 40
 FLOAT_ADD = 41
 FLOAT_SUBTRACT = 42
+FLOAT_MULTIPLY = 49
+FLOAT_DIVIDE = 50
+FLOAT_TRUNCATED = 51
+
 math_ops = {
     FLOAT_ADD: operator.add,
-    FLOAT_SUBTRACT: operator.sub
+    FLOAT_SUBTRACT: operator.sub,
+    FLOAT_MULTIPLY: operator.mul,
+    FLOAT_DIVIDE: operator.div,
     }
 for (code,op) in math_ops.items():
+    @primitive(code)
     @stack(2)
     def func(args, (w_v1, w_v2), op=op): # n.b. capture op value
         v1 = unwrap_float(w_v1)
         v2 = unwrap_float(w_v2)
         w_res = objtable.wrap_float(op(v1, v2))
         return w_res
-    prim_table[code] = func
+
+ at primitive(FLOAT_TRUNCATED)
+ at stack(1)
+def func(args, (w_float,)): 
+    f = unwrap_float(w_float)
+    w_res = objtable.wrap_int(int(f))
+    return w_res
+
 
 # ___________________________________________________________________________
 # Subscript and Stream Primitives
@@ -571,6 +585,7 @@
 @primitive(PRIMITIVE_PERFORM)
 @stack(2)
 def func(args, (w_rcvr, w_sel)):
+    # XXX we can implement this when lookup on shadow class is done
     raise PrimitiveNotYetWrittenError()
 
 @primitive(PRIMITIVE_PERFORM_WITH_ARGS)

Modified: pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_primitives.py	(original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_primitives.py	Thu Oct 25 14:17:25 2007
@@ -139,9 +139,30 @@
     prim_fails(p.BIT_SHIFT, [4, 29])
     prim_fails(p.BIT_SHIFT, [4, 28])
 
-def test_float():
+def test_float_add():
     assert prim(p.FLOAT_ADD, [1.0,2.0]).value == 3.0
-    assert prim(p.FLOAT_ADD, [3,4.5]).value == 7.5
+    assert prim(p.FLOAT_ADD, [3.0,4.5]).value == 7.5
+
+def test_float_subtract():
+    assert prim(p.FLOAT_SUBTRACT, [1.0,2.0]).value == -1.0
+    assert prim(p.FLOAT_SUBTRACT, [15.0,4.5]).value == 10.5
+
+def test_float_multiply():
+    assert prim(p.FLOAT_MULTIPLY, [10.0,2.0]).value == 20.0
+    assert prim(p.FLOAT_MULTIPLY, [3.0,4.5]).value == 13.5
+
+def test_float_divide():
+    assert prim(p.FLOAT_DIVIDE, [1.0,2.0]).value == 0.5
+    assert prim(p.FLOAT_DIVIDE, [3.5,4.0]).value == 0.875
+
+def test_float_truncate():
+    assert prim(p.FLOAT_TRUNCATED, [-4.6]).value == -4
+    assert prim(p.FLOAT_TRUNCATED, [-4.5]).value == -4
+    assert prim(p.FLOAT_TRUNCATED, [-4.4]).value == -4
+    assert prim(p.FLOAT_TRUNCATED, [4.4]).value == 4
+    assert prim(p.FLOAT_TRUNCATED, [4.5]).value == 4
+    assert prim(p.FLOAT_TRUNCATED, [4.6]).value == 4
+
 
 def test_at():
     w_obj = mockclass(0, varsized=True).as_class_get_shadow().new(1)



More information about the Pypy-commit mailing list