Example #1
0
 def test_declSequence(self):
     t_declSeq = declSequence([decl("x", 1), "x"])
     self.assertEqual(t_declSeq.sequence, [decl("x", 1)])
     self.assertEqual(t_declSeq.expr, "x")
     t_declSeq = declSequence([decl("x", 1), decl("x", func([], 1)), 1])
     self.assertEqual(t_declSeq.sequence, [decl("x", 1), decl("x", func([], 1))])
     self.assertEqual(t_declSeq.expr, 1)
Example #2
0
def sd(var, data, depth, index, context):
    if type(data) == str:
        if data == var:
            return [depth, index]
        else:
            return "_"
    elif type(data) == int:
        return data
    elif type(data) == func:
        rightness = 0
        for arg in data.args:
            if arg != var:
                data.body = sd(var, data.body, depth + 1, index, context)
                data.body = sd(arg, data.body, 0, rightness, context + 1)
            else:
                data.body = sd(arg, data.body, depth, index, context + 1)
            rightness += 1
        removed_s = []
        for s in data.args:
            removed_s.append("_")
        data.args = removed_s
        return data
    elif type(data) == call:
        param_list = []
        for param in data.params:
            param_list.append(sd(var, param, depth, index, context))
        f = sd(var, data.fun, depth, index, context)
        return call(f, param_list)
    elif type(data) == cond:
        condition = sd(var, data.condition, depth, index, context)
        t = sd(var, data.true_expression, depth, index, context)
        f = sd(var, data.false_expression, depth, index, context)
        return cond(condition, t, f)
    elif type(data) == decl:
        if data.var != var:
            data.rhs = sd(var, data.rhs, depth, index, context)
            data.rhs = sd(data.var, data.rhs, 0, context, context)
        else:
            data.rhs = sd(data.var, data.rhs, 0, context, context)
        data.var = "_"
        return data
    elif type(data) == declSequence:
        seq = data.sequence
        seq.append(data.expr)
        new_sequence = sd(var, seq, depth, index, context)
        return declSequence(new_sequence)
    elif type(data) == list:
        if len(data) > 1:
            if type(data[0]) == decl:
                data[1:] = sd(data[0].var, data[1:], depth, context,
                              context + 1)
            data[1:] = sd(var, data[1:], depth, index, context + 1)
            data[0] = sd(var, data[0], depth, index, context)
        else:
            if type(data[0]) == list:
                depth += 1
            data[0] = sd(var, data[0], depth, index, context)
        return data
Example #3
0
def split(toy, k):
    global COUNT
    if type(toy) == str:
        if toy in opList:
            return call(k, ops_mapping[toy])
        return call(k, toy)
    if type(toy) == int:
        return call(k, toy)
    if type(toy) == func:
        return call(k, value(toy))
    if type(toy) == call:
        return split_call(toy, k)
    if type(toy) == cond:
        conditional = cond("of-tst", split(toy.true_expression, k),
                           split(toy.false_expression, k))
        f = func(["of-tst"], conditional)
        return call(receive(toy.condition), f)
    if type(toy) == decl:
        toy.rhs = value(toy.rhs)
        return toy
    if type(toy) == declSequence:
        new_sequence = []
        for declaration in toy.sequence:
            new_sequence.append(split(declaration, k))
        toy.sequence = new_sequence
        toy.expr = split(toy.expr, k)
        return toy
    if type(toy) == grab:
        return declSequence(
            [decl(toy.var, func(["x", "f"], call(k, "f"))),
             split(toy.rhs, k)])
    if type(toy) == list:
        if len(toy) < 1:
            return k  # ?
        else:
            call_arg = func(["of-rst"], call(k, "of-rst"))
            f = func(["of-fst"], call(receive(toy[1:]), call_arg))
            return call(receive(toy[0]), f)
    if type(toy) == stop:
        return stop(receive(toy.toy))
Example #4
0
def parseList(jso):
    if len(jso) > 0:
        if jso[0] == "fun*":
            body = parse(jso[2])
            return func(jso[1], body)
        if jso[0] == "call":
            params = []
            for parameter in jso[2:]:
                params.append(parse(parameter))
            c = call(parse(jso[1]), params)
            return c
        if jso[0] == "if-0":
            return cond(parse(jso[1]), parse(jso[2]), parse(jso[3]))
        if jso[0] == "let":
            return decl(jso[1], parse(jso[3]))
        # No need for infix operation checking in the list section, since it cannot happen
        if jso[0] == "seq*":
            args = []
            for s in jso[1:-1]:
                p = parse(s)
                args.append(p)
            f = call(func(rand_stringer(len(args)), parse(jso[-1])),
                     args[::-1])
            return f
        if jso[0] == "grab":
            return grab(parse(jso[1]), parse(jso[2]))
        if jso[0] == "stop":
            return stop(parse(jso[1]))
        if type(jso) == list:
            return_array = []
            for item in jso:
                return_array.append(parse(item))
            if len(return_array) > 1 and type(return_array[0]) == decl:
                return declSequence(return_array)
            return return_array
    return jso
Example #5
0
    def test_cps(self):
        # Using the alpha_equals() for equality

        t_json = 1
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k1"], call("k1", [1]))
        self.assertTrue(alpha_equal(cps, test))

        t_json = "a"
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k2"], call("k2", ["a"]))
        self.assertTrue(alpha_equal(cps, test))

        t_json = "+"
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k3"], call("k3", ops_mapping["+"]))
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["call", ["fun*", ["x", "y"], "y"], 1, 2]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func("k5", call(func("k6", call("k6", 2)),
                               func("of-1", call(func("k7", call("k7", 1)),
                                                 func("of-0",
                                                      call(func(["k8"],
                                                                call("k8",
                                                                     func(["k", "x", "y"], call("k", "y")))),
                                                           func("of-f",
                                                                call("of-f", ["k5", "of-0", "of-1"]))))))))
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["if-0", 0, 1, 2]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k9"], call(func(["k10"], call("k10", [0])), func(["of-tst"], cond("of-tst",
                                                                                        call("k9", [1]),
                                                                                        call("k9", [2])))))
        self.assertTrue(alpha_equal(cps, test))

        t_json = [["let", "a", "=", 5], ["let", "b", "=", 6], "b"]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = declSequence([decl("a", 5), decl("b", 6), func(["k11"], call("k11", ["b"]))])
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["fun*", ["x"], "x"]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k12"], call("k12", func(["k", "x"], call("k", ["x"]))))
        self.assertTrue(alpha_equal(cps, test))

        # Unit tests against names we would generate
        t_json = ["fun*", ["kb"], "kb"]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k12"], call("k12", func(["k", "kb"], call("k", ["kb"]))))
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["call", "+", 1, 2]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func("k13", call(func("k14", call("k14", 2)),
                                func("of-1",
                                     call(func("k15", call("k15", 1)),
                                          func("of-0", call(func("k", call("k", ops_mapping["+"])),
                                                            func("k", call("k", ["k13", "of-0", "of-1"]))))))))
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["grab", "x", ["call", "x", 10]]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func(["k"], declSequence([decl("x", func(["x", "f"], call("k", "f"))),
                                         call(func("kc", call("kc", 10)),
                                              func("fa",
                                                   call(func("kd", call("kd", "x")),
                                                        func("fb", call("fb", ["kb", "fa"])))))]))
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["stop", 10]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func("k", stop(func("kc", call("kc", 10))))
        self.assertTrue(alpha_equal(cps, test))

        t_json = ["call", "+", 1, ["stop", 10]]
        parsed_json = parse(t_json)
        cps = receive(parsed_json)
        test = func("k13", call(func("k", stop(func("kc", call("kc", 10)))),
                                func("of-1",
                                     call(func("k15", call("k15", 1)),
                                          func("of-0", call(func("k", call("k", ops_mapping["+"])),
                                                            func("k", call("k", ["k13", "of-0", "of-1"]))))))))
        self.assertTrue(alpha_equal(cps, test))

        t_json = declSequence([decl("x", 15), "x"])
        cps = receive(t_json)
        test = declSequence([decl("x", 15), func("ke", call("ke","x"))])
        self.assertTrue(alpha_equal(cps, test))
Example #6
0
 def test_alpha_equals(self):
     self.assertTrue(alpha_equal("a", "b"))
     self.assertTrue(not alpha_equal(1, "b"))
     self.assertTrue(alpha_equal(func("a", "a"), func("b", "b")))
     self.assertTrue(alpha_equal(declSequence([decl("x", func("a", "a")), "x"]),
                                 declSequence([decl("asdf", func("gfd", "gfd")), "asdf"])))
Example #7
0
    def test_interpret(self):
        t_json = 1
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 1)

        t_json = ["fun*", [], 1]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(str(result), "\"closure\"")

        t_json = "!"
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(str(result), "\"closure\"")

        t_json = ["call", "@", 1]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(str(result), "\"cell\"")

        t_json = "a"
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        self.assertRaises(errors.UndeclaredException,
                          stop_catch_interpretter, call(toy, func(["x"], "x")), env, store_)

        t_json = ["call", "^", 1, -1]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        self.assertRaises(errors.exponentiationError,
                          stop_catch_interpretter, call(toy, func(["x"], "x")), env, store_)

        t_json = ["call", "^", 1]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        self.assertRaises(errors.ArgumentParameterMismatch,
                          stop_catch_interpretter, call(toy, func(["x"], "x")), env, store_)

        t_json = ["call", 1]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        self.assertRaises(errors.ClosureOrPrimopExpected,
                          stop_catch_interpretter, call(toy, func(["x"], "x")), env, store_)

        t_json = ["grab", "c", ["call", ["fun*", ["v"], ["call", "v", 10]], "c"]]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 10)

        t_json = ["grab", "c", ["call", ["fun*", ["v"], ["call", "v", 10]], "c"]]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 10)

        t_json = ["call", "+",
                  ["stop",
                   [["let", "y", "=", 10],
                    ["call", "^", 2, "y"]]],
                  10]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 1024)

        t_json = ["call", "*",
                  ["call", ["fun*", [], 2]],
                  5]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 10)

        t_json = ["call", ["fun*", [], 1]]
        parsed_json = parse(t_json)
        toy = receive(parsed_json)
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 1)

        toy = func("k13", call(func("k", stop(func("kc", call("kc", 10)))),
                               func("of-1",
                                    call(func("k15", call("k15", 1)),
                                         func("of-0", call(func("k", call("k", ops_mapping["+"])),
                                                           func("k", call("k", ["k13", "of-0", "of-1"]))))))))
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 10)

        toy = func(["kb"], declSequence([decl("x", func(["x", "f"], call("kb", "f"))),
                                         call(func("kc", call("kc", 10)),
                                              func("fa",
                                                   call(func("kd", call("kd", "x")),
                                                        func("fb", call("fb", ["kb", "fa"])))))]))
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 10)

        toy = func("k13", call(func("k14", call("k14", 2)),
                               func("of-1",
                                    call(func("k15", call("k15", 1)),
                                         func("of-0", call(func("k", call("k", ops_mapping["+"])),
                                                           func("k", call("k", ["k13", "of-0", "of-1"]))))))))
        env, store_ = interpret_init()
        result, new_store = stop_catch_interpretter(call(toy, func(["x"], "x")), env, store_)
        self.assertEqual(result, 3)