~jasper/type_stack_calc

507b2d7f3eb0714a9f532ec9ebf34d3493572d94 — Jasper den Ouden 2 years ago e3b237a
`if` stuff, need unpack potential variables & list-out a generator
2 files changed, 16 insertions(+), 6 deletions(-)

M type_stack_calc/call/if_call.py
M type_stack_calc/ib/if_block.py
M type_stack_calc/call/if_call.py => type_stack_calc/call/if_call.py +9 -4
@@ 12,19 12,24 @@ from type_stack_calc.base.component import BaseComponent, ReturnStop
from type_stack_calc.tp.intersection import TpIntersection
from type_stack_calc.tp.bool import TpBool

def de_var_1(got):
    return got.val if isinstance(got, BaseComponent) else got

def de_var(gen): return map(de_var_1, gen)

def ret_stack_smoosh(a, b):
    """Combine the return stacks, assuming both are possible."""
    if len(a) > 0 and a[-1] == ReturnStop:
        if len(b) > 0 and b[-1] == ReturnStop:  # Both return.
    if len(a) > 0 and de_var_1(a[-1]) == ReturnStop:
        if len(b) > 0 and de_var_1(b[-1]) == ReturnStop:  # Both return.
            return [ReturnStop]
        else:  # Only `a` returns, `b` goes on as stack.
            return b
    elif len(b) > 0 and b[-1] == ReturnStop:  # Only `b` returns.
    elif len(b) > 0 and de_var_1(b[-1]) == ReturnStop:  # Only `b` returns.
        return a
    else:  # Both go on stack, have to be combined.
        assert len(a) == len(b), \
            f"Smooshing stacks failed, differing lengths. ({len(a)} vs {len(b)})\n{a}, {b}"
        return [ea.smoosh(eb) for ea, eb in zip(a, b)]
        return [ea.smoosh(eb) for ea, eb in zip(de_var(a), de_var(b))]

class IfCall:
    """Code objects have a `.if` are set to this. Produces an IfBlock if needed."""

M type_stack_calc/ib/if_block.py => type_stack_calc/ib/if_block.py +7 -2
@@ 23,7 23,9 @@ class IfOneBranch:
        # Entirely extract condition.
        extractor[1](extract(reversed(self.cond + [PostExtractDrop])))
        # Extract as normal the body.
        extractor.extract_1(reversed(self.body))
        extractor.extract(reversed(self.body))

    extract_top = extract

class IfBlock:
    """`if_block.py`: `{..if..} {..else..} (..cond..).if` provider."""


@@ 34,7 36,7 @@ class IfBlock:
    def extract_cond(self, extractor, gen):
        lst = [] # Type-calced data needs reversing.
        Extractor((lst.append, extractor[1])).extract(reversed(self.cond))
        self.cond = list_flatten(lst)  # Remainder can stay.
        self.cond = list(list_flatten(lst))  # Remainder can stay.

    def extract(self, extractor, gen, _obj):
        self.extract_cond(extractor, gen)


@@ 75,3 77,6 @@ class IfBlock:
            wfile(prep + "}")
        else:
            wfile(prep + "}")

    def __repr__(self):
        return '{' + f"ifblock:{self.cond}:{self.yes}:{self.no}" + '}'