tomatoaiu の Tech Blog

プログラミングやツールについてのまとめブログ

【Bosque】 導入して数字の5を出す

はじめに

MicrosoftからBosqueLanguage(Bosque言語)が登場しました。
手軽にできそうだったので触ってみるぞ!

環境

  • OS: windows8.1
  • Editor: vscode
  • Sh: git bash
  • Language: node v9.11.1
  • PackageManager: npm v5.6.0

clone

とりあえず, masterブランチ(しかないけど)とcommitの数を一つだけという指定をし、容量を軽くしてgit cloneする。

git clone --branch master --depth 1 https://github.com/Microsoft/BosqueLanguage.git

install typescript

npmからライブラリをグローバルに持ってくる。

npm i typescript -g

build

このref_impl内で実行するみたい。

cd BosqueLanguage/ref_impl
npm install && npm run-script build && npm test

ビルドが完了すると、binフォルダができる。

.bsqファイル実行

サンプルで三目ならべのbsqファイルがあるので実行してみる。

/BosqueLanguage/ref_impl (master)
$ node bin/test/app_runner.js ./src/test/apps/tictactoe/main.bsq

実行が成功すると以下のような結果になるみたいですね。

Reading app code...
Executing app code...
Done with -- NSMain::Game@{ board=NSMain::Board@{ cells=NSCore::List[T=NSCore::None|NSCore::String[NSCore::Any]]@{ none, 'o'#NSMain::PlayerMark, 'o'#NSMain::PlayerMark, 'x'#NSMain::PlayerMark, 'x'#NSMain::PlayerMark, 'x'#NSMain::PlayerMark, none, none, none } }, winner='x'#NSMain::PlayerMark }

自分でも.bsqファイルを作成してやってみる

とりあえず数値の5を出すプログラムを作成してみる。
sumフォルダを作成して、その中にmain.bsqファイルを作成。

/BosqueLanguage/ref_impl (master)
$ mkdir ./src/test/apps/sum
$ touch ./src/test/apps/sum/main.bsq

/sum/main.bsqファイルに書き込む。

namespace NSMain;

entrypoint function main(): Int {
  return 2 + 3;
}

作成したファイルを実行。

/BosqueLanguage/ref_impl (master)
$ node bin/test/app_runner.js ./src/test/apps/sum/main.bsq
Reading app code...
Executing app code...
Done with -- 5

5がでたーーーーーー!

vs codeの拡張ツールでシンタックスハイライトを入れる

git cloneしてきたファイルの中にbosque-language-toolsフォルダがあるので、
それをホームディレクトリの.vscode/extensionsフォルダにそのまんま入れる。
vscodeを更新するか再起動で色がつくようになる。

構文を見てみる

Bosque言語を無事に導入できたので構文をさらっと見るぞ。
導入編はこちらです。
初期段階(2019-04-20当時)なのでこれから変わるかもしれないけど。。。

ディレクトリ

  • /BosqueLanguage/ref_impl
    • src/test/apps/playground/main.bsq

実行方法

/BosqueLanguage/ref_impl (master)
$ node bin/test/app_runner.js ./src/test/apps/playground/main.bsq

main()

main関数なのかな。これがないと動かないみたい。
mainの名前をaaaとかにすると、
fail with exception -- TypeError: Cannot read property 'invoke' of undefined
のようなエラーメッセージが出るぞ。
同様なエラーがentrypointを書かない場合もでるぞ。

namespace NSMain;

entrypoint function main(): Int {
  return x + y;
}

Immutable Values

変数が基本的には再代入不可のようですね。
変数の定義はvarで、再代入したい場合のみvar!と書くようです。

namespace NSMain;

entrypoint function main(): Int {
  var sum = 2 + 3;
  sum = -1;
  return sum; // output: Error
}
namespace NSMain;

entrypoint function main(): Int {
  var! sum = 2 + 3;
  sum = -1;
  return sum; // output: -1
}

function

typescriptみたいに型を付けることができるみたいですね。

namespace NSMain;

function add2(x: Int, y: Int): Int {
    return x + y;
}

entrypoint function main(): Int {
  return add2(2, 3); // output: 5
}

JsのObjectみたいなの

@をつけるみたいですね。

namespace NSMain;

entrypoint function main(): {f: Int, g: Int, h: Int} {
  var x = @{f=1, g=2, h=3};
  return x; // output: @{ f=1, g=2, h=3 }
}

取り出すときにx@のようにすることで必要分のプロパティ?が取得できました。

namespace NSMain;

entrypoint function main(): {f: Int, h: Int} {
  var x = @{f=1, g=2, h=3};
  return x@{f, h}; // output: @{ f=1, h=3 }
}

他にも<+, <~等あるのですが、動きませんでした。
それかやりかたが間違ったのか。

None Processing

noneというjsでいうところのnull, undefinedのような状態を判断できるようです。

function foo(val?: {tag: Int, value?: String}): String {
    return val?.value ?| "[No Value]";
}

entrypoint function main(): String {
  return foo(@{tag=1}); // output: [No Value]
}

終わりに

他にも機能はありますが、うまく動かせないものが多かったので、いつか再挑戦してみます。
githubのmarkdownのコードブロックでbosqueがサポートされた頃にもう一度触ってみたい。

備考

見慣れない予約語っぽいのとか演算子っぽいの一覧

entrypoint

entity // クラス?
provides // interface的な?
concept

method
field
factory
override
requires

// Bulk Algebraic Data Operations
@
<~
<+

// Iterative Processing
|>
|??>
|?>

// Type System
<:
<!
===

// None Coalescing
?|
?&

// Validation
assert 
check 

// Statement Expressions
match
case

参考

  • microsoft/BosqueLanguage: The Bosque programming language is an experiment in regularized design for a machine assisted rapid and reliable software development lifecycle., 入手先 https://github.com/microsoft/BosqueLanguage