ThinScript - C/JavaScript/WebAssemblyに変換できるJavaScript代替言語
ここ数年、JavaScriptの代替言語が多数出てきています。また、JavaScript自身も進化しており、個人的にはECMA2016が書けるようになればCoffeeScriptなどは頼らなくとも良いのではないかと思い始めています。 そして今回はさらに新しい言語としてThinScriptを紹介します。TypeScriptにインスパイアされたというこの言語の大きな特徴はWebAssemblyへの変換をサポートしていることでしょう。
ThinScriptの使い方
ThinScriptの例文です。よく見るWebAssemblyはJavaScriptではなくCをベースにしているので、それに比べるとずいぶんJavaScript依りな気がします。
declare function print(text: string): void;
class Link {
value: int;
next: Link;
}
class List {
first: Link;
last: Link;
append(value: int): void {
var link = new Link();
link.value = value;
// Append the new link to the end of the chain
if (this.first == null) this.first = link;
else this.last.next = link;
this.last = link;
}
}
extern function demo(): int {
var list = new List();
list.append(1);
list.append(2);
list.append(3);
var total = 0;
var link = list.first;
while (link != null) {
total = total + link.value;
link = link.next;
}
#if JS
print("Hello from JavaScript");
#elif WASM
print("Hello from WebAssembly");
#elif C
print("Hello from C");
#else
print("Unknown target");
#endif
return total;
}
これを変換すると、JavaScript/WebAssembly/Cのコードに変換できます。
WebのデモでCに変換した例。
JavaScriptに変換して実行もできます。
こちらはWebAssembly。実行よりもダウンロードする方が良さそうです。
WebAssemblyがいかに高速であってもJavaScriptと別な言語であると流行るのが難しいかも知れません。ThinScriptであればJavaScriptに近く、JavaScript/WebAssembly両方いけるので、習得しておくとWebAssemblyによる開発が早くなるかも知れません。
ThinScriptはnode/JavaScript製のソフトウェア(ソースコードは公開されていますがライセンスは明記されていません)です。
ThinScript Compiler Demo evanw/thinscript: A low-level programming language inspired by TypeScript