-
Notifications
You must be signed in to change notification settings - Fork 0
Intermediate zhtw
English | 中文 | Русский | Español | Português | 繁體中文 | Deutsch
實際應用:串流與派生、類別與物件、
need假設系統、陣列、檔案 IO、執行緒,以及建置真實專案。
簽章串流宣告串流能做些什麼;**實作(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")動態存取屬性。 - 被沖走的屬性會拒絕存取:「屬性 X 已被沖走」。
need 宣告依賴;提供者會被自動收集:
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()。 - 執行緒是協同式的——沒有搶佔;執行緒必須
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