-
Notifications
You must be signed in to change notification settings - Fork 0
Intermediate zh
English | 中文 | Русский | Español | Português | 繁體中文 | Deutsch
实际使用:流与派生、类与对象、
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 声明一个依赖;提供者(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; 一次响应多个值;它们以原始数组的形式到达。
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 维护一个内存中的字符串,IO 方法对它进行读写——很适合构建文本:
SIO::println("Hello");
SIO::print("World");
CIO::println(SIO::content()); // Hello\nWorld
CIO::println(SIO::buf()); // whole buffer incl. consumed bytes
SIO::clear();
工具方法:format、length、upper、lower、trim、contains、substring、replace。
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::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 interpretedpackage.toml:
name = "myapp"
version = "0.1.0"
[dependencies]
libfoo = { version = "1.0.0", repo = "https://github.com/user/libfoo" }依赖解析顺序:依赖自身的 repo → BIOLANG_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。
- Home
- Beginner — first steps
- Intermediate — real usage
- Advanced — masterclass
- Build & Run
- Packaging
- Language-Reference
- BR-Model
- BTM-Model