3.1: Syntactic Elements
- Page ID
- 36342
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Expressions are composed of the following building blocks: (i) six reserved keywords, or pseudo-variables: self
, super
, nil
, true
, false
, and thisContext
, (ii) constant expressions for literal objects including numbers, characters, strings, symbols and arrays, (iii) variable declarations, (iv) assignments, (v) block closures, and (vi) messages.
We can see examples of the various syntactic elements in Table \(\PageIndex{1}\).
Syntax | What it represents |
---|---|
startPoint | a variable name |
Transcript | a global variable name |
self | pseudo-variable |
1 | decimal integer |
2r101 | binary integer |
1.5 | floating point number |
2.4e7 | exponential notation |
$a | the character ‘a’ |
'Hello' | the string “Hello” |
#Hello | the symbol #Hello |
#(1 2 3) | a literal array |
{1. 2. 1+2} | a dynamic array |
"a comment" | a comment |
| x y | | declaration of variables x and y |
x := 1 | assign 1 to x |
[ x + y ] | a block that evaluates to x+y |
<primitive: 1> | virtual machine primitive or annotation |
3 factorial | unary message |
3+4 | binary messages |
2 raisedTo: 6 modulo: 10 | keyword message |
\(\uparrow\) true | return the value true |
Transcript show: ’hello’. Transcript cr | expression separator (.) |
Transcript show: ’hello’; cr | message cascade (;) |
Local variables. startPoint
is a variable name, or identifier. By convention, identifiers are composed of words in “camelCase” (i.e., each word except the first starting with an upper case letter). The first letter of an instance variable, method or block argument, or temporary variable must be lower case. This indicates to the reader that the variable has a private scope.
Shared variables. Identifiers that start with upper case letters are global variables, class variables, pool dictionaries or class names. Transcript
is a global variable, an instance of the class TranscriptStream
.
The receiver. self
is a keyword that refers to the object inside which the current method is executing. We call it “the receiver” because this object will normally have received the message that caused the method to execute. self
is called a “pseudo-variable” since we cannot assign to it.
Integers. In addition to ordinary decimal integers like 42, Squeak also provides a radix notation. 2r101
is 101
in radix 2 (i.e., binary), which is equal to decimal 5.
Floating point numbers can be specified with their base-ten exponent: 2.4e7
is \( 2.4 \times 10^{7} \).
Characters. A dollar sign introduces a literal character: $a
is the literal for ‘a’. Instances of non-printing characters can be obtained by sending appropriately named messages to the Character
class, such as Character
space and Character
tab.
Strings. Single quotes are used to define a literal string. If you want a string with a quote inside, just double the quote, as in 'G''day'.
Symbols Are like Strings, in that they contain a sequence of characters. However, unlike a string, a literal symbol is guaranteed to be globally unique. There is only one Symbol object #Hello but there may be multiple String objects with the value 'Hello'.
Compile-time arrays are defined by #( )
, surrounding space-separated literals. Everything within the parentheses must be a compile-time constant. For example, #(27 #(true false) abc)
is a literal array of three elements: the integer 27, the compile-time array containing the two booleans, and the symbol #abc
.
Run-time arrays. Curly braces { }
define a (dynamic) array at run-time. Elements are expressions separated by periods. So { 1. 2. 1+2 }
defines an array with elements 1, 2, and the result of evaluating 1+2. (The curly-brace notation is peculiar to the Squeak dialect of Smalltalk! In other Smalltalks you must build up dynamic arrays explicitly.)
Comments are enclosed in double quotes. "hello" is a comment, not a string, and is ignored by the Squeak compiler. Comments may span multiple lines.
Local variable definitions. Vertical bars | |
enclose the declaration of one or more local variables in a method (and also in a block).
Assignment. :=
assigns an object to a variable. Sometimes you will see \(\leftarrow\) used instead. Unfortunately, since this is not an ASCII character, it will appear as an underscore unless you are using a special font. So, x := 1
is the same as x ← 1
or x _ 1
. You should use :=
since the other representations have been deprecated since Squeak 3.9.
Blocks. Square brackets []
define a block, also known as a block closure or a lexical closure, which is a first-class object representing a function. As we shall see, blocks may take arguments and can have local variables.
Primitives. <primitive: ...>
denotes an invocation of a virtual machine primitive. (<primitive: 1>
is the VM primitive for SmallInteger\(\gg\)+.) Any code following the primitive is executed only if the primitive fails. Since Squeak 3.9, the same syntax is also used for method annotations.
Unary messages consist of a single word (like factorial
) sent to a receiver (like 3).
Binary messages are operators (like+) sent to a receiver and taking a single argument. In 3+4
, the receiver is 3 and the argument is 4.
Keyword messages consist of multiple keywords (like raisedTo:modulo:
), each ending with a colon and taking a single argument. In the expression 2 raisedTo: 6 modulo: 10
, the message selector raisedTo:modulo:
takes the two arguments 6 and 10, one following each colon. We send the message to the receiver 2.
Method return. \(\uparrow\) is used to return a value from a method. (You must type ^ to obtain the \(\uparrow\) character.)
Sequences of statements. A period or full-stop (.) is the statement separator. Putting a period between two expressions turns them into independent statements.
Cascades. Semicolons can be used to send a cascade of messages to a single receiver. In Transcript show: 'hello'; cr
we first send the keyword message show: 'hello'
to the receiver Transcript, and then we send the unary message cr
to the same receiver.
The classes Number
, Character
, String
and Boolean
are described in more detail in Chapter 8.