def override(num: int) -> str: "スタックの先頭要素で、スタックの該当要素を上書き. 1<=numであること." pos = -ELEMENT_SIZE * (num + 1) return c.block_of(c.override_data(TOP + 3, pos + 3), c.override_data(TOP + 2, pos + 2), c.override_data(TOP + 1, pos + 1), c.override_data(TOP + 0, pos + 0), c.move_ptr(TOP))
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 test_override_data_1(self): source = c.override_data(1, 2) sim = BfSim(source) sim.memory[1] = 3 sim.memory[2] = 4 sim.run(10000) self.assertEqual(sim.memory[1], 0) self.assertEqual(sim.memory[2], 3)
def _if_top_decimal_is_nz_then_override() -> str: "TOPの小数部が0以外なら、TOPをSECONDに移動" return c.if_z_tricky( TOP + IDX_DEC, ELEMENT_SIZE, # work1 = NOW + IDX_DEC ELEMENT_SIZE, # work2 = NEXT + IDX_DEC then_statement=c.clear_pos(TOP + IDX_SGN), else_statement=c.block_of( c.override_data(TOP + IDX_SGN, SECOND + IDX_SGN), c.move_data(TOP + IDX_DEC, SECOND + IDX_DEC)))
def _top_minus_second() -> str: "SECONDの小数部の分、TOPから減算して、SECONDの位置に移動" return c.block_of( # 符号を移動(tricky対策で、先に移動) c.override_data(TOP + IDX_SGN, SECOND + IDX_SGN), # SECONDの小数部だけデクリメント c.for_loop(SECOND + IDX_DEC, c.dec_data_tricky(TOP + IDX_DEC, 2)), # 結果を SECONDに移動 c.move_data(TOP + IDX_DEC, SECOND + IDX_DEC), c.move_data(TOP + IDX_INT, SECOND + IDX_INT) # TODO -0.0 を 0.0 に変換すべき? )