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

Translate property declaration and initialization #12

Closed krzema12 closed 3 years ago

krzema12 commented 3 years ago

Regressions:

krzema12 commented 3 years ago

Example failing code with UnboundLocalError error:

  13998 def box():
  13999     init_properties_python_kt()
  14000     return 'OK'
  14001 
  14002 pass
  14003 def init_properties_python_kt():
  14004     if not (properties_initialized_python_kt):
  14005         properties_initialized_python_kt = True
  14006         xi = 0
  14007         xin = 0
  14008         xinn = None
  14009         xl = Long(0, 0)
  14010         xln = Long(0, 0)
  14011         xlnn = None
  14012         xb = visitConst_other_Byte
  14013         xbn = visitConst_other_Byte
  14014         xbnn = None
  14015         xf = kotlin_Float(0)
  14016         xfn = kotlin_Float(0)
  14017         xfnn = None
  14018         xd = 0.0
  14019         xdn = 0.0
  14020         xdnn = None
  14021         xs = visitConst_other_Short
  14022         xsn = visitConst_other_Short
  14023         xsnn = None
  14024     
krzema12 commented 3 years ago

Asked for help here: https://kotlinlang.slack.com/archives/C0289CS37AS/p1627302570005200 and here: https://kotlinlang.slack.com/archives/C0BUHC9HD/p1628503799004600

krzema12 commented 3 years ago

To solve Unit_instance, we have to define it globally and then use global Unit_instance in Unit_getInstance. Then, support visitExpression_other__inToPyStatementTransformer_org_jetbrains_kotlin_ir_expressions_impl_IrConstructorCallImpl.

krzema12 commented 3 years ago

Next problem: there's a clash in companion object names:

> less python/experiments/out_ir.py | grep Companion_instance
Companion_instance = None
    global Companion_instance
    if Companion_instance == None:
    return Companion_instance
        Companion_instance = self
Companion_instance = None
    global Companion_instance
    if Companion_instance == None:
    return Companion_instance
        Companion_instance = self
Companion_instance = None
    global Companion_instance
    if Companion_instance == None:
    return Companion_instance
        Companion_instance = self
Companion_instance = None
    global Companion_instance
    if Companion_instance == None:
    return Companion_instance

In JS, some numbers are added to ensure isolation:

> less python/experiments/out-ir.js | grep Companion_instance
    Companion_instance = this;
  var Companion_instance;
    if (Companion_instance == null)
    return Companion_instance;
    Companion_instance_0 = this;
  var Companion_instance_0;
    if (Companion_instance_0 == null)
    return Companion_instance_0;
    Companion_instance_1 = this;
  var Companion_instance_1;
    if (Companion_instance_1 == null)
    return Companion_instance_1;
    Companion_instance_2 = this;
  var Companion_instance_2;
    if (Companion_instance_2 == null)
    return Companion_instance_2;
    Companion_instance_3 = this;
  var Companion_instance_3;
    if (Companion_instance_3 == null)
    return Companion_instance_3;

The logic responsible for it is in compiler/ir/backend.py/src/org/jetbrains/kotlin/ir/backend/py/utils/NameTables.kt and it's executed, but it doesn't end up in the output. TODO: find why.

Conclusion: we have to use the context.getNameFor... in every transformer method to get the names right.

krzema12 commented 3 years ago

TODO: once #27 and #28 is merged, ensure that all tests that passed so far thanks to this PR also pass in the other mentioned PRs, and then close this PR without merging.

krzema12 commented 3 years ago

20 tests pass thanks to this PR, and not on python-backend