feat: translate doc to en, add index files

This commit is contained in:
samsonium
2026-10-19 21:34:12 +03:00
parent 195b82408b
commit c43b5dfef0
25 changed files with 849 additions and 18 deletions
+34
View File
@@ -0,0 +1,34 @@
# Introduction
Welcome to this short book on the basics of FZLang, and thank you for giving this programming language a try.
First, let's talk about what FZLang is and how this book is organized.
## What is FZLang?
FZLang is a programming language with its own toolchain, which includes a compiler and a virtual machine. The compiler checks and compiles source code into a portable file, and the virtual machine runs programs written in FZLang.
This project originally began as a way to learn about building compilers. I (Samsonium) wanted to explore a few questions:
- Why is there a meaningful difference between a compiler and an interpreter?
- How does a program called a compiler understand what I want it to do?
- How does execution of a compiled program work? And how does the process of translating from human-readable code to machine instructions happen?
I've been programming for more than ten years, but had never taken the time to find out!
So this project became a place to experiment and learn more about the foundations of the tools programmers use every day. Along the way, the language began to take shape and develop a philosophy of its own.
Today, I would describe that philosophy as: “Be as convenient as a tool, while always trying to remain predictable.” I imagine every language community wants something similar for its language, but this principle has helped me make choices at important points in the project's development.
### How FZLang handles memory
The question first came up when I introduced stacks to the virtual machine: “A garbage collector, or something else?” Learning how garbage collection works would certainly have been interesting. However, having chosen Java as my first language, I was familiar with its “weight” and runtime behavior. Of course, its garbage collector deserves some of the credit for that.
So instead of a GC, I decided to explore RAII. This led to a simple model for how objects move through a program: when an object is passed as an argument, it is “borrowed” into a nested environment; when returned, it is moved into the parent environment. This approach avoids a GC and, most importantly, gives objects a deterministic lifetime—and therefore more predictable memory use, as far as possible.
In short, FZLang uses scope-based RAII.
---
Next: [Getting started](./01_getting-started.md)
+44
View File
@@ -0,0 +1,44 @@
# Getting started
To get started with FZLang, you need two executable files on your computer:
- `fzc` — the compiler
- `fz` — the virtual machine
To compile a program, run `fzc` and pass it the paths to your source files. If compilation succeeds, run the compiled program with `fz`, passing the path to the compiled file. If you do not specify an output path for the compiler, it saves the compiled program as `a.fzb`.
## Compiler options
To see the available compiler options, run `fzc --help` (or `fzc -h`). The help currently looks like this:
```
Usage: fzc [flags] <sources...>
Available flags:
-h --help Print help
-v --verbose Display compilation info (tokens, AST, consts table, etc)
-V --version Print compiler version
-o --out Set compiled file name
```
The `-h` (`--help`) flag prints the same help text. The `-v` / `--verbose` flags print detailed compilation information, including:
- The tokens that were read;
- The syntax tree that was built;
- The values stored in the constants table;
- The opcodes written to the program.
The `-V` / `--version` flags print the compiler version. The `-o` / `--out` flags let you choose the output file path.
## Virtual machine options
The virtual machine has two options: `--help` (`-h`) and `-d`.
- `--help` (or `-h`) prints the help text;
- `-d <path to file>` prints the opcodes with their indices. Use this flag to see how the VM will read your program.
---
Next: [Basics and types](./02_basics-and-types.md)
Previous: [Introduction](./00_introduction.md)
+161
View File
@@ -0,0 +1,161 @@
# Basics and types
In our first program, we asked FZLang to print some text. That's a good start: a program isn't mysterious; it's a set of instructions that the computer carries out one after another. Now let's see how those instructions work, how to store data, and how to ask a program to make a decision.
Don't worry if some words—such as “type” or “expression”—sound unfamiliar for now. We'll unpack them with small examples. Try changing the values as you read: seeing what changes is one of the best ways to understand what a program does.
## A first program: instructions and comments
The built-in `printline` function writes the value you pass to it to standard output:
```fz
printline("Hello, FZLang!");
```
Here we call `printline` and pass it text inside parentheses. The quotation marks show where the text begins and ends. The semicolon marks the end of the instruction. Try replacing the greeting with your own—the program will print the text you gave it.
Code doesn't have to contain only instructions. You can add notes for yourself and other readers, called comments. A single-line comment starts with `//`; the program ignores everything from those characters to the end of the line:
```fz
// First, calculate a result, then print it.
printline(2 + 2);
```
A comment doesn't affect how the program runs. It helps explain why the code is there, especially when you come back to it later.
## Variables: giving values names
Imagine that a program needs to use the same value more than once. You could write it out every time, but it's more convenient to save it under a name. That's what variables are: named places to keep values.
To declare a variable, write its type, its name, and, if you like, an initial value:
```fz
int count = 10;
str greeting = "Hello there!";
bool ready = true;
```
These lines create three variables: `count` stores a number, `greeting` stores text, and `ready` stores a yes-or-no value. A type helps the language understand what kind of value we're working with. For example, numbers can be added, while text is useful for messages.
You can also declare a variable first and assign a value to it later:
```fz
u64 total;
total = 15;
printline(total);
```
Once a variable has been declared, you can use its name to read its value or assign it a new one. For example, `count = count + 1;` means: take the current value of `count`, add one, and store the result back in `count`. If it was 10, it will be 11 afterward.
### Numbers: integers and their sizes
FZLang has signed and unsigned integer types. Signed numbers can be negative; unsigned types are for non-negative values. For example, a temperature below zero fits a signed type, while a visitor count can use an unsigned type:
```fz
int temperature = -5;
uint visitors = 42;
```
For more precise choices, there are fixed-width types: `i8`, `i16`, `i32`, `i64` and their unsigned counterparts `u8`, `u16`, `u32`, `u64`. The number after `i` or `u` tells you the type's width in bits. `int` and `uint` are the size of a machine word.
```fz
i16 offset = -120;
u8 level = 7;
```
For now, it's enough to remember that `int` works well for ordinary integers. You can choose an exact width when it matters. Numeric types are compatible with one another under the language's rules, but you still state a variable's type when declaring it.
### Boolean values and text
The `bool` type has just two values: `true` and `false`. It's useful for storing the answer to a question—for example, whether a door is open or a program is ready to continue.
```fz
bool is_open = true;
printline(is_open);
```
Text uses the `str` type, short for “string”:
```fz
str language = "FZLang";
printline(language);
```
The type reference also lists `char`, but it isn't currently used by the language. We'll leave classes and object values for later sections; for now, numbers, text, and boolean values are plenty to get comfortable with.
## Expressions: asking the program to calculate
An expression is a piece of code that can be evaluated to produce a value. The simple expression `2 + 2` is one example. Expressions can be stored in variables or passed to functions:
```fz
int result = 2 + 2 * 2;
printline(result);
```
The program evaluates the expression, stores the result in `result`, and then prints it. Just as in ordinary arithmetic, multiplication happens before addition, so the result is 6. To change the order, use parentheses: `(2 + 2) * 2`.
Numbers support addition `+`, subtraction `-`, multiplication `*`, division `/`, remainder `%`, and exponentiation `^`. A unary minus lets you write a negative number, such as `-5`.
Comparisons let us ask questions about values. `==` and `!=` check whether values are equal or unequal; `<`, `<=`, `>` and `>=` compare numbers. A comparison produces a boolean value, either `true` or `false`:
```fz
bool enough = result >= 4 && result != 0;
printline(enough);
```
Here `enough` is true only if `result` is at least 4 and is not zero. The `&&` operator means “and,” `||` means “or,” and `!` flips a boolean value. The [Operators](../reference/operators.md) reference lists all operations and their limitations. Note that comparing objects with `==` or `!=` checks whether the variables refer to the same object, not whether the objects contain the same values.
## Blocks and conditions: choosing what happens next
Sometimes a program needs to do different things in different situations. The `if` condition lets it choose a path. It checks a boolean expression and runs the matching branch:
```fz
bool is_ready = true;
if (is_ready)
{
printline("We can continue");
}
else
{
printline("Let's wait");
}
```
If `is_ready` is `true`, the program prints the first message; otherwise, it prints the second. You can leave out `else` when there's nothing to do in the other case. If there are more than two possibilities, add another condition with `else if`.
Curly braces `{}` group instructions into a block. You've already seen one in the condition example; functions use blocks too. Blocks make it clear which instructions belong together. An `if` condition must have type `bool`, such as a comparison result or a boolean variable.
## Functions: putting useful actions together
When you want to use the same sequence of actions in different places, you can put it in a function. A function receives values through parameters, runs instructions, and can return a result.
```fz
fn sum(int left, int right) int
{
return left + right;
}
int result = sum(2, 3);
printline(result);
```
A declaration starts with `fn`, followed by the function name, its parameters, and its result type. Here, `sum` takes two `int` values, adds them, and returns an `int`. The `return` keyword sends the result back to the code that called the function. That's why `sum(2, 3)` can be used as an expression—for example, to initialize a variable.
## What to remember
You've now met some of the basic building blocks of a program: instructions, comments, variables, types, expressions, conditions, and functions. There's no need to memorize every operator right away; you can return to the reference as you need it.
- A variable has a name and a type, and stores a value.
- For your first programs, you'll use numbers (`int`, `uint`, and fixed-width types), boolean values (`bool`), and strings (`str`).
- Expressions calculate new values, and comparisons help a program make decisions.
- `if` conditions and `{}` blocks control which instructions the program runs.
- Functions group actions and can return a result.
- Instructions usually end with a semicolon, and `printline` helps you see what your program did.
---
Next: [Type system](../reference/types_system.md)
Previous: [Getting started](./01_getting-started.md)
+8
View File
@@ -0,0 +1,8 @@
# The FZLang book
A gentle introduction to FZLang, from getting set up to writing programs with variables, types, and functions.
- [Introduction](./00_introduction.md)
- [Getting started](./01_getting-started.md)
- [Basics and types](./02_basics-and-types.md)