Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Writing your first Oxiby program

As is tradition, we’ll begin learning Oxiby with the “Hello, World!” program.

To create a new Oxiby project, run:

$ obc new hello_oxiby

This will create a new directory structured like this:

hello_oxiby
└── src
    └── main.ob

Change to the new directory:

$ cd hello_oxiby

main.ob is the file where we’ll write our program. Open it in your preferred text editor and replace its default contents with the following:

// File: examples/chapter_01_first/hello_world.ob

// Welcome to Oxiby!
fn main() {
    print_line("Hello, World!")
}

Recall from conventions that the first line of that source file is just a convention in this book to show you where to find the example code in the book’s own Git repository. You don’t need to copy it into your own file, though it’s fine if you do.

Use the Oxiby compiler to run the program and see its output:

$ obc run
Hello, World!

Remember this command because we’ll use it a lot.

This simple program teaches us a few things about Oxiby.

Comments

Two slashes (//) begin a comment. Any text afterwards on the same line will be ignored by the compiler and removed from the Ruby source code produced.

String literals

"Hello, World!"

Strings, sequences of textual characters, are created by writing the text inside a pair of double quotes.

“Literal” just means that there is dedicated syntax in the language to construct a string. There are other kinds of literals for different types in the language, such as integers (e.g. 123) and boolean values (i.e. true and false).

Functions

fn example() {
    expression
}

Functions are declared with the keyword fn, followed by a function name, a pair of parentheses, and a function body inside curly braces. The body is a sequence of zero or more expressions. An expression is anything that can be reduced to a single value.

In our program, main has a body consisting of one expression.

Calling functions

print_line("Hello, World!")

A function is called by writing its name followed by any function arguments in parentheses. An argument is input to the function that changes how it behaves.

Writing to stdout

The function print_line is used to write text to the standard output.

This is what causes “Hello, World!” to appear in the terminal when we run our program.

The main function

When the program runs, if a function named main is defined, it will be automatically executed.