Пример #1
0
 def test_parser(self):
     test = lambda src, tokens: self.assertEqual(crianza.parse(src), tokens)
     test("1", [1])
     test("1 2", [1, 2])
     test("123 dup * .", [123, "dup", "*", "."])
     test("1 2 3 4 5 * * * *", [1, 2, 3, 4, 5, "*", "*", "*", "*"])
     test(": square\n\tdup * ;\n\n12 square .\n", [":", "square", "dup", "*",
         ";", 12, "square", "."])
Пример #2
0
def xcompile(source_code, args=0, optimize=True):
    """Parses Crianza source code and returns a native Python function.

    Args:
        args: The resulting function's number of input parameters.

    Returns:
        A callable Python function.
    """
    code = crianza.compile(crianza.parse(source_code), optimize=optimize)
    return crianza.native.compile(code, args=args)
Пример #3
0
def xcompile(source_code, args=0, optimize=True):
    """Parses Crianza source code and returns a native Python function.

    Args:
        args: The resulting function's number of input parameters.

    Returns:
        A callable Python function.
    """
    code = crianza.compile(crianza.parse(source_code), optimize=optimize)
    return crianza.native.compile(code, args=args)
Пример #4
0
    def test_mul2(self):
        code = crianza.compile(crianza.parse("2 *"))
        mul2 = crianza.native.compile(code, args=1, name="mul2",
                docstring="Multiplies number with two.")

        self.assertIsNotNone(mul2)
        self.assertEqual(mul2.__doc__, "Multiplies number with two.")
        self.assertEqual(mul2.__name__, "mul2")

        for n in xrange(100):
            self.assertEqual(n*2, mul2(n))

        for __ in range(10):
            n = random.randint(-1000000, 1000000)
            self.assertEqual(n*2, mul2(n))
Пример #5
0
    def test_program_fibonacci(self):
        code = crianza.compile(crianza.parse(fibonacci_source))
        # TODO: Unembed this:
        #self.assertEqual(code, native_types([0, 13, 'call', 1,
        #    13, 'call', '@', 16, 'call', 13, 'call', 'return', 'exit', 'dup',
        #    '.', 'return', 'swap', 'over', '+', 'return']))

        machine = crianza.Machine(code, output=None)

        # skip to main loop
        machine.run(11)

        sequence = []
        numbers_to_generate = 15

        for its in xrange(0, numbers_to_generate):
            sequence.append(machine.top)
            machine.run(13) # next number

        self.assertEqual(sequence, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
            233, 377, 610])