Exemplo n.º 1
0
def put_str(message: str) -> str:
    result = c.clear_pos(NOW)
    for ch in message:
        result += c.init_value(NOW, ord(ch))
        # TODO ↑前の値との差分のほうが短いかどうかチェックしたほうが良い
        result += c.exec_pos(NOW, ".")
        result += c.clear_pos(NOW)
    return c.delete_useless(result)
Exemplo n.º 2
0
def _multi_decimal_abs() -> str:
    "3byte固定小数点の絶対値の乗算"
    # 整数部と小数部は筆算と同様に、byte単位で計算する
    # TODO カラツバ法を使う?
    #             A1. A2
    #           x B1. B2
    # ---------------------
    #               .A1xB2 A2xB2
    #       +  A1xB1.A2xB1
    # ----------------------
    #            R1 .  R2    R3
    #          <---------> 必要な範囲
    idx_a1 = SECOND + IDX_INT
    idx_a2 = SECOND + IDX_DEC
    idx_b1 = TOP + IDX_INT
    idx_b2 = TOP + IDX_DEC
    idx_r1 = NOW + IDX_INT
    idx_r2 = NOW + IDX_DEC
    idx_r3 = NOW + IDX_DEC + 1

    # 繰り上がり処理の関係で、上の桁から計算する
    return c.block_of(
        c.multi_data_tricky(idx_a1, idx_b1, idx_r1, 1),  # AxC (1byte 分)
        c.multi_data_tricky(idx_a1, idx_b2, idx_r2, 2),  # AxD (繰り上げ含め2byte)
        c.multi_data_tricky(idx_a2, idx_b1, idx_r2, 2),  # BxC (繰り上げ含め2byte)
        c.multi_data_tricky(idx_a2, idx_b2, idx_r3, 3),  # BxD (繰り上げ含め3byte)
        c.clear_pos(idx_r3),  # R3 は繰り上げ分のみが必要なのでクリア
    )
Exemplo n.º 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))
Exemplo n.º 4
0
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)))
Exemplo n.º 5
0
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))
Exemplo n.º 6
0
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))
Exemplo n.º 7
0
def drop() -> str:
    "スタックの先頭を破棄"
    return c.block_of(c.clear_pos(TOP + 3), c.clear_pos(TOP + 2),
                      c.clear_pos(TOP + 1), c.clear_pos(TOP + 0),
                      c.move_ptr(TOP))
Exemplo n.º 8
0
def loop_last(num: int) -> str:
    "ループを終了できる状態にする. num はループの制御変数の位置. 処理は続行する"
    pos = -ELEMENT_SIZE * (num + 1)
    return c.clear_pos(pos + IDX_BYTE)