Pharos

The guide

This is the whole language, in the order it makes sense to learn it. Read it start to finish and nothing later will depend on something you have not met yet. Every program here is short enough to type out, and typing them out is the point.

On this page

  1. Running a program
  2. Printing
  3. Values and variables
  4. Arithmetic and joining text
  5. Asking questions
  6. Choosing what to do
  7. Repeating
  8. Codeblocks
  9. Giving back an answer
  10. Notes to yourself
  11. When something is wrong

1. Running a program

A Pharos program is a plain text file whose name ends in .pharos. Put this in a file called hello.pharos:

print "Hello, world"
Hello, world

and run it:

pharos run hello.pharos

There is no setup line, no wrapper, and nothing to declare first. The file is the program, and it runs from the top down.

While you are experimenting it is often easier to use the prompt, which keeps everything you have typed so far and shows you the value of anything you write:

$ pharos repl
pharos> num x = 20
pharos> x + 1
21
pharos> print "hi " + x
hi 20

2. Printing

print puts something on the screen and then moves to the next line. If print feels like the wrong word, out and output mean exactly the same thing; pick whichever you remember.

print "first"
out "second"
output "third"
first
second
third

3. Values and variables

Pharos works with three kinds of value:

KindHoldsFor example
numa number, whole or not7, -2, 3.5
stringa piece of text"Ada", 'hello'
boolyes or notrue, false

To make a variable you say which kind it is, give it a name, and give it a starting value. All three parts are required.

num score = 7
string name = "Ada"
bool finished = false

print name
print score
print finished
Ada
7
false

After that, changing a variable needs only its name. You do not repeat the kind.

num score = 7
score = score + 3
print score
10

What a variable cannot do is change into a different kind of value. Saying num score is a promise that score will always be a number, and Pharos holds you to it.

num score = 7
score = "seven"
line 2: "score" was declared as num, so it cannot hold a string

This feels strict at first and saves a great deal of confusion later. A variable that could quietly turn from a number into text is the source of a whole family of bugs that are very hard to see.

4. Arithmetic and joining text

Numbers use the four symbols you already know: +, -, * and /. Multiplication and division happen before addition and subtraction, and brackets override that.

print 2 + 3 * 4
print (2 + 3) * 4
print 7 / 2
14
20
3.5

+ also joins text together. When one side is text and the other is a number or a yes/no value, the number is written out as part of the text, which is what makes printing a sentence about a value easy.

string name = "Ada"
num score = 7

print "Hello, " + name
print name + " scored " + score
Hello, Ada
Ada scored 7

5. Asking questions

A question about two values is written out in words, and its answer is always true or false.

QuestionTrue when
a is greater than ba is the larger number
a is less than ba is the smaller number
a is equal to bthe two are the same
a is not equal to bthe two differ
num age = 12

print age is greater than 10
print age is equal to 12
print age is not equal to 12
true
true
false

is greater than and is less than compare numbers. is equal to works on any two values of the same kind, so text and yes/no values can be compared too.

Questions combine with and, or and not. and is true when both sides are; or when at least one is; not flips an answer over.

num temperature = 18
bool raining = true

print temperature is greater than 10 and not raining
print temperature is greater than 10 or raining
false
true

6. Choosing what to do

if runs part of a program only when a question is true. The question is followed by then, the part to run goes on the lines below, and end if marks where it stops.

num temperature = 30

if temperature is greater than 25 then
  print "warm"
end if
warm

otherwise covers the case where the question was false, and otherwise if asks a further question. You can chain as many otherwise if parts as you need. Pharos tries them from the top and takes the first one that is true.

num temperature = 18

if temperature is greater than 25 then
  print "warm"
otherwise if temperature is greater than 10 then
  print "mild"
otherwise
  print "cold"
end if
mild

The indentation is there to help you read it. Pharos does not mind how much you use, but being consistent will save you from misreading your own program.

7. Repeating

