[pypy-svn] r26460 - in pypy/dist/pypy/translator/cli: . src test
antocuni at codespeak.net
antocuni at codespeak.net
Thu Apr 27 21:30:15 CEST 2006
Author: antocuni
Date: Thu Apr 27 21:30:05 2006
New Revision: 26460
Added:
pypy/dist/pypy/translator/cli/test/test_dict.py (contents, props changed)
Modified:
pypy/dist/pypy/translator/cli/cts.py
pypy/dist/pypy/translator/cli/src/pypylib.cs
Log:
Added some support for dicts. By now all operation are supported
except for iteration.
Modified: pypy/dist/pypy/translator/cli/cts.py
==============================================================================
--- pypy/dist/pypy/translator/cli/cts.py (original)
+++ pypy/dist/pypy/translator/cli/cts.py Thu Apr 27 21:30:05 2006
@@ -4,9 +4,10 @@
import exceptions
-from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
+#from pypy.rpython.lltypesystem.lltype import Signed, Unsigned, Void, Bool, Float
from pypy.rpython.lltypesystem.lltype import SignedLongLong, UnsignedLongLong
-from pypy.rpython.ootypesystem.ootype import Instance, Class, StaticMethod, List, Record
+#from pypy.rpython.ootypesystem.ootype import Instance, Class, StaticMethod, List, Record, Dict
+from pypy.rpython.ootypesystem import ootype
from pypy.translator.cli.option import getoption
from pypy.translator.cli import oopspec
@@ -16,20 +17,24 @@
py.log.setconsumer("cli", ansi_log)
PYPY_LIST = '[pypylib]pypy.runtime.List`1<%s>'
+PYPY_DICT = '[pypylib]pypy.runtime.Dict`2<%s, %s>'
_lltype_to_cts = {
- Void: 'void',
- Signed: 'int32',
- Unsigned: 'unsigned int32',
+ ootype.Void: 'void',
+ ootype.Signed: 'int32',
+ ootype.Unsigned: 'unsigned int32',
SignedLongLong: 'int64',
UnsignedLongLong: 'unsigned int64',
- Bool: 'bool',
- Float: 'float64',
- Class: 'class [mscorlib]System.Type',
-
- # TODO: it seems a hack
- List.SELFTYPE_T: 'class ' + (PYPY_LIST % '!0'),
- List.ITEMTYPE_T: '!0',
+ ootype.Bool: 'bool',
+ ootype.Float: 'float64',
+ ootype.Class: 'class [mscorlib]System.Type',
+
+ # maps generic types to their ordinal
+ ootype.List.SELFTYPE_T: 'class ' + (PYPY_LIST % '!0'),
+ ootype.List.ITEMTYPE_T: '!0',
+ ootype.Dict.SELFTYPE_T: 'class ' + (PYPY_DICT % ('!0', '!1')),
+ ootype.Dict.KEYTYPE_T: '!0',
+ ootype.Dict.VALUETYPE_T: '!1',
}
_pyexception_to_cts = {
@@ -59,17 +64,21 @@
return result
def lltype_to_cts(self, t, include_class=True):
- if isinstance(t, Instance):
+ if isinstance(t, ootype.Instance):
self.db.pending_class(t)
return self.__class(t._name, include_class)
- elif isinstance(t, Record):
+ elif isinstance(t, ootype.Record):
name = self.db.pending_record(t)
return self.__class(name, include_class)
- elif isinstance(t, StaticMethod):
+ elif isinstance(t, ootype.StaticMethod):
return 'void' # TODO: is it correct to ignore StaticMethod?
- elif isinstance(t, List):
+ elif isinstance(t, ootype.List):
item_type = self.lltype_to_cts(t._ITEMTYPE)
return self.__class(PYPY_LIST % item_type, include_class)
+ elif isinstance(t, ootype.Dict):
+ key_type = self.lltype_to_cts(t._KEYTYPE)
+ value_type = self.lltype_to_cts(t._VALUETYPE)
+ return self.__class(PYPY_DICT % (key_type, value_type), include_class)
return _get_from_dict(_lltype_to_cts, t, 'Unknown type %s' % t)
@@ -86,7 +95,7 @@
ret_type, ret_var = self.llvar_to_cts(graph.getreturnvar())
func_name = func_name or graph.name
- args = [arg for arg in graph.getargs() if arg.concretetype is not Void]
+ args = [arg for arg in graph.getargs() if arg.concretetype is not ootype.Void]
if is_method:
args = args[1:]
@@ -97,13 +106,13 @@
def method_signature(self, obj, name):
# TODO: use callvirt only when strictly necessary
- if isinstance(obj, Instance):
+ if isinstance(obj, ootype.Instance):
owner, meth = obj._lookup(name)
class_name = obj._name
full_name = 'class %s::%s' % (class_name, name)
return self.graph_to_signature(meth.graph, True, full_name), True
- elif isinstance(obj, List):
+ elif isinstance(obj, ootype.BuiltinType):
meth = oopspec.get_method(obj, name)
class_name = self.lltype_to_cts(obj)
ret_type = self.lltype_to_cts(meth.RESULT)
Modified: pypy/dist/pypy/translator/cli/src/pypylib.cs
==============================================================================
--- pypy/dist/pypy/translator/cli/src/pypylib.cs (original)
+++ pypy/dist/pypy/translator/cli/src/pypylib.cs Thu Apr 27 21:30:05 2006
@@ -80,4 +80,37 @@
}
*/
}
+
+ public class Dict<TKey, TValue>: System.Collections.Generic.Dictionary<TKey, TValue>
+ {
+ public int ll_length()
+ {
+ return this.Count;
+ }
+
+ public TValue ll_get(TKey key)
+ {
+ return this[key];
+ }
+
+ public void ll_set(TKey key, TValue value)
+ {
+ this[key] = value;
+ }
+
+ public bool ll_remove(TKey key)
+ {
+ return this.Remove(key);
+ }
+
+ public bool ll_contains(TKey key)
+ {
+ return this.ContainsKey(key);
+ }
+
+ public void ll_clear()
+ {
+ this.Clear();
+ }
+ }
}
Added: pypy/dist/pypy/translator/cli/test/test_dict.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/cli/test/test_dict.py Thu Apr 27 21:30:05 2006
@@ -0,0 +1,8 @@
+from pypy.translator.cli.test.runtest import check
+
+
+def test_dict():
+ def func(x, y):
+ d = {x: x+1, y: y+1}
+ return d[x]
+ check(func, [int, int], (42, 13))
More information about the Pypy-commit
mailing list