~jasper/type_stack_calc

66e6f9b3c0262f183929f115c65302f135344f5a — Jasper den Ouden 7 months ago 0507f59
Had issue with zero which was interpreted as Falsy without me being aware.. This fixes integermath test. Added acceptable-values-for-stack testing.
2 files changed, 16 insertions(+), 10 deletions(-)

M test/integermath.py
M type_stack_calc/tp_calc.py
M test/integermath.py => test/integermath.py +6 -2
@@ 28,7 28,7 @@ def add_n_stuff(p_d=0.9, n_max=4, min=-10, max=10,
            if r < 0:
                try:  # Should automatically skip it if not possible.
                    args = [randint(min, max) for i in range(n - 1)]
                    val = fun(val, *args)
                    val = fun(val, *args)  # Update value and code to test.
                    s += ' ' + ' '.join(map(str, args)) + ' ' + k
                    break
                except:


@@ 41,8 41,12 @@ from type_stack_calc.intro import intro
from type_stack_calc.util.scopify import scopify

def ignore(_x): pass

vs = scopify(intro)
assert vs.get('Set')

for i in range(10):
    got, = StackCalc().tp_calc(parser(f"{i}"), vs, [], ignore)
    assert got.single(i), (i, got)

# Introduce this function.
StackCalc().tp_calc(parser("(x) {x x *}.fun sqr.set ;"), vs, [], ignore)

M type_stack_calc/tp_calc.py => type_stack_calc/tp_calc.py +10 -8
@@ 13,11 13,11 @@ from type_stack_calc.tp.other_sets import Range
from type_stack_calc.tp.str import ConstStr
import re

def check_no_none(in_gen, *ctx):
def assert_acceptable_vals(in_gen, *ctx):
    ctx = '\n'.join(map(repr, ctx))
    for i, el in enumerate(in_gen):
        assert el is not None, \
            f"Expected no none, but got for {i}\nctx: {ctx}"
        assert type(el) not in {type(None), float, int}, \
            f"Expected no `{el}`({type(el)}), but got for {i}\n ctx: {ctx}"

def interpret_num_word(word):
    if re.match("-?\d+", word):  # Plain integer.


@@ 26,10 26,12 @@ def interpret_num_word(word):
        return int(word[2:], 16)
    elif re.match("\d+\.\d*", word): # Floating point number.
        return float(word)
    assert word not in "0123456789"

def interpret_word(word):
    """Interprets constant values."""
    if x := interpret_num_word(word):
    x = interpret_num_word(word)
    if x is not None:
        return Range(x, x)  # TODO for integers might want to indicate that.
    elif word[0] == '"':  # Constant string.
        return ConstStr(word[1:])


@@ 91,11 93,11 @@ class StackCalc:
                assert type(el) not in {str, int, float}, el
                el.relevant_comp = relevant_comp

            check_no_none(rest, rest)
            check_no_none(stack, rest)
            assert_acceptable_vals(rest, rest)
            assert_acceptable_vals(stack, rest)
            return (rest + stack, *tc_code)

        check_no_none(stack, val)
        assert_acceptable_vals(stack, val)

        return stack[:-1] + [var], var  # No calculation needed.



@@ 144,4 146,4 @@ class StackCalc:
                    assert el is not None, \
                        f"Expected no None objects {otp}\n  (from {got})"
                    into(el)
                check_no_none(stack, got, code)
                assert_acceptable_vals(stack, got, code)