예제 #1
0
파일: auditors.py 프로젝트: zarutian/typhon
 def coerce(self, specimen, ej):
     if specimen.auditedBy(self):
         return specimen
     from typhon.objects.constants import NullObject
     from typhon.objects.ejectors import throw
     throw(ej, NullObject)
     return NullObject
예제 #2
0
파일: auditors.py 프로젝트: dckc/typhon
 def coerce(self, specimen, ej):
     if specimen.auditedBy(transparentStamp):
         return specimen
     from typhon.objects.constants import NullObject
     from typhon.objects.ejectors import throw
     throw(ej, NullObject)
     return NullObject
예제 #3
0
def unwrapList(o, ej=None):
    from typhon.objects.refs import resolution
    l = resolution(o)
    if isinstance(l, ConstList):
        return l.strategy.fetch_all(l)
    if isinstance(l, FlexList):
        return l.strategy.fetch_all(l)
    throw(ej, StrObject(u"Not a list!"))
예제 #4
0
def unwrapList(o, ej=None):
    from typhon.objects.refs import resolution
    l = resolution(o)
    if isinstance(l, ConstList):
        return l.strategy.fetch_all(l)
    if isinstance(l, FlexList):
        return l.strategy.fetch_all(l)
    throw(ej, StrObject(u"Not a list!"))
예제 #5
0
def unsealException(specimen, ej):
    """
    Unseal a specimen.
    """

    if isinstance(specimen, SealedException):
        trail = ConstList([StrObject(s) for s in specimen.trail])
        return ConstList([specimen.value, trail])
    throw(ej, StrObject(u"Cannot unseal non-thrown object"))
예제 #6
0
 def listPattern(self, size):
     ej = self.pop()
     xs = unwrapList(self.pop(), ej)
     if len(xs) != size:
         throw(ej, StrObject(u"Failed list pattern (needed %d, got %d)" % (size, len(xs))))
     while size:
         size -= 1
         self.push(xs[size])
         self.push(ej)
예제 #7
0
def unsealException(specimen, ej):
    """
    Unseal a specimen.
    """

    if isinstance(specimen, SealedException):
        trail = ConstList([StrObject(s) for s in specimen.trail])
        return ConstList([specimen.value, trail])
    throw(ej, StrObject(u"Cannot unseal non-thrown object"))
예제 #8
0
    def recv(self, atom, args):
        from typhon.nodes import Noun, Method, Obj
        from typhon.objects.equality import optSame, EQUAL
        from typhon.objects.user import Audition

        if atom is _UNCALL_0:
            from typhon.objects.collections.maps import EMPTY_MAP

            return ConstList([subrangeGuardMaker, StrObject(u"get"), ConstList([self.superGuard]), EMPTY_MAP])

        if atom is AUDIT_1:
            audition = args[0]
            if not isinstance(audition, Audition):
                raise userError(u"not invoked with an Audition")
            ast = audition.ast
            if not isinstance(ast, Obj):
                raise userError(u"audition not created with an object expr")
            methods = ast._script._methods
            for m in methods:
                if isinstance(m, Method) and m._verb == u"coerce":
                    mguard = m._g
                    if isinstance(mguard, Noun):
                        rGSG = audition.getGuard(mguard.name)
                        if isinstance(rGSG, FinalSlotGuard):
                            rGSG0 = rGSG.valueGuard
                            if isinstance(rGSG0, SameGuard):
                                resultGuard = rGSG0.value

                                if optSame(resultGuard, self.superGuard) is EQUAL or (
                                    SUPERSETOF_1 in self.superGuard.respondingAtoms()
                                    and self.superGuard.call(u"supersetOf", [resultGuard]) is wrapBool(True)
                                ):
                                    return wrapBool(True)
                                raise userError(
                                    u"%s does not have a result guard implying "
                                    u"%s, but %s" % (audition.fqn, self.superGuard.toQuote(), resultGuard.toQuote())
                                )
                            raise userError(
                                u"%s does not have a determinable "
                                u"result guard, but <& %s> :%s" % (audition.fqn, mguard.name, rGSG.toQuote())
                            )
                    break
            return self
        if atom is PASSES_1:
            return wrapBool(args[0].auditedBy(self))
        if atom is COERCE_2:
            specimen, ej = args[0], args[1]
            if specimen.auditedBy(self):
                return specimen
            c = specimen.call(u"_conformTo", [self])
            if c.auditedBy(self):
                return c
            throw(ej, StrObject(u"%s does not conform to %s" % (specimen.toQuote(), self.toQuote())))

        raise Refused(self, atom, args)
