Next: , Previous: Operation, Up: Using GNU Smalltalk

1.3 Syntax of gnu Smalltalk

The language that gnu Smalltalk accepts is basically the same that other Smalltalk environment accept and the same syntax used in the Blue Book, also known as Smalltalk-80: The Language and Its Implementation. The return operator, which is represented in the Blue Book as an up-arrow, is mapped to the ASCII caret symbol ^; the assignment operator (left-arrow) is usually represented as :=1.

Actually, the grammar of gnu Smalltalk is slightly different from the grammar of other Smalltalk environments in order to simplify interaction with the system in a command-line environment as well as in full-screen editors.

Statements are executed one by one; multiple statements are separated by a period. At end-of-line, if a valid statement is complete, a period is implicit. For example,

     8r300. 16rFFFF

prints out the decimal value of octal 300 and hex FFFF, each followed by a newline.

Multiple statements share the same local variables, which are automatically declared. To delete the local variables, terminate a statement with ! rather than . or newline. Here,

     a := 42

the first two as are printed as 42, but the third one is uninitialized and thus printed as nil.

In order to evaluate multiple statements in a single block, wrap them into an eval block as follows:

     Eval [
         a := 42.  a printString

This won't print the intermediate result (the integer 42), only the final result (the string '42').

     ObjectMemory quit

exits from the system. You can also type a C-d to exit from Smalltalk if it's reading statements from standard input.

GNU Smalltalk provides three extensions to the language that make it simpler to write complete programs in an editor. However, it is also compatible with the file out syntax as shown in the Green Book (also known as Smalltalk-80: Bits of History, Words of Advice by Glenn Krasner).

A new class is created using this syntax:

     superclass-name subclass: new-class-name [
     | instance variables |
     message-pattern-1 [ statements ]
     message-pattern-2 [ statements ]
     class-variable-1 := expression.
     class-variable-2 := expression.

In short:

A similar syntax is used to define new methods in an existing class.

     class-expression extend [

The class-expression is an expression that evaluates to a class object, which is typically just the name of a class, although it can be the name of a class followed by the word class, which causes the method definitions that follow to apply to the named class itself, rather than to its instances.

     Number extend [
         radiusToArea [
             ^self squared * Float pi
         radiusToCircumference [
             ^self * 2 * Float pi

A complete treatment of the Smalltalk syntax and of the class library can be found in the included tutorial and class reference (see Class Reference).

More information on the implementation of the language can be found in the Blue Book; the relevant parts are also available online as html documents, at


[1] It also bears mentioning that there are two assignment operators: _ and :=. Both are usable interchangeably, provided that they are surrounded by spaces. The gnu Smalltalk kernel code uses the := form exclusively, but _ is supported a) for compatibility with previous versions of gnu Smalltalk b) because this is the correct mapping between the assignment operator mentioned in the Blue Book and the current ASCII definition. In the ancient days (like the middle 70's), the ASCII underscore character was also printed as a back-arrow, and many terminals would display it that way, thus its current usage. Anyway, using _ may lead to portability problems.