Fill in some more basic syntax docs.

This commit is contained in:
Bob Nystrom
2013-12-04 21:51:23 -08:00
parent c91fcd12cc
commit 7355dd3dc7

View File

@ -2,6 +2,11 @@
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. 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.
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).
## Comments ## Comments
Line comments start with `//` and end at the end of the line: Line comments start with `//` and end at the end of the line:
@ -9,45 +14,55 @@ Line comments start with `//` and end at the end of the line:
:::wren :::wren
// This is a comment. // This is a comment.
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: 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:
:::wren :::wren
/* This is /* a nested */ comment. */ /* This is /* a nested */ comment. */
## Literals ## Reserved Words
Wren currently supports a few atomic types: booleans, numbers, strings, and the null type. Numbers are double-precision floating point values, and strings are text. Wren doesn't support a lot of fancy formats for them yet, just the basic: 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:
:::wren :::wren
0 class else false fn for if is null
1234 return static this true var while
-432.1
"a string"
"another string"
"supported escapes: \" \n \\"
Wren also has a couple of special values: ## Newlines
* `null` indicates the absence of a value. Like many scripting languages, newlines are significant in Wren and are used to
* `this` inside a method refers to the method's receiver. separate statements. You can keep your semicolons safely tucked away.
* `true` and `false` are boolean values.
## Variables
Variables are named references to values. Naming rules mostly follow C. They start with a letter and can contain letters, digits, and underscores (`_`). Some examples:
:::wren :::wren
foo // Two statements:
Bar io.write("hi")
bestFriends4eva io.write("bye")
under_score
Variables can be declared using `var`: Sometimes, though, you want to wrap a single statement on multiple lines. To
make that easier, Wren has a very simple rule. It will ignore a newline
following any token that can't end a statement. Specifically, that means any of
these:
:::wren :::wren
var pie = "pumpkin" ( [ { . , * / % + - | || & && ! ~ = < > <= >= == !=
class else if is static var while
**TODO: Scoping** Everywhere else, a newline is treated just like a `;`.
## Names
Identifiers are similar to other programming languages. They start with a letter or underscore and may contain letters, digits, and underscores. Case is sensitive.
:::wren
hi
camelCase
PascalCase
_under_score
abc123
ALL_CAPS
Identifiers that start with underscore (`_`) are special in Wren. They are used to indicate fields and private members of classes.
## Method calls ## Method calls