Ejemplo n.º 1
0
    # if valid_op2(t):
    if is_op2(t):
        return frozenset([t[0]]) | op_expr(t[1]) | op_expr(t[2])
    assert False


def ops(t):
    # assert valid(t)
    # if valid_fold(t[2]) and t[2][2] == '0':
    if isinstance(t[2], list) and t[2][0] == "fold":
        return frozenset(["tfold"]) | op_expr(t[2][3][2])
    return op_expr(t[2])


def gen(t):
    if isinstance(t, list):
        return "(" + " ".join(map(gen, t)) + ")"
    return t


if __name__ == "__main__":
    assert valid_op2(parse("(or y z)"))
    assert valid(parse("(lambda (x) (fold x 0 (lambda (y z) (or y z))))"))
    assert sz(parse("(lambda (x) (fold x 0 (lambda (y z) (or y z))))")) == 8
    assert op_expr(parse("(fold x 0 (lambda (y z) (or y z)))")) == frozenset(["fold", "or"])
    assert ops(parse("(lambda (x) (fold x 0 (lambda (y z) (or y z))))")) == frozenset(["tfold", "or"])
    assert (
        gen(parse("(lambda (x) (fold x 0 (lambda (y z) (or y z))))"))
        == "(lambda (x) (fold x 0 (lambda (y z) (or y z))))"
    )
Ejemplo n.º 2
0
while True:
    sys.stdout.write(":> ")
    l = sys.stdin.readline()
    if len(l) == 0:
        break
    if len(l) == 1:
        continue
    try:
        if l[0] == "=":
            if p is None:
                sys.stdout.write("No function set.\n")
                continue
            s = l[1:]
            if s[0:2] == "0b":
                x = int(s[2:], 2)
            elif s[0:2] == "0x":
                x = int(s[2:], 16)
            else:
                x = int(s)
            z = evl(p, x)
            sys.stdout.write("Res: %d\n" % z)
            sys.stdout.write("Hex: %s\n" % hex(z))
            sys.stdout.write("Bin: %s\n" % bin(z))
        else:
            p = parse(l[:-1])
    except (AssertionError, ValueError) as ex:
        sys.stdout.write("Error.\n")
        raise

sys.stdout.write("\nThank you for playing.\n")