def sub_decimal() -> str: "固定小数点小数の減算" # 符号を反転して加算(A-B => A+(-B)) plus_flag = NOW + IDX_DMY return c.block_of(c.inc_pos(plus_flag), c.if_one_then(TOP + IDX_SGN, c.dec_pos(plus_flag)), c.if_one_then(plus_flag, c.inc_pos(TOP + IDX_SGN)), add_decimal())
def test_if_z_tricky_2(self): source = c.if_z_tricky(1, 1, 1, c.inc_pos(5), c.inc_pos(6)) sim = BfSim(source) sim.memory[1] = 0 sim.run(10000) self.assertEqual(sim.memory[1], 0) self.assertEqual(sim.memory[5], 1) self.assertEqual(sim.memory[6], 0)
def _xor_sign() -> str: # 符号は同じなら+、異なるならマイナス idx_as = SECOND + IDX_SGN idx_bs = TOP + IDX_SGN idx_rs = NOW + IDX_SGN sign_work = NEXT return c.block_of(c.for_loop(idx_as, c.inc_pos(sign_work)), c.for_loop(idx_bs, c.dec_pos(sign_work)), c.if_nz_then(sign_work, c.inc_pos(idx_rs)))
def if_nz_decimal(then_statement: str, else_statement: str = "") -> str: "3byte固定小数点が NZ の場合. 終了後スタック先頭は捨てる" nz_flag = TOP + IDX_DMY else_flag = TOP + IDX_INT then_flag = TOP + IDX_DEC return c.block_of( c.clear_pos(TOP + IDX_SGN), c.if_nz_then(TOP + IDX_DEC, c.inc_pos(nz_flag)), c.if_nz_then(TOP + IDX_INT, c.inc_pos(nz_flag)), c.inc_pos(else_flag), c.if_nz_then(nz_flag, c.dec_pos(else_flag) + c.inc_pos(then_flag)), c.if_one_then(then_flag, then_statement), c.if_one_then(else_flag, else_statement), c.move_ptr(TOP))
def add_decimal() -> str: "固定小数点小数の加算" # 符号が同じなら、絶対値の加算 # 符号が異なるなら、絶対値の減算 work = SECOND + IDX_DMY diff_work = TOP + IDX_DMY same_flag = SECOND + IDX_DMY return c.block_of( c.for_safe(SECOND + IDX_SGN, work, c.inc_pos(diff_work)), c.for_safe(TOP + IDX_SGN, work, c.dec_pos(diff_work)), c.inc_pos(same_flag), c.if_nz_then(diff_work, c.dec_pos(same_flag) + _sub_abs()), c.if_nz_then(same_flag, _add_abs()), drop())
def test_if_one_then_2(self): source = c.if_one_then(1, c.inc_pos(3)) sim = BfSim(source) sim.memory[1] = 0 sim.run(10000) self.assertEqual(sim.memory[1], 0) self.assertEqual(sim.memory[3], 0)
def _add_abs() -> str: "3byte固定小数点の絶対値の加算" # SECOND/TOP の符号は同じであることを想定 return c.block_of( c.clear_pos(TOP + IDX_SGN), c.for_loop(SECOND + IDX_DEC, c.inc_data_tricky(TOP + IDX_DEC, 2)), c.for_loop(SECOND + IDX_INT, c.inc_pos(TOP + IDX_INT)), c.override_data(TOP + IDX_DEC, SECOND + IDX_DEC), c.override_data(TOP + IDX_INT, SECOND + IDX_INT))
def if_negative_decimal(then_statement: str, else_statement: str = "") -> str: "3byte固定小数点が負数の場合. 終了後スタック先頭は捨てる" then_flag = TOP + IDX_DEC else_flag = TOP + IDX_INT return c.block_of( c.clear_pos(then_flag), c.set_value(else_flag, 1), c.if_nz_then(TOP + IDX_SGN, c.dec_pos(else_flag) + c.inc_pos(then_flag)), c.if_one_then(then_flag, then_statement), c.if_one_then(else_flag, else_statement), c.move_ptr(TOP))
def add_byte() -> str: "1byteの加算" return c.block_of(c.for_loop(TOP + IDX_BYTE, c.inc_pos(SECOND + IDX_BYTE)), c.move_ptr(TOP))