Smalltalk Syntax Cheat Sheet/en: Unterschied zwischen den Versionen

Aus expecco Wiki (Version 2.x)
Zur Navigation springen Zur Suche springen
(Die Seite wurde neu angelegt: „A short, one page summary of what beginners need to know about Smalltalk/X (ST/X) syntax. Non standard ST/X extensions are marked with (*). <BR>A list of most…“)
 
 
(11 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 6: Zeile 6:


=== Comments ===
=== Comments ===

<table border>
<table border>
<tr>
<tr>
Zeile 19: Zeile 20:
<tr>
<tr>
<td width=50%>Token-Comment (*)</td>
<td width=50%>Token-Comment (*)</td>
<td width=45%><code>"&lt;&lt;END<br>a multiline token-comment up to<br>a line starting with 'END'<br>more commented stuff here<br>...<br>comment ends here<br>END</code></td>
<td width=45%><code>"&lt;&lt;END
<br>a multiline token-comment up to
<br>a line starting with 'END'
<br>more commented stuff here
<br>...
<br>comment ends here
<br>END</code></td>
</tr>
</tr>


</table>
</table>


=== Literal Constants ===

<H3>Literal Constants</H3>


<table border>
<table border>
Zeile 47: Zeile 41:
<tr>
<tr>
<td width=50%>Integers with radix</td>
<td width=50%>Integers with radix</td>
<td width=45%><code>16rAFFE 0xFEED
<td width=45%>hex:<br><code>16rAFFE 0xFEED<br>16r-AFFE -0xBEAF<br>-16rAFFE
<br>binary:<br>2r010101 0b11011<br>2r-010101 -0b1100<br>octal:<br>8r0777<p></code>(the 0x, 0b variants are extensions to standard Smalltalk)</td>
<br>16r-AFFE -0xBEAF
<br>-16rAFFE
<br>2r010101 0b11011
<br>2r-010101 -0b1100
<br>8r0777</code>* the 0x, 0b variants are extensions to standard Smalltalk</td>
</tr>
</tr>


Zeile 98: Zeile 88:
<td width=45%><code>123.456s2
<td width=45%><code>123.456s2
<br>10s4</code>
<br>10s4</code>
</td>
</tr>

<tr>
<td width=50%>Complex<br></td>
<td width=45%><code>(3+4i)
<br>(3.14+4.56i)</code>
</td>
</td>
</tr>
</tr>
Zeile 111: Zeile 108:
<tr>
<tr>
<td width=50%>Strings<br></td>
<td width=50%>Strings<br></td>
<td width=45%><code>'hello'</code><p>with C-escape sequences (*):
<td width=45%><code>'hello'</code><p>with C-escape sequences (*):<br>&nbsp;<code>c'hello\nworld'</code>
<br>&nbsp;<code>c'hello\nworld'</code>
<p>with embedded expressions (*):<br>&nbsp;<code>e'it is {Time now}\nHave fun'</code>
<p>internationalization (*):<br>&nbsp;<code>i'Sunday'</code> or <code>i'Sunday {Date today}'</code>
<p>with embedded expressions (*):
<br>&nbsp;<code>e'it is {Time now}\nHave fun'</code>
<p>regex (*):<br>&nbsp;<code>r'a*'</code> or <code>r'[a-z][0-9a-z]*'</code>
<p>internationalization (*):
<br>&nbsp;<code>i'Sunday'</code> or <code>i'Sunday {Date today}'</code>
<p>regex (*):
<br>&nbsp;<code>r'a*'</code> or <code>r'[a-z][0-9a-z]*'</code>
</td>
</td>
</tr>
</tr>
Zeile 164: Zeile 157:
</table>
</table>


<H3>Blocks (aka Lambdas / Closures)</H3>
=== Blocks (aka Lambdas / Closures) ===


<table border>
<table border>
Zeile 192: Zeile 185:
</table>
</table>


<H3>Expressions</H3>
=== Expressions ===


<table border>
<table border>
Zeile 228: Zeile 221:
<tr>
<tr>
<td width="50%">Cascade Expression<br>(sending multiple messages to the same receiver)</td>
<td width="50%">Cascade Expression<br>(sending multiple messages to the same receiver)</td>
<td width="45%"><code><i>receiver</i><br> <i>message1</i> ;<br> <i>message2</i> ;<br> ...<br> <i>messageN</i></code></td>
<td width="45%"><code><i>receiver</i><br> <i>message1</i> ;<br> <i>message2</i> ;<br> ...<br> <i>messageN</i></code><p>the receiver expression is evaluated, then multiple messages (separated by ";") are sent to this receiver.
<p>the receiver expression is evaluated, then multiple messages (separated by ";") are sent to this receiver.
<br>The value of the cascade is the value returned by the last message.
<br>The value of the cascade is the value returned by the last message.
</td>
</td>
Zeile 262: Zeile 254:
</table>
</table>


<H3>Remaining Syntax</H3>
=== Remaining Syntax ===


<table border>
<table border>
Zeile 291: Zeile 283:
</table>
</table>


<H3>Wellknown (Common) Messages</H3>
=== Wellknown (Common) Messages ===


<table border>
<table border>
Zeile 342: Zeile 334:
</table>
</table>


<H2>Wellknown Globals</H2>
=== Wellknown Globals ===


<table border>
<table border>
Zeile 359: Zeile 351:
</table>
</table>


<H2>Most Wellknown Classes</H2>
=== Most Wellknown Classes ===


==== Numbers ====
<table border>
<table border>


<tr>
<tr>
<td width=50%>Numbers</td>
<td width=50%>Numbers</td>
<td width=45%>[http://live.exept.de/ClassDoc/classDocOf:,Number <code>Number</code>] (abstract superclass)

<br>[http://live.exept.de/ClassDoc/classDocOf:,Integer <code>Integer</code> ] (integral numbers),<br> [http://live.exept.de/ClassDoc/classDocOf:,Float <code>Float</code>] (inexact)<br>[http://live.exept.de/ClassDoc/classDocOf:,Fraction <code>Fraction</code>] (exact)<br>[http://live.exept.de/ClassDoc/classDocOf:,Complex <code>Complex</code>]<br>a few more...
<td width=45%><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Number">Number</A></code> (abstract superclass)
<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Integer">Integer</A></code> (integral numbers),<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Float">Float</A></code> (inexact)
<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Fraction">Fraction</A></code>(exact)<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Complex">Complex</A></code><br>a few more...
</td>
</td>
</tr>
</tr>
</table>


==== Collections ====

<table border>
<tr>
<tr>
<td width=50%>Collections</td>
<td width=50%>Collections</td>


<td width=45%><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Array">Array</A></code> (fixed size array)
<td width=45%><br>[http://live.exept.de/ClassDoc/classDocOf:,Collection <code>Collection</code>] (abstract superclass)<br>[http://live.exept.de/ClassDoc/classDocOf:,SequenceableCollection <code>SequenceableCollection</code>] (abstract superclass)<p>[http://live.exept.de/ClassDoc/classDocOf:,Array <code>Array</code>] (fixed size array)<br>[http://live.exept.de/ClassDoc/classDocOf:,OrderedCollection <code>OrderedCollection</code>],
<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,OrderedCollection">OrderedCollection</A></code>,
[http://live.exept.de/ClassDoc/classDocOf:,List <code>List</code>] (variable size)
<code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,List">List</A></code> (variable size)
<br>[http://live.exept.de/ClassDoc/classDocOf:,SortedCollection <code>SortedCollection</code>]<br>[http://live.exept.de/ClassDoc/classDocOf:,Set <code>Set</code>],
<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,SortedCollection">SortedCollection</A></code><br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Set">Set</A></code>,
[http://live.exept.de/ClassDoc/classDocOf:,Bag <code>Bag</code>] (unordered)
<code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Bag">Bag</A></code> (unordered)
<br>[http://live.exept.de/ClassDoc/classDocOf:,Dictionary <code>Dictionary</code>] (mapped collections)
<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,Dictionary">Dictionary</A></code> (mapped collections)
<br>[http://live.exept.de/ClassDoc/classDocOf:,BinaryTree <code>BinaryTree</code>],
<br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,BinaryTree">BinaryTree</A></code>,
[http://live.exept.de/ClassDoc/classDocOf:,AVLTree <code>AVLTree</code>]<br>[http://live.exept.de/ClassDoc/classDocOf:,SharedQueue <code>SharedQueue</code>] (shared, synchronized)
<p>many more...
<code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,AVLTree">AVLTree</A></code><br><code><A HREF="http://live.exept.de/ClassDoc/classDocOf:,SharedQueue">SharedQueue</A></code> (shared, synchronized)
<br>many more...
</td>
</td>
</tr>
</tr>
</table>

==== Process Handling ====

<table border>


<tr>
<tr>
Zeile 394: Zeile 393:
</td>
</td>
</tr>
</tr>
</table>

==== Files & Streams ====

<table border>


<tr>
<tr>
Zeile 404: Zeile 408:
</td>
</td>
</tr>
</tr>
</table>

==== Low Level Access ====

<table border>


<tr>
<tr>
Zeile 409: Zeile 418:
<td width=45%><code>OperatingSystem</code> (OS API calls)
<td width=45%><code>OperatingSystem</code> (OS API calls)
<br><code>Smalltalk</code> (startup, shutdown and globals)
<br><code>Smalltalk</code> (startup, shutdown and globals)
<br><code>ObjectMemory </code>(VM access)
</td>
</td>
</tr>
</tr>
Zeile 416: Zeile 424:
</table>
</table>


== Examples ==
<H2>Command Line</H2>


=== File Handling ===
A more complete description is found in the
<A href="../getstart/TOP.html#COMMANDLINE">Command Line</A> section
of the getting started document.


==== Open a file, write some lines, close it ====
<table border>
|fn stream nr nr1 nr2 nr3|. "/ local variables
nr := 123.
nr1 := 10.
nr2 := 20.
nr3 := 30.
fn := 'myFile' asFilename. "/ convert string to filename
stream := fn writeStream. "/ open a stream for writing
stream nextPutLine:'bla bla'. "/ write with line-end-character
stream nextPut:'word1'. "/ without line-end-character
stream cr. "/ line-end-character
stream printf: c'%3d\n' with:nr. "/ formatted print
stream printf: c'%3d %3x %03x\n' withAll:{nr1 . nr2 . nr3}.
stream close


==== Open the file, read some lines, close it ====
<tr>
|fn stream line word nrs|. "/ local variables
<td width=50%>-I</td>
<td width=45%>ignore snapshot image
stream := 'myFile' asFilename readStream. "/ open for reading
</td>
line := stream nextLine. "/ read a line
</tr>
word := stream next:5. "/ read 5 characters

stream skipThroughEndOfLine. "/ skip up-to and including line-end
<tr>
nrs := stream scanf: c'%3d\n'. "/ formatted read; returns vector (with 1 nr in it)
<td width=50%>--quick</td>
nr1 := nrs at:1.
<td width=45%>quick startup; no banner shown
nrs := stream scanf: c'%3d %3x %03x'. "/ returns a vector (with 3 nrs in it)
</td>
stream close
</tr>

<tr>
<td width=50%>--repl</td>
<td width=45%>start an interactive read-eval-print-loop
</td>
</tr>

<tr>
<td width=50%>--print <i>expr</i></td>
<td width=45%>eval expr, print its value then exit
</td>
</tr>

<tr>
<td width=50%>--eval <i>expr</i></td>
<td width=45%>eval expr (without printing) then exit
</td>
</tr>

<tr>
<td width=50%>--execute <i>filename</i></td>
<td width=45%>execute the script in fileName then exit
</td>
</tr>

<tr>
<td width=50%>--loadPackage <i>package-ID</i></td>
<td width=45%>preload a compiled smalltalk class library (package)
</td>
</tr>

<tr>
<td width=50%>--help</td>
<td width=45%>show all other command line options
</td>
</tr>

</table>

Aktuelle Version vom 6. Juni 2024, 13:52 Uhr

A short, one page summary of what beginners need to know about Smalltalk/X (ST/X) syntax. Non standard ST/X extensions are marked with (*).
A list of most useful messages is found "List of Useful Selectors".

Syntax[Bearbeiten]

Comments[Bearbeiten]

Regular comment " this is a comment "
EOL-Comment (*) "/ this is an End-of-Line comment
Token-Comment (*) "<<END
a multiline token-comment up to
a line starting with 'END'
more commented stuff here
...
comment ends here
END

Literal Constants[Bearbeiten]

Integers 12345
-12345
Large integers 1234567890123456789012345...
(arbitrary size)
Integers with radix hex:
16rAFFE 0xFEED
16r-AFFE -0xBEAF
-16rAFFE
binary:
2r010101 0b11011
2r-010101 -0b1100
octal:
8r0777

(the 0x, 0b variants are extensions to standard Smalltalk)

Floats
(IEEE double; roughly 17 decimals)
12.456


1.234e17
-6.9
-6e-10
Float pi
Float e

Short floats
(IEEE single; roughly 8 decimals)
1.234f17


-6f-10

Long floats
(IEEE quad; roughly 20 decimals) (*)
1.234q17


-6q-10

Fractions
(1/3)


(17/2)

ScaledDecimal
123.456s2


10s4

Complex
(3+4i)


(3.14+4.56i)

Characters
$a


Character space
Character nl

Also: "null", "tab", "return", "bell"...

Strings
'hello'

with C-escape sequences (*):
 c'hello\nworld'

with embedded expressions (*):
 e'it is {Time now}\nHave fun'

internationalization (*):
 i'Sunday' or i'Sunday {Date today}'

regex (*):
 r'a*' or r'[a-z][0-9a-z]*'

Symbols
#'hello'
#foo
(without quotes, if only alphaNumeric characters)
Arrays
#( el1 el2 ... elN )


each element being a literal constant
for arrays within an array constant,
the leading hash ("#") can be ommitted
Notice: these are constants, allocated at compile time;
(for arrays constructed at run time, see brace construct below)

Byte arrays
#[ b1 b2 ... bN ]


each byte-element being an integer constant in 0..255

Special number arrays (*)
#XX( v1 v2 ... vN )


XX: is one of
 'u8', 's8', 'u16', 's16', 'u32', 's32', 'u64', 's64', 'f32', 'f64'
 and each element being an integer or float constant

Immediate Inline Objects (*)
#{ foo: fooValue . bar: barValue }


fooValue and barValue: constant

Blocks (aka Lambdas / Closures)[Bearbeiten]

Without argument [ expr1 . expr2 ... exprN ]


when evaluated (by sending it the "value" message),
 the value is the value from
 the last expression, exprN

One argument [:arg |
expr1 . expr2 ... exprN
]


evaluate by sending it the "value:" message

Multiple arguments [:a1 :a2 ... :aN |
expr1 . expr2 ... exprN
]


evaluate by sending it a "value:...value:" message

Expressions[Bearbeiten]

Unary Expression (without argument) receiver messageName

receiver is itself either a constant, a lambda-block, a unary expression or a parenthesized expression

Keyword Expression: (1 arg) receiver messageNamePart:argExpression

whitespace between the colon and the argExpression is optional.

Keyword Expression: (any number of args) receiver
    messageNamePart1:argExpression1
    messageNamePart2:argExpression2
    ...
    messageNamePartN:argExpressionN
Binary Expression receiver binOP arg

with binOP being any combination of special characters: * , + , - , % , & , / , \ , | , = , < , > , ? and ,
I.e. "foo ==> 123" is the binary message "==>" sent to foo with 123 as argument.
And "foo ? 123" is the binary message "?" (or-if-nil) sent to foo with 123 as argument.
And "foo , bar" is the binary message "," (concatenation) sent to foo with bar as argument.

Cascade Expression
(sending multiple messages to the same receiver)
receiver
message1 ;
message2 ;
...
messageN

the receiver expression is evaluated, then multiple messages (separated by ";") are sent to this receiver.


The value of the cascade is the value returned by the last message.

Parentheses for grouping ( any expression )
Assignment variable := expression
can be used as expression.
Computed Array (Brace Construct) { expr1 . expr2 . ... . exprN }

instantiates a new Array object with elements from the expressions.

Computed Inline Objects (*)
{ foo: fooExpr . bar: barExpr }


fooExpr and barExpr are expressions. Notice the period to separate fields.

Remaining Syntax[Bearbeiten]

Local variables (in block or method) | var1 var2 ... varN |

variable declarations must be at the beginning, before any expression.

Separating multiple expressions (sequence) expr1 . expr2 . ... exprN

expressions (statements) are separated by fullstop (period) characters.
The last expression may or may not be followed by a fullstop; if there is one, this is treated like a followup empty statement.

Return from method ^ expression

returns from the enclosing method (also if inside a block-closure)

Wellknown (Common) Messages[Bearbeiten]

Conditional Execution boolExpr1 ifTrue:[ ... ] ifFalse:[ ... ]

variations without true part (ifFalse:), without false part (ifTrue:) and with the order reversed (ifFalse:ifTrue:) are available.

While-Loop [ boolExpr1 ] whileTrue:[ ... ]

notice the receiver being a block.
A whileFalse variant is also available.
 Loops with test at end are written as "[...] doWhile:[...]" or "[...] doUntil:[...]"

For-Loop start to:stop do:[:iterVar | ... ]

evaluates the lambda for each value in start..stop.

Enumerating Collections collection do:[:elementVar | ... ]

evaluates the lambda for each element in the collection.

Evaluating a Lambda Block
  without arguments:

  with arguments:


aBlock value

aBlock value:expr1 ... value:exprN

the number of arguments must match the number expected by the lambda (although varArg lambdas are also available)

Starting a background thread aBlock fork

evaluates the lambda in a separate thread. Alternatives are "forkNamed:" to give the thread a user friendly name.

Wellknown Globals[Bearbeiten]

Logging and Messaging Logger - redefinable logger (defaults to standard error)


Transcript - console window
Stdout - standard output
Stderr - standard error
Stdin - standard input

Most Wellknown Classes[Bearbeiten]

Numbers[Bearbeiten]

Numbers Number (abstract superclass)


Integer (integral numbers),
Float (inexact)
Fraction (exact)
Complex
a few more...

Collections[Bearbeiten]

Collections
Collection (abstract superclass)
SequenceableCollection (abstract superclass)

Array (fixed size array)
OrderedCollection,

List (variable size)
SortedCollection
Set, Bag (unordered)
Dictionary (mapped collections)
BinaryTree, AVLTree
SharedQueue (shared, synchronized)

many more...

Process Handling[Bearbeiten]

Process Handling Process (lightweight thread)
OSProcess (heavyweight OS process)


Semaphore, RecursionLock (synchronization)
Monitor, BoltLock
Delay

Files & Streams[Bearbeiten]

Files & Streams Filename (file naming, directory access, mime type)


Stream (basic stream framework)
ReadStream, WriteStream, EncodedStream
ExternalStream, FileStream, Socket (OS streams)

Low Level Access[Bearbeiten]

Low Level Access OperatingSystem (OS API calls)


Smalltalk (startup, shutdown and globals)

Examples[Bearbeiten]

File Handling[Bearbeiten]

Open a file, write some lines, close it[Bearbeiten]

|fn stream nr nr1 nr2 nr3|. "/ local variables

nr := 123.
nr1 := 10.
nr2 := 20.
nr3 := 30.

fn := 'myFile' asFilename.     "/ convert string to filename
stream := fn writeStream.      "/ open a stream for writing
stream nextPutLine:'bla bla'.  "/ write with line-end-character
stream nextPut:'word1'.        "/ without line-end-character
stream cr.                     "/ line-end-character
stream printf: c'%3d\n' with:nr.  "/ formatted print
stream printf: c'%3d %3x %03x\n' withAll:{nr1 . nr2 . nr3}.
stream close

Open the file, read some lines, close it[Bearbeiten]

|fn stream line word nrs|. "/ local variables

stream := 'myFile' asFilename readStream.       "/ open for reading
line := stream nextLine.                        "/ read a line
word := stream next:5.                          "/ read 5 characters
stream skipThroughEndOfLine.                    "/ skip up-to and including line-end
nrs := stream scanf: c'%3d\n'.                  "/ formatted read; returns vector (with 1 nr in it)
nr1 := nrs at:1.
nrs := stream scanf: c'%3d %3x %03x'.           "/ returns a vector (with 3 nrs in it)
stream close



Copyright © 2014-2024 eXept Software AG