blech-lang / blech

Blech is a language for developing reactive, real-time critical embedded software.
Apache License 2.0
64 stars 5 forks source link

type cast does not respect parentheses #8

Closed mterber closed 2 years ago

mterber commented 2 years ago

Describe the bug The following blech code

activity Main ()()
    await true
    var x: nat32 = 142000
    var y: nat16 = (x / 10) as! nat16
end

leads to the following C implementation

blc_pc_t blc_01test01_Main (struct blc_01test01_Main *blc_00_ctx) {
    if ( blc_00_ctx->pc_1 == 2 ) {
        blc_00_ctx->pc_1 = 5; /* proceed from surface to depth */
    }
    if ( blc_00_ctx->pc_1 == 4 ) {
        if ( 1 ) {
            blc_00_ctx->blc_Main_x = 142000;
            blc_00_ctx->blc_Main_y = (blc_nat16)blc_00_ctx->blc_Main_x / 10;
            blc_00_ctx->pc_1 = 0; /* end */
        }
    }

    BLC_SWITCH_TO_NEXTSTEP(blc_00_ctx->pc_1);
    return blc_00_ctx->pc_1 ;
}

Line blc_00_ctx->blc_Main_y = (blc_nat16)blc_00_ctx->blc_Main_x / 10; shows that the parentheses of the original blech code var y: nat16 = (x / 10) as! nat16 are missing. Since the type cast has a higher precedence than the division operator this leads to a wrong computation result - the type cast is done before the division by 10.

Expected behaviour The expected behavior is

Additional context Blech toolchain in use:

[blech-tools]$ git log
commit 9799d3b3f9251286f738a02cebfa1db31de90ce7 (HEAD -> main, tag: v0.7.2, origin/main, origin/HEAD)
Author: fjg <schorg@gmail.com>
Date:   Tue Apr 19 11:23:13 2022 +0200

    Update windows installer

    Signed-off-by: fjg <schorg@gmail.com>
FriedrichGretz commented 2 years ago

Bug confirmed