Esempio n. 1
0
 def testHashEqual(self):
     d = monteSet()
     d[IntObject(42)] = None
     d[CharObject(u'¡')] = None
     a = ConstSet(d)
     b = ConstSet(d)
     self.assertEqual(a.hash(), b.hash())
Esempio n. 2
0
 def testHashEqual(self):
     d = monteSet()
     d[IntObject(42)] = None
     d[CharObject(u'¡')] = None
     a = ConstSet(d)
     b = ConstSet(d)
     self.assertEqual(a.hash(), b.hash())
Esempio n. 3
0
    def recv(self, atom, args):
        if atom is GETDOCSTRING_0:
            if self.docstring is not None:
                return StrObject(self.docstring)
            return NullObject

        if atom is GETMETHODS_0:
            d = monteSet()
            for atom in self.atoms:
                d[ComputedMethod(atom.arity, None, atom.verb)] = None
            return ConstSet(d)

        raise Refused(self, atom, args)
Esempio n. 4
0
    def recv(self, atom, args):
        if atom is COERCE_2:
            return args[0]

        if atom is SUPERSETOF_1:
            return wrapBool(True)

        if atom is EXTRACTGUARDS_2:
            g = args[0]
            ej = args[1]
            if isinstance(g, AnyOfGuard):
                return ConstList(g.subguards)
            else:
                ej.call(u"run", [StrObject(u"Not an AnyOf guard")])

        if atom is GETMETHODS_0:
            return ConstSet(monteSet())

        if atom.verb == u"get":
            return AnyOfGuard(args)

        raise Refused(self, atom, args)
Esempio n. 5
0
    def recv(self, atom, args):
        if atom is ADD_1:
            other = args[0]
            if isinstance(other, BytesObject):
                return BytesObject(self._bs + other._bs)
            if isinstance(other, IntObject):
                return BytesObject(self._bs + str(chr(other._i)))

        if atom is ASLIST_0:
            from typhon.objects.collections.lists import ConstList
            return ConstList(self.asList())

        if atom is ASSET_0:
            from typhon.objects.collections.sets import ConstSet
            return ConstSet(self.asSet())

        if atom is CONTAINS_1:
            needle = args[0]
            if isinstance(needle, IntObject):
                return wrapBool(chr(needle._i) in self._bs)
            if isinstance(needle, BytesObject):
                return wrapBool(needle._bs in self._bs)

        if atom is GET_1:
            index = unwrapInt(args[0])
            if not 0 <= index < len(self._bs):
                raise userError(u"string.get/1: Index out of bounds: %d" %
                                index)
            return IntObject(ord(self._bs[index]))

        if atom is INDEXOF_1:
            needle = unwrapBytes(args[0])
            return IntObject(self._bs.find(needle))

        if atom is INDEXOF_2:
            needle = unwrapBytes(args[0])
            offset = unwrapInt(args[1])
            if offset < 0:
                raise userError(u"indexOf/2: Negative offset %d not supported"
                                % offset)
            return IntObject(self._bs.find(needle, offset))

        if atom is JOIN_1:
            from typhon.objects.collections.lists import unwrapList
            return BytesObject(self.join(unwrapList(args[0])))

        if atom is LASTINDEXOF_1:
            needle = unwrapBytes(args[0])
            return IntObject(self._bs.rfind(needle))

        if atom is MULTIPLY_1:
            amount = args[0]
            if isinstance(amount, IntObject):
                return BytesObject(self._bs * amount._i)

        if atom is OP__CMP_1:
            return polyCmp(self._bs, unwrapBytes(args[0]))

        if atom is REPLACE_2:
            return BytesObject(replace(self._bs,
                                       unwrapBytes(args[0]),
                                       unwrapBytes(args[1])))

        if atom is SIZE_0:
            return IntObject(len(self._bs))

        if atom is SLICE_1:
            start = unwrapInt(args[0])
            if start < 0:
                raise userError(u"Slice start cannot be negative")
            return BytesObject(self._bs[start:])

        if atom is SLICE_2:
            start = unwrapInt(args[0])
            stop = unwrapInt(args[1])
            if start < 0:
                raise userError(u"Slice start cannot be negative")
            if stop < 0:
                raise userError(u"Slice stop cannot be negative")
            return BytesObject(self._bs[start:stop])

        if atom is SPLIT_1:
            from typhon.objects.collections.lists import ConstList
            return ConstList(self.split(unwrapBytes(args[0])))

        if atom is SPLIT_2:
            from typhon.objects.collections.lists import ConstList
            return ConstList(self.split(unwrapBytes(args[0]),
                                        unwrapInt(args[1])))

        if atom is TOLOWERCASE_0:
            return BytesObject(self.toLowerCase())

        if atom is TOUPPERCASE_0:
            return BytesObject(self.toUpperCase())

        if atom is TRIM_0:
            return BytesObject(self.trim())

        if atom is WITH_1:
            return BytesObject(self._bs + chr(unwrapInt(args[0])))

        if atom is _MAKEITERATOR_0:
            return bytesIterator(self._bs)

        raise Refused(self, atom, args)
