dbaumgarten / yodk

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

Potential optimisation for addition #109

Closed TheCodedOne closed 2 years ago

TheCodedOne commented 2 years ago

Is your feature request related to a problem? Please describe. When doing additive assignment it compiles to something needlessly long sometimes

Describe the solution you'd like compiler could automatically figure out a simpler addition format

$ a = 0
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1
a+=1

$ b = 0
b+=a
b+=1
b+=2

compiles to

a=0 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1 a+=1
b=0 b+=a b+=1 b+=2

realistically could be turned into

a=0+1+1+1+1+1+1+1+1+1+1+1+1
b=0+a+1+2

or further into

a=12
b=a+3

Additional context It's probably reliant on the type system (#48) for it to be able to work correctly.

It's also a very weird case, there's always the option of just manually making sure your additions are as optimised as they can be, which is obviously the preferred way anyway, however depending on the complexity of implementation could be worth the potential for optimisations

dbaumgarten commented 2 years ago

What you are suggesting is basically a subest of the feature suggested in #52 .

That would be a relatively safe way to create expressions of optimal length. Automatically splitting large expressions into smaller once, would be an alternative, but could result in a lot of unexpected timing-behaviour. Having large expressions defined as multiple small assignments, which are then combined into a minimal amount of expressions seems much safer.

The only hard part about this is, that merging the assignments must be postponed until it is known how much space is left on the previous line, so that it results in the maximum code-density.

I think that might even be possible without a type-system.

dbaumgarten commented 2 years ago

In an effort to clean up a little, I am closing this as a duplicate of #52