dbaumgarten / yodk

Development Kit for Starbase's ingame programming language YOLOL
MIT License
57 stars 16 forks source link

if statement constant #87

Closed lizelive closed 2 years ago

lizelive commented 2 years ago

Is your feature request related to a problem? Please describe. Ability to remove dead code at compile time for at trival case

Describe the solution you'd like have if [constant], then only do the branch that can run

Describe alternatives you've considered commenting out lines

dbaumgarten commented 2 years ago

That is something I have been considering for a while now. In it's simplest form it's relatively easy to do, but there are some caveats to consider. For example: Jump labels need to be unique and currently that is checked before any ifs are converted, so it could happen that a jump-label is removed by dead-code elimination and still causes an error.

Also, most code is never really "dead" in yolol. Even when using an if with an always false condition, the code inside it could still be reached with a goto. But I guess if you put code into a usually unreachable position nobody should be surprised that jumping to it gives strange results.

Tl;Dr: Good idea. Planned to be added, but might need some time to get it right.

dbaumgarten commented 2 years ago

So, I guess thats a decent attempt. It does not allow conditional defines or includes, but i think thats fine. Everything else works exactly as expected.

define foo=1
if foo==1 then
    :o1=1
else if foo==2 then
    :o1=2
    :err++
else if foo==3 then
    :o1=3
    :err++
else
    :o1=4
    :err++
end

compiles to

:o1=1

(It's not yet published. If you want to test it out you would need to compile yodk yourself from the develop-branch)

dbaumgarten commented 2 years ago

Update: You do not longer have to build it youself to test unreleased features. There is now an automated uild of the develop-branch: https://github.com/dbaumgarten/yodk/releases/tag/latest

dbaumgarten commented 2 years ago

Released as part of v0.1.8