Beispiel #1
0
class VmTests(unittest.TestCase):
    def setUp(self) -> None:
        self.vm = Vm()

    @parameterized.expand([
        ['+'],
        ['negate'],
        ['` negate 1'],
        ['` ` + 1 2'],
    ])
    def test_parse_exp(self, exp):
        self.assertEqual(exp, str(self.vm.parse_exp(exp)))

    @parameterized.expand([
        ['+', '+'],
        ['1', '1'],
        ['` negate 1', '-1'],
        ['` ` + 1 2', '3'],
        ['` ` pair 1 2', '"1 2"'],
        ['` ` pair 1 emptyList', '[1]'],
        ['` ` pair 2 ` ` pair 1 emptyList', '[2, 1]'],
        ['` isEmptyList ` ` pair 1 emptyList', 'F'],
        ['` isEmptyList emptyList', 'K'],
        ['` ` K a b', 'a'],
        ['` ` K a ` 1 2', 'a'],
        ['` ` F a b', 'b'],
        ['` ` ` S K b c', 'c'],
        ['` ` ` B ` + 1 negate 20', '-19'],
        ['` I 20', '20'],
        ['` ` ` C + 1 2', '3'],
        ['` ` + x x', '20', 'x = 10'],
        ['` head ` ` pair 10 20', '10'],
        ['` tail ` ` pair 10 20', '20'],
        [
            '` n1035 ` ` pair 10 emptyList', '1',
            'n1035 = ` ` S ` ` C isEmptyList 0 ` ` B ` + 1 ` ` B n1035 tail'
        ],
        [
            '` ` drawClickedPixelProgram emptyList ` ` pair 0 0',
            '[0, [], [["0 0"]]]',
            'drawClickedPixelProgram = ` ` C ` ` B B ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C ` ` B pair ` ` C pair emptyList emptyList'
        ],
        [
            '` ` paintProgram emptyList ` ` pair 0 0',
            '[0, ["0 0"], [["0 0"]]]',
            'paintProgram = ` ` B ` B ` ` S ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C pair emptyList ` C pair'
        ],
        [
            '` ` paintProgram ` ` pair ` ` pair 0 0 emptyList ` ` pair 1 1',
            '[0, ["1 1", "0 0"], [["1 1", "0 0"]]]',
            'paintProgram = ` ` B ` B ` ` S ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C pair emptyList ` C pair'
        ],
    ])
    def test_eval(self, exp, expected, *statements):
        for statement in statements:
            self.vm.execute_statement(statement)
        self.assertEqual(expected,
                         Vm.format(self.vm.eval(self.vm.parse_exp(exp))))
Beispiel #2
0
n1175 = ` ` B isEmptyList ` ` C n1040 ` ` C == 0
n1176 = ` ` C ` ` B n1033 ` ` C ` ` B n1040 ` ` C ` ` C n1034 pair 0 ` ` C ` ` B == head 0 ` ` B I tail
n1177 = ` ` B ` B I ` ` B ` B tail ` ` C ` ` B C n1174 3
n1178 = ` ` S ` ` B B ` ` B B ` ` C ` ` C < 0 n1164 ` ` S ` ` B S ` ` B ` B B ` ` C ` ` B S ` ` B ` S ` ` B ` ` S I I n1172 ` ` B ` B C ` ` C ` ` B C ` ` B ` B == ` C n1043 0 I ` ` B ` B ` S ` ` S ` ` B S ` ` C ` ` B S ` ` C ` ` B S ` ` B ` B ` ` S I I ` C n1169 ` ` B isEmptyList ` ` C n1040 ` ` C == 0 I ` ` S ` ` B C ` ` B ` S n1045 ` ` B ` C n1177 ` ` B ` + 3 negate ` ` B ` + 3 negate ` ` C ` ` B C ` ` B ` B B ` C n1045 I
n1179 = ` ` S ` ` B n1060 ` ` C ` ` S ` ` B B ` ` C ` ` B B + negate ` ` B ` S * / 3 ` ` C / 3
n1180 = ` ` C ` ` B B ` ` C ` ` B C ` ` C ` ` B B ` ` C == 1 ` ` C pair emptyList emptyList n1179
n1181 = ` ` C ` ` B B ` ` C ` ` B B ` ` C ` ` C == 2 emptyList ` ` C pair emptyList n1179
n1182 = ` ` S ` ` B B ` ` C isEmptyList ` n1069 ` ` pair n1164 ` ` pair emptyList emptyList ` ` S ` ` B C ` ` B ` C ` ` B C ` ` B ` S ` ` B S ` ` C ` ` B B n1172 ` ` B n1069 ` ` B ` pair n1164 ` ` C pair emptyList ` ` C ` ` B B B ` ` C ` ` B C ` ` B ` B B ` ` B ` B B ` ` B ` C ` ` B B ` ` B ` S ` ` B ` B n1069 ` ` C ` ` B B pair ` ` C pair emptyList ` ` S ` ` B C ` ` B ` S n1175 ` ` B ` B n1056 ` C ` ` B n1029 n1167 I ` ` C ` ` B C ` C n1178 1 n1166 ` ` C n1043 1 ` ` C n1043 0
n1183 = ` ` S ` ` C isEmptyList ` n1183 ` ` pair n1164 ` ` pair emptyList emptyList ` ` S ` ` B ` C ` ` C ` ` B B ` ` B ` S ` ` C ` ` C == 0 ` ` pair ` n1089 n1165 emptyList ` ` S ` ` B C ` ` B ` C ` ` B C ` ` B ` C ` ` B B pair ` ` B ` C pair ` ` C ` ` B pair ` ` C ` ` B n1068 n1085 ` ` n1060 0 ` negate 6 emptyList ` ` B n1039 ` ` C ` ` C n1034 n1181 0 ` ` B n1039 ` ` C ` ` C n1034 n1180 0 ` ` B ` ` B ` + 5 negate n1035 ` ` C n1043 1 ` ` C n1043 0
n1184 = ` ` B ` B ` ` S n1070 n1183 ` ` C ` ` B C ` ` B ` B n1043 n1182 1
n1185 = ` ` B ` B ` ` S ` ` B n1070 ` ` B ` pair n1139 ` ` C ` ` B pair ` ` B I ` ` C n1043 1 ` ` pair emptyList emptyList ` ` C n1043 2 n1184
operationSystem = n1141'''

    vm = Vm()
    for line in program.split('\n'):
        vm.execute_statement(line)

    vm.execute_statement(
        'paintProgram = ` ` B ` B ` ` S ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C pair emptyList ` C pair'
    )
    vm.execute_statement(
        'drawClickedPixelProgram = ` ` C ` ` B B ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C ` ` B pair ` ` C pair emptyList emptyList'
    )

    # vm.execute_statement('boot = paintProgram')
    # vm.execute_statement('boot = drawClickedPixelProgram')
    vm.execute_statement('boot = operationSystem')

    state = None
    event = (0, 0)
    while True: