-
Notifications
You must be signed in to change notification settings - Fork 0
Beginner zh
Enoch-199811 edited this page Aug 9, 2026
·
2 revisions
English | 中文 | Русский | Español | Português | 繁體中文 | Deutsch
本页讲解核心概念和控制流。你只需要一个构建好的
bio二进制文件和一个文本编辑器。
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;
每一次调用都会返回一个请求结果:要么是成功(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 (score >= 90) {
CIO::println("A");
} else if (score >= 60) {
CIO::println("B");
} else {
CIO::println("C");
}
int i = 0;
while (i < 5) {
CIO::println("i =", i);
i = i + 1;
}
for (int i = 0; i < 5; i = i + 1;) {
CIO::println("square", i, "=", i * i);
}
注意更新表达式后面的 ;——三个子句之间用分号分隔,整个头部以 ; 结尾。
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 是控制台流:print、println、get、getln、readInt、readNumber。
一个小型猜数字游戏(用到了上面所有内容):
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);
}
}
- Home
- Beginner — first steps
- Intermediate — real usage
- Advanced — masterclass
- Build & Run
- Packaging
- Language-Reference
- BR-Model
- BTM-Model