Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
Changed code to construct frozenset object instead of set
Browse files Browse the repository at this point in the history
  • Loading branch information
BPYap committed Jul 6, 2018
1 parent 8d7e0c5 commit 5daac69
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 51 deletions.
5 changes: 5 additions & 0 deletions python/common/org/python/types/FrozenSet.java
Expand Up @@ -31,6 +31,11 @@ public FrozenSet(java.util.Set<org.python.Object> frozenSet) {
this.value = java.util.Collections.unmodifiableSet(frozenSet);
}

public FrozenSet(org.python.types.Set set) {
super();
this.value = java.util.Collections.unmodifiableSet(set.value);
}

@org.python.Method(
__doc__ = "frozenset() -> empty frozenset object" +
"frozenset(iterable) -> frozenset object\n" +
Expand Down
109 changes: 58 additions & 51 deletions voc/python/blocks.py
Expand Up @@ -173,65 +173,65 @@ def add_complex(self, value):
java.Init('org/python/types/Complex', 'D'),
)

def add_tuple(self, data):
self.add_opcodes(
java.New('org/python/types/Tuple'),
def _add_value(self, val):
if isinstance(val, bool):
if val is True:
self.add_opcodes(
JavaOpcodes.GETSTATIC('org/python/types/Bool', 'TRUE', 'Lorg/python/types/Bool;'),
)
else:
self.add_opcodes(
JavaOpcodes.GETSTATIC('org/python/types/Bool', 'FALSE', 'Lorg/python/types/Bool;'),
)

java.New('java/util/ArrayList'),
java.Init('java/util/ArrayList'),
)
elif isinstance(val, int):
self.add_int(val)

def add_value(val):
if isinstance(val, bool):
if val is True:
self.add_opcodes(
JavaOpcodes.GETSTATIC('org/python/types/Bool', 'TRUE', 'Lorg/python/types/Bool;'),
)
else:
self.add_opcodes(
JavaOpcodes.GETSTATIC('org/python/types/Bool', 'FALSE', 'Lorg/python/types/Bool;'),
)
elif isinstance(val, float):
self.add_opcodes(
java.New('org/python/types/Float'),
JavaOpcodes.LDC2_W(val),
java.Init('org/python/types/Float', 'D'),
)

elif isinstance(val, int):
self.add_int(val)
elif isinstance(val, str):
self.add_opcodes(
python.Str(val),
)

elif isinstance(val, float):
self.add_opcodes(
java.New('org/python/types/Float'),
JavaOpcodes.LDC2_W(val),
java.Init('org/python/types/Float', 'D'),
)
elif isinstance(val, bytes):
self.add_opcodes(
java.New('org/python/types/Bytes'),
JavaOpcodes.LDC_W(val.decode('ISO-8859-1')),
java.Init('org/python/types/Bytes', 'Ljava/lang/String;'),
)

elif isinstance(val, str):
self.add_opcodes(
python.Str(val),
)
elif isinstance(val, tuple):
self.add_tuple(val)

elif isinstance(val, bytes):
self.add_opcodes(
java.New('org/python/types/Bytes'),
JavaOpcodes.LDC_W(val.decode('ISO-8859-1')),
java.Init('org/python/types/Bytes', 'Ljava/lang/String;'),
)
elif isinstance(val, complex):
self.add_opcodes(
java.New('org/python/types/Complex'),
DCONST_val(val.real),
DCONST_val(val.imag),
java.Init('org/python/types/Complex', 'D', 'D'),
)

elif isinstance(val, tuple):
self.add_tuple(val)
elif isinstance(val, types.CodeType):
self.add_opcodes(
JavaOpcodes.ACONST_NULL()
)

elif isinstance(val, complex):
self.add_opcodes(
java.New('org/python/types/Complex'),
DCONST_val(val.real),
DCONST_val(val.imag),
java.Init('org/python/types/Complex', 'D', 'D'),
)
else:
raise RuntimeError("Unknown constant type %s" % type(val))

elif isinstance(val, types.CodeType):
self.add_opcodes(
JavaOpcodes.ACONST_NULL()
)
def add_tuple(self, data):
self.add_opcodes(
java.New('org/python/types/Tuple'),

else:
raise RuntimeError("Unknown constant type %s" % type(val))
java.New('java/util/ArrayList'),
java.Init('java/util/ArrayList'),
)

for value in data:
self.add_opcodes(
Expand All @@ -244,20 +244,27 @@ def add_value(val):
)
elif isinstance(value, frozenset):
self.add_opcodes(
java.New('org/python/types/FrozenSet'),
python.Set(),
)
for elt in value:
self.add_opcodes(
JavaOpcodes.DUP()
)

add_value(elt)
self._add_value(elt)

self.add_opcodes(
python.Set.add()
)
self.add_opcodes(
java.Init(
'org/python/types/FrozenSet',
'Lorg/python/types/Set;',
)
)
else:
add_value(value)
self._add_value(value)

self.add_opcodes(
java.List.add(),
Expand Down

0 comments on commit 5daac69

Please sign in to comment.