Пример #1
0
    def resolve(self, collection):
        if self._resolved is not None:
            return

        if self.name == "#=":
            self._resolved = ObjectMemberGetterInvocation(
                self.name,
                collection.get_delegation().get_typename(), self.modifier)
            return

        item = collection.get(self.name)
        if item is not None:
            self._resolved = ObjectMemberGetterInvocation(
                self.name, item.object.get_typename(), self.modifier)
            return

        from machaon.core.message import select_method
        delg = collection.get_delegation()
        if delg is not None and not (self.modifier & INVOCATION_BASE_RECIEVER):
            self._resolved = select_method(self.name,
                                           delg.type,
                                           reciever=delg.value,
                                           modbits=self.modifier)
            self.modifier |= INVOCATION_DELEGATED_RECIEVER
        else:
            self._resolved = select_method(self.name, modbits=self.modifier)
            if isinstance(self._resolved, InstanceMethodInvocation
                          ):  # ObjectCollectionのインスタンスメソッドは使用しない
                raise BadObjectMemberInvocation(self.name)

        self.must_be_resolved()
Пример #2
0
def test_objcol_select():
    StrType = fundamental_type.get("Str")

    # delegate有り
    col = ObjectCollection()
    col.push("apple", Object(StrType, "リンゴ"))
    col.push("gorilla", Object(StrType, "ゴリラ"))
    col.push("trumpet", Object(StrType, "ラッパ"))
    col.set_delegation(Object(StrType, "コレクション"))

    ColType = fundamental_type.get("ObjectCollection")
    om = select_method("apple", ColType, reciever=col)
    assert om
    assert om.display() == ("ObjectMember", "apple", "")

    # メソッドの移譲
    dm = select_method("startswith", ColType, reciever=col)
    assert dm
    assert dm.display() == ("ObjectMember", "startswith", "")

    # delegate無し
    col.set_delegation(None)

    om = select_method("gorilla", ColType, reciever=col)
    assert om
    assert om.display() == ("ObjectMember", "gorilla", "")
Пример #3
0
def test_anyobject_method_select():
    AnyType = fundamental_type.get("Any")

    im = select_method("instance-method", AnyType)
    assert im
    assert im.display() == ("InstanceMethod", "instance_method", "")

    gm = select_method("<", AnyType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:less", "")
Пример #4
0
 def make_compare_operator(self, context, lessthan=True):
     typ = self.current_type(context)
     if lessthan:
         inv = select_method("lt", typ)
     else:
         inv = select_method("!lt", typ)
     if inv is None:
         return None
     inv.set_result_typehint("Bool")
     return inv
Пример #5
0
def test_straight_select():
    StrType = fundamental_type.get("Str")

    tm = select_method("reg-match", StrType)
    assert tm
    assert tm.display() == ("TypeMethod", "Str:reg-match", "")

    im = select_method("startswith", StrType)
    assert im
    assert im.display() == ("InstanceMethod", "startswith", "")

    gm = select_method("+", StrType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:add", "")
Пример #6
0
def test_objcol_no_delegation():
    col = ObjectCollection()
    col.push("apple", Object(StrType, "リンゴ"))

    # ObjectCollectionのinstance method (失敗する)
    dm = select_method("startswith", ColType, reciever=col)
    assert dm
    assert dm.display() == ("ObjectMember", "startswith", "")
Пример #7
0
 def eval(self, subject, context):
     # 呼び出しの度に解決する
     inv = select_method(self._method, subject.type, reciever=subject.value)
     fn = MemberGetter(self._method, inv)
     obj = fn.run_function(subject, context)
     # 共通型を定義しなおす:値型ではなく、宣言された型を参照する
     decltype = context.get_type(inv.get_result_spec().get_typename())
     self._compose_last_eval_type(decltype, context)
     return obj
Пример #8
0
def test_modified_select():
    StrType = fundamental_type.get("Str")

    tm = select_method("!reg-search", StrType)
    assert tm
    assert tm.display() == ("TypeMethod", "Str:reg-search", "negate")

    im = select_method("!startswith", StrType)
    assert im
    assert im.display() == ("InstanceMethod", "startswith", "negate")

    gm = select_method("~in", StrType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:is-in",
                            "reverse-args")

    gm = select_method("`=", StrType)
    assert gm
    assert gm.display() == ("TypeMethod", "GenericMethods:identical", "basic")