2015-01-01 21:04:14 -08:00
<!DOCTYPE html>
< html >
< head >
< meta http-equiv = "Content-type" content = "text/html;charset=UTF-8" / >
2015-11-09 08:01:19 -08:00
< title > Syntax – Wren< / title >
2015-01-01 21:04:14 -08:00
< link rel = "stylesheet" type = "text/css" href = "style.css" / >
2015-01-15 21:18:18 -08:00
< link href = '//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel = 'stylesheet' type = 'text/css' >
2015-01-01 21:04:14 -08:00
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
the viewport. -->
< meta name = "viewport" content = "width=device-width, initial-scale=1, maximum-scale=1" / >
< / head >
< body id = "top" >
< header >
< div class = "page" >
< div class = "main-column" >
2015-01-18 15:37:50 -08:00
< h1 > < a href = "./" > wren< / a > < / h1 >
2015-01-01 21:04:14 -08:00
< h2 > a classy little scripting language< / h2 >
< / div >
< / div >
< / header >
< div class = "page" >
2015-11-09 08:01:19 -08:00
< nav class = "big" >
2015-01-01 21:04:14 -08:00
< ul >
< li > < a href = "getting-started.html" > Getting Started< / a > < / li >
2015-11-09 08:01:19 -08:00
< li > < a href = "contributing.html" > Contributing< / a > < / li >
2015-01-01 21:04:14 -08:00
< / ul >
< section >
2015-11-09 08:01:19 -08:00
< h2 > language guide< / h2 >
2015-01-01 21:04:14 -08:00
< ul >
< li > < a href = "syntax.html" > Syntax< / a > < / li >
< li > < a href = "values.html" > Values< / a > < / li >
< li > < a href = "lists.html" > Lists< / a > < / li >
< li > < a href = "maps.html" > Maps< / a > < / li >
2015-11-09 08:01:19 -08:00
< li > < a href = "method-calls.html" > Method Calls< / a > < / li >
< li > < a href = "control-flow.html" > Control Flow< / a > < / li >
< li > < a href = "variables.html" > Variables< / a > < / li >
< li > < a href = "functions.html" > Functions< / a > < / li >
< li > < a href = "classes.html" > Classes< / a > < / li >
< li > < a href = "concurrency.html" > Concurrency< / a > < / li >
< li > < a href = "error-handling.html" > Error Handling< / a > < / li >
< li > < a href = "modularity.html" > Modularity< / a > < / li >
2015-01-01 21:04:14 -08:00
< / ul >
< / section >
< section >
< h2 > reference< / h2 >
< ul >
2015-11-09 08:01:19 -08:00
< li > < a href = "modules" > Modules< / a > < / li >
2017-10-19 07:05:45 -07:00
< li > < a href = "embedding" > Embedding< / a > < / li >
2015-01-01 21:04:14 -08:00
< li > < a href = "performance.html" > Performance< / a > < / li >
< li > < a href = "qa.html" > Q & A< / a > < / li >
< / ul >
< / section >
< / nav >
2015-11-09 08:01:19 -08:00
< nav class = "small" >
< table >
< tr >
< td > < a href = "getting-started.html" > Getting Started< / a > < / td >
< td > < a href = "contributing.html" > Contributing< / a > < / td >
< / tr >
< tr >
< td colspan = "2" > < h2 > language guide< / h2 > < / td >
< td > < h2 > reference< / h2 > < / td >
< / tr >
< tr >
< td >
< ul >
< li > < a href = "syntax.html" > Syntax< / a > < / li >
< li > < a href = "values.html" > Values< / a > < / li >
< li > < a href = "lists.html" > Lists< / a > < / li >
< li > < a href = "maps.html" > Maps< / a > < / li >
< li > < a href = "method-calls.html" > Method Calls< / a > < / li >
< li > < a href = "control-flow.html" > Control Flow< / a > < / li >
< / ul >
< / td >
< td >
< ul >
< li > < a href = "variables.html" > Variables< / a > < / li >
< li > < a href = "functions.html" > Functions< / a > < / li >
< li > < a href = "classes.html" > Classes< / a > < / li >
< li > < a href = "concurrency.html" > Concurrency< / a > < / li >
< li > < a href = "error-handling.html" > Error Handling< / a > < / li >
< li > < a href = "modularity.html" > Modularity< / a > < / li >
< / ul >
< / td >
< td >
< ul >
< li > < a href = "modules" > Modules< / a > < / li >
2017-10-19 07:05:45 -07:00
< li > < a href = "embedding" > Embedding< / a > < / li >
2015-11-09 08:01:19 -08:00
< li > < a href = "performance.html" > Performance< / a > < / li >
< li > < a href = "qa.html" > Q & A< / a > < / li >
< / ul >
< / td >
< / tr >
< / table >
< / nav >
2015-01-01 21:04:14 -08:00
< main >
< h1 > Syntax< / h1 >
2015-11-09 08:01:19 -08:00
< p > Wren’ s syntax is designed to be familiar to people coming from C-like languages
while being a bit simpler and more streamlined. < / p >
< p > Scripts are stored in plain text files with a < code > .wren< / code > 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 < a href = "performance.html" > efficiency< / a > , but that’ s an implementation detail.) < / p >
2015-01-01 21:04:14 -08:00
< h2 > Comments < a href = "#comments" name = "comments" class = "header-anchor" > #< / a > < / h2 >
< p > Line comments start with < code > //< / code > and end at the end of the line:< / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "c1" > // This is a comment.< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > Block comments start with < code > /*< / code > and end with < code > */< / code > . They can span multiple lines: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "cm" > /* This < / span >
2015-11-09 08:01:19 -08:00
< span class = "cm" > is < / span >
< span class = "cm" > a < / span >
< span class = "cm" > multi-line < / span >
< span class = "cm" > comment. */< / span >
< / pre > < / div >
< p > Unlike C, block comments can nest in Wren: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "cm" > /* This is /* a nested */ comment. */< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > This is handy because it lets you easily comment out an entire block of code,
even if the code already contains block comments. < / p >
2015-01-01 21:04:14 -08:00
< h2 > Reserved words < a href = "#reserved-words" name = "reserved-words" class = "header-anchor" > #< / a > < / h2 >
2015-11-09 08:01:19 -08:00
< p > One way to get a quick feel for a language’ s style is to see what words it
reserves. Here’ s what Wren has: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "k" > break< / span > < span class = "k" > class< / span > < span class = "k" > construct< / span > < span class = "k" > else< / span > < span class = "kc" > false< / span > < span class = "k" > for< / span > < span class = "k" > foreign< / span > < span class = "k" > if< / span > < span class = "k" > import< / span >
2015-09-22 08:02:21 -07:00
< span class = "k" > in< / span > < span class = "k" > is< / span > < span class = "kc" > null< / span > < span class = "k" > return< / span > < span class = "k" > static< / span > < span class = "k" > super< / span > < span class = "nb" > this< / span > < span class = "kc" > true< / span > < span class = "k" > var< / span > < span class = "k" > while< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-01-03 23:27:54 -08:00
< h2 > Identifiers < a href = "#identifiers" name = "identifiers" class = "header-anchor" > #< / a > < / h2 >
2015-11-09 08:01:19 -08:00
< p > 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. < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "n" > hi< / span >
2015-11-09 08:01:19 -08:00
< span class = "n" > camelCase< / span >
< span class = "vg" > PascalCase< / span >
< span class = "vi" > _under_score< / span >
< span class = "n" > abc123< / span >
2015-09-22 08:02:21 -07:00
< span class = "vg" > ALL_CAPS< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > Identifiers that start with underscore (< code > _< / code > ) are special in Wren. They are used
to indicate < a href = "classes.html#fields" > fields< / a > in classes. < / p >
2015-01-03 23:27:54 -08:00
< h2 > Newlines < a href = "#newlines" name = "newlines" class = "header-anchor" > #< / a > < / h2 >
2015-11-09 08:01:19 -08:00
< p > Newlines (< code > \n< / code > ) are meaningful in Wren. They are used to separate statements: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "c1" > // Two statements:< / span >
2015-09-22 08:02:21 -07:00
< span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " hi" < / span > < span class = "p" > )< / span > < span class = "c1" > // Newline.< / span >
< span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " bye" < / span > < span class = "p" > )< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > 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. < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "c1" > // Newline here is ignored.< / span >
2015-09-22 08:02:21 -07:00
< span class = "s" > " hi" < / span > < span class = "p" > )< / span >
2015-01-03 23:27:54 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > In practice, this means you can put each statement on its own line and wrap
them across lines as needed without too much trouble. < / p >
2015-01-01 21:04:14 -08:00
< h2 > Blocks < a href = "#blocks" name = "blocks" class = "header-anchor" > #< / a > < / h2 >
2015-11-09 08:01:19 -08:00
< p > Wren uses curly braces to define < em > blocks< / em > . You can use a block anywhere a
statement is allowed, like in < a href = "control-flow.html" > control flow< / a > statements.
< a href = "classes.html#methods" > Method< / a > and < a href = "functions.html" > function< / a > bodies are also
blocks. For example, here we have a block for the then case, and a single
statement for the else: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "k" > if< / span > < span class = "p" > (< / span > < span class = "n" > happy< / span > < span class = "o" > & & < / span > < span class = "n" > knowIt< / span > < span class = "p" > )< / span > < span class = "p" > {< / span >
2015-11-09 08:01:19 -08:00
< span class = "n" > hands< / span > < span class = "o" > .< / span > < span class = "n" > clap< / span > < span class = "p" > ()< / span >
2015-09-22 08:02:21 -07:00
< span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " sad" < / span > < span class = "p" > )< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > Blocks have two similar but not identical forms. Typically, blocks contain a
series of statements like: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "p" > {< / span >
2015-11-09 08:01:19 -08:00
< span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " one" < / span > < span class = "p" > )< / span >
< span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " two" < / span > < span class = "p" > )< / span >
< span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " three" < / span > < span class = "p" > )< / span >
2015-01-01 21:04:14 -08:00
< span class = "p" > }< / span >
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > Blocks of this form when used for method and function bodies automatically
return < code > null< / code > after the block has completed. If you want to return a different
value, you need an explicit < code > return< / code > statement. < / p >
2017-10-19 22:14:16 -07:00
< p > 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 < code > => < / code > to
define these. Wren uses: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "p" > {< / span > < span class = "s" > " single expression" < / span > < span class = "p" > }< / span >
2015-01-01 21:04:14 -08:00
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< p > If there is no newline after the < code > {< / code > (or after the parameter list in a
< a href = "functions.html" > function< / a > ), then the block may only contain a single
2017-10-19 22:14:16 -07:00
expression, and it automatically returns the result of it. It’ s exactly the same
as doing: < / p >
2017-10-19 07:05:45 -07:00
< div class = "codehilite" > < pre > < span > < / span > < span class = "p" > {< / span >
2015-11-09 08:01:19 -08:00
< span class = "k" > return< / span > < span class = "s" > " single expression" < / span >
2015-01-03 23:27:54 -08:00
< span class = "p" > }< / span >
< / pre > < / div >
2015-11-09 08:01:19 -08:00
2017-10-19 07:05:45 -07:00
< p > Statements are not allowed in this form (since they don’ t produce values), which
means nothing starting with < code > class< / code > , < code > for< / code > , < code > if< / code > , < code > import< / code > , < code > return< / code > ,
< code > var< / code > , or < code > while< / code > . If you want a block that contains a single statement,
put a newline in there: < / p >
< div class = "codehilite" > < pre > < span > < / span > < span class = "p" > {< / span >
< span class = "k" > if< / span > < span class = "p" > (< / span > < span class = "n" > happy< / span > < span class = "p" > )< / span > < span class = "p" > {< / span >
< span class = "vg" > System< / span > < span class = "o" > .< / span > < span class = "n" > print< / span > < span class = "p" > (< / span > < span class = "s" > " I' m feelin' it!" < / span > < span class = "p" > )< / span >
< span class = "p" > }< / span >
< span class = "p" > }< / span >
< / pre > < / div >
2017-10-19 22:14:16 -07:00
< p > Using an initial newline after the < code > {< / code > does feel a little weird or magical, but
newlines are already significant in Wren, so it’ s not totally crazy. The nice
thing about this syntax as opposed to something like < code > => < / code > is that the < em > end< / em > of
the block has an explicit delimiter. That helps when chaining: < / p >
< div class = "codehilite" > < pre > < span > < / span > < span class = "n" > numbers< / span > < span class = "o" > .< / span > < span class = "n" > map< / span > < span class = "p" > {< / span > < span class = "o" > |< / span > < span class = "err" > n< / span > < span class = "o" > |< / span > < span class = "err" > n< / span > < span class = "o" > *< / span > < span class = "mi" > 2< / span > < span class = "p" > }< / span > < span class = "o" > .< / span > < span class = "n" > where< / span > < span class = "p" > {< / span > < span class = "o" > |< / span > < span class = "err" > n< / span > < span class = "o" > |< / span > < span class = "err" > n< / span > < span class = "o" > < < / span > < span class = "mi" > 100< / span > < span class = "p" > }< / span >
< / pre > < / div >
2015-11-09 08:01:19 -08:00
< h2 > Precedence and Associativity < a href = "#precedence-and-associativity" name = "precedence-and-associativity" class = "header-anchor" > #< / a > < / h2 >
< p > 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. < / p >
< p > It shows which expressions have higher < em > precedence< / em > — which ones bind more
tightly than others— and their < em > associativity< / em > — how a series of the
same kind of expression is ordered. Wren mostly follows C, except that it fixes
< a href = "http://www.lysator.liu.se/c/dmr-on-or.html" > the bitwise operator mistake< / a > . The full precedence table, from
tightest to loosest, is: < / p >
< table class = "precedence" >
< tbody >
< tr >
< th > Prec< / th >
< th > Operator< / th >
< th > Description< / th >
< th > Associates< / th >
< / tr >
< tr >
< td > 1< / td >
< td > < code > ()< / code > < code > []< / code > < code > .< / code > < / td >
< td > Grouping, < a href = "method-calls.html" > Subscript, Method call< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 2< / td >
< td > < code > -< / code > < code > !< / code > < code > ~< / code > < / td >
< td > < a href = "method-calls.html#operators" > Negate, Not, Complement< / a > < / td >
< td > Right< / td >
< / tr >
< tr >
< td > 3< / td >
< td > < code > *< / code > < code > /< / code > < code > %< / code > < / td >
< td > < a href = "method-calls.html#operators" > Multiply, Divide, Modulo< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 4< / td >
< td > < code > +< / code > < code > -< / code > < / td >
< td > < a href = "method-calls.html#operators" > Add, Subtract< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 5< / td >
< td > < code > ..< / code > < code > ...< / code > < / td >
< td > < a href = "method-calls.html#operators" > Inclusive range, Exclusive range< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 6< / td >
< td > < code > < < < / code > < code > > > < / code > < / td >
< td > < a href = "method-calls.html#operators" > Left shift, Right shift< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 7< / td >
2016-02-27 11:10:16 -08:00
< td > < code > & < / code > < / td >
< td > < a href = "method-calls.html#operators" > Bitwise and< / a > < / td >
2015-11-09 08:01:19 -08:00
< td > Left< / td >
< / tr >
< tr >
< td > 8< / td >
2016-02-27 11:10:16 -08:00
< td > < code > ^< / code > < / td >
< td > < a href = "method-calls.html#operators" > Bitwise xor< / a > < / td >
2015-11-09 08:01:19 -08:00
< td > Left< / td >
< / tr >
< tr >
< td > 9< / td >
2016-02-27 11:10:16 -08:00
< td > < code > |< / code > < / td >
< td > < a href = "method-calls.html#operators" > Bitwise or< / a > < / td >
2015-11-09 08:01:19 -08:00
< td > Left< / td >
< / tr >
< tr >
< td > 10< / td >
2016-02-27 11:10:16 -08:00
< td > < code > < < / code > < code > < =< / code > < code > > < / code > < code > > =< / code > < / td >
< td > < a href = "method-calls.html#operators" > Comparison< / a > < / td >
2015-11-09 08:01:19 -08:00
< td > Left< / td >
< / tr >
< tr >
< td > 11< / td >
2016-02-27 11:10:16 -08:00
< td > < code > is< / code > < / td >
< td > < a href = "method-calls.html#operators" > Type test< / a > < / td >
2015-11-09 08:01:19 -08:00
< td > Left< / td >
< / tr >
< tr >
< td > 12< / td >
2016-02-27 11:10:16 -08:00
< td > < code > ==< / code > < code > !=< / code > < / td >
< td > < a href = "method-calls.html#operators" > Equals, Not equal< / a > < / td >
2015-11-09 08:01:19 -08:00
< td > Left< / td >
< / tr >
< tr >
< td > 13< / td >
< td > < code > & & < / code > < / td >
< td > < a href = "control-flow.html#logical-operators" > Logical and< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 14< / td >
< td > < code > ||< / code > < / td >
< td > < a href = "control-flow.html#logical-operators" > Logical or< / a > < / td >
< td > Left< / td >
< / tr >
< tr >
< td > 15< / td >
< td > < code > ?:< / code > < / td >
< td > < a href = "control-flow.html#the-conditional-operator-" > Conditional< / a > < / td >
< td > Right< / td >
< / tr >
< tr >
< td > 16< / td >
< td > < code > =< / code > < / td >
< td > < a href = "variables.html#assignment" > Assignment< / a > , < a href = "method-calls.html#setters" > Setter< / a > < / td >
< td > Right< / td >
< / tr >
< / tbody >
< / table >
< p > < a class = "right" href = "values.html" > Values → < / a >
< a href = "getting-started.html" > ← Getting Started< / a > < / p >
2015-01-01 21:04:14 -08:00
< / main >
< / div >
< footer >
< div class = "page" >
< div class = "main-column" >
2015-11-09 08:01:19 -08:00
< p > Wren lives
< a href = "https://github.com/munificent/wren" > on GitHub< / a >
— Made with ❤ by
< a href = "http://journal.stuffwithstuff.com/" > Bob Nystrom< / a > and
< a href = "https://github.com/munificent/wren/blob/master/AUTHORS" > friends< / a > .
< / p >
2015-01-01 21:04:14 -08:00
< div class = "main-column" >
< / div >
< / footer >
< / body >
2015-04-25 08:50:08 -07:00
< / html >