sannybuilder / dev

Sanny Builder Bug Tracker and Roadmap development
https://sannybuilder.com
44 stars 0 forks source link

Allow const & const comparations #310

Open MiranDMC opened 3 months ago

MiranDMC commented 3 months ago

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 commented 2 months ago

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.

MatiDragon-YT commented 1 month ago

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
x87 commented 1 month ago

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