GithubHelp home page GithubHelp logo

krzema12 / kotlin-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jetbrains/kotlin

47.0 47.0 1.0 1.74 GB

Python target for the Kotlin Programming Language. See https://github.com/krzema12/kotlin-python/tree/python-backend/python

Home Page: https://discuss.kotlinlang.org/t/idea-python-backend/19852

Kotlin 77.34% Java 9.75% Lex 0.02% C 2.91% C++ 2.05% LLVM 0.01% Swift 5.47% Objective-C 0.43% Groovy 0.17% Batchfile 0.02% Objective-C++ 0.18% Dockerfile 0.01% Python 0.61% Shell 0.05% JavaScript 0.97% CSS 0.01% HTML 0.01% Pascal 0.01% CMake 0.01% Ruby 0.01%

kotlin-python's Introduction

Hi there 👋

My name is Piotr Krzemiński. I'm a software development engineer, passionate about software quality, automation and constant process improvement. I work full-stack, mainly with JVM languages and React. My programming languge of choice is Kotlin which I use for both backend and frontend, and which I try to spread through a local Kotlin User Group. I try my best to write clean, functional code.

In the open-source world, I own an ecosystem of projects for creating type-safe GitHub workflows:

How to reach me

Feel free to drop me a line via LinkedIn.

kotlin-python's People

Contributors

4u7 avatar abreslav avatar alexeytsvetkov avatar asedunov avatar bashor avatar darthorimar avatar demiurg906 avatar dimonchik0036 avatar dnpetrov avatar dzharkov avatar erokhins avatar goodwinnk avatar ilgonmic avatar ilmat192 avatar ilya-g avatar ligee avatar mglukhikh avatar nataliaukhorskaya avatar ptalanov avatar romanart avatar semoro avatar svtk avatar svyatoslavscherbina avatar t-kameyama avatar udalov avatar valentinkip avatar vvlevchenko avatar yanex avatar yole avatar zarechenskiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

servb-s

kotlin-python's Issues

Understand (and solve if trivial) MemoryErrors when using MicroPython

For example, when running e2e tests with micropython instead of python3, I got:

Traceback (most recent call last):
  File "python/e2e-tests/out/python-consumer.py", line 1, in <module>
  File "/home/piotr/repos/priv/kotlin-python/python/e2e-tests/out/compiled.py", line 12986, in <module>
MemoryError: memory allocation failed, allocating 26032 bytes

Looking at box tests report, >1000 tests fail with this reason. The good thing is that it's fairly reproducible: for a given box test, it always fails with such MemoryError.

My suspicion is that too much code is fed to micropython. Kotlin/Python doesn't do any Dead Code Elimination for now (to be done in #102), and the compiler output weights ~420 KiB.

Document data flow and steps

The goal is to describe what happens with the input Kotlin code, starting with the CLI, going through the frontend, backend, and ending with the Python code.

Run MicroPython e2e tests

With no changes, I get this:

Traceback (most recent call last):
  File "python/e2e-tests/out/python-consumer.py", line 5, in <module>
OSError: [Errno 13] EACCES

When not writing to file, only one test fails, and the rest produces something that is expected:

$ JDK_9="$JAVA_HOME" python/e2e-tests/run.sh
Starting end-to-end tests
===== Executing 'constructors' =====
  Compiling to Python...
