|
|
|
@@ -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)
|
|
|
|
|
|