LoginSignup
65
40

More than 5 years have passed since last update.

トランプ氏をイメージしたプログラミング言語 TrumpScriptでFizzBuzz

Last updated at Posted at 2016-11-11

初めに

トランプ氏が大統領選で勝利したということで, トランプ氏を基にしたプログラミング言語の TrumpScript でFizzBuzzを書いてみようと思います.

TrumpScriptとは?

ライス大学のハッカソンで学生によって(多分)ノリで作られたプログラミング言語です.リポジトリはコチラ

僕は README.md だけでご飯3杯はいけます.文法の説明とかは置いておいて,まずは書いてみましょう!

使い方

$ git clone https://github.com/samshadwell/TrumpScript.git
$ export PATH=$PATH:/path/to/TrumpScript/bin
$ TRUMP hoge.tr

プログラム

Hello, World

初めはおなじみの Hello, World を出力しちゃいましょう.

hello.tr
say "Hello, World"

TrumpScriptでは標準出力への出力を say または tell で行います.文字列はダブルクォーテーションで囲う必要があり,コード中の大文字小文字は全て無視されます.

$ TRUMP hello.tr
Exception: Mac? 'Boycott all Apple products  until such time as Apple gives cellphone info to authorities regarding radical Islamic terrorist couple from Cal'

ああ!なんということでしょう… Apple製品は全てボイコット されるようです…  ちなみにWindowsでも動かないようです(トランプ氏はPC(= 政治的正しさ)を信じるような男ではないかららしいです).

とはいえ,こんなところで躓いていてはどうしようもないので,--shut-upオプションを付与して 黙らせます

$ TRUMP --shut-up hello.tr
Exception: Trump will ensure that 'America is great'

ああ!なんということでしょう… America is great でプログラムを終えなければならないようです.

$ cat hello.tr
say "Hello, World"
America is great.
$ TRUMP --shut-up hello.tr
hello, world

出力できましたね.

FizzBuzz

いよいよFizzBuzzを書いていきます.とりあえず1から99くらいまでのループを書きましょう.

fizzbuzz.tr
trump is 1
as long as, trump fewer 100; do:
    trump is, trump plus 1;
!

America is great.

なんだかゴチャゴチャしてますね.ここで使われている文法をまとめると.

  • 代入は isare かもしくは makeで行う
  • ループは as long as
  • 比較演算は < とか less とか fewer とか smaller とか
  • ( ) ではなく , ; で囲む
  • { } ではなく : ! で囲む
  • 四則演算は + - * /plus minus times over
  • ちなみに do を書いたりとかインデントしたりとか改行したりは筆者が勝手にやったもので必ずしも必要はない

めちゃくちゃですね.では実行してみましょう.

$ TRUMP --shut-up fizzbuzz.tr
Exception: Part of the beauty of me is I'm very rich.

ああ!なんということでしょう… トランプ氏は非常にリッチなのでした.そのためTrumpScriptでは 100万未満の整数リテラルは書けません ちなみに小数点以下のチンケな数値はどうでもいいのでTrumpScriptでは 浮動小数点数を扱えません 超ロックですね.

fizzbuzz.tr
trump is 1000000
as long as, trump fewer 100000000; do:
    trump is, trump plus 1000000;
!

America is great.

ループ部分ができたので次は余りを求めていきましょう.なお,TrumpScriptには余りを求める演算子はありません.

fizzbuzz.tr
trump is 1000000
zero is, 1000000 minus 1000000;

as long as, trump fewer 100000000; do:
    mod3 is trump
    as long as, not mod3 fewer 3000000; do:
        mod3 is, mod3 minus 3000000;
    !

    mod5 is trump
    as long as, not mod5 fewer 5000000; do:
        mod5 is, mod5 minus 5000000;
    !

    mod15 is trump
    as long as, not mod15 fewer 15000000; do:
        mod15 is, mod15 minus 15000000;
    !

    trump is, trump plus 1000000;
!

America is great.

かなり強引ですが,実行してみましょう.

$ TRUMP --shut-up fizzbuzz.tr
Exception: We have a country where to assimilate you have to speak English

ああ!なんということでしょう… TrumpScriptでは変数名には トランプ氏が好きな2100個程の英単語しか使えません 使える単語のリストはコチラです.とりあえず適当に置換しておきましょう

fizzbuzz.tr
trump is 1000000
zero is, 1000000 minus 1000000;

as long as, trump fewer 100000000; do:
    earth is trump
    as long as, not earth fewer 3000000; do:
        earth is, earth minus 3000000;
    !

    her is trump
    as long as, not her fewer 5000000; do:
        her is, her minus 5000000;
    !

    soul is trump
    as long as, not soul fewer 15000000; do:
        soul is, soul minus 15000000;
    !

    trump is, trump plus 1000000;
!

America is great.

最後に条件部分を書いていきます.

fizzbuzz.tr
trump is 1000000
zero is, 1000000 minus 1000000;

as long as, trump fewer 100000000; do:
    earth is trump
    as long as, not earth fewer 3000000; do:
        earth is, earth minus 3000000;
    !

    her is trump
    as long as, not her fewer 5000000; do:
        her is, her minus 5000000;
    !

    soul is trump
    as long as, not soul fewer 15000000; do:
        soul is, soul minus 15000000;
    !

    if, soul is zero?; do:
        say "FizzBuzz"
    !
    else do:
        if, earth is zero?; do:
            say "Fizz"
        !
        if, her is zero?; do:
            say "Buzz"
        !
    !

    if, not earth is zero?; do:
        if, not her is zero?; do:
            say trump
        !
    !

    trump is, trump plus 1000000;
!

America is great.

else if とか and を使おうとすると実行時エラーでどうやっても使えなかったのでかなり冗長なことになってしまいました.二度と書きたくありません.とりあえず実行しましょう.

$ TRUMP --shut-up fizzbuzz.tr
1000000
2000000
fizz
4000000
buzz
fizz
7000000
8000000
fizz
buzz
11000000
fizz
13000000
14000000
fizzbuzz
16000000
17000000
fizz
19000000
buzz
fizz
22000000
23000000
fizz
buzz
26000000
fizz
28000000
29000000
fizzbuzz
31000000
.
.
.
.

TrumpScriptでFizzBuzzができました🎉🎉🎉🎉🎉🎉🎉

桁数を直すのは疲れたのでもうしません.

終わりに

酔った勢いでQiitaを執筆するものではないなと改めて感じました.

この記事で言及した超ロックな言語仕様以外にもいろいろと面白いものが有ります.ぜひ README.md を熟読しましょう.例えば中国製のマシンでは実行できなかったり.

需要は1ミリも無いと思いますが,一応このコードはGitHubにあげました.kagemiku/trump-fizzbuzz

参考サイト

65
40
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
65
40