dev icon indicating copy to clipboard operation
dev copied to clipboard

Allow const & const comparations

Open MiranDMC opened this issue 10 months ago • 3 comments

Currently code below won't compile:

{$CLEO .cs}
nop

const
    version_alpha = 1
end

while true
    if
        version_alpha == 0
    then
        print_help_formatted {text} "this is release version"
    else
        print_help_formatted {text} "this is alpha version %d" {args} version_alpha
    end
end

terminate_this_custom_script

as there is no opcode handling imm int == imm int operation.

Operation on constant values like that should be performed during compilation time. Then if condition with constants becomes special case, like while true currently is. One thing leads to another and the result is that if blocks would be actually be able to include just one side of the branch, making them effectively analogue of C's #ifdef macros.

Request very simple on the surface, but I think not that much more complicated to implement. Multi-condition if and switch statements needs some extra logic during compilation, but all those seems to be just simple checks.

MiranDMC avatar Mar 28 '24 23:03 MiranDMC

Optimizing out unused branch is not so trivial, considering fact there can be label inside. Then there is no way to simply be sure the code inside is never used. Cases like that can be compiled as jump over the "unused" branch.

MiranDMC avatar Apr 13 '24 06:04 MiranDMC

here is a solution

if is(version_alpha, 0)
then
[...]

:is 
// 0@ first value
// 1@ second value
int 0@, 1@
0@ = 0@ ^ 1@
if 0@ == 0
then cleo_return 1 true
else cleo_return 1 false
end
cleo_return 0

MatiDragon-YT avatar May 12 '24 23:05 MatiDragon-YT

here is a solution

if is(version_alpha, 0)
then
[...]

:is 
// 0@ first value
// 1@ second value
int 0@, 1@
0@ = 0@ ^ 1@
if 0@ == 0
then cleo_return 1 true
else cleo_return 1 false
end
cleo_return 0
if equals(version_alpha, 0)
[...]

function equals(a: int, b: int)
  return a == b
end

x87 avatar May 12 '24 23:05 x87