Git Product home page Git Product logo

clojureswift's Introduction

ClojureSwift

A prototype of a compiler from Clojure to Swift.

Why ClojureSwift?

I enjoy programming in Clojure. Its value orientation, dynamism, and simplicity enable a level of power and flexibility that many other programming languages, including Swift, cannot provide. This got me thinking, 'Can I use Clojure to program iOS apps?.' There is ClojureScript, which, in combination with React Native, can be used to build iOS apps.1 However, with React Native, you still don't have direct access to the platform's native APIs, and, as a result, you still need to use Swift sometimes. So, I decided to see if I could compile Clojure to Swift. It would also be a great way to learn more about Clojure and compilers, and to have some fun :).

How does ClojureSwift work?

At a high level, the Clojure compiler can be broken down into the following components:

reader -> analyzer -> emitter
  • The reader reads Clojure text and transforms it into Clojure data structures.
  • The analyzer analyzes the Clojure data and enriches it, outputting an AST.
  • The emitter walks the AST and emits the target code. In the case of Clojure, the emitter emits JVM bytecode; in the case of ClojureScript, JavaScript code; and in the case of ClojureSwift, Swift code.

Fortunately, the Clojure compiler is very modular. ClojureSwift reuses the reader and tools.analyzer, which is a host-agnostic analyzer for Clojure code. On top of it, it provides tools.analyzer.swift for Swift-specific analysis, and a Swift-specific emitter.

Ultimately, the idea is that the emitter would automatically feed into the Swift compiler, which would then compile the Swift code down to native code.

Status

As said above, this is a prototype. It's nowhere near production-ready; however, it demonstrates the ability to compile certain Clojure forms for Swift. For instance:

(def id #uuid "1369709c-2bdc-4e35-9ae1-1cde9068f672")

is compiled to:

var id = Foundation.UUID(uuidString: "1369709c-2bdc-4e35-9ae1-1cde9068f672")!

Development

To run the tests:

clj -X:test:test/run

Footnotes

  1. At the time of this writing, there is also ClojureDart, but this didn't exist when I decided to prototype ClojureSwift. โ†ฉ

clojureswift's People

Contributors

sdduursma avatar

Watchers

 avatar

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.