示例#1
0
文件: state.py 项目: erp12/Pysh
    def __init__(self, type_library: PushTypeLibrary):
        super().__init__()
        self.stdout = ""
        self.inputs = []
        self.untyped = deque([])
        self.type_library = type_library

        for name, push_type in type_library.items():
            self[name] = PushStack(push_type)
示例#2
0
文件: state.py 项目: nayabur/pyshgp
    def __init__(self, type_library: PushTypeLibrary, push_config: PushConfig):
        super().__init__()
        self.stdout = ""
        self.inputs = []
        self.untyped = deque([])
        self.type_library = type_library
        self.push_config = push_config

        for name, push_type in type_library.items():
            self[name] = PushStack(push_type, push_config)
示例#3
0
文件: common.py 项目: erp12/Pysh
def instructions(type_library: PushTypeLibrary):
    """Return all core numeric instructions."""
    i = []

    for push_type in type_library.keys():
        i.append(SimpleInstruction(
            "{t}_pop".format(t=push_type),
            lambda x: [],
            input_stacks=[push_type],
            output_stacks=[],
            code_blocks=(1 if push_type == "exec" else 0),
            docstring="Pops the top {t}.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_dup".format(t=push_type),
            lambda x: [x, x],
            input_stacks=[push_type],
            output_stacks=[push_type, push_type],
            code_blocks=(1 if push_type == "exec" else 0),
            docstring="Duplicates the top {t}.".format(t=push_type)
        ))

        i.append(ProducesManyOfTypeInstruction(
            "{t}_dup_times".format(t=push_type),
            _dup_times,
            input_stacks=["int", push_type],
            output_stack=push_type,
            code_blocks=(1 if push_type == "exec" else 0),
            docstring="Duplicates the top {t} `n` times where `n` is from the int stack.".format(t=push_type)
        ))

        # Disabled due to performance issues.
        # i.append(StateToStateInstruction(
        #     "{t}_dup_top_n".format(t=push_type),
        #     _dup_top_n_factory(push_type),
        #     stacks_used=[push_type, "int"],
        #     code_blocks=0,
        #     docstring="Duplicates the top n items on the {t} stack.".format(t=push_type)
        # ))

        i.append(SimpleInstruction(
            "{t}_swap".format(t=push_type),
            lambda a, b: [a, b],
            input_stacks=[push_type, push_type],
            output_stacks=[push_type, push_type],
            code_blocks=(2 if push_type == "exec" else 0),
            docstring="Swaps the top two {t}s.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_rot".format(t=push_type),
            lambda a, b, c: [b, a, c],
            input_stacks=[push_type] * 3,
            output_stacks=[push_type] * 3,
            code_blocks=(3 if push_type == "exec" else 0),
            docstring="Rotates the top three {t}s.".format(t=push_type)
        ))

        i.append(StateToStateInstruction(
            "{t}_flush".format(t=push_type),
            _flusher(push_type),
            stacks_used=[push_type],
            code_blocks=0,
            docstring="Empties the {t} stack.".format(t=push_type)
        ))

        i.append(SimpleInstruction(
            "{t}_eq".format(t=push_type),
            lambda a, b: [a == b],
            input_stacks=[push_type, push_type],
            output_stacks=["bool"],
            code_blocks=0,
            docstring="Pushes True if the top two {t} are equal. Otherwise pushes False.".format(t=push_type)
        ))

        i.append(TakesStateInstruction(
            "{t}_stack_depth".format(t=push_type),
            _stack_depther(push_type),
            output_stacks=["int"],
            other_stacks=[push_type],
            code_blocks=0,
            docstring="Pushes the size of the {t} stack to the int stack.".format(t=push_type)
        ))

        i.append(StateToStateInstruction(
            "{t}_yank".format(t=push_type),
            _yanker(push_type),
            stacks_used=[push_type, "int"],
            code_blocks=0,
            docstring="Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top.".format(t=push_type)
        ))

        i.append(StateToStateInstruction(
            "{t}_yank_dup".format(t=push_type),
            _yank_duper(push_type),
            stacks_used=[push_type, "int"],
            code_blocks=0,
            docstring="Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top.".format(t=push_type)
        ))

        i.append(StateToStateInstruction(
            "{t}_shove".format(t=push_type),
            _shover(push_type),
            stacks_used=[push_type, "int"],
            code_blocks=(1 if push_type == "exec" else 0),
            docstring="Shoves the top {t} deep in the stack based on an index from the int stack.".format(t=push_type)
        ))

        i.append(StateToStateInstruction(
            "{t}_shove_dup".format(t=push_type),
            _shove_duper(push_type),
            stacks_used=[push_type, "int"],
            code_blocks=(1 if push_type == "exec" else 0),
            docstring="Shoves a copy of the top {t} deep in the stack based on an index from the int stack.".format(t=push_type)
        ))

        i.append(TakesStateInstruction(
            "{t}_is_empty".format(t=push_type),
            _is_emptyer(push_type),
            output_stacks=["bool"],
            other_stacks=[push_type],
            code_blocks=0,
            docstring="Pushes True if the {t} stack is empty. Pushes False otherwise.".format(t=push_type)
        ))

    for push_type_name, push_type in type_library.items():
        if push_type_name == "code":
            continue
        i.append(SimpleInstruction(
            "code_from_{t}".format(t=push_type_name),
            partial(_make_code, push_type=push_type),
            input_stacks=[push_type_name],
            output_stacks=["code"],
            code_blocks=(1 if push_type_name == "exec" else 0),
            docstring="Moves the top {t} to the code stack.".format(t=push_type_name)
        ))

    return i
