(この記事はFarmnote Advent Calendar 21 日目の記事です)
札幌オフィスの金井(@kn1kn1)です。主にFarmnote ColorのETLを担当しています。
私にとっての2018年は、RubyKaigiでの登壇やAlgorave Tokyoへの出演があり、そして何よりも第二子誕生があって多忙な日々でした。関係した皆様に感謝致します。
さて、アドベントカレンダーということですが、諸々の都合(主に私の時間の都合)で技術的にゆるい話題になりました。きちんとした内容の記事をご所望の方は、ぜひ他のメンバーのエントリーをどうぞ。
本日紹介するのはRockstarです。Rockstarはプログラミング言語で、https://github.com/dylanbeattie/rockstar に仕様があります。
例えばFizzBuzzは以下のようなコードになります。(ロックというより、厨二的というほうが正しいかもしれない)
Midnight takes your heart and your soul
While your heart is as high as your soul
Put your heart without your soul into your heart
Give back your heart
Desire is a lovestruck ladykiller
My world is nothing
Fire is ice
Hate is water
Until my world is Desire
Build my world up
If Midnight taking my world, Fire is nothing and Midnight taking my world, Hate is nothing
Shout "FizzBuzz!"
Take it to the top
If Midnight taking my world, Fire is nothing
Shout "Fizz!"
Take it to the top
If Midnight taking my world, Hate is nothing
Say "Buzz!"
Take it to the top
Whisper my world
実行すると以下のようになります。
1.2.Fizz!4.Buzz!Fizz!7.8.Fizz!Buzz!11.Fizz!13.14.FizzBuzz!16.17.Fizz!19.Buzz!Fizz!22.23.Fizz!Buzz!26.Fizz!28.29.FizzBuzz!31.32.Fizz!34.Buzz!Fizz!37.38.Fizz!Buzz!41.Fizz!43.44.FizzBuzz!46.47.Fizz!49.Buzz!Fizz!52.53.Fizz!Buzz!56.Fizz!58.59.FizzBuzz!61.62.Fizz!64.Buzz!Fizz!67.68.Fizz!Buzz!71.Fizz!73.74.FizzBuzz!76.77.Fizz!79.Buzz!Fizz!82.83.Fizz!Buzz!86.Fizz!88.89.FizzBuzz!91.92.Fizz!94.Buzz!Fizz!97.98.Fizz!Buzz!
Rockstarはいくつかの言語で実装されているようですが、今回はOCamlによる実装であるrockstar-mlを試してみましょう。
$ git clone https://github.com/lkwq007/rockstar-ml.git
$ cd rockstar-ml
$ make
ocamlbuild -use-menhir rockstar.native
make: ocamlbuild: No such file or directory
make: *** [all] Error 1
git cloneしてmakeしますがエラーになってしまうので、必要なものをインストールして再度makeします。
$ brew install ocaml opam
$ opam init
$ eval $(opam env)
$ opam install menhir
$ make
ocamlbuild -use-menhir rockstar.native
+ menhir --ocamlc ocamlc.opt --infer parser.mly
Warning: 7 states have shift/reduce conflicts.
Warning: 44 shift/reduce conflicts were arbitrarily resolved.
+ ocamlc.opt -c -o lexer.cmo lexer.ml
File "lexer.mll", line 97, characters 15-31:
Warning 3: deprecated: Stdlib.String.lowercase
Use String.lowercase_ascii instead.
File "lexer.mll", line 97, characters 74-90:
Warning 3: deprecated: Stdlib.String.lowercase
Use String.lowercase_ascii instead.
+ ocamlopt.opt -c -o lexer.cmx lexer.ml
File "lexer.mll", line 97, characters 15-31:
Warning 3: deprecated: Stdlib.String.lowercase
Use String.lowercase_ascii instead.
File "lexer.mll", line 97, characters 74-90:
Warning 3: deprecated: Stdlib.String.lowercase
Use String.lowercase_ascii instead.
Finished, 24 targets (6 cached) in 00:00:01.
warnは出ていますが上手くいったようです。
Bohemian is Rhapsody
Whisper Bohemian
このコードをb.rock
として保存して、rockstar.native
の引数に渡します。
$ ./rockstar.native b.rock
8.%
8
が出力されれば上手くいってます。ありがとうフレディ。
上の例では、Bohemian
という変数をRhapsody
で初期化しています。8
という数値になるのは、Rhapsody
が変数として定義されておらず、8文字だったからです。これは、Poetic Number Literalsと呼ばれているものです。
他にも、
Tommy was a lovestruck ladykiller
と書くと、Tommy
には100
が入ったり、
A killer is on the loose
だと、A killer
には235
が入ります。
文字列型も同じように初期化できるかと思ったのですが違ったようで、以下のようなコードを書くと、
Put "Hello, I love you" into the door
the door
に"Hello, I love you"が入ります。ありがとうJim。
○○ takes △△
で関数が定義できます。呼び出す際は、○○ taking △△
です。
Axis takes a bold and your love
Put a bold without your love into your telling
Give back your telling
Put Axis taking 6, 9 into Exp
Say Exp
この例では、Axis
が関数名でa bold
とyour love
が引数になります。
実行して-3
が出力されましたか?ありがとうJimi。左手で握手しよう。
文法は他にもありますが、ここまで見てきたものを使って簡単なプログラムを作りました。
Dawn is confusion disillusion
Confusion is my epitaph
Word is carried
Sundown takes the outing and the starless
My world is nothing
The shrieking is us
Put the outing with the outing into the evening
Put the starless without the evening into your cruel
Put your cruel over the shrieking into the emptiness
Put "\n" into the tangle
Until my world is higher than the emptiness
Put " " into the yearns
Whisper the yearns
Build my world up
The completeness is nothing
Until the completeness is the evening
Put "*" into the dazzling
Whisper the dazzling
Build the completeness up
Whisper the tangle
Innocents is a je
Purred is scream neurosurgion
My greed is nothing
Until my greed is Innocents
Sundown taking my greed, Purred
Build my greed up
My tongue is nothing
Until my tongue is Innocents
Put my tongue with Word into the claw
Sundown taking the claw, Purred
Build my tongue up
My injury is nothing
Until my injury is Innocents
Put my injury with Innocents into the starving
Sundown taking the starving, Purred
Build my injury up
The shade is nothing
Until the shade is Word
Sundown taking Word, Purred
Build the shade up
これをt.rock
として保存して、実行してみましょう。
$ ./rockstar.native t.rock
**
****
******
********
**********
************
**************
****************
******************
********************
**********************
**************
****************
******************
********************
**********************
************************
**************************
****************************
******************************
********************************
**********************************
************************************
************************
**************************
****************************
******************************
********************************
**********************************
************************************
**************************************
****************************************
******************************************
********************************************
**********************************************
**************
**************
**************
**************
**************
**************
**************
少し早いですが良いクリスマスを!ではまたどこかで。