Skip to content

Beginner zhtw

Enoch-199811 edited this page Aug 9, 2026 · 1 revision

入門——BioLang 第一步

English | 中文 | Русский | Español | Português | 繁體中文 | Deutsch

本頁教授核心概念與控制流程。你只需要一個建置好的 bio 二進位檔和一個文字編輯器。

BioLang 是什麼?

BioLang 是一種串流導向的語言。這裡不是「萬物皆物件」,而是萬物皆串流——請求與回應的流動。你執行的每個操作都是一個可能被拒絕請求。這使得錯誤處理成為語言的一等公民,而非事後才補救。

執行你的第一個程式

建置直譯器:

make
# → bin/bio

建立 hello.bio

program main;

Main {
    void exec() {
        CIO::println("Hello, BioLang!");
    }
}

執行它:

bin/bio hello.bio
# Hello, BioLang!

每個程式都有相同的骨架:

  • program main; — 宣告此檔案是一個主程式(具有進入點)。
  • Main { ... } — 主程式串流。
  • void exec() { ... } — 程式啟動時執行的方法。

基礎型別與變數

五種基礎型別:

型別 意義 範例
int 整數 42-7
float 單精度數字 3.14
double 雙精度數字 3.14159265358979
string 文字 "hello"
char 單一字元 'x'

宣告變數:

int age = 30;
string name = "BioLang";
double pi = 3.14159;
char grade = 'A';

const 是程式層級的唯讀常數:

const int SPEED = 9;

請求模型:resrefgetcause

每個呼叫都會回傳一個請求結果,可能是成功(res)或拒絕(ref)。這就是 BioLang 的核心。

ALL result = add(3, 4);     // a request result

使用 get 取出成功值,使用 cause 取得拒絕原因:

CIO::println(get add(3, 4));        // 7
CIO::println(cause div(1, 0));      // refused: division by zero
  • get x — 成功請求的值。
  • cause x — 請求被拒絕的原因。
  • ALL x = ... — 同時捕捉兩者;.res.cause 也可以使用。

運算子

算術運算子:+ - * / %

int a = 7 + 3;      // 10
int b = a * 2;      // 20
int c = b / 7;      // 2  (integer division)
int d = b % 7;      // 6  (remainder)

遞增與遞減:i++i--,以及複合形式。

控制流程

if / else if / else

if (score >= 90) {
    CIO::println("A");
} else if (score >= 60) {
    CIO::println("B");
} else {
    CIO::println("C");
}

while

int i = 0;
while (i < 5) {
    CIO::println("i =", i);
    i = i + 1;
}

for

for (int i = 0; i < 5; i = i + 1;) {
    CIO::println("square", i, "=", i * i);
}

請注意更新運算式後面的 ;——三個子句以分號分隔,整個標頭以 ; 結束。

breakcontinue

for (int i = 0; i < 10; i = i + 1;) {
    if (i == 3) continue;    // skip 3
    if (i == 7) break;       // stop at 7
    CIO::println(i);
}

主控台輸入與輸出

CIO::print("Enter your name: ");     // no newline
string name = CIO::getln();          // read a line of text
CIO::println("Hello,", name);        // prints: Hello, <name>

CIO 是主控台串流:printprintlngetgetlnreadIntreadNumber

綜合應用

一個小型猜數字遊戲(使用上述所有內容):

program main;

Main {
    void exec() {
        const int SECRET = 7;
        CIO::println("Guess the number (1-10)!");

        for (int tries = 0; tries < 3; tries = tries + 1;) {
            int guess = CIO::readInt();
            if (guess == SECRET) {
                CIO::println("Correct! You win.");
                res;                  // success, end the program
            } else if (guess < SECRET) {
                CIO::println("Too low.");
            } else {
                CIO::println("Too high.");
            }
        }
        CIO::println("Out of tries. The answer was", SECRET);
    }
}

下一步

  • 進階 — 串流、類別、檔案、專案、執行緒。
  • 高手 — 智慧引用、二進位函式庫、封裝、跨平台建置。
  • 儲存庫中的範例 — 15 個可執行、註解詳盡的程式。

Clone this wiki locally