Kotlift is the first source-to-source language transpiler from Kotlin to Swift

Related tags

Kotlin Kotlift
Overview

Kotlift

Kotlift: The first source-to-source language transpiler from Kotlin to Swift

Kotlift is the first source-to-source language transpiler from Kotlin to Swift.

Kotlift helps you to write business logic once, and reuse most of the code on iOS. Porting an app from Android to iOS will be faster and less error-prone. With Kotlift, Kotlin truly is the Swift of Android.

The program is not intended to support the full Kotlin or Swift language, but most of the generated Swift code will be valid. All framework interfacing code from Android or iOS/Cocoa is not supported. Many functions from the Kotlin stdlib are the same in Swift, therefore less logic has to be rewritten.

Supported versions: Kotlin 1.0.1, Swift 2.2

Supported features

The following language features are currently transpiled, but some edge cases might produce invalid Swift code.

  • Variables, arrays and lists
  • Basic types (String, Boolean, Double, Float, Long, Int, Short, Byte)
  • Functions with parameters
  • If and elvis operator
  • For and while loops
  • String interpolation
  • Main function calling (in Swift playground)
  • Custom rewrites (comment the preceding line with // SWIFT: this.will(be.replaced) to replace the following line)
  • Null safety, safe calls, null coalescing
  • Basic try-catch
  • @Throws annotation / throws
  • Casting
  • Ranges and iteration
  • When / switch case
  • Classes with none or one constructor
  • Inheritance
  • Abstract classes
  • Interfaces / protocols
  • Data classes (constructor and description will be generated)
  • Generics
  • Extension functions
  • Companion objects / static class properties
  • Properties with getters and setters / backing fields / computed properties
  • Visibility modifiers
  • Basic lambdas / closures
  • Maps / dictionaries
  • Sets
  • Very basic smart casts
  • Enums

See also the testcases in Kotlin and the generated Swift files.

Unsupported features

The following features are currently unsupported, but are ordered by likelihood to be implemented soon:

  • (Data) classes, functions or getters & setters with opening & closing brackets in same line or without brackets at all
  • Line-wrapped function and class definitions
  • Unnamed constructor parameter
  • Unnamed function parameters (except the first one)
  • Auto inferred function return types
  • Finally / defer
  • Full support for smart casts
  • ...

Usage

The repository contains an IntelliJ project. Usage of the precompiled jar:

java -jar moshbit.kotlift.jar test-src dest replacementFile.json test-dest

  1. parameter: folder of Kotlin source files. (mandatory)
  2. parameter: destination folder where Swift code should be written to. (mandatory)
  3. parameter: replacement file, used for standard language replacements and may be customized. (mandatory)
  4. parameter: Swift testcase folder. If given, all files in the destination folder are compared to the files in this folder. (optional)

Kotlift uses various regular expressions and a simple structure tree.

For advanced Kotlift usage in your project, modify replacementFile.json to customize replacements (such as .toString() to .mySwiftyToStringFunction()). Include the kotlift.swift file in your project for a basic mapping of the most important Kotlin std-lib functions that are different in Swift.

Dependencies

  • Kotlin 1.0.1

Contribution guidelines

For every transpiled language feature there is a Kotlin test file in test-src and a Swift file in test-dest. Executing Kotlift with a fourth argument not only transpiles all test files, but also checks them for any differences.

Pull requests are welcome. If you add new features, please add Kotlin and Swift test files.

Please contact [email protected] for any inquiries.

Comments
  • replacementFile.json is empty

    replacementFile.json is empty

    It seems that is the replacementFile.json isn't in the format where one object is on each line then loadReplacements fails with replacementFile is empty.

    I have to format the file like {"from": "val", "to": "let","multiple": true} in order for the loading of the replacement to succeed.

    I think maybe we can just create an poko and something like jackson and gson to deserialize.

    opened by donhill 7
  • How to re-build  the jar file

    How to re-build the jar file

    I try to re-build the project to re-generate the jar file ( witch seems outdated) It will be good , to add a section in readme , witch describe the method

    Best regards

    opened by SylvainHocq 0
  • Parsing fails with java.lang.IllegalStateException

    Parsing fails with java.lang.IllegalStateException

    After editing replacementFile.json to fix the json parsing problem, an attempt to translate my own kotlin files to swift fails, as follows:

    $ java -jar moshbit.kotlift.jar dullahan dull.swift/ replacementFile.json [Replacement(from=val, to=let, multiple=true), Replacement(from=null, to=nil, multiple=true), Replacement(from=this, to=self, multiple=true), Replacement(from=Unit, to=Void, multiple=true), Replacement(from=Boolean, to=Bool, multiple=true), Replacement(from=Float, to=Double, multiple=true), Replacement(from=Long, to=Int64, multiple=true), Replacement(from=Int, to=Int32, multiple=true), Replacement(from=Short, to=Int16, multiple=true), Replacement(from=Byte, to=Int8, multiple=true), Replacement(from=println, to=print, multiple=true), Replacement(from=print(), to=print(""), multiple=true), Replacement(from=interface, to=protocol, multiple=true), Replacement(from=get(), to=get, multiple=true), Replacement(from=protected, to=internal, multiple=true), Replacement(from=Collection, to=Array, multiple=true), Replacement(from=.invoke, to=, multiple=true)] [/home/dougm/scalableHealth/HS-Dullahan-K/src/main/kotlin/edu/rice/sh/healthsense/dullahan/Interpreter.kt, /home/dougm/scalableHealth/HS-Dullahan-K/src/main/kotlin/edu/rice/sh/healthsense/dullahan/program/Program.kt, /home/dougm/scalableHealth/HS-Dullahan-K/src/main/kotlin/edu/rice/sh/healthsense/dullahan/program/instructions.kt, /home/dougm/scalableHealth/HS-Dullahan-K/src/main/kotlin/edu/rice/sh/healthsense/dullahan/program/parseXmlCode.kt, /home/dougm/scalableHealth/HS-Dullahan-K/src/main/kotlin/edu/rice/sh/healthsense/dullahan/program/uiElements.kt] Parsing... Exception in thread "main" java.lang.IllegalStateException: Structure tree should be empty, but is [com.moshbit.kotlift.Class@50040f0c] at com.moshbit.kotlift.Transpiler.transpile(Transpiler.kt:636) at com.moshbit.kotlift.Transpiler.parse(Transpiler.kt:17) at com.moshbit.kotlift.MainKt.main(Main.kt:43)

    opened by unkadoug 0
  • Android Officially support kotlin

    Android Officially support kotlin

    @moshbit I think after Google Officially support kotlin in Android this project can be one of the famous projects I hope you take my opinion in consideration to support swift 3 Thanks

    opened by amorenew 0
  • Add support for Swift 3

    Add support for Swift 3

    Xcode 8 is out, and it requires that you use either Swift 3, or Swift 2.3 (which is a transitional version intended, if I got it right, to help "modernizing" Swift 2.2 code).

    opened by noamtamim 1
Owner
Studo
Reliable Services for Students and Universities
Studo
Library to use Kotlin Coroutines from Swift code in KMP apps

KMP-NativeCoroutines A library to use Kotlin Coroutines from Swift code in KMP apps. Flows Kotlin Create an extension property to expose the Flow as a

Rick Clephas 508 Jan 3, 2023
Swift-friendly api generator for Kotlin/Native frameworks

MOKO KSwift KSwift it's gradle plugin for generation Swift-friendly API for Kotlin/Native framework. Kotlin sealed interface/class to Swift enum Kotli

IceRock Development 226 Dec 28, 2022
KmMScientists is a Kotlin multiplatform app with swift ui, jetpack compose, koin and realm

KmMScientists KmMScientists is a Kotlin multiplatform app built with swift ui, jetpack compose, koin and realm. Whats Shared? Local Data Persistence w

Kashif Mehmood 20 Dec 27, 2022
This is a first kotlin project

SmallPocket This is a first kotlin app, help user to save links easily, and can export to Evernote as weekly. Steps: copy link anywhere open SmallPock

KotlinChina 31 Dec 17, 2022
A React Native library making file access easier for developers as first class citizens, without the tears

React Native File Gateway A React Native library making file access easier for developers as first class citizens, without the tears. ⚠️ NOTE: This li

Jimmy Wei 4 Sep 11, 2021
Shoe Store project first Attempt

Shoe Store project first Attempt User Info: email: [email protected] password:12345 I had problem to select the home Screen for the navigation gr

null 0 Nov 25, 2021
LifecycleMvp 1.2 0.0 Kotlin is MVP architecture implementation with Android Architecture Components and Kotlin language features

MinSDK 14+ Download Gradle Add to project level build.gradle allprojects { repositories { ... maven { url 'https://jitpack.io' }

Robert 20 Nov 9, 2021
An example for who are all going to start learning Kotlin programming language to develop Android application.

Kotlin Example Here is an example for who are all going to start learning Kotlin programming language to develop Android application. First check this

Prabhakar Thota 56 Sep 16, 2022
🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.

???? 中文 / ???? 日本語 / ???? English ?? Debug Bottle An Android debug / develop tools written using Kotlin language. All the features in Debug bottle are

Yuriel Arlencloyn 846 Nov 14, 2022
ATH Sample is a sample Authentication and Authorization Application with Kotlin Language and MVVM architecture.

ATH Sample ATH Sample is a sample Authentication and Authorization Application with Kotlin Language and MVVM architecture. Overview ATH Sample is a sa

AbolfaZl RezaEi 4 Jun 8, 2021
A furry-themed assembly language and interpreter written in Kotlin.

A furry-themed assembly language and interpreter written in Kotlin. Inspired by Furcode, the Synacor challenge, and JVM bytecode. I spent multiple hou

Maow 9 Nov 22, 2021
An under development minecraft plugin (1.8.8) to learning Kotlin language

CorePlus CorePlus is a minecraft plugin coded with Kotlin language. Still under development CorePlus will be an essential for each minecraft servers !

Gonz 3 Jun 16, 2021
Lambda-snake.kt - Snake Game Implementation for Web using Kotlin programming language compiled for Javascript

Projeto da disciplina de Linguagem de Programação Funcional 2021.1 (jan/2022) ??

Alex Candido 3 Jan 10, 2022
FaceTimeClone app that implements Coroutines , mvvm architecture , clean architecture , navigation component , hilt , etc.... using kotlin language

This repository contains a FaceTimeClone app that implements Coroutines , mvvm architecture , clean architecture , navigation component , hilt , etc.... using kotlin language

null 17 Dec 13, 2022
Kotlin DALL·E 2 is a new AI system that can create realistic images and art from a description in natural language.

OpenAI Dall•E AI Kotlin Mobile App OpenAI Dall•E Application Build With Kotlin MVVM (Model - View - ViewModel) Clean Architecture, Beautiful Design UI

Murat ÖZTÜRK 15 Jan 1, 2023
Metremenqeemi - Android/iOS app to teach the Coptic Language

ⲙⲉⲧⲣⲉⲙⲛ̀ⲭⲏⲙⲓ The Open Source Android/iOS app to learn how to read and understand the Coptic Language Join our Discord Channel About the Curriculum The

Mark Yacoub 8 Aug 30, 2022
fusion4j - declarative rendering language for the JVM based on Neos.Fusion

fusion4j - declarative rendering language for the JVM based on Neos.Fusion Supports the Neos Fusion syntax/semantic as described in the official Neos

sandstorm 2 May 3, 2022
Open source Crypto Currency Tracker Android App made fully in Kotlin

CoinBit CoinBit is a beautiful CryptoCurrency app, completely open sourced and 100% in kotlin. It supports following features Track prices of over 300

Pranay Airan 50 Dec 5, 2022
KMM RSS Reader: an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile.

KMM RSS Reader This is an open-source, mobile, cross-platform application built with Kotlin Multiplatform Mobile. It's a simple RSS reader, and you ca

Kotlin 1.4k Jan 4, 2023