예제 #9
0
 def coerce(self, specimen, ej):
     specimen = resolution(specimen)
     val = self.subCoerce(specimen)
     if val is None:
         newspec = specimen.call(u"_conformTo", [self])
         val = self.subCoerce(newspec)
         if val is None:
             throw(ej, StrObject(u"%s does not conform to %s" % (specimen.toQuote(), self.toQuote())))
         else:
             return val
     else:
         return val
예제 #10
0
 def listPattern(self, size):
     ej = self.pop()
     xs = unwrapList(self.pop(), ej)
     if len(xs) != size:
         throw(
             ej,
             StrObject(u"Failed list pattern (needed %d, got %d)" %
                       (size, len(xs))))
     while size:
         size -= 1
         self.push(xs[size])
         self.push(ej)
예제 #11
0
    def recv(self, atom, args):
        from typhon.objects.constants import wrapBool, NullObject
        if atom is PASSES_1:
            return wrapBool(args[0].auditedBy(transparentStamp))

        if atom is COERCE_2:
            if args[0].auditedBy(transparentStamp):
                return args[0]
            from typhon.objects.ejectors import throw
            throw(args[1], NullObject)
            return NullObject

        raise Refused(self, atom, args)
예제 #12
0
    def recv(self, atom, args):
        from typhon.objects.constants import wrapBool, NullObject
        if atom is PASSES_1:
            return wrapBool(args[0].auditedBy(transparentStamp))

        if atom is COERCE_2:
            if args[0].auditedBy(transparentStamp):
                return args[0]
            from typhon.objects.ejectors import throw
            throw(args[1], NullObject)
            return NullObject

        raise Refused(self, atom, args)
예제 #13
0
def nearGuard(specimen, ej):
    """
    A guard over references to near values.

    This guard admits any near value, as well as any resolved reference to any
    near value.

    This guard is unretractable.
    """

    specimen = resolution(specimen)
    if isinstance(specimen, Promise):
        msg = u"Specimen is in non-near state %s" % specimen.state().repr
        throw(ej, StrObject(msg))
    return specimen
예제 #14
0
파일: guards.py 프로젝트: washort/typhon
 def coerce(self, specimen, ej):
     specimen = resolution(specimen)
     val = self.subCoerce(specimen)
     if val is None:
         newspec = specimen.call(u"_conformTo", [self])
         val = self.subCoerce(newspec)
         if val is None:
             throw(
                 ej,
                 StrObject(u"%s does not conform to %s" %
                           (specimen.toQuote(), self.toQuote())))
         else:
             return val
     else:
         return val
예제 #15
0
def nearGuard(specimen, ej):
    """
    A guard over references to near values.

    This guard admits any near value, as well as any resolved reference to any
    near value.

    This guard is unretractable.
    """

    specimen = resolution(specimen)
    if isinstance(specimen, Promise):
        msg = u"Specimen is in non-near state %s" % specimen.state().repr
        throw(ej, StrObject(msg))
    return specimen
예제 #16
0
    def recv(self, atom, args):
        from typhon.objects.constants import wrapBool
        from typhon.objects.constants import NullObject
        if atom is AUDIT_1:
            return wrapBool(True)

        if atom is PASSES_1:
            return wrapBool(args[0].auditedBy(selfless))

        if atom is COERCE_2:
            if args[0].auditedBy(selfless):
                return args[0]
            from typhon.objects.ejectors import throw
            throw(args[1], NullObject)
            return NullObject

        raise Refused(self, atom, args)
예제 #17
0
    def recv(self, atom, args):
        from typhon.objects.constants import wrapBool
        from typhon.objects.constants import NullObject
        if atom is AUDIT_1:
            return wrapBool(True)

        if atom is PASSES_1:
            return wrapBool(args[0].auditedBy(selfless))

        if atom is COERCE_2:
            if args[0].auditedBy(selfless):
                return args[0]
            from typhon.objects.ejectors import throw
            throw(args[1], NullObject)
            return NullObject

        raise Refused(self, atom, args)
