vinivendra / Gryphon

The Swift to Kotlin translator.
https://vinivendra.github.io/Gryphon/
Other
606 stars 46 forks source link

Support for optional function type invoke #50

Closed rlinoz closed 4 years ago

rlinoz commented 4 years ago

Describe the bug Invoking an optional function type is not being properly translated.

How to reproduce it When invoking a optional function type in Swift foo?()

The result in Kotlin is foo?()

When it should be foo?.invoke()

Your environment (please complete the following information):

vinivendra commented 4 years ago

This one might be a bit more complicated to solve... here's what I'd try to do:

  1. Create a new transpilation pass in TranspilationPass.swift. Call it something like OptionalFunctionsTranspilationPass or whatever.

  2. Override the processCallExpression method.

  3. Inside the method, check if the callExpression's function is an OptionalExpression. If it is, return the expression you want in its place. It'll probably be something like this:

    CallExpression(
    function: DotExpression(
        left: callExpression.function,
        right: DeclarationReferenceExpression(identifier: "invoke"))
    parameters: callExpression.parameters)

    only more complicated since I omitted some arguments. If the function isn't an OptionalExpression, return something like super.processCallExpression(callExpression).

  4. Make sure to add the necessary // gryphon insert: and // gryphon annotation: comments to the transpilation pass (just look at the other transpilation passes to see where they go). You'll know you did it right if the BootstrappingTests are passing (run them with ./runTests.sh -b).

  5. Make the code run your new transpilation pass, probably somewhere around here.

That's it :)

I'd try to solve #49 before this, it might make things easier.