python/e2e-tests/out/kotlin-code.kt:3:26: warning: parameter 'a' is never used
class MultiConstructors1(a: Int) {
                         ^
python/e2e-tests/out/kotlin-code.kt:9:17: warning: parameter 'b' is never used
    constructor(b: String) : this(2) {
                ^
python/e2e-tests/out/kotlin-code.kt:24:17: warning: parameter 'a' is never used
    constructor(a: Int) {
                ^
python/e2e-tests/out/kotlin-code.kt:28:17: warning: parameter 'b' is never used
    constructor(b: String) {
                ^
python/e2e-tests/out/kotlin-code.kt:43:17: warning: parameter 'a' is never used
    constructor(a: Int) {
                ^
python/e2e-tests/out/kotlin-code.kt:47:17: warning: parameter 'b' is never used
    constructor(b: String) : this(3) {
                ^
python/e2e-tests/out/kotlin-code.kt:51:17: warning: parameter 'b' is never used
    constructor(b: Boolean) : this("c") {
                ^
python/e2e-tests/out/kotlin-code.kt:70:17: warning: parameter 'i' is never used
    constructor(i: Int) {
                ^
python/e2e-tests/out/kotlin-code.kt:77:17: warning: parameter 'a' is never used
    constructor(a: Int) : super(4) {
                ^
  Running Python to produce the output...
body 11
Traceback (most recent call last):
  File "python/e2e-tests/out/python-consumer.py", line 6, in <module>
  File "/home/piotr/repos/priv/kotlin-python/python/e2e-tests/out/compiled.py", line 13692, in multi12
  File "/home/piotr/repos/priv/kotlin-python/python/e2e-tests/out/compiled.py", line 13670, in MultiConstructors1_init__Create_
AttributeError: type object 'MultiConstructors1' has no attribute '__new__'
===== Finished with 'constructors' =====
===== Executing 'integers' =====
  Compiling to Python...
  Running Python to produce the output...
True
False
120
4
-11
===== Finished with 'integers' =====
===== Executing 'lambdas' =====
  Compiling to Python...
  Running Python to produce the output...
40
20
21
143
===== Finished with 'lambdas' =====
===== Executing '_simple' =====
  Compiling to Python...
  Running Python to produce the output...
4
===== Finished with '_simple' =====
===== Executing 'stdlib' =====
  Compiling to Python...
  Running Python to produce the output...
123
===== Finished with 'stdlib' =====
All done

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

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().

Adjust for no `__new__` method on MicroPython

When trying to run e2e tests for MicroPython, the constructors test fails with:

  File "python/e2e-tests/out/python-consumer.py", line 6, in <module>
  File "/home/piotr/repos/priv/kotlin-python/python/e2e-tests/out/compiled.py", line 13692, in multi12
  File "/home/piotr/repos/priv/kotlin-python/python/e2e-tests/out/compiled.py", line 13670, in MultiConstructors1_init__Create_
AttributeError: type object 'MultiConstructors1' has no attribute '__new__'

Looks like __new__ is another magic method unavailable on MicroPython. We had to use + instead of __add__ already. Not sure if there's an equivalent here, but still it's a must to fix it given we want to support MP.

Implement DCE

Something is already inherited from JS backend, maybe it can be reused easily.
I also hope that DCE is a solution to MemoryError in MicroPython.

Move box tests reports generation to CI

It takes time and effort to rerun everything locally. Like @SerVB proposed #82, we could move the workflow to GitHub Actions.

As the last step of building a given pull request, there should be an automated commit that updates all the reports (stuff inside python/box.tests/reports).

Be wary of an endless loop: the automation adds the commit, it triggers another build in the PR, which adds another commit, and so on. In theory this loop should be broken as soon as the N-th iteration regenerates the reports and the diff is empty, then I think committing will fail. This case has to be handled gracefully, best if just one build (per test task) is ran for each PR, it commits the reports and that's it.

Optimize `Unit` returns

Originally created by @SerVB.

If it's just Unit, just return Python's None; if it's nullable (Unit?), then do some more complex logic like null -> None and Unit -> Unit.getInstance(). However doing this will also require being careful with Unit assignments, for example, if there is code like val b = println(), it should be compiled like println(); val b = Unit (achievable via introducing an extra lowering) because println() will return None and it's not what we want to assign to b.

Move all the python-related modules under the python dir

For now, python modules are made next to js ones, and that is handy in terms of similarity. However, in terms of developing, looks like having everything in a single dir is more convenient. Since we are going away from js code more and more, maybe we can consider changing the structure of our subprojects and move them.

Assess support for MicroPython

See https://kotlinlang.slack.com/archives/C0289CS37AS/p1637517359061900?thread_ts=1637517359.061900&cid=C0289CS37AS

https://core-electronics.com.au/tutorials/Python_and_MicroPython.html

Let's check how costly it would be to support both regular Python and Micropython (MP).

Ideas/steps:

  • check if there's a regular Linux interpreter for MP
  • smoke test: try running a sample output from current Kotlin Python compiler and see how it goes
  • run box tests with MP interpreter and see what's the success rate comparing to what we have on Python
  • hook MP interpreter into the box tests so that CI runs both
  • think of cases where it would be challenging to support both at the same time

Minify names

For now, in case of names collisions, we have some ineffective logic. At least for classes, if there are classes named as A in different packages, they become A_0 and A_1.

We can minify those, for example, make them at least A and A_0 or even A and A0.

Remove unneeded lowerings and refactor needed ones

Lowerings are a big part of backend logic, so let's ensure we know what happens there and that we don't have anything surplus or weird.

  • #95
  • analyze lowerings and dependencies between them
  • iteratively remove lowerings to get a minimal list of needed ones
  • remove unneeded Gradle module dependencies
  • add unit tests for each lowering, if possible. It's to be able to understand what they really do, so it serves documentation purposes
  • think outside the box: maybe for some case, the JS way is not the correct one? E. g. constructors, it may be a better way to lower them for Python. Now we base on JS version

Support do-while loop fully

Now continue is unsupported. Probably we can support it by compiling do { <body> } while (<condition>) as follows:

firstIterationLoopName = true
while firstIterationLoopName or (<condition>):
    firstIterationLoopName = false
    <body>

We need to introduce loop name, but there is already some logic for it in PyGenerationContext – we can pick the missing parts inside js-ir. Or we could use a concept of tmpVariable, this can be even easier.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.