2 A First Program
2.1 Hello
Make a file called hello.zeno with one line in it:
hello.zeno
print("Hello, Zeno!")$ zeno run hello.zeno
Hello, Zeno!
That is the whole program. There is no main function to
write, nothing to import and no semicolon at the end. Zeno starts at the
top of the file and does each line in turn, and when it runs out of
lines the program is over.
print shows what it is given and then ends the line.
Give it several things and it puts a space between each; give it nothing
and it prints an empty line:
print("one", "two", 3)
print()
print("after an empty line")one two 3
after an empty line
Text is written between double quotes, "like this".
Numbers are written as they are. Each has a chapter of its own
later.
2.2 Comments
A # starts a comment. Everything from it to the end of
the line is for the reader, and Zeno skips it:
# Greet the world. This whole line is a comment.
print("Hello") # and so is the end of this oneHello
2.3 One line, one statement
Each line is one statement: one thing for the program to do.
The end of the line ends the statement, so Zeno needs no
;.
A long statement can go on to the next line when its first line is
clearly not finished. That is so inside brackets, and after anything
that cannot end a statement, such as +, , or
=:
let total = 1 +
2 +
3
print(total,
"is the total")6 is the total
The other way round does not work. A line that could stand on its own
does stand on its own, so a line that starts with
+ is a mistake:
let total = 1
+ 2error: expected an expression, found `+`
--> main.zeno:2:5
|
2 | + 2
| ^
The line break already means "next statement" to the person reading,
so a ; on every line only says it again. The rule for going
on to the next line, "an unfinished line continues", can never be
misread, because a line that looks finished is finished.
2.4 Names for things
let gives a value a name, and from then on the name
stands for the value:
let name = "Ada"
let year = 1843
print(name, "wrote the first program in", year)
print(f"Hello, {name}!")Ada wrote the first program in 1843
Hello, Ada!
The last line is a formatted string. The f in
front of the quotes says that what stands inside { } is to
be worked out and put into the text. Values and Variables and Text say much more about both.
2.5 When something is wrong
Zeno reads the whole program before it runs any of it, and if it finds a mistake it says so and runs nothing. Here is a program with two:
let name = "Ada"
name = "Grace"
print(nmae)error: `name` cannot be changed. Declare it with `var` to allow that
--> main.zeno:2:1
|
2 | name = "Grace"
| ^
error: `nmae` is not defined
--> main.zeno:3:7
|
3 | print(nmae)
| ^
2 errors found. Nothing was run.
Each message says what is wrong, and where: the file, the line and
the column, the line itself, and a ^ under the place. Where
Zeno can tell what you meant, it says what to write instead. The first
mistake here is that a name given with let cannot be given
another value, and the message names the word that would allow it. The
second is a misspelt name.
Habits from other languages get messages of their own:
def greet():
print("hi")error: Zeno spells `def` as `function`
--> main.zeno:1:1
|
1 | def greet():
| ^
let ready = true
if ready && true {
print("go")
}error: Zeno spells this `and`
--> main.zeno:2:10
|
2 | if ready && true {
| ^
print("hello");error: Zeno has no semicolons. A line break ends a statement
--> main.zeno:1:15
|
1 | print("hello");
| ^
An error message is the language teaching the person who reads it, at the moment the lesson is most useful. So every message in Zeno aims to say what to write instead, not only what is wrong.
Notice that the last three programs each showed one message, while the one before showed two. A mistake in the shape of a program, such as a missing brace, leaves Zeno unsure where it is, and what it would report after that could be nonsense caused by the first. So it stops at the first. A mistake in the meaning, such as a misspelt name, leaves it sure where it is, so it reports every one it finds.
2.6 Checking without running
zeno run checks a program and then runs it.
zeno check only checks it:
greet.zeno
let name = "Ada"
print(f"Hello, {name}!")$ zeno check greet.zeno
No errors found.
It prints nothing when there is nothing wrong. That makes it useful for a program that would take a long time to run, or one that drives hardware you do not want to move yet.
A program is a file of statements, done from the top, one to a line.
print shows values, # starts a comment, and
let names a value. Zeno checks the whole program before it
runs a line of it, and a message says what is wrong, where, and usually
what to write instead.
Print your own name and the year you were born on one line, with one
print.Leave the closing quote off a string and read the message. Then leave off a closing bracket.
Write
elifwhere Zeno expectselse if, andx := 1for a variable. What does Zeno say to each?Put two mistakes of meaning in one program and run
zeno check. How many messages are there? Now add a mistake of shape, such asprint("a". How many now, and why?