Esempio n. 6
0
 def testToString(self):
     d = monteSet()
     d[IntObject(42)] = None
     self.assertEqual(ConstSet(d).toString(), u"[42].asSet()")
Esempio n. 7
0
 def testToStringEmpty(self):
     d = monteSet()
     self.assertEqual(ConstSet(d).toString(), u"[].asSet()")
Esempio n. 8
0
    def _recv(self, atom, args):
        if atom is _UNCALL_0:
            from typhon.objects.collections.maps import EMPTY_MAP
            return ConstList([
                self.snapshot(),
                StrObject(u"diverge"),
                ConstList([]), EMPTY_MAP
            ])

        if atom is ADD_1:
            other = args[0]
            return ConstList(self.strategy.fetch_all(self) + unwrapList(other))

        if atom is ASMAP_0:
            from typhon.objects.collections.maps import ConstMap
            return ConstMap(self.asMap())

        if atom is ASSET_0:
            from typhon.objects.collections.sets import ConstSet
            return ConstSet(self.asSet())

        if atom is DIVERGE_0:
            return FlexList(self.strategy.fetch_all(self))

        if atom is EXTEND_1:
            from typhon.objects.refs import resolution
            l = resolution(args[0])
            # The early exits are essential here; without them, we might pass
            # an empty list to strategy.append(), which causes a crash. ~ C.
            if isinstance(l, ConstList):
                if l.size() == 0:
                    return NullObject
                data = l.strategy.fetch_all(l)
            elif isinstance(l, FlexList):
                if l.size() == 0:
                    return NullObject
                data = l.strategy.fetch_all(l)
            else:
                data = listFromIterable(l)[:]
            self.strategy.append(self, data)
            return NullObject

        if atom is GET_1:
            # Lookup by index.
            index = unwrapInt(args[0])
            if index >= self.strategy.size(self) or index < 0:
                raise userError(u"Index %d is out of bounds" % index)
            return self.strategy.fetch(self, index)

        if atom is INDEXOF_1:
            return IntObject(self.indexOf(args[0]))

        if atom is INSERT_2:
            index = unwrapInt(args[0])
            value = args[1]
            if index < 0:
                raise userError(u"Index %d is out of bounds" % index)
            self.strategy.insert(self, index, [value])
            return NullObject

        if atom is LAST_0:
            size = self.strategy.size(self)
            if size:
                return self.strategy.fetch(self, size - 1)
            raise userError(u"Empty list has no last element")

        if atom is MULTIPLY_1:
            # multiply/1: Create a new list by repeating this list's contents.
            index = unwrapInt(args[0])
            return ConstList(self.strategy.fetch_all(self) * index)

        if atom is POP_0:
            try:
                return self.strategy.pop(self, self.strategy.size(self) - 1)
            except IndexError:
                raise userError(u"pop/0: Pop from empty list")

        if atom is PUSH_1:
            self.strategy.append(self, args)
            return NullObject

        if atom is PUT_2:
            # Replace by index.
            index = unwrapInt(args[0])
            return self.put(index, args[1])

        if atom is REVERSE_0:
            new = self.strategy.fetch_all(self)[:]
            new.reverse()
            return ConstList(new)

        if atom is REVERSEINPLACE_0:
            new = self.strategy.fetch_all(self)[:]
            new.reverse()
            self.strategy.store_all(self, new)
            return NullObject

        if atom is WITH_1:
            # with/1: Create a new list with an appended object.
            return ConstList(self.strategy.fetch_all(self) + args)

        if atom is WITH_2:
            # Make a new ConstList.
            return self.snapshot().put(unwrapInt(args[0]), args[1])

        raise Refused(self, atom, args)