loop until repeats the lines inside it, checking a question each time round, and stops as soon as that question becomes true. Read it as the sentence it is: keep going until this is true.

num count = 1

loop until count is greater than 5
  print count
  count = count + 1
end loop
1
2
3
4
5

That count = count + 1 line is the important one. Something inside the loop has to move the program towards the question being true, or it never will be.

If you do forget, nothing terrible happens. Pharos notices a loop going round far too many times, stops the program, and tells you which loop was the problem. You will not have to force-quit anything.

Because the question is checked before the first pass, a loop whose question is already true simply does not run.

num count = 99

loop until count is greater than 5
  print "this never happens"
end loop

print "finished"
finished

8. Codeblocks

A codeblock is a piece of program with a name. You write it once and run it whenever you like, which keeps you from copying the same lines over and over.

codeblock banner
  print "----------"
end codeblock

banner
print "Results"
banner
----------
Results
----------

A codeblock that needs nothing is run by writing its name on a line of its own. To hand it something to work with, name the values it expects after with, separated by commas.

codeblock greet with who
  print "Hello, " + who
end codeblock

greet with "Ada"
greet with "Alan"
Hello, Ada
Hello, Alan

Inside the codeblock, who is an ordinary variable holding whatever was handed over. Notice that it needs no num or string in front of it: a codeblock takes whatever kind of value you give it.

A codeblock can see the variables around it, so it can also change something defined outside itself.

num total = 0

codeblock add with amount
  total = total + amount
end codeblock

add with 5
add with 7
print total
12

9. Giving back an answer

Codeblocks so far have done something. A codeblock can instead work something out and hand the answer back with give back, which lets you use it anywhere a value belongs.

codeblock area with width, height
  give back width * height
end codeblock

print area with 3, 4
12

One thing to watch: everything after with is taken as the values being handed over, all the way to the end of the line. So print "a wall of " + area with 10, 8 + " tiles" does not do what it looks like, because 8 + " tiles" gets read as the second value.

Brackets settle it. Put them around the whole call whenever you use one in the middle of something larger:

codeblock area with width, height
  give back width * height
end codeblock

print "a wall of " + (area with 10, 8) + " tiles"
a wall of 80 tiles

give back also stops the codeblock there and then, which is useful for handling a simple case first and leaving the rest below.

codeblock describe with n
  if n is equal to 0 then
    give back "nothing"
  end if

  if n is less than 0 then
    give back "below zero"
  end if

  give back "above zero"
end codeblock

print describe with 0
print describe with -3
print describe with 8
nothing
below zero
above zero

A codeblock is even allowed to use itself, which is how you work through something a step at a time.

codeblock countdown with n
  if n is equal to 0 then
    give back "lift off"
  end if

  print n
  give back countdown with n - 1
end codeblock

print countdown with 3
3
2
1
lift off

10. Notes to yourself

Two dashes start a note. Pharos ignores everything after them to the end of the line, so you can explain what a program is for, or park a line you are not using yet.

-- Works out how much paint we need.
num walls = 4          -- one per side
print walls * 12
-- print "not this one"
48

11. When something is wrong

Pharos tries to say what went wrong in the same words you wrote the program in, and always tells you the line. There are two moments it can happen.

Before anything runs, Pharos reads the whole program. If a sentence is unfinished it says so, and it reports every such problem at once rather than making you fix them one at a time.

num count = 1

loop until count is greater than 3
  print count
  count = count + 1
line 3: this "loop" is missing its "end loop"

Then, as the program runs, it stops at the first thing that does not make sense and explains it.

num total = 10
num people = 0
print total / people
line 3: cannot divide by zero

A name you have not introduced yet is one of the most common slips, and usually means a typo:

num score = 10
print scor
line 2: I have not heard of "scor" yet

That is all of it

There is no chapter twelve. What you have read is the entire language: three kinds of value, four questions, three ways to combine them, if, loop until, and codeblock. The examples put the pieces together into complete programs, and the reference is there for when you only need to check a spelling.

← Home Examples →