Skip to content
FutureBlitz

    Install

    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.

    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:

    hello.fb
    Import std.core
    
    Print("Hello, world!")
    Output
    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.

    hello.fb
    Import std.core
    
    Local name = "Ada"
    Print("Hello, {name}!")
    Output
    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.

    hello.fb
    Import std.core
    
    Local name = "Ada"
    Print("Hello, {name}!")
    
    For i = 3 To 1 Step -1
        Print("{i}...")
    End For
    Print("Liftoff.")
    Output
    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.fb

    fb says what it’s building, and how long the build took:

       Compiling hello.fb
        Finished debug build in 0.69s

    Read 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:

    hello.fb
    Import std.core
    
    Local name = "Ada"
    Print("Hello, {nme}!")
    
    For i = 3 To 1 Step -1
        Print("{i}...")
    End For
    Print("Liftoff.")
    $ fb run hello.fb
       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 error

    Each 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:

    hello.fb
    Import std.core
    
    For i = 3 To 1 Step -1
        Print("{i}...")
    end For
    $ fb run hello.fb
       Compiling 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.core brings in Print and the rest of the standard library’s core.
    • Local declares a variable, and "{name}" puts a value in a string.
    • For ... End For counts, and a block ends with End and its keyword, as End For does.
    • fb run builds and runs a program, fb build builds it, and errors point at the line.

    Report an issue with this page or with FutureBlitz on GitHub.