Files
wren/doc/site/syntax.markdown
T

114 lines
3.3 KiB
Markdown
Raw Normal View History

2013-11-21 21:38:36 -08:00
^title Syntax
2014-04-14 21:23:46 -07:00
^category language
2013-11-21 21:38:36 -08:00
2015-01-03 23:27:02 -08:00
Wren's syntax is designed to be familiar to people coming from C-like languages
while being a bit simpler and more streamlined.
2013-11-21 21:38:36 -08:00
2013-12-04 21:51:23 -08:00
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
2015-01-03 10:02:45 -08:00
bytecode for efficiency, but that's an implementation detail.)
2013-12-04 21:51:23 -08:00
2013-11-21 21:38:36 -08:00
## Comments
Line comments start with `//` and end at the end of the line:
2014-01-20 21:44:51 -08:00
:::dart
2013-11-21 21:38:36 -08:00
// This is a comment.
2013-12-04 21:51:23 -08:00
Block comments start with `/*` and end with `*/`. They can span multiple lines
or be within a single one. Unlike C, block comments can nest in Wren:
2013-11-21 21:38:36 -08:00
2014-01-20 21:44:51 -08:00
:::dart
2013-11-21 21:38:36 -08:00
/* This is /* a nested */ comment. */
2014-04-07 21:03:16 -07:00
## Reserved words
2013-11-21 21:38:36 -08:00
2013-12-04 21:51:23 -08:00
Some people like to see all of the reserved words in a programming language in
one lump. If you're one of those folks, here you go:
2013-11-21 21:38:36 -08:00
2014-01-20 21:44:51 -08:00
:::dart
2015-01-03 23:27:02 -08:00
break class else false for if in is new null
return static super this true var while
2014-04-11 10:45:20 -07:00
2015-01-03 23:27:02 -08:00
## Identifiers
2014-04-11 10:45:20 -07:00
2015-01-03 23:27:02 -08:00
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.
2014-04-14 21:23:46 -07:00
:::dart
hi
camelCase
PascalCase
_under_score
abc123
ALL_CAPS
2015-01-03 23:27:02 -08:00
Identifiers that start with underscore (`_`) are special in Wren. They are used
to indicate [fields](classes.html#fields) in classes.
2014-04-14 21:23:46 -07:00
2015-01-03 10:02:45 -08:00
## Newlines
2013-11-21 21:38:36 -08:00
2015-01-03 10:02:45 -08:00
Newlines (`\n`) are meaningful in Wren. They are used to separate statements:
2013-11-21 21:38:36 -08:00
2014-01-20 21:44:51 -08:00
:::dart
2013-12-04 21:51:23 -08:00
// Two statements:
2015-01-03 10:02:45 -08:00
IO.print("hi") // Newline.
2014-04-06 08:38:00 -07:00
IO.print("bye")
2013-11-21 21:38:36 -08:00
2015-01-03 10:02:45 -08:00
Sometimes, though, a statement doesn't fit on a single line and jamming a
2015-01-03 23:27:02 -08:00
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.
2013-11-21 21:38:36 -08:00
2015-01-03 10:02:45 -08:00
:::dart
IO.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.
2013-12-04 21:51:23 -08:00
2014-04-08 21:18:17 -07:00
## Blocks
2015-01-03 23:27:02 -08:00
Wren uses curly braces to define *blocks*. You can use a block anywhere a
statement is allowed, like in [control flow](control-flow.html) statements.
[Method](classes.html#methods) and [function](functions.html) bodies are also
blocks. For example, here we have a block for the then case, and a single
statement for the else:
2014-04-08 21:18:17 -07:00
:::dart
if (happy && knowIt) {
hands.clap
} else IO.print("sad")
2015-01-03 23:27:02 -08:00
Blocks have two similar but not identical forms. Typically, blocks contain a
series of statements like:
2014-04-08 21:18:17 -07:00
2014-04-14 21:23:46 -07:00
:::dart
2014-04-08 21:18:17 -07:00
{
IO.print("one")
IO.print("two")
IO.print("three")
}
2015-01-03 23:27:02 -08:00
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. For that, Wren has a more
compact notation:
2014-04-08 21:18:17 -07:00
2014-04-14 21:23:46 -07:00
:::dart
2015-01-03 23:27:02 -08:00
{ "single expression" }
2014-04-08 21:18:17 -07:00
2015-01-03 23:27:02 -08:00
If there is no newline after the `{` (or after the parameter list in a of
[function](functions.html)), then the block may only contain a single
expression, and it automatically returns the result of it. It's exactly the
same as doing:
2014-04-08 21:18:17 -07:00
2015-01-03 23:27:02 -08:00
:::dart
{
return "single expression"
}