Skip to main content
Engineering LibreTexts

4.1: Syntactic Elements

  • Page ID
    39584
    \( \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}}\)

    Expressions are composed of the following building blocks:

    1. The six reserved keywords, or pseudo-variables: self, super, nil, true, false, and thisContext
    2. Constant expressions for literal objects including numbers, characters, strings, symbols and arrays.
    3. Variable declarations
    4. Assignments
    5. Block closures
    6. Messages

    We can see examples of the various syntactic elements in Table \(\PageIndex{1}\) below.

    Table \(\PageIndex{1}\): Syntactic elements.
    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 | x + 2 ] a block that evaluates to x + 2
    <primitive: 1> virtual machine primitive or annotation
    3 factorial unary message factorial
    3 + 4 binary message +
    2 raisedTo: 6 modulo: 10 keyword message raisedTo:modulo:
    ^ true return the value true
    x := 2 . x := x + x 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 ThreadSafeTranscript.

    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 has 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, Pharo 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 X 10^7.

    Characters. A dollar sign introduces a literal character: $a is the literal for the character '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 single quote inside, just double the quote, as in 'G''day'.

    Symbols. 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. (Note that this is the same as #(27 #(true false) #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.

    Comments. are enclosed in double quotes ” ”. ”hello” is a comment, not a string, and is ignored by the Pharo 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.

    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 ([:i | ...]) and can have local variables.

    Primitives. < primitive: ... > denotes an invocation of a virtual machine primitive. For example, < primitive: 1 > is the VM primitive for SmallInteger. Any code following the primitive is executed only if the primitive fails. The same syntax is also used for method annotations (pragmas).

    Unary messages. These consist of a single word (like factorial) sent to a receiver (like 3). In 3 factorial, 3 is the receiver, and factorial is the message selector.

    Binary messages. These are message with an argument and whose selector looks like mathematical expressions (for example: +) sent to a receiver, and taking a single argument. In 3 + 4, the receiver is 3, the message selector is +, and the argument is 4.

    Keyword messages. They 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. ^ is used to return a value from a method.

    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 : Basic Classes.


    This page titled 4.1: Syntactic Elements is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.