Example #1
0
 def test_expr_space(self):
     input_string = 'assign x { 3 * 4 }'
     expected = [
         Instruction(OpCode.PUSHQ, 3),
         Instruction(OpCode.PUSHQ, 4),
         Instruction(OpCode.OP, Operator.MUL),
         Instruction(OpCode.POP, "x")
     ]
     actual = self.parser.parse(input_string)
     self.assertEqual(expected, actual, "Error with space in expression.")
Example #2
0
 def test_println(self):
     input_string = 'println 10'
     expected = [
         Instruction(OpCode.MOVEQ, 10, Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(OpCode.OUT, IoOp.PRINT, os.linesep)
     ]
     actual = self.parser.parse(input_string)
     self.assertEqual(
         expected, actual,
         'println test failed: {} {}'.format(expected, actual))
Example #3
0
 def test_multi_zone(self):
     input_string = 'set "Strip" zone 3 5'
     expected = [
         Instruction(OpCode.MOVEQ, "Strip", Register.NAME),
         Instruction(OpCode.MOVEQ, 3, Register.FIRST_ZONE),
         Instruction(OpCode.MOVEQ, 5, Register.LAST_ZONE),
         Instruction(OpCode.MOVEQ, Operand.MZ_LIGHT, Register.OPERAND),
     ]
     actual = _filter(self.parser.parse(input_string))
     self.assertEqual(expected, actual,
                      "Multi-zone failed: {} {}".format(expected, actual))
Example #4
0
 def add_instruction(self,
                     op_code,
                     param0=None,
                     param1=None) -> Instruction:
     inst = Instruction(op_code, param0, param1)
     self._code.append(inst)
     return inst
Example #5
0
 def code_for_set(cls, name, operand, params):
     return [
         Instruction(OpCode.MOVEQ, UnitMode.RAW, Register.UNIT_MODE),
         Instruction(OpCode.MOVEQ, params[0], Register.HUE),
         Instruction(OpCode.MOVEQ, params[1], Register.SATURATION),
         Instruction(OpCode.MOVEQ, params[2], Register.BRIGHTNESS),
         Instruction(OpCode.MOVEQ, params[3], Register.KELVIN),
         Instruction(OpCode.MOVEQ, name, Register.NAME),
         Instruction(OpCode.MOVEQ, operand, Register.OPERAND),
         Instruction(OpCode.COLOR)
     ]
Example #6
0
def build_instructions():
    program = []
    it = iter(get_assembly())
    op_code = next(it, None)
    while op_code is not None:
        param_count = _param_counts[OpCode(op_code)]
        param0 = None if param_count < 1 else next(it)
        param1 = None if param_count < 2 else next(it)
        program.append(Instruction(op_code, param0, param1))
        op_code = next(it, None)
    return program
Example #7
0
def build_instructions():
    program = []
    it = iter(get_assembly())
    op_code = next(it, None)
    while op_code is not None:
        if op_code in params_0:
            param_count = 0
        elif op_code in params_1:
            param_count = 1
        else:
            param_count = 2
        param0 = None if param_count < 1 else next(it)
        param1 = None if param_count < 2 else next(it)
        program.append(Instruction(op_code, param0, param1))
        op_code = next(it, None)
    return program
Example #8
0
 def test_print(self):
     input_string = "assign y 100 print y print y"
     expected = [
         Instruction(OpCode.MOVEQ, 100, "y"),
         Instruction(OpCode.MOVE, "y", Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(OpCode.OUT, IoOp.PRINT, ' '),
         Instruction(OpCode.MOVE, "y", Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(OpCode.OUT, IoOp.PRINT, ' ')
     ]
     actual = self.parser.parse(input_string)
     self.assertEqual(
         expected, actual,
         "print test failed: {} {}".format(expected, actual))
Example #9
0
 def test_single_zone(self):
     input_string = 'set "Strip" zone 7'
     expected = [
         Instruction(OpCode.WAIT),
         Instruction(OpCode.MOVEQ, "Strip", Register.NAME),
         Instruction(OpCode.MOVEQ, 7, Register.FIRST_ZONE),
         Instruction(OpCode.MOVEQ, None, Register.LAST_ZONE),
         Instruction(OpCode.MOVEQ, Operand.MZ_LIGHT, Register.OPERAND),
         Instruction(OpCode.COLOR)
     ]
     actual = self.parser.parse(input_string)
     self.assertEqual(expected, actual,
                      "Single zone failed: {} {}".format(expected, actual))
Example #10
0
    def test_if_else(self):
        code_gen = CodeGen()
        marker = code_gen.if_start()
        code_gen.add_instruction(OpCode.NOP)
        code_gen.if_else(marker)
        code_gen.add_instruction(OpCode.NOP)
        code_gen.if_end(marker)

        marker = code_gen.if_start()
        code_gen.add_instruction(OpCode.NOP)
        code_gen.if_end(marker)

        self.assertListEqual(code_gen.program, [
            Instruction(OpCode.JUMP, JumpCondition.IF_FALSE, 3),
            Instruction(OpCode.NOP),
            Instruction(OpCode.JUMP, JumpCondition.ALWAYS, 2),
            Instruction(OpCode.NOP),
            Instruction(OpCode.JUMP, JumpCondition.IF_FALSE, 2),
            Instruction(OpCode.NOP)
        ])
Example #11
0
 def code_for_get(cls, name, operand):
     return [
         Instruction(OpCode.MOVEQ, name, Register.NAME),
         Instruction(OpCode.MOVEQ, operand, Register.OPERAND),
         Instruction(OpCode.GET_COLOR)
     ]
Example #12
0
 def _eval_test(self, code, expected):
     code.append(Instruction(OpCode.POP, "x"))
     machine = Machine()
     machine.run(code)
     self.assertEqual(machine.get_variable("x"), expected)
Example #13
0
 def setUp(self):
     self._test_cases = ((-31, [
         Instruction(OpCode.PUSH, 17),
         Instruction(OpCode.PUSH, 23),
         Instruction(OpCode.OP, Operator.ADD),
         Instruction(OpCode.PUSH, 3),
         Instruction(OpCode.OP, Operator.MUL),
         Instruction(OpCode.PUSH, 5),
         Instruction(OpCode.PUSH, 2),
         Instruction(OpCode.OP, Operator.MUL),
         Instruction(OpCode.OP, Operator.DIV),
         Instruction(OpCode.PUSH, 43),
         Instruction(OpCode.OP, Operator.SUB)
     ]), (4, [
         Instruction(OpCode.PUSH, 20),
         Instruction(OpCode.PUSH, 5),
         Instruction(OpCode.OP, Operator.DIV)
     ]), (-500, [
         Instruction(OpCode.PUSH, 10000),
         Instruction(OpCode.PUSH, 20),
         Instruction(OpCode.OP, Operator.DIV),
         Instruction(OpCode.OP, Operator.USUB)
     ]), (True, [
         Instruction(OpCode.PUSH, 5),
         Instruction(OpCode.PUSH, 20),
         Instruction(OpCode.OP, Operator.LT)
     ]), (True, [
         Instruction(OpCode.PUSH, 17),
         Instruction(OpCode.PUSH, 23),
         Instruction(OpCode.OP, Operator.GT),
         Instruction(OpCode.OP, Operator.NOT)
     ]))
     test_module.configure()
Example #14
0
 def test_printf(self):
     input_string = """
         assign y 60 define z "hello"
         printf "{} {brightness} {} {} {y} {}" 500 saturation "there" z
     """
     expected = [
         Instruction(OpCode.MOVEQ, 60, "y"),
         Instruction(OpCode.CONSTANT, "z", "hello"),
         Instruction(OpCode.MOVEQ, 500, Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(OpCode.MOVE, Register.SATURATION, Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(OpCode.MOVEQ, "there", Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(OpCode.MOVEQ, "hello", Register.RESULT),
         Instruction(OpCode.OUT, IoOp.REGISTER, Register.RESULT),
         Instruction(
             OpCode.OUT, IoOp.PRINTF, "{} {brightness} {} {} {y} {}")
     ]
     actual = self.parser.parse(input_string)
     self.assertEqual(
         expected, actual,
         "printf test failed: {} {}".format(expected, actual))