Reference
The whole language on one page, for when you know what you want and only need to check how it is spelled. If you are meeting Pharos for the first time, the guide is the better place to start.
How a program is laid out
| Rule | Detail |
|---|---|
| One statement per line | There are no semicolons. A line break ends a statement. |
| Blank lines are free | Use them wherever they help. |
| Indentation is for you | Pharos does not measure it. Being consistent still pays. |
Notes start with -- | Everything to the end of that line is ignored. |
| Keywords ignore case | If and if are the same word. Variable names do not ignore case, so score and Score are two different variables. |
Files end in .pharos | By convention, not by rule. |
Kinds of value
| Keyword | Holds | Written as |
|---|---|---|
num | A number, whole or fractional, positive or negative. | 0, 42, -7, 3.5 |
string | A piece of text. | "Ada", 'Ada' |
bool | A yes-or-no value. | true, false |
Text can use single or double quotes, as long as the pair matches. Inside
text, \n is a line break, \t is a tab, and
\" is a quote that does not end the text.
Variables
| Form | Meaning |
|---|---|
num x = 5 | Introduces x and fixes it as a number forever. |
x = 6 | Changes a variable that already exists. |
A starting value is required; there is no way to declare a variable and leave it empty. Declaring the same name twice in the same place is an error, as is assigning a value of the wrong kind, or assigning to a name that was never declared.
Variables declared inside an if, a loop or a
codeblock belong to that block and are gone when it ends.
Assigning to a name from an enclosing block changes that outer variable
rather than making a new one.
Printing
| Form | Meaning |
|---|---|
print x | Writes x followed by a line break. |
out x | The same thing. |
output x | The same thing again. |
Text is printed without its quotes, and whole numbers without a decimal point.
Arithmetic
| Operator | Does | Notes |
|---|---|---|
+ | Adds numbers, or joins text. | If either side is text, the result is text. |
- | Subtracts. | Numbers only. |
* | Multiplies. | Numbers only. |
/ | Divides. | Numbers only. Dividing by zero is an error. |
-x | Makes a number negative. | Numbers only. |
Division gives an exact answer rather than rounding, so 7 / 2
is 3.5. There is no remainder operator yet.
Questions
| Question | True when | Works on |
|---|---|---|
a is greater than b | a is larger | numbers |
a is less than b | a is smaller | numbers |
a is equal to b | both are the same | any two values of the same kind |
a is not equal to b | they differ | any two values of the same kind |
Comparing two different kinds of value is an error rather than simply false, because it is nearly always a mistake.
Combining questions
| Operator | True when | Notes |
|---|---|---|
a and b | both are true | If a is false, b is never looked at. |
a or b | at least one is true | If a is true, b is never looked at. |
not a | a is false |
These work only on true and false. A number is
not treated as a stand-in for either.
Choosing
if question then
...
otherwise if question then
...
otherwise
...
end if
| Part | Required | Notes |
|---|---|---|
if ... then | yes | The question must be true or false. |
otherwise if ... then | no | As many as you like. Tried from the top down. |
otherwise | no | At most one, and it goes last. |
end if | yes |
Repeating
loop until question
...
end loop
The question is checked before each pass, so a loop whose question starts out true never runs at all. A loop that runs a million times without its question becoming true is stopped and reported, on the assumption that something inside it forgot to change.
Codeblocks
codeblock name with first, second
...
give back value
end codeblock
| Form | Meaning |
|---|---|
codeblock name | Starts a codeblock that takes nothing. |
codeblock name with a, b | Starts one that expects two values. |
give back x | Stops the codeblock and answers with x. |
end codeblock | Ends the definition. |
name | Runs a codeblock that takes nothing. |
name with 1, 2 | Runs one, handing over two values. |
The values a codeblock expects have no declared kind, so they accept whatever is handed over. Handing over the wrong number of values is an error. A codeblock can see the variables around it and may change them, and it may call itself. Calling itself ten thousand deep without finishing is stopped and reported.
Everything after with is read as the values being handed
over, to the end of the line. When a call sits inside a larger
expression, put brackets around it:
print "total: " + (add with 1, 2).
What binds tightest
Loosest at the top, tightest at the bottom:
| Level | Operators |
|---|---|
| 1 | or |
| 2 | and |
| 3 | is equal to, is not equal to |
| 4 | is greater than, is less than |
| 5 | +, - |
| 6 | *, / |
| 7 | not, negative - |
| 8 | with |
Brackets override all of it.
Running Pharos
| Command | Does |
|---|---|
pharos run file.pharos | Runs a program. |
pharos file.pharos | The same, with less typing. |
pharos repl | Opens a prompt that keeps what you have typed. |
pharos version | Prints the version. |
pharos help | Prints the commands. |
A program that fails leaves a non-zero exit status, so Pharos behaves properly inside scripts and other tools.
Words Pharos keeps for itself
These cannot be used as variable names:
num, string, bool,
true, false,
print, out, output,
if, then, otherwise,
loop, until,
codeblock, with,
and, or, not.
end, is, give and
back are only special as part of a longer phrase
(end if, is equal to, give back),
so on their own they are ordinary names.