Skip to content

Intermediate zh

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

进阶——流、类、文件与项目

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

实际使用:流与派生、类与对象、need 假设系统、数组、文件 IO、线程,以及构建真实项目。

流:签名与实现

**签名流(signature stream)**声明一个流能做什么;**派生(fork)**实现它。调用经由签名发出,实现插入其中。

// Signature: what the stream offers
Stream Greeter {
    void greet(name string);
}

// Fork: how it works
Greeter FriendlyGreeter {
    void greet(name string) {
        CIO::println("hello,", name);
    }
}

调用:

Greeter::greet("TAK");
// hello, TAK

按签名流名称调用会回退到它的实现流。

带字段的流

Stream Counter {
    int count;
    void bump();
}
Counter C {
    void bump() { this::count = count + 1; }
}

this::count 引用流自身的字段;count 沿作用域链解析。在流内部可以直接使用裸调用(bump())。

类与对象

类是可以实例化的流:

Class Hero {
    int hp;
    void __init__() {
        this::hp = 100;
    }
    void takeDamage(d int) {
        this::hp = hp - d;
    }
}

Main {
    void exec() {
        Hero h = new Hero();          // __init__ runs
        h::takeDamage(30);
        CIO::println("hp =", h.hp);   // 70
    }
}
  • new ClassName() 创建对象;__init__ 是构造函数。
  • obj::method(...) 调用方法,obj.field 读取字段。
  • Obj::set("name", v) / Obj::get("name") 动态访问属性。
  • 被冲刷掉(washed away)的属性会拒绝访问,报错 "property X was washed away"(属性 X 已被冲刷掉)。

need 假设系统

need 声明一个依赖;提供者(providers)会被自动收集:

need value GREETING;          // a constant
need function greet;          // a method named greet
need Stream Greeter;          // a stream
need Class Hero;              // a class
// provider file (any included file):
const string GREETING = "Hello from utils/";

bio build 会从 main 入口递归打包所有可达的提供者——need 没有对应提供者是错误

数组

BioLang 的数组会自动增长:

int[] squares = new int[4];
for (int i = 0; i < 4; i = i + 1;) { squares[i] = i * i; }
CIO::println(squares);                    // [ 0 1 4 9 ]

ALL a = new Array(3);
a::set(0, 10);
a::push(40);                              // grows
CIO::println(a::join("-").res);           // join into a string

Array 类和 Vector 类都是用 BioLang 本身编写的;Arrays::count()Arrays::forget(v) 用于管理存活的实例。

多返回值

void triple(a int) { res a, a * 2, a * 3; }   // respond with several values

ALL t = triple(10);       // t.res is an array [10, 20, 30]
CIO::println(get t);

res a, b, c; 一次响应多个值;它们以原始数组的形式到达。

文件 IO

FIO 是文件流。打开文件,然后通过 IO 核心方法读写:

// Writing:
FIO::open("notes.txt", "w");
FIO::println("line one");
FIO::close();

// Reading (text):
FIO::open("notes.txt");
string line = FIO::getln();
CIO::println("read:", line);
FIO::close();

// Reading (bytes):
FIO::open("data.bin");
int byte = FIO::read();           // 0-255, -1 at EOF

文本方法:print / println / get / getln。字节方法:write / read。还有用于整文件操作的 FIO::writeFile(path, content)FIO::readFile(path)

SIO——字符串缓冲区

SIO 维护一个内存中的字符串,IO 方法对它进行读写——很适合构建文本:

SIO::println("Hello");
SIO::print("World");
CIO::println(SIO::content());     // Hello\nWorld
CIO::println(SIO::buf());         // whole buffer incl. consumed bytes
SIO::clear();

工具方法:formatlengthupperlowertrimcontainssubstringreplace

线程(协作式)

Calc Worker {
    void factorial(n int) {
        int r = 1;
        for (int i = 2; i <= n; i = i + 1;) r = r * i;
        res r;
    }
}

Main {
    void exec() {
        ALL t = Threads::spawn("factorial", 10);
        ALL result = Threads::join(t);
        CIO::println("10! =", get result);    // 3628800
    }
}
  • Threads::spawn(name, args...) 在新线程上启动一个裸方法。
  • Threads::join(t) 等待并取回其结果。
  • Threads::active()Threads::self()Threads::yield()
  • 线程是**协作式(cooperative)**的——没有抢占;一个线程必须 yield 或结束,其他线程才能运行。

Taskm——任务管理器

任务的轮转调度:

Taskm::interval(1);                  // rotate roughly every 1 ms
ALL t1 = Taskm::add("jobA", 5);
Taskm::run();                        // run until all tasks finish
CIO::println(get Threads::join(t1));

构建项目

一个项目 = package.toml + src/ + utils/ + .biolang/deps/

bio init myapp          # skeleton
# edit src/main.bio, add utils/, declare dependencies
bio build myapp -s      # standalone executable
bio run myapp           # or run interpreted

package.toml

name = "myapp"
version = "0.1.0"
[dependencies]
libfoo = { version = "1.0.0", repo = "https://github.com/user/libfoo" }

依赖解析顺序:依赖自身的 repoBIOLANG_CONFIG 文件 → ~/.biolang/config.toml

编译与打包

bio shell build hello.bio          # → bin/hello (standalone executable)
bio build myapp -s                 # project → standalone
bio build myapp -m                 # project → bin/myapp.img (app + platform CLI + libs)
bio build myapp -m dist.zip        # → .zip package
bio pack hello.img --entry hello bin/hello
bio run hello.img                  # run a package

.img / .zip 格式详见 Packaging

下一步

  • 高手——智能引用、二进制库、内存、跨平台。
  • 如果你跳过了基础部分,请回到入门

Clone this wiki locally