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 _dec_both_abs_decimal() -> str: "小数部を片方が0になるまで両方をデクリメント" count = NOW work1 = NOW + 1 return c.block_of( c.copy_data(SECOND + 2, count, work1), c.for_loop( count, c.if_z_tricky( TOP + IDX_DEC, ELEMENT_SIZE, # work2 = NOW + IDX_DEC ELEMENT_SIZE, # work3 = NEXT + IDX_DEC then_statement=loop_last(count), else_statement=c.block_of(c.dec_pos(SECOND + IDX_DEC), c.dec_pos(TOP + IDX_DEC)))))
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 _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_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 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 if_nz(then_statement: str, else_statement: str = "") -> str: "スタック先頭 1byteが NZ の場合. 終了後スタック先頭は捨てる" else_statement = c.delete_useless(else_statement) if else_statement != "": else_flag = TOP + IDX_DMY1 return c.block_of( c.set_value(else_flag, 1), c.if_nz_then(TOP + IDX_BYTE, c.dec_pos(else_flag) + then_statement), c.if_one_then(else_flag, else_statement), c.move_ptr(TOP)) else: return c.block_of(c.if_nz_then(TOP + IDX_BYTE, then_statement), c.move_ptr(TOP))
def sub_byte() -> str: "1byteの減算" return c.block_of(c.for_loop(TOP + IDX_BYTE, c.dec_pos(SECOND + IDX_BYTE)), c.move_ptr(TOP))