Wren’s syntax is designed to be familiar to people coming from C-like languages while being a bit simpler and more streamlined.
Scripts are stored in plain text files with a .wren
file extension. Wren does
not compile ahead of time: programs are run directly from source, from top to
bottom like a typical scripting language. (Internally, programs are compiled to
bytecode for efficiency, but that’s an implementation detail.)
Line comments start with //
and end at the end of the line:
// This is a comment.
Block comments start with /*
and end with */
. They can span multiple lines:
/* This is a multi-line comment. */
Unlike C, block comments can nest in Wren:
/* This is /* a nested */ comment. */
This is handy because it lets you easily comment out an entire block of code, even if the code already contains block comments.
One way to get a quick feel for a language’s style is to see what words it reserves. Here’s what Wren has:
as break class construct continue else false for foreign if import in is null return static super this true var while
Naming rules are similar to other programming languages. Identifiers start with a letter or underscore and may contain letters, digits, and underscores. Case is sensitive.
hi camelCase PascalCase _under_score abc123 ALL_CAPS
Identifiers that start with underscore (_
) are special in Wren. They are used
to indicate fields in classes.
Newlines (\n
) are meaningful in Wren. They are used to separate statements:
// Two statements: System.print("hi") // Newline. System.print("bye")
Sometimes, though, a statement doesn’t fit on a single line and jamming a newline in the middle would trip it up. To handle that, Wren has a very simple rule: It ignores a newline following any token that can’t end a statement.
System.print( // Newline here is ignored. "hi")
In practice, this means you can put each statement on its own line and wrap them across lines as needed without too much trouble.
Wren uses curly braces to define blocks. You can use a block anywhere a statement is allowed, like in control flow statements. Method and function bodies are also blocks. For example, here we have a block for the then case, and a single statement for the else:
if (happy && knowIt) { hands.clap() } else System.print("sad")
Blocks have two similar but not identical forms. Typically, blocks contain a series of statements like:
{ System.print("one") System.print("two") System.print("three") }
Blocks of this form when used for method and function bodies automatically
return null
after the block has completed. If you want to return a different
value, you need an explicit return
statement.
However, it’s pretty common to have a method or function that just evaluates and
returns the result of a single expression. Some other languages use =>
to
define these. Wren uses:
{ "single expression" }
If there is no newline after the {
(or after the parameter list in a
function), then the block may only contain a single
expression, and it automatically returns the result of it. It’s exactly the same
as doing:
{ return "single expression" }
Statements are not allowed in this form (since they don’t produce values), which
means nothing starting with class
, for
, if
, import
, return
,
var
, or while
. If you want a block that contains a single statement,
put a newline in there:
{ if (happy) { System.print("I'm feelin' it!") } }
Using an initial newline after the {
does feel a little weird or magical, but
newlines are already significant in Wren, so it’s not totally unreasonable. The nice
thing about this syntax as opposed to something like =>
is that the end of
the block has an explicit delimiter. That helps when chaining:
numbers.map {|n| n * 2 }.where {|n| n < 100 }
We’ll talk about Wren’s different expression forms and what they mean in the next few pages. But if you want to see how they interact with each other grammatically, here’s the whole table.
It shows which expressions have higher precedence—which ones bind more tightly than others—and their associativity—how a series of the same kind of expression is ordered. Wren mostly follows C, except that it fixes the bitwise operator mistake. The full precedence table, from tightest to loosest, is:
Prec | Operator | Description | Associates |
---|---|---|---|
1 | () [] . |
Grouping, Subscript, Method call | Left |
2 | - ! ~ |
Negate, Not, Complement | Right |
3 | * / % |
Multiply, Divide, Modulo | Left |
4 | + - |
Add, Subtract | Left |
5 | .. ... |
Inclusive range, Exclusive range | Left |
6 | << >> |
Left shift, Right shift | Left |
7 | & |
Bitwise and | Left |
8 | ^ |
Bitwise xor | Left |
9 | | |
Bitwise or | Left |
10 | < <= > >= |
Comparison | Left |
11 | is |
Type test | Left |
12 | == != |
Equals, Not equal | Left |
13 | && |
Logical and | Left |
14 | || |
Logical or | Left |
15 | ?: |
Conditional | Right |
16 | = |
Assignment, Setter | Right |