-
Notifications
You must be signed in to change notification settings - Fork 0
Intermediate pt
English | 中文 | Русский | Español | Português | 繁體中文 | Deutsch
Uso no mundo real: fluxos e implementações, classes e objetos, o sistema de suposições
need, arrays, E/S de arquivos, threads e a compilação de projetos reais.
Um fluxo de assinatura declara o que um fluxo pode fazer; uma implementação o concretiza. As chamadas passam pela assinatura; as implementações se conectam.
// 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);
}
}
Chamando:
Greeter::greet("TAK");
// hello, TAK
Uma chamada pelo nome do fluxo de assinatura recai no seu fluxo de implementação.
Stream Counter {
int count;
void bump();
}
Counter C {
void bump() { this::count = count + 1; }
}
this::count se refere ao campo do próprio fluxo; count é resolvido pela
cadeia de escopos. Chamadas simples (bump()) funcionam dentro do fluxo.
Uma classe é um fluxo que pode ser instanciado:
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()cria um objeto;__init__é o construtor. -
obj::method(...)chama métodos;obj.fieldlê campos. -
Obj::set("name", v)/Obj::get("name")acessam atributos dinamicamente. - Um atributo removido recusa o acesso: "a propriedade X foi removida".
need declara uma dependência; os provedores são coletados automaticamente:
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 empacota recursivamente todo provedor alcançável a partir da
entrada main — um need sem provedor é um erro.
Os arrays do BioLang crescem automaticamente:
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
As classes Array e Vector são escritas no próprio BioLang;
Arrays::count(), Arrays::forget(v) gerenciam as instâncias vivas.
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; responde com vários valores de uma vez; eles chegam como um
array bruto.
FIO é o fluxo de arquivos. Abra um arquivo e então leia/escreva pelos
métodos centrais de E/S:
// 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
Métodos de texto: print / println / get / getln. Métodos de bytes:
write / read. Há também FIO::writeFile(path, content) e
FIO::readFile(path) para operações com o arquivo inteiro.
SIO mantém uma string em memória sobre a qual os métodos de E/S leem e
escrevem — útil para construir texto:
SIO::println("Hello");
SIO::print("World");
CIO::println(SIO::content()); // Hello\nWorld
CIO::println(SIO::buf()); // whole buffer incl. consumed bytes
SIO::clear();
Ferramentas: 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...)inicia um método avulso em uma nova thread. -
Threads::join(t)espera e busca o resultado. -
Threads::active(),Threads::self(),Threads::yield(). - As threads são cooperativas — sem preempção; uma thread precisa dar
yieldou terminar para que as outras executem.
Escalonamento round-robin de tarefas:
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));
Um projeto = 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" }Resolução de dependências: o repo da própria dependência → arquivo
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 packageVeja Packaging para os formatos .img / .zip.
- Home
- Beginner — first steps
- Intermediate — real usage
- Advanced — masterclass
- Build & Run
- Packaging
- Language-Reference
- BR-Model
- BTM-Model