LoginSignup
2
1

More than 5 years have passed since last update.

他の言語に変換可能なプログラム言語 Zolang から Kotlin, Swift への変換を試す

Last updated at Posted at 2018-10-11

Zolang でコードを1度記述すると、多数の言語に変換できるという目的で作られた言語のようです。
まだまだ作り途中っぽい。

例えば、Swift, TypeScript, JavaScript, Ruby のそれぞれで同一の Model を扱うようなユースケースを想定しているとのこと。

Install

Mac OS でのインストールは以下のコマンドでできます

$ curl -LO https://github.com/Zolang/Zolang/releases/download/0.1.12/zolang; mv zolang /usr/local/bin/; chmod +x /usr/local/bin/zolang
  • Install の確認
$ zolang


                   @@@     @
                  @###+@  `##
                @+++##+#+@+@@;
             `@++'#'#+##+#@#@;
             `#++++++####+##@;
 :::::::     #+#,...,######@@;    ;;          ;;     ;;     ;;     ';;;;
 ;;;;;;;    `'+'.::.,+;;;#+##;    ;;          :;     ;;     ;;     ;;;;;
 :::::;;    `;+':';,,+;#;####     ;;       .,,``,,`  ;;,,.  ;;   ,, ````
     .;;    `+;+....,####@##;     ;;       ,;;  ';`  ;;;;.  ;;   ;;
     .;;     +,'....@#######      ;;       ,;;  ';`  ;;;;.  ;;   ;;
   ';.       `.:...:.####@#;      ;;       ,;;;;;;`  ;;  `;;;;   ;;  ,;;
   ';.        +.,,.'@@#####       ;;       ,;;;;;;`  ;;  `;;;;   ;;  ,;;
 ..```        +..,..,######       ;;       ,;;  ';`  ;;     ;;   ;;  ,;;
 ;;           `.....,+####;       ;;       ,;;  ';`  ;;     ;;   :;  ,;;
 ;;            '....,#####        ;;.....  ,;;  ';`  ;;     ;;   .......
 ;;;;;;;       `.,.+:#@##;        ;;;;;;;  ,;;  ';`  ;;     ;;     ;;`
 :;;;;;;        +.,';#@##;        ';;;;;'  ,';  ';.  ;;     ';     ';`
                +...,###+
                 @..,###'
                  `',+'


Thanks for using Zolang 0.1.12
USAGE: zolang [ACTION]

    HELP: The Zolang CLI is the compiler and template manager for the Zolang programming language

    ACTIONS:
          init                Initializes a new Zolang project with a ./zolang.json and some example code.
          build               Compiles Zolang based on the settings specified in ./zolang.json
          watch               Runs compiler in hot-reload mode, observeing file changes for source files at paths specified in ./zolang.json

Zolang => Kotlin, Swift に変換

  • 作業ディレクトリの作成
$ mkdir  MyZolangProject
$ cd MyZolangProject
  • プロジェクトの作成
$ zolang init
Creating folder structure ...
Fetching templates ...
Cloning into 'ZolangTemplates'...
remote: Enumerating objects: 144, done.
remote: Counting objects: 100% (144/144), done.
remote: Compressing objects: 100% (94/94), done.
remote: Total 144 (delta 65), reused 101 (delta 39), pack-reused 0
Receiving objects: 100% (144/144), 17.84 KiB | 59.00 KiB/s, done.
Resolving deltas: 100% (65/65), done.



Done
  • config の設定( zolang.json )

以下はデフォルトのままです

{
  "buildSettings": [
    {
      "sourcePath": "./.zolang/src/",
      "stencilPath": "./.zolang/templates/swift",
      "buildPath": "./.zolang/build/swift",
      "fileExtension": "swift",
      "separators": {
        "CodeBlock": "\n"
      }
    },
    {
      "sourcePath": "./.zolang/src/",
      "stencilPath": "./.zolang/templates/kotlin",
      "buildPath": "./.zolang/build/kotlin",
      "fileExtension": "kt",
      "separators": {
        "CodeBlock": "\n"
      }
    }
  ]
}
  • %project_root%/.zolang/src/example.zolang を編集

文法は Zolang/Zolan の README にのってます

let i as number be 0

while (30 < i) {
  print(i)
  make i be i plus 1
}
  • build
$ zolang build
Compiling Zolang...
Compiling example.zolang to /path/to/MyZolangProject/.zolang/build/swift/example.swift
Finished compiling to /path/to/MyZolangProject/.zolang/build/swift/example.swift
Compiling example.zolang to /path/to/MyZolangProject/.zolang/build/kotlin/example.kt
Finished compiling to /path/to/MyZolangProject/.zolang/build/kotlin/example.kt
Success!

結果を確認

swift のコード

var i: Double = 0.0
while 30.0 > i {
    print(i)
    <<<<<<< HEAD
    i = i + 1.0

}

実行結果

<<<<<<< HEAD の部分はバグかな?削除して実行しました

0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
11.0
12.0
13.0
14.0
15.0
16.0
17.0
18.0
19.0
20.0
21.0
22.0
23.0
24.0
25.0
26.0
27.0
28.0
29.0

ideone での実行結果

kotlin のコード

var i: Double = 0.0
while (30.0 > i) {
    print(i)
    i = i + 1.0

}

こちらは <<<<<<< HEAD がなくて、そのまま実行できるかな・・・

prog.kt:2:1: error: expecting a top level declaration
while (30.0 > i) {
^
prog.kt:2:7: error: expecting a top level declaration
while (30.0 > i) {
      ^
prog.kt:2:8: error: expecting a top level declaration
while (30.0 > i) {
       ^
prog.kt:2:13: error: expecting a top level declaration
while (30.0 > i) {
            ^
prog.kt:2:15: error: expecting a top level declaration
while (30.0 > i) {
              ^
prog.kt:2:16: error: expecting a top level declaration
while (30.0 > i) {
               ^
prog.kt:2:18: error: expecting a top level declaration
while (30.0 > i) {
                 ^
prog.kt:2:18: error: function declaration must have a name
while (30.0 > i) {
                 ^

ideone での実行結果

無念・・・。

Kotlin 全く知らないので、誰かツッコミあればコメントお願いします。

雑感

まだまだ先が長そうですが、仕上がったら面白いかも?

2
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1