Learn
Lesson 1

Your first program

Write a program, run it, and turn it into an executable. Meet log, comments and the compiler.

An OSL program is a file of statements that run from the top to the bottom. There is no main function to write, no class to wrap it in and no build file. This is a complete program:

main.osl
log "Hello, world"
Click the code to edit it

Press Run above to see it. Every example in this course works like that: Run shows what the program prints, and clicking into the code lets you change it. The compiler runs in your browser and checks what you type as you type it.

Printing with log#

log prints its value and a newline. Give it more than one value, separated by spaces, and it prints them on one line:

main.osl
log "one"
log "two" "three"
log 6 * 7
Click the code to edit it

log is a command, so a space separates it from what it prints. You'll meet two more commands like it: warn and error print the same way but to the error stream, for messages that aren't the program's normal output.

Statements and comments#

Each line is one statement, and there are no semicolons. Comments start with // and run to the end of the line:

main.osl
// Work out how long a trip takes.
number distance = 150   // kilometres
number speed = 60       // kilometres an hour
log distance / speed
Click the code to edit it

The lines that start with a type, like number distance = 150, declare variables. They are the subject of the next lesson.

Run it on your machine#

Save a program as hello.osl and run it:

Terminal
osl run hello.osl

osl run compiles the program and runs it straight away. When you want something to keep, compile it into an executable:

Terminal
osl compile hello.osl
./hello

osl compile writes a native program named after the file. It doesn't need OSL or Go to run, so you can copy it to another machine with the same operating system and run it there. Use -o to choose its name: osl compile hello.osl -o bin/greeter.

When the compiler says no#

The compiler reads the whole program before anything runs, and stops if something is wrong. Press Compile on this one:

main.osl
log "Hello, world
Click the code to edit it

The message says what is wrong, shows the line, and points at the place. OSL's errors try to tell you how to fix the problem as well; you'll see many of them in this course, and they're worth reading closely.

Quick check

You run a file that contains only log 1 + 1. What happens?

A main function, if you want one#

Some people like a program's entry point to have a name. Define a function called main and OSL calls it after the file's top-level code has run:

main.osl
log "setting up"

def main() (
  log "running"
)
Click the code to edit it

Most OSL programs don't bother. The top of the file is the entry point.

Your turn

Three lines

Make the program print these three lines, in this order.

main.osl
Ctrl Enter runs
Expected output
Hello
I am learning OSL
6
0 of 24 lessons done