Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow const & const comparations #310

Open
MiranDMC opened this issue Mar 28, 2024 · 3 comments
Open

Allow const & const comparations #310

MiranDMC opened this issue Mar 28, 2024 · 3 comments

Comments

@MiranDMC
Copy link

MiranDMC commented Mar 28, 2024

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
Copy link
Author

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
Copy link
Member

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
Copy link
Collaborator

x87 commented May 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants