Files
wren/doc/site/syntax.markdown

81 lines
2.7 KiB
Markdown
Raw Normal View History

2013-11-21 21:38:36 -08:00
^title Syntax
Wren's syntax is designed to be familiar to people coming from C-like languages while being as simple and expressive as possible within that framework.
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
bytecode for efficiency, but that's an implementation detail).
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. */
2013-12-04 21:51:23 -08: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
2013-12-24 21:04:11 -08:00
break class else false fn for if in is
null return static this true var while
2013-11-21 21:38:36 -08:00
2013-12-17 09:39:26 -08:00
## Statement terminators
2013-11-21 21:38:36 -08:00
2013-12-17 09:39:26 -08:00
Officially, statements are terminated by a semicolon (`;`) like in other
languages in the C tradition. However, Wren treats newlines as equivalent
to a semicolon whenever it makes sense. In practice, this means you almost
never write `;` unless you want to cram a bunch of statements on one line.
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:
IO.write("hi")
IO.write("bye")
2013-11-21 21:38:36 -08:00
2013-12-17 09:39:26 -08:00
Sometimes, though, a statement doesn't fit on a single line and treating the
newline as a semicolon would trip things up. To handle that, Wren has a very
simple rule. It ignores a newline following any token that can't end a
statement. Specifically, that means any of these:
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
( [ { . , * / % + - | || & && ! ~ = < > <= >= == !=
class else if is static var while
2013-11-21 21:38:36 -08:00
2013-12-17 09:39:26 -08:00
Everywhere else, a newline is treated just like a `;`. Note that this is a very
different system from how JavaScript handles semicolons. If you've been burned
there, don't worry, you should be fine here.
2013-11-21 21:38:36 -08:00
2013-12-04 21:51:23 -08:00
## Names
2013-11-21 21:38:36 -08:00
2013-12-04 21:51:23 -08:00
Identifiers are similar to other programming languages. They start with a letter or underscore and may contain letters, digits, and underscores. Case is sensitive.
2014-01-20 21:44:51 -08:00
:::dart
2013-12-04 21:51:23 -08:00
hi
camelCase
PascalCase
_under_score
abc123
ALL_CAPS
2013-12-17 09:39:26 -08:00
Identifiers that start with underscore (`_`) are special in Wren. They are used to indicate fields in [classes](classes.html).
2013-11-21 21:38:36 -08:00
2013-12-21 09:15:30 -08:00
**TODO: Move this somewhere else:*
### The `is` operator
The `is` keyword can be used as an infix operator in expression. It performs a
type test. The left operand is an object and the right operand is a class. It
evaluates to `true` if the object is an instance of the class (or one of its
subclasses).
2014-01-20 21:44:51 -08:00
**TODO: blocks, assignment, maps**