Getting started, 2 of 4
Your first program
Write a program in one file, run it, and read what fb says when the code is wrong.
What you’ll build
A program in one file that greets someone by name and counts down. On the way, you’ll see how fb runs a file, what a program’s first line is for, and how fb reports a mistake.
Print a line
Make a file named hello.fb with these two lines, and run it from the folder it’s in with
fb run hello.fb. fb compiles the file into a native executable and runs it, and the program
prints its line:
Import std.core
Print("Hello, world!")Hello, world!
A program is the code at the top level of its file, run from the first line to the last. There’s
no Main function to write. Import std.core makes the standard library’s core available, and
Print is one of its functions: it writes the values it’s given, then a line break. Every part of the
standard library is imported by name, std.core included, so a file shows at its top what it uses.
Keep a value in a variable
Local declares a variable. Its type comes from the value it starts with, so name is a
String. A string shows any value inside braces, which is called interpolation.
Import std.core
Local name = "Ada"
Print("Hello, {name}!")Hello, Ada!
Count down
A For loop counts from one number to another. Step -1 counts down, and the loop ends with
End For. A block ends with End and the word that started it, as End For ends a For.
Import std.core
Local name = "Ada"
Print("Hello, {name}!")
For i = 3 To 1 Step -1
Print("{i}...")
End For
Print("Liftoff.")Hello, Ada! 3... 2... 1... Liftoff.
Build an executable
fb run builds the program and runs it at once. fb build only builds it, and leaves the
executable beside the source, as hello.exe on Windows and hello on macOS and Linux, to run or
share on its own. fb build --release builds it optimized, which is how a program is shipped.
fb build hello.fbfb says what it’s building, and how long the build took:
Compiling hello.fb
Finished debug build in 0.69sRead an error
fb checks the whole program before it builds anything. Change {name} to {nme} and run the
program again. It doesn’t build, and fb says where the problem is:
Import std.core
Local name = "Ada"
Print("Hello, {nme}!")
For i = 3 To 1 Step -1
Print("{i}...")
End For
Print("Liftoff.") Compiling hello.fb
hello.fb:4:16: error: 'nme' isn't declared
|
4 | Print("Hello, {nme}!")
| ^^^
error: couldn't build hello.fb because of 1 errorEach error names the file, the line, and the column, and points at the code. Keywords are
case-sensitive and written in PascalCase, so end For is an error too, and fb’s note gives the
keyword’s spelling:
Import std.core
For i = 3 To 1 Step -1
Print("{i}...")
end ForCompiling hello.fb hello.fb:5:1: error: calls need parentheses, like end(...) | 5 | end For | ^^^ = note: 'end' is a name here; keywords are written as they are, as 'End' hello.fb:5:1: error: 'end' isn't declared | 5 | end For | ^^^ hello.fb:6:1: error: expected 'End For', found end of file | 6 | | ^ hello.fb:3:1: note: opened here | 3 | For i = 3 To 1 Step -1 | --- error: couldn't build hello.fb because of 3 errors
What you learned
- A program is the top-level code of its file, run from top to bottom.
Import std.corebrings inPrintand the rest of the standard library’s core.Localdeclares a variable, and"{name}"puts a value in a string.For ... End Forcounts, and a block ends withEndand its keyword, asEnd Fordoes.fb runbuilds and runs a program,fb buildbuilds it, and errors point at the line.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.