Esempio n. 9
0
    def _recv(self, atom, args):
        if atom is ADD_1:
            other = unwrapList(args[0])
            if len(other):
                return ConstList(self.strategy.fetch_all(self) + other)
            else:
                return self

        if atom is ASMAP_0:
            from typhon.objects.collections.maps import ConstMap
            return ConstMap(self.asMap())

        if atom is ASSET_0:
            from typhon.objects.collections.sets import ConstSet
            return ConstSet(self.asSet())

        if atom is DIVERGE_0:
            return FlexList(self.strategy.fetch_all(self)[:])

        if atom is GET_1:
            # Lookup by index.
            index = unwrapInt(args[0])
            if index < 0:
                raise userError(u"Index %d cannot be negative" % index)
            if index >= self.strategy.size(self):
                raise userError(u"Index %d is out of bounds" % index)
            return self.strategy.fetch(self, index)

        if atom is INDEXOF_1:
            return IntObject(self.indexOf(args[0]))

        if atom is LAST_0:
            size = self.strategy.size(self)
            if size:
                return self.strategy.fetch(self, size - 1)
            raise userError(u"Empty list has no last element")

        if atom is MULTIPLY_1:
            # multiply/1: Create a new list by repeating this list's contents.
            count = unwrapInt(args[0])
            if count < 0:
                raise userError(u"Can't multiply list %d times" % count)
            elif count == 0:
                return ConstList([])
            return ConstList(self.strategy.fetch_all(self) * count)

        if atom is OP__CMP_1:
            other = unwrapList(args[0])
            return IntObject(self.cmp(other))

        if atom is REVERSE_0:
            # This might seem slightly inefficient, and it might be, but I
            # want to make it very clear to RPython that we are not mutating
            # the list after we assign it to the new object.
            new = self.strategy.fetch_all(self)[:]
            new.reverse()
            return ConstList(new)

        if atom is SORT_0:
            return self.sort()

        if atom is STARTOF_1:
            return IntObject(self.startOf(unwrapList(args[0])))

        if atom is STARTOF_2:
            start = unwrapInt(args[1])
            if start < 0:
                raise userError(u"startOf/2: Negative start %d not permitted" %
                                start)
            return IntObject(self.startOf(unwrapList(args[0]), start))

        if atom is WITH_1:
            # with/1: Create a new list with an appended object.
            return ConstList(self.strategy.fetch_all(self) + args)

        if atom is WITH_2:
            # Replace by index.
            index = unwrapInt(args[0])
            return self.put(index, args[1])

        if atom is _UNCALL_0:
            from typhon.scopes.safe import theMakeList
            from typhon.objects.collections.maps import EMPTY_MAP
            return ConstList([theMakeList, StrObject(u"run"), self, EMPTY_MAP])

        raise Refused(self, atom, args)
Esempio n. 10
0
    def recv(self, atom, args):
        # _makeIterator/0: Create an iterator for this collection's contents.
        if atom is _MAKEITERATOR_0:
            return self._makeIterator()

        if atom is _PRINTON_1:
            printer = args[0]
            self.printOn(printer)
            return NullObject

        # contains/1: Determine whether an element is in this collection.
        if atom is CONTAINS_1:
            return wrapBool(self.contains(args[0]))

        # size/0: Get the number of elements in the collection.
        if atom is SIZE_0:
            return IntObject(self.size())

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_1:
            start = unwrapInt(args[0])
            try:
                return self.slice(start)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # slice/1 and slice/2: Select a subrange of this collection.
        if atom is SLICE_2:
            start = unwrapInt(args[0])
            stop = unwrapInt(args[1])
            try:
                return self.slice(start, stop)
            except IndexError:
                raise userError(u"slice/1: Index out of bounds")

        # snapshot/0: Create a new constant collection with a copy of the
        # current collection's contents.
        if atom is SNAPSHOT_0:
            return self.snapshot()

        from typhon.objects.collections.lists import ConstList

        if atom is _UNCALL_0:
            return ConstList(self._uncall())

        if atom is ASSET_0:
            from typhon.objects.collections.sets import ConstSet
            return ConstSet(self.objectMap)

        if atom is DIVERGE_0:
            return FlexMap(self.objectMap)

        if atom is FETCH_2:
            key = args[0]
            thunk = args[1]
            rv = self.objectMap.get(key, None)
            if rv is None:
                rv = thunk.call(u"run", [])
            return rv

        if atom is GETKEYS_0:
            return ConstList(self.objectMap.keys())

        if atom is GETVALUES_0:
            return ConstList(self.objectMap.values())

        if atom is GET_1:
            key = args[0]
            try:
                return self.objectMap[key]
            except KeyError:
                raise userError(u"Key not found: %s" % (key.toString(), ))

        # or/1: Unify the elements of this collection with another.
        if atom is OR_1:
            return self._or(args[0])

        if atom is REVERSE_0:
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            # Reverse it!
            l.reverse()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is SORTKEYS_0:
            # Extract a list, sort it, pack it back into a dict.
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            KeySorter(l).sort()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is SORTVALUES_0:
            # Same as sortKeys/0.
            d = monteMap()
            l = [(k, v) for k, v in self.objectMap.iteritems()]
            ValueSorter(l).sort()
            for k, v in l:
                d[k] = v
            return ConstMap(d)

        if atom is WITH_2:
            # Replace by key.
            key = args[0]
            value = args[1]
            d = self.objectMap.copy()
            d[key] = value
            return ConstMap(d)

        if atom is WITHOUT_1:
            key = args[0]
            d = self.objectMap.copy()
            # Ignore the case where the key wasn't in the map.
            if key in d:
                del d[key]
            return ConstMap(d)

        if atom is PUT_2:
            key = args[0]
            value = args[1]
            self.put(key, value)
            return NullObject

        if atom is REMOVEKEY_1:
            key = args[0]
            self.removeKey(key)
            return NullObject

        if atom is POP_0:
            return self.pop()

        raise Refused(self, atom, args)