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:
log "Hello, world"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:
log "one"
log "two" "three"
log 6 * 7log 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:
// Work out how long a trip takes.
number distance = 150 // kilometres
number speed = 60 // kilometres an hour
log distance / speedThe 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:
osl run hello.oslosl run compiles the program and runs it straight away. When you want something to keep, compile it into an executable:
osl compile hello.osl
./helloosl 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:
log "Hello, worldThe 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.
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:
log "setting up"
def main() (
log "running"
)Most OSL programs don't bother. The top of the file is the entry point.
Three lines
Make the program print these three lines, in this order.
Hello I am learning OSL 6