next up previous contents index
Next: 3 The Slate World Up: 2 Language Reference Previous: 2.6 Macro Message-sends   Contents   Index

Subsections


2.7 Literal Syntax

2.7.1 Numbers

2.7.1.1 Integers

Integers are read in as an arbitrary-precision sequence of digits, without separators.

2.7.1.2 Floats

Floats are read in as an arbitrary-precision sequence of digits, with a period noting the decimal position.

2.7.1.3 Radix Prefixes

Integers or Floats may be entered with radix up to 36, using the digits 0 to 9 and then A to Z in order, by prefixing the literal with the radix (numeric base) and 'r'. So 3r100 evaluates to 9, and 2r0.1 evaluates to 0.5. Case is disregarded for the extra-decimal digits. The Float radix notation is always read in as a Float, and not an infinite-precision Fraction.

2.7.2 Characters

Slate's default support for character literals uses the $ symbol as a prefix. For example, $a, $3, $>, and $$ are all Character object literals for a, 3, >, and $, respectively. Printable and non-printable characters require backslash escapes as shown and listed in Table cap:Character-Literal-Escapes.


Table 1: Character Literal Escapes
Character name Literal
Escape $\e
Newline $\n
Carriage Return $\r
Tab $\t
Backspace $\b
Null $\0
Bell $\a
Form Feed $\f
Vertical Feed $\v
Space $\s
Backslash $\\


2.7.3 Strings

Strings are comprised of any sequence of characters surrounded by single-quote characters. Strings can include the commenting character (double-quotes) without an escape. Embedded single-quotes can be provided by using the backslash character to escape them (\'). Slate's character literal syntax also embeds into string literals, omitting the $ prefix. All characters that require escapes in character literal syntax also require escapes when used within string literals, with the exception of double-quote marks and the addition of single-quote marks.

The following are all illustrative examples of Strings in Slate:

'a string comprises any sequence of characters, surrounded by single quotes'

'strings can include the "comment delimiting" character'

'and strings can include embedded single quote characters by escaping\' them'

'strings can contain embedded

newline characters'

'and escaped \ncharacters'

'' "and don't forget the empty string"

2.7.4 Symbols

Symbol literal syntax starts with the pound sign character (#) and consists of all following characters up to the next non-escaped whitespace or reserved character (whichever comes first), unless the pound sign is followed exactly by a string literal (in single quotes), in which case the string's contents become the identifier for the Symbol. So, for example, the following are all valid Symbols and Symbol literals:

#$

#20

#+

#key:word:expression:

#something_with_underscores

#'A full string with a \nnewline in it.'

#'@'

A property of Symbols and their literals is that any literal with the same value as another also refers to the same instance as any other Symbol literal with that value in a Slate system. This allows fast hashes and comparisons by identity rather than value hashes. In particular, as with Slate identifiers, a Symbol's value is case-sensitive, so #a and #A are distinct.

Internally, Slate currently keeps one global table for Symbols, and uses individual context objects to hold local bindings.5

2.7.5 Arrays

Arrays can be literally and recursively specified by curly-brace notation using stops as separators. Array indices in Slate are 0-based. So:

{4. 5. {foo. bar}}.
returns an array with 4 in position 0, 5 at 1, and an array with objects foo and bar inserted into it at position 2.

Immediate array syntax - #{4. 5. {foo. bar}} - is provided as an alternative to create the array when the method is compiled, instead of creating a new array on each method invocation. The syntax is identical except that the first opening brace is preceded by the pound sign. The disadvantage is that no run-time values will be usable.

A special ``literal array'' syntax is also provided, in the manner of Smalltalk-80, in which all tokens within are treated symbolically, evaluating to an array of literals as read (but not evaluated) by Slate. Naturally, these are all evaluated when the surrounding context is compiled. For example:

Slate> #(1 2 3). 
{1. 2. 3} 
Slate> #(3 + 4). 
{3. #'+'. 4} 
Slate> #(quux: a :bar). 
{#quux:. #a. #:bar} 
Slate> #(1 . _ 2e3). 
{1. #'.'. #_. 2000.0}

2.7.6 Blocks

Block syntax basics were covered in 2.1.1; the precise, full specification includes more features and outlines some necessary logical rules. Primarily, blocks are square-bracket-delimited statement sequences with an optional header that specifies input and local slots (input slots being arguments).

Slot names must be valid unary message selectors (see 2.2.1). Inputs are distinguished by a prefix colon character (:), and must occur in the same positional order that the invocation will use or expect, although they can be interspersed among other slot declarations at will.

Optional keyword arguments are specified with an ampersand prefix character (&), and may occur in any order.

For example,

[| x :y &z :w | ]
evaluates to a block which takes inputs y and w in that order, has locals x (and z), and takes an optional parameter to specify z's value when called.

A single ``rest'' parameter which becomes an array containing all extra positional (non-keyword) arguments passed may be specified once in the header in any position relative to the other parameters, prefixed with an asterisk (e.g. *rest).

Blocks may be used to perform arbitrary compile-time calculations, using the #-prefix as used for literal arrays and strings. So #[3 + 4] will result in 7 in the resulting code for the surrounding context (perhaps a method or top-level expression), as though the block were never there.


next up previous contents index
Next: 3 The Slate World Up: 2 Language Reference Previous: 2.6 Macro Message-sends   Contents   Index
Brian Rice 2005-11-21