Syntax¶
stone’s grammar is derived from Python’s, trimmed down and changed so that common code needs
fewer Shift presses. The full grammar is
docs/grammar/stone.gram.
Compared with Python¶
Python |
stone |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Keywords¶
These names are reserved and cannot be used for variables or functions:
keyword |
meaning |
|---|---|
|
defines a function |
|
returns from a function, or ends the program at the top level |
|
conditional blocks |
|
loops while a condition holds |
|
loops over a list or a |
|
leaves the innermost loop |
|
skips to the next iteration of the innermost loop |
|
boolean operators, with short-circuiting |
|
the |
|
the only value of type |
Lines and blocks¶
A statement ends at the end of its line. A statement that opens a block (def, if, elif,
else, while, for) ends in ;, followed either by an indented block on the next lines or
by a single simple statement on the same line:
for i in range(3);
print(i)
while true; break
Indent with spaces or tabs, as long as each block is consistent. Comments start with // outside
a string and run to the end of the line, and blank and comment-only lines may sit at any
indentation. Comment lines directly above a function or variable definition document it, and
editors show them when you hover over its name.
Literals¶
literal |
examples |
|---|---|
int |
|
float |
|
string |
|
bool and none |
|
list |
|
Negative numbers are the unary - applied to a literal, as in -7. Strings use double quotes,
must close on the line they open on, and have no escape sequences, so "a\tb" holds a backslash
and a t.
Statements¶
statement |
example |
|---|---|
expression |
|
assignment |
|
function definition |
|
|
|
|
|
|
|
|
|
|
inside a loop only |
A for loop’s variable must be a plain name. Functions can only be defined at the top level.
Operators¶
From lowest to highest precedence:
operators |
operands |
|---|---|
|
|
|
|
|
an |
|
numbers; |
|
two ints or two floats; |
|
two ints or two floats |
unary |
an int or a float |
|
an int or float base and an |
calls |
Comparisons chain, so a < b <= c means a < b and b <= c with b evaluated once.
** groups right to left and binds tighter than a unary sign on its left, as in Python, so
2 ** 3 ** 2 == 512 and -2 ** 2 == -4, while 2 ** -1 raises to -1.
Parentheses group as usual. Operands are evaluated left to right.
A method call such as xs.append(1) evaluates the value before the . first, then the
arguments. The builtin methods are len and append, documented in
Builtins. A . right after an int starts a method call, so 1. is not a
float; write 1.0.