Skip to content

Beginner zh

Enoch-199811 edited this page Aug 9, 2026 · 2 revisions

入门——BioLang 第一步

English | 中文 | Русский

本页讲解核心概念和控制流。你只需要一个构建好的 bio 二进制文件和一个文本编辑器。

BioLang 是什么?

BioLang 是一门面向流(stream-oriented)的语言。与"万物皆对象"不同,在这里万物皆流——一条由请求和响应组成的流。你执行的每一个操作都是一个请求(request),它可能被拒绝(refused)。这使得错误处理成为语言的一等公民,而不是事后的补充。

运行你的第一个程序

构建解释器:

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