예제 #18
0
    def recv(self, atom, args):
        if atom is COERCE_2:
            for g in self.subguards:
                with Ejector() as ej:
                    try:
                        return g.call(u"coerce", [args[0], ej])
                    except Ejecting as e:
                        if e.ejector is ej:
                            continue
            throw(args[1], StrObject(u"No subguards matched"))
        if atom is SUPERSETOF_1:
            for g in self.subguards:
                if not unwrapBool(g.call(u"supersetOf", [args[0]])):
                    return wrapBool(False)
            return wrapBool(True)

        if atom is _UNCALL_0:
            from typhon.objects.collections.maps import EMPTY_MAP

            return ConstList([anyGuard, StrObject(u"get"), ConstList(self.subguards), EMPTY_MAP])
        raise Refused(self, atom, args)
예제 #19
0
파일: guards.py 프로젝트: washort/typhon
    def recv(self, atom, args):
        if atom is COERCE_2:
            for g in self.subguards:
                with Ejector() as ej:
                    try:
                        return g.call(u"coerce", [args[0], ej])
                    except Ejecting as e:
                        if e.ejector is ej:
                            continue
            throw(args[1], StrObject(u"No subguards matched"))
        if atom is SUPERSETOF_1:
            for g in self.subguards:
                if not unwrapBool(g.call(u"supersetOf", [args[0]])):
                    return wrapBool(False)
            return wrapBool(True)

        if atom is _UNCALL_0:
            from typhon.objects.collections.maps import EMPTY_MAP
            return ConstList([
                anyGuard,
                StrObject(u"get"),
                ConstList(self.subguards), EMPTY_MAP
            ])
        raise Refused(self, atom, args)
예제 #20
0
파일: guards.py 프로젝트: washort/typhon
    def recv(self, atom, args):
        from typhon.nodes import Noun, Method, Obj
        from typhon.objects.equality import optSame, EQUAL
        from typhon.objects.user import Audition
        if atom is _UNCALL_0:
            from typhon.objects.collections.maps import EMPTY_MAP
            return ConstList([
                subrangeGuardMaker,
                StrObject(u"get"),
                ConstList([self.superGuard]), EMPTY_MAP
            ])

        if atom is AUDIT_1:
            audition = args[0]
            if not isinstance(audition, Audition):
                raise userError(u"not invoked with an Audition")
            ast = audition.ast
            if not isinstance(ast, Obj):
                raise userError(u"audition not created with an object expr")
            methods = ast._script._methods
            for m in methods:
                if isinstance(m, Method) and m._verb == u"coerce":
                    mguard = m._g
                    if isinstance(mguard, Noun):
                        rGSG = audition.getGuard(mguard.name)
                        if isinstance(rGSG, FinalSlotGuard):
                            rGSG0 = rGSG.valueGuard
                            if isinstance(rGSG0, SameGuard):
                                resultGuard = rGSG0.value

                                if (optSame(resultGuard,
                                            self.superGuard) is EQUAL or
                                    (SUPERSETOF_1
                                     in self.superGuard.respondingAtoms()
                                     and self.superGuard.call(
                                         u"supersetOf",
                                         [resultGuard]) is wrapBool(True))):
                                    return wrapBool(True)
                                raise userError(
                                    u"%s does not have a result guard implying "
                                    u"%s, but %s" %
                                    (audition.fqn, self.superGuard.toQuote(),
                                     resultGuard.toQuote()))
                            raise userError(
                                u"%s does not have a determinable "
                                u"result guard, but <& %s> :%s" %
                                (audition.fqn, mguard.name, rGSG.toQuote()))
                    break
            return self
        if atom is PASSES_1:
            return wrapBool(args[0].auditedBy(self))
        if atom is COERCE_2:
            specimen, ej = args[0], args[1]
            if specimen.auditedBy(self):
                return specimen
            c = specimen.call(u"_conformTo", [self])
            if c.auditedBy(self):
                return c
            throw(
                ej,
                StrObject(u"%s does not conform to %s" %
                          (specimen.toQuote(), self.toQuote())))

        raise Refused(self, atom, args)