LoginSignup
0
0

More than 3 years have passed since last update.

「文言」ruby出力に挑戦

Last updated at Posted at 2020-01-04

はじめに

漢文風プログラミング言語「文言/wenyan-lang」は、初期commitが2019年12月という新しい言語で、現在進行形で開発が進んでいます。その美しいレンダリングは漢字圏に住む我々日本人としても目の離せない言語です。

初期時はハードルが高めでしたが、現在はnpmからインストールし簡単に使い始められます。まだの方はこちらの記事を参考に今すぐインストールしてみて下さい

この記事では「文言/wenyan-lang」で書かれたプログラムからrubyソースに変換し、実行してみます

動作確認

こちらの記事の確認環境は以下の通りです

npm v6.13.4
wenyan v0.2.0

実行

公式を見ると
Screen Shot 2020-01-04 at 1.24.14.png

ruby出力に対応しているとの事、
usageを確認します

$ wenyan
,_ ,_
 \/ ==
 /\ []

WENYAN LANG 文言 Compiler v0.2.0

Usage: wenyan [options] [files...]

Options:
  -v, --version        Output the version
  -l, --lang <lang>    Target language, can be "js", "py" or "rb" (default: 
                       "js")
  -c, --compile        Output the compiled code instead of executing it
  -e, --eval <code>    Evaluate script
  -i, --interactive    Interactive REPL
  -o, --output [file]  Output compiled code or executing result to file
  -r, --render         Outputs renderings
  --roman [method]     Romanize identifiers. The method can be "pinyin", 
                       "baxter" or "unicode"
  --outputHanzi        Convert output to hanzi (default: true)
  --log <file>         Save log to file
  --title <title>      Override title in rendering
  -h, --help           Display help

--lang rb で行けそうです。
実行してみると

$ wenyan -l rb examples/helloworld+.wy 
Target language "rb" is not supported for direct executing. Please use --compile option instead.

替わりに--compileオプションを使えとの事、実行してみます

$ wenyan --compile rb examples/helloworld+.wy 
ENOENT: no such file or directory, open '/xxxxxxx/wenyan-lang/rb'

--compileのオプションの指定方法がいまいちのようです
言語指定してみましょう

$ wenyan --compile -l rb examples/helloworld+.wy 
# encoding: UTF-8
require 'forwardable'
<略 45行>
#####=3
甲.times do |_rand1|
    _ans1="問天地好在。"
    p([_ans1].join)
end 

なんか50行を超える行が出力されましたが、最後の5行は公式にあった出力です。やった:v:

ruby出力方法がわかったので、公式にある「エラトステネスの篩」を実行してみます
Screen Shot 2020-01-04 at 2.56.37.png

また、その実行結果を保存し、rubyで実行してみます

$ wenyan --compile  -l rb -o sieve.rb examples/sieve.wy 
$ tail -n 42 sieve.rb 
def 埃氏篩()
    =Ctnr.new
    .times do |_rand1|
        .push(true)
    end 
    _ans1=/2;
    甲半=_ans1;   =2
    while true do
        if ==甲半
            break
        end 
        =2
        while true do
            if ==甲半
                break
            end 
            _ans2=*;
            =_ans2;          if <=
                [-1]=false
            else
                break
            end 
            _ans3=1+;            =_ans3
        end 
        _ans4=1+;        =_ans4
    end 
    諸素=Ctnr.new
    =2
    while true do
        if ==.length
            break
        end 
_ans5=[-1];
        素耶=_ans5;       if 素耶
            諸素.push()
        end 
        _ans6=1+;        =_ans6
    end 
    return 諸素
end

_ans7=埃氏篩(100);p([_ans7].join)
$ ruby sieve.rb
"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]"

rubyで100までの素数が求められました:ok_hand:

おわりに

美しいレンダリングとruby出力がしたくて、「文言」言語の存在を知った12月19日その日に「文言」アドベントカレンダーを作成しました。

はじめは環境も作れず参加者も増えず、無謀なのかと思いましたが、開発が進み簡単に実行できるようになりました。みなさんも是非美しいレンダリング、漢字でプログラム出来る新感覚、体験してみて下さい

入力がちょっと大変なのですが、リファレンススニペットも活用し楽しんで下さい

0
0
0

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
0
0