Yuta Koshizawa • 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.
6 comments
Wataru Nishimoto • about 9 years ago
面白そうですね!
大学の授業でやった程度しかトランスパイラの知識はないですし,JSについては本当にさっぱりなんですが,興味があります
Rintaro Ishizaki • about 9 years ago
Interesting!
I think I can contribute around Lexer/Parser area.
Are you planning to use any specific parser framework?
Yuta Koshizawa • about 9 years ago
> watura さん
ぜひ一緒にやりましょう!
> Rintaro-san
I am planning to use TryParsec https://github.com/tryswift/TryParsec . TryParsec is a parser combinator implemented in Swift, which was demonstrated in Yasuhiro Inami's great talk last year https://realm.io/news/tryswift-yasuhiro-inami-parser-combinator/ at the first try! Swift conference. I think that is the one to use for the first try! Swift hackathon!
Wataru Nishimoto • about 9 years ago
> Yuta さん
ぜひ!
So I need to read TryParsec and understand very basic grammar of JS!!
Yuta Koshizawa • about 9 years ago
> watura-san
:-)
This https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide may help you to learn JavaScript.
And this talk https://realm.io/news/tryswift-yasuhiro-inami-parser-combinator/ is useful to understand TryParsec.
Yuta Koshizawa • about 9 years ago
I created a project and a "Submission" for SwiftScript.