Examples
Complete programs, not fragments. Each one lives in the examples folder of the repository, so you can run any of them straight after building Pharos, and the output below is what the test suite says they print.
pharos run examples/hello.pharos
hello.pharos
Where everybody starts.
-- Your first Pharos program.
-- Run it with: pharos run examples/hello.pharos
print "Hello, world"
Hello, world
variables.pharos
The three kinds of value, arithmetic, and joining a number onto a piece of text.
-- Pharos keeps three kinds of value, and you say which one you mean.
num score = 7
string name = "Ada"
bool finished = false
print name
print score
print finished
-- Numbers do arithmetic.
print score * 2
print score / 2
-- Text joins together with +, and a number will join onto text too.
print "Hello, " + name
print name + " scored " + score
-- A variable can change as often as you like.
score = score + 3
print "now " + score
-- What it cannot do is change into a different kind of value. Uncomment the
-- next line and Pharos will explain why it refuses.
-- score = "eleven"
Ada
7
false
14
3.5
Hello, Ada
Ada scored 7
now 10
conditions.pharos
Choosing between paths, and the fact that a question on its own is just a value you can print.
-- Asking questions about a value, and doing different things with the answer.
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
-- A question on its own is either true or false.
print temperature is greater than 10
print temperature is equal to 18
print temperature is not equal to 18
-- Questions join together with and, or and not.
bool raining = true
if temperature is greater than 10 and not raining then
print "a good day for a walk"
otherwise
print "better to stay in"
end if
mild
true
true
false
better to stay in
loops.pharos
Counting up, then counting down.
-- A loop repeats itself until its question becomes true.
num count = 1
loop until count is greater than 5
print count
count = count + 1
end loop
-- Counting down works the same way.
num remaining = 3
loop until remaining is equal to 0
print "T minus " + remaining
remaining = remaining - 1
end loop
print "lift off"
1
2
3
4
5
T minus 3
T minus 2
T minus 1
lift off
codeblocks.pharos
Naming a piece of program, handing it values, getting an answer back, and letting it call itself.
-- A codeblock is a piece of program you name once and run whenever you like.
codeblock greet with who
print "Hello, " + who
end codeblock
greet with "Ada"
greet with "Alan"
-- Use "give back" when a codeblock should answer with a value.
codeblock area with width, height
give back width * height
end codeblock
print "the area is " + area with 3, 4
-- A codeblock that needs nothing is run by writing its name on its own.
codeblock banner
print "----------"
end codeblock
banner
-- A codeblock is allowed to use itself.
codeblock countdown with n
if n is equal to 0 then
print "done"
give back 0
end if
print n
give back countdown with n - 1
end codeblock
countdown with 3
Hello, Ada
Hello, Alan
the area is 12
----------
3
2
1
done
print "the area is " + area with 3, 4 works here only
because the call is the last thing on the line. Had anything followed it,
that would have been swallowed as a third value; brackets are the fix.
report-card.pharos
Most of the language at once: a codeblock with a chain of conditions inside it, a loop building up a running total, and a variable declared fresh on every pass.
-- A longer program that uses most of the language at once.
codeblock grade with score
if score is greater than 89 then
give back "A"
otherwise if score is greater than 79 then
give back "B"
otherwise if score is greater than 69 then
give back "C"
otherwise
give back "F"
end if
end codeblock
num students = 4
num total = 0
num highest = 0
num i = 0
loop until i is equal to students
num score = 60 + i * 11
total = total + score
if score is greater than highest then
highest = score
end if
print "score " + score + " earns a " + grade with score
i = i + 1
end loop
print "highest: " + highest
print "average: " + total / students
score 60 earns a F
score 71 earns a C
score 82 earns a B
score 93 earns a A
highest: 93
average: 76.5
Things to try
The fastest way to learn a language is to break it on purpose. A few suggestions:
- Change
loops.pharosto count from 10 down to 1, then to count in twos. - Make
report-card.pharosprint the lowest score as well as the highest. - Add a
codeblocktoreport-card.pharosthat turns a grade letter into a sentence, and print that instead. - Delete an
end ifand read what Pharos says. Then delete athen. - Write a loop that never finishes, and watch Pharos stop it rather than hang.