krzema12 / kotlin-python

Python target for the Kotlin Programming Language. See https://github.com/krzema12/kotlin-python/tree/python-backend/python
https://discuss.kotlinlang.org/t/idea-python-backend/19852
48 stars 1 forks source link

Implement `fun main(args: Array<String>)` #74

Closed krzema12 closed 2 years ago

krzema12 commented 2 years ago

The goal is to be able to create a CLI, so that the compiled module can be called like python3 out_ir.py --some --arg 123 and it will be available in Kotlin's fun main(args: Array<String>), in simple cases just fun main().

krzema12 commented 2 years ago

Left to do: add tests.

krzema12 commented 2 years ago

@SerVB I actually have no idea how to implement the tests easily for the main function. Box tests: they rely on calling the box function. End-to-end tests: apart from the fact they would need to be enhanced to call the module as a main one, not the library (easily doable), we miss the ability to print something - translation to println in our backend doesn't work yet. Let me resolve it for now, unless you come up with some feasible way to test it.

SerVB commented 2 years ago

Probably we can:

  1. Define external fun print or another one that has a visible side effect and call it from our main.
  2. Name the file like __main__ and import it from a py consumer. In our main, we can modify a global var and access it from the consumer (as we do in an e2e test already).
  3. Another way to ensure our main is called is to create an exception there (even not via throwing that we don't support yet but do something like 0 / 0 for arithmetic exception), it should be visible in stderr.
  4. Not import our compiled py directly from consumer, but run it as a subprocess.

Some combination of those points could work, but maybe not...

For now, a combination of 3 and 4 looks workable

krzema12 commented 2 years ago

Nice ideas, I'll give them a shot, thanks!