mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 06:38:45 +01:00
379 lines
13 KiB
HTML
379 lines
13 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
|
|
<title>Syntax – Wren</title>
|
|
<script type="application/javascript" src="prism.js" data-manual></script>
|
|
<script type="application/javascript" src="codejar.js"></script>
|
|
<script type="application/javascript" src="wren.js"></script>
|
|
<link rel="stylesheet" type="text/css" href="prism.css" />
|
|
<link rel="stylesheet" type="text/css" href="style.css" />
|
|
<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'>
|
|
<!-- 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">
|
|
<h1><a href="./">wren</a></h1>
|
|
<h2>a classy little scripting language</h2>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<div class="page">
|
|
<nav class="big">
|
|
<a href="./"><img src="./wren.svg" class="logo"></a>
|
|
<ul>
|
|
<li><a href="getting-started.html">Getting Started</a></li>
|
|
<li><a href="contributing.html">Contributing</a></li>
|
|
<li><a href="blog">Blog</a></li>
|
|
<li><a href="try">Try it!</a></li>
|
|
</ul>
|
|
<section>
|
|
<h2>guides</h2>
|
|
<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>
|
|
<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>
|
|
</section>
|
|
<section>
|
|
<h2>API docs</h2>
|
|
<ul>
|
|
<li><a href="modules">Modules</a></li>
|
|
</ul>
|
|
</section>
|
|
<section>
|
|
<h2>reference</h2>
|
|
<ul>
|
|
<li><a href="cli">Wren CLI</a></li>
|
|
<li><a href="embedding">Embedding</a></li>
|
|
<li><a href="performance.html">Performance</a></li>
|
|
<li><a href="qa.html">Q & A</a></li>
|
|
</ul>
|
|
</section>
|
|
</nav>
|
|
<nav class="small">
|
|
<table>
|
|
<tr>
|
|
<div><a href="getting-started.html">Getting Started</a></div>
|
|
<div><a href="contributing.html">Contributing</a></div>
|
|
<div><a href="blog">Blog</a></div>
|
|
<div><a href="try">Try it!</a></div>
|
|
</tr>
|
|
<tr>
|
|
<td colspan="2"><h2>guides</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">API/Modules</a></li>
|
|
<li><a href="embedding">Embedding</a></li>
|
|
<li><a href="performance.html">Performance</a></li>
|
|
<li><a href="qa.html">Q & A</a></li>
|
|
</ul>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</nav>
|
|
<main>
|
|
<h2>Syntax</h2>
|
|
<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>
|
|
<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>
|
|
<pre class="snippet">
|
|
// This is a comment.
|
|
</pre>
|
|
|
|
<p>Block comments start with <code>/*</code> and end with <code>*/</code>. They can span multiple lines:</p>
|
|
<pre class="snippet">
|
|
/* This
|
|
is
|
|
a
|
|
multi-line
|
|
comment. */
|
|
</pre>
|
|
|
|
<p>Unlike C, block comments can nest in Wren:</p>
|
|
<pre class="snippet">
|
|
/* This is /* a nested */ comment. */
|
|
</pre>
|
|
|
|
<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>
|
|
<h2>Reserved words <a href="#reserved-words" name="reserved-words" class="header-anchor">#</a></h2>
|
|
<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>
|
|
<pre class="snippet">
|
|
break class construct else false for foreign if import
|
|
in is null return static super this true var while
|
|
</pre>
|
|
|
|
<h2>Identifiers <a href="#identifiers" name="identifiers" class="header-anchor">#</a></h2>
|
|
<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>
|
|
<pre class="snippet">
|
|
hi
|
|
camelCase
|
|
PascalCase
|
|
_under_score
|
|
abc123
|
|
ALL_CAPS
|
|
</pre>
|
|
|
|
<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>
|
|
<h2>Newlines <a href="#newlines" name="newlines" class="header-anchor">#</a></h2>
|
|
<p>Newlines (<code>\n</code>) are meaningful in Wren. They are used to separate statements:</p>
|
|
<pre class="snippet">
|
|
// Two statements:
|
|
System.print("hi") // Newline.
|
|
System.print("bye")
|
|
</pre>
|
|
|
|
<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>
|
|
<pre class="snippet">
|
|
System.print( // Newline here is ignored.
|
|
"hi")
|
|
</pre>
|
|
|
|
<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>
|
|
<h2>Blocks <a href="#blocks" name="blocks" class="header-anchor">#</a></h2>
|
|
<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>
|
|
<pre class="snippet">
|
|
if (happy && knowIt) {
|
|
hands.clap()
|
|
} else System.print("sad")
|
|
</pre>
|
|
|
|
<p>Blocks have two similar but not identical forms. Typically, blocks contain a
|
|
series of statements like:</p>
|
|
<pre class="snippet">
|
|
{
|
|
System.print("one")
|
|
System.print("two")
|
|
System.print("three")
|
|
}
|
|
</pre>
|
|
|
|
<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>
|
|
<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>
|
|
<pre class="snippet">
|
|
{ "single expression" }
|
|
</pre>
|
|
|
|
<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
|
|
expression, and it automatically returns the result of it. It’s exactly the same
|
|
as doing:</p>
|
|
<pre class="snippet">
|
|
{
|
|
return "single expression"
|
|
}
|
|
</pre>
|
|
|
|
<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>
|
|
<pre class="snippet">
|
|
{
|
|
if (happy) {
|
|
System.print("I'm feelin' it!")
|
|
}
|
|
}
|
|
</pre>
|
|
|
|
<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>
|
|
<pre class="snippet">
|
|
numbers.map {|n| n * 2 }.where {|n| n < 100 }
|
|
</pre>
|
|
|
|
<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>
|
|
<td><code>&</code></td>
|
|
<td><a href="method-calls.html#operators">Bitwise and</a></td>
|
|
<td>Left</td>
|
|
</tr>
|
|
<tr>
|
|
<td>8</td>
|
|
<td><code>^</code></td>
|
|
<td><a href="method-calls.html#operators">Bitwise xor</a></td>
|
|
<td>Left</td>
|
|
</tr>
|
|
<tr>
|
|
<td>9</td>
|
|
<td><code>|</code></td>
|
|
<td><a href="method-calls.html#operators">Bitwise or</a></td>
|
|
<td>Left</td>
|
|
</tr>
|
|
<tr>
|
|
<td>10</td>
|
|
<td><code><</code> <code><=</code> <code>></code> <code>>=</code></td>
|
|
<td><a href="method-calls.html#operators">Comparison</a></td>
|
|
<td>Left</td>
|
|
</tr>
|
|
<tr>
|
|
<td>11</td>
|
|
<td><code>is</code></td>
|
|
<td><a href="method-calls.html#operators">Type test</a></td>
|
|
<td>Left</td>
|
|
</tr>
|
|
<tr>
|
|
<td>12</td>
|
|
<td><code>==</code> <code>!=</code></td>
|
|
<td><a href="method-calls.html#operators">Equals, Not equal</a></td>
|
|
<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><br><hr>
|
|
<a class="right" href="values.html">Values →</a>
|
|
<a href="getting-started.html">← Getting Started</a></p>
|
|
</main>
|
|
</div>
|
|
<footer>
|
|
<div class="page">
|
|
<div class="main-column">
|
|
<p>Wren lives
|
|
<a href="https://github.com/wren-lang/wren">on GitHub</a>
|
|
— Made with ❤ by
|
|
<a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a> and
|
|
<a href="https://github.com/wren-lang/wren/blob/main/AUTHORS">friends</a>.
|
|
</p>
|
|
<div class="main-column">
|
|
</div>
|
|
</footer>
|
|
</body>
|
|
</html>
|