コンテンツにスキップ

構文の概要

拡張子出力説明
.purus.js標準JavaScript
.cpurus.cjsCommonJSモジュール
.mpurus.mjsESモジュール
-- これは行コメントです
--- これはブロック
コメントです ---

文字列はトリプルスラッシュ /// で囲みます:

const greeting be ///Hello, World///
エスケープ結果
\n改行
\tタブ
\\バックスラッシュ
\//
\[[
\]]
const i be 42
const f be 3.14
const a be true
const b be false
const c be null
const d be nil -- nullのエイリアス
const e be undefined
const arr be [1, 2, 3]
const arr2 be [1; 2; 3] -- セミコロンも使用可能
const empty be []
const inclusive be [0..5] -- [0, 1, 2, 3, 4, 5]
const exclusive be [0...5] -- [0, 1, 2, 3, 4]

..(包含)または ...(排他)を使って配列の一部を切り出します:

const numbers be [0, 1, 2, 3, 4, 5, 6]
const middle be numbers[2..4] -- [2, 3, 4]
const partial be numbers[1...4] -- [1, 2, 3]

コンパイル結果:

const middle = numbers.slice(2, 4 + 1);
const partial = numbers.slice(1, 4);

スライスに代入することで配列の一部を置換できます:

numbers[2..4] be [///a///; ///b///; ///c///]
-- numbers は [0, 1, "a", "b", "c", 5, 6] になります

コンパイル結果:

numbers.splice(2, 4 - 2 + 1, "a", "b", "c");

配列から変数に値を取り出します:

const weather be [///Sunny///; ///Rainy///]
const [today; tomorrow] be weather
-- 変数の値を入れ替える
[today; tomorrow] be [tomorrow; today]

コンパイル結果:

const [today, tomorrow] = weather;
[today, tomorrow] = [tomorrow, today];
const obj be [name be ///Alice///, age be 30]
const empty-obj be [be] -- 空のオブジェクト

Purusでは関数呼び出し、配列、オブジェクト、グループ化のすべてに [] を使用します。(){} は使いません。

ブロックはインデントで定義されます(2スペース推奨):

if x gt 0
console.log[///positive///]
else
console.log[///non-positive///]

識別子にはハイフン(-)を含めることができ、JavaScript出力ではアンダースコアに変換されます:

const my-variable be 42
-- コンパイル結果: const my_variable = 42;