•   about 9 years ago

SwiftScript: prototyping a transpiler from Swift to JavaScript / SwiftをJavaScriptに変換するトランパイラを試作する

Example

// Swift
let numbers: [Int] = [2, 3, 5]
for number in numbers {
    print(number)
}
let squared = numbers.map { $0 * $0 }

// JavaScript (ES6)
const numbers = [2, 3, 5];
for (number of numbers) {
    console.log(number);
}
const squared = numbers.map(x => x * x);

Goal

To develop a prototype of a transpiler from Swift to JavaScript and an online REPL like TypeScript Playground for demonstrations

Swift から JavaScript に変換するトランスパイラのプロトタイプと、 TypeScript Playground のようなデモ用のオンライン REPL の開発

Motivation

I love Swift and I want to write codes in Swift for every purpose. Front-end web development is one of the fields in which I am eager to write codes in Swift. WebAssembly may realize it far in the future. However I can not wait that long! Transpilers from Swift to JavaScript can be a solution.

僕は Swift が好きなので、ありとあらゆる目的のために Swift を書きたいと思っています。 Web のフロントエンド開発は特に僕が望んでいる分野の一つです。遠い将来には WebAssembly がそれを実現してくれるかもしれません。しかし、そんなに長く待てません! Swift から JavaScript へのトランスパイラは一つの解になり得ると思います。

FAQ

Q. Is it possible to implement the transpiler in 7 hours? / 7 時間でトランスパイラを実装するなんて可能なんですか?

I think supporting a subset of Swift makes it possible. I also plan to convert ASTs directly to JavaScript codes without semantic analysis in this Hackathon. It is useless for practical use, but enough to demonstrate the concept of the transpiler.

Swift のサブセットをサポートするだけなら可能だと思います。また、今回のハッカソンでは意味解析をせずに抽象構文木を直接 JavaScript のコードに変換するつもりです。実用的ではありませんが、このトランスパイラのコンセプトをデモンストレーションするには十分だと思います。

Q. How about ShiftJS? / ShiftJS はどうですか?

It fails transpilations by just adding `Int?` or `throws` to a code. It is also implemented by JavaScript. I want one implemented by Swift because it makes it possible to transpile the transpiler itself in the future and we get both implementations by Swift and JavaScript ;-)

ShiftJS は `Int?` や `throws` をコードに追加するだけでトランスパイルに失敗します。また、 ShiftJS は JavaScript で実装されています。 Swift で実装されたトランスパイラであれば、将来的にはトランスパイラ自身をトランスパイルすることで、 Swift と JavaScript の両方の実装を手に入れることができます ;-)

Comments are closed.