示例#4
0
def instructions(type_library: PushTypeLibrary):
    """Return all core numeric instructions."""
    i = []

    for push_type in type_library.keys():
        i.append(
            SimpleInstruction(
                "{t}_pop".format(t=push_type),
                _noop,
                input_stacks=[push_type],
                output_stacks=[],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring="Pops the top {t}.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_dup".format(t=push_type),
                _dup,
                input_stacks=[push_type],
                output_stacks=[push_type, push_type],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring="Duplicates the top {t}.".format(t=push_type)))

        i.append(
            ProducesManyOfTypeInstruction(
                "{t}_dup_times".format(t=push_type),
                _dup_times,
                input_stacks=["int", push_type],
                output_stack=push_type,
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Duplicates the top {t} `n` times where `n` is from the int stack."
                .format(t=push_type)))

        # Disabled due to performance issues.
        # i.append(StateToStateInstruction(
        #     "{t}_dup_top_n".format(t=push_type),
        #     _dup_top_n_factory(push_type),
        #     stacks_used=[push_type, "int"],
        #     code_blocks=0,
        #     docstring="Duplicates the top n items on the {t} stack.".format(t=push_type)
        # ))

        i.append(
            SimpleInstruction(
                "{t}_swap".format(t=push_type),
                _swap,
                input_stacks=[push_type, push_type],
                output_stacks=[push_type, push_type],
                code_blocks=(2 if push_type == "exec" else 0),
                docstring="Swaps the top two {t}s.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_rot".format(t=push_type),
                _rot,
                input_stacks=[push_type] * 3,
                output_stacks=[push_type] * 3,
                code_blocks=(3 if push_type == "exec" else 0),
                docstring="Rotates the top three {t}s.".format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_flush".format(t=push_type),
                partial(_flush, type_name=push_type),
                stacks_used=[push_type],
                code_blocks=0,
                docstring="Empties the {t} stack.".format(t=push_type)))

        i.append(
            SimpleInstruction(
                "{t}_eq".format(t=push_type),
                _eq,
                input_stacks=[push_type, push_type],
                output_stacks=["bool"],
                code_blocks=0,
                docstring=
                "Pushes True if the top two {t} are equal. Otherwise pushes False."
                .format(t=push_type)))

        i.append(
            TakesStateInstruction(
                "{t}_stack_depth".format(t=push_type),
                partial(_stack_depth, type_name=push_type),
                output_stacks=["int"],
                other_stacks=[push_type],
                code_blocks=0,
                docstring="Pushes the size of the {t} stack to the int stack.".
                format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_yank".format(t=push_type),
                partial(_yank, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=0,
                docstring=
                "Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_yank_dup".format(t=push_type),
                partial(_yank_dup, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=0,
                docstring=
                "Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_shove".format(t=push_type),
                partial(_shove, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Shoves the top {t} deep in the stack based on an index from the int stack."
                .format(t=push_type)))

        i.append(
            StateToStateInstruction(
                "{t}_shove_dup".format(t=push_type),
                partial(_shove_dup, type_name=push_type),
                stacks_used=[push_type, "int"],
                code_blocks=(1 if push_type == "exec" else 0),
                docstring=
                "Shoves a copy of the top {t} deep in the stack based on an index from the int stack."
                .format(t=push_type)))

        i.append(
            TakesStateInstruction(
                "{t}_is_empty".format(t=push_type),
                partial(_is_empty, type_name=push_type),
                output_stacks=["bool"],
                other_stacks=[push_type],
                code_blocks=0,
                docstring=
                "Pushes True if the {t} stack is empty. Pushes False otherwise."
                .format(t=push_type)))

    for push_type_name, push_type in type_library.items():
        if push_type_name == "code":
            continue
        i.append(
            SimpleInstruction(
                "code_from_{t}".format(t=push_type_name),
                partial(_make_code, push_type=push_type),
                input_stacks=[push_type_name],
                output_stacks=["code"],
                code_blocks=(1 if push_type_name == "exec" else 0),
                docstring="Moves the top {t} to the code stack.".format(
                    t=push_type_name)))

    return i