koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.16k stars 151 forks source link

Code Formatter #521

Open TimWhiting opened 1 month ago

TimWhiting commented 1 month ago

An autoformatter would be good to have, to keep style consistent (at least within projects).

Koka touts flexibility in syntax. However, despite the flexibility, it is remarkably consistent, and makes it easy to stay consistent.

Obviously there are points where braces are either required (multiple trailing lambdas), or just cleaner (suspenders for creating unit functions). Both of these situations for example happen to occur in the while function.

So there would need to be some rules obviously - you cannot translate all braces to indentation, while you can go the other way.

Additionally, I don't think we should get rid of any with statements that the user introduces, while we could automatically add with statements if the rest of the body is under the lambda.

The biggest things that a formatter would be helpful for though is to

I'm of the opinion at this point that the autoformatter should be written in Koka itself, in order to avoid adding more to the Haskell implementation which still struggles sometimes with memory usage due to laziness.

woojamon commented 1 month ago

I'm of the opinion at this point that the autoformatter should be written in Koka itself

Yes, so yes 🥹

...struggles sometimes with memory usage due to laziness

omg me too

TimWhiting commented 1 month ago

By the way #525 improves memory usage and computation time a lot. Hopefully we can get the merged and released soon!

zephyrtronium commented 1 month ago

I'm of the opinion at this point that the autoformatter should be written in Koka itself

Is there an implementation of Koka syntax in Koka already? Worth noting that an auto-formatter needs a concrete or at least less-abstract syntax representation in order to preserve comments.

TimWhiting commented 1 month ago

Good question. I do have an implementation already in one of my repositories somewhere. However, the lexer could be improved I think, currently it is slow. I have a version of the Alex lexer generator used in koka's Haskell compiler with some updates to generate Koka code for a lexer, but ran into issues with type checking taking a lot of time and memory consumption, however PR #525 fixes that, so I might be able to retry using that lexer instead of the current lexer, and see if it is more usable now. (There have also been some changes to Koka's grammar - in particular with implicit parameters, that would need to be updated).

I can make the code available somewhere probably sometime next week if someone wants to work on this issue.