~jasper/type_stack_calc

d145fb1083d6c076b070e9fb7fd6a64e1cf0b216 — Jasper den Ouden 1 year, 6 months ago bce4b04
minor
M test/sets/intersection.py => test/sets/intersection.py +0 -1
@@ 18,7 18,6 @@ assert si(r(2,6), s(0,5)) == si(r(5,5))

is_empty(si(r(10, 12), s(2,20))[0])
is_empty(si(r(-10, 10), s(-11,30))[0])
is_empty(s(0,30).in_set_clamps_hard(1,10))

assert len(si(r(0,20), s(0,1))) == 2


M type_stack_calc/base/function.py => type_stack_calc/base/function.py +0 -1
@@ 116,7 116,6 @@ class BaseFunction(Named):

    def tp_calc(self, _sc, _args, _vs):
        assert False, "You must define `tp_calc` if deriving form `BaseFunction`."
        # return (self.tp(sc, args), (self, args))

    def __repr__(self):
        return "{}:{}:{}".format(self.repr_name, self.seen_name or "", self.n)

M type_stack_calc/ib/scope.py => type_stack_calc/ib/scope.py +1 -0
@@ 144,6 144,7 @@ def scopify(variables):
    return variables

class ScopeCall:
    """Gives a code object its own little scope."""
    def tp_calc(self, sc, vs, stack):
        scope = Scope(vs, may_set=True)
        scope.absorb(sc, stack[0])

M type_stack_calc/tp_calc.py => type_stack_calc/tp_calc.py +3 -10
@@ 29,7 29,6 @@ def interpret_word(word):
    """Interprets constant values."""
    x = interpret_num_word(word)
    if x is not None:
        # TODO for integers might want to indicate that.
        return TpIntersection(TpRange(x, x),)
    elif word[0] == '"':  # Constant string.
        return ConstStr(word[1:])


@@ 51,8 50,6 @@ class StackCalc:
        if tp_fun := getattr(val, 'tp_calc', None):
            if val.fun_tp == 'plain':
                stack = stack[:-1]  # Ditch the origin.
#            elif val.fun_tp == 'class_method':
#                assert isinstance(stack[-1], Scope)
            else:
                assert val.fun_tp == 'method', \
                    "Only `plain` and `method` function types permitted. Got: {val.fun_tp}"


@@ 66,7 63,7 @@ class StackCalc:
                assert len(inp) == n, \
                    f"Not enough arguments available {len(inp)} vs needed {n}\n  {val}\n\n{stack} {inp}"
            # Figure out the local variables that might be relevant.
            relevant_comp = dict()  # TODO relevant for what.
            relevant_comp = dict()  # NOTE: not really used much afaik.
            for a in inp:
                if isinstance(a, ScopeVariable):
                    relevant_comp[id(a)] = a


@@ 78,7 75,7 @@ class StackCalc:
            if not getattr(val, 'with_vars', None):
                inp = [(a.val if isinstance(a, BaseComponent) else a)
                       for a in inp]
                assert not any(isinstance(a, BaseComponent) for a in inp)
                assert not any(isinstance(a, (type(None), BaseComponent)) for a in inp)
            # Actually calculate the type, return new stack and result.
            stack, *tc_code = tp_fun(self, vs, inp)
            assert isinstance(stack, list), (val)


@@ 118,9 115,6 @@ class StackCalc:
                continue

            if word[:1] == '.' or word in self.special_fun:  # It's a component.
                assert len(stack) >= 1, "Trying to access, but empty stack."
                assert_acceptable_vals((stack[-1],), stack)

                got = stack[-1].get_comp(word[1:] if word[:1]=='.' else word)
            elif word == '\n':  # Newlines currently ignored.
                continue


@@ 136,9 130,8 @@ class StackCalc:
                assert getattr(got.val, 'tp_calc', None) is None, "BUG: cannot have both `raw_tp_calc` and `tp_calc`."
                plus, stack, *tc_code = raw_fun(self, code, vs, stack)
                inserted = plus + inserted  # Can add-in some code.
                yield from tc_code
            else:
                stack, *tc_code = self.deal_with_var(got, code, vs, stack)
                yield from tc_code # Insert the tc code.
            yield from tc_code

            assert_acceptable_vals(stack, got, code)