05.08.2014 Views

here - Stefan-Marr.de

here - Stefan-Marr.de

here - Stefan-Marr.de

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.2. SOM: Simple Object Machine<br />

1 SOMUniverse = ( " ... "<br />

2 bootstrapFrameWithArguments : args = (<br />

3 ( interpreter pushNewFrameWithMethod : self bootstrapMethod )<br />

4 push : ( self globalAt : # system );<br />

5 push : args ;<br />

6 yourself " convenience method to return self " ) )<br />

Listing 4.2: Casca<strong>de</strong>d Message Sends<br />

the language. Instead, the binding of global variables can only be changed by<br />

the explicit use of a VM primitive. Thus, supposedly constant globals such as<br />

true, false, and class references cannot simply be assigned. While they might<br />

appear to be keywords or special literals, similar to how they are treated in<br />

languages such as C++ or Java, in SOM, they are treated like any other global<br />

and will be looked up in the globals dictionary. Following the Smalltalk spirit<br />

of everything is an object, true, false, and nil are objects as well. More precisely,<br />

they are the sole instances of their corresponding class. Consequently,<br />

they can be adapted and customized when necessary.<br />

Casca<strong>de</strong>d Message Sends Some co<strong>de</strong> examples use Smalltalk’s casca<strong>de</strong>d message<br />

sends for conciseness, which is a language feature that is uncommon in<br />

other languages. The example in Lst. 4.2 <strong>de</strong>fines a method that uses a complex<br />

expression in line 3 that yields a frame object. This frame object still needs to<br />

be populated with initial data, i. e., the method pushes a number of objects<br />

onto the frame. For conciseness, instead of assigning the frame object to a<br />

temporary variable, the method uses casca<strong>de</strong>d message sends. Thus, the resulting<br />

frame object of the expression becomes the receiver of the first regular<br />

message send in line 4, which pushes the #system global, and then indicated<br />

by the semicolons, also becomes the receiver of the two following message<br />

sends in lines 5 and 6. Note the last message #yourself in Lst. 4.2, it is a<br />

convenience method <strong>de</strong>fined in Object. It is often used in casca<strong>de</strong>d message<br />

sends to return self. In this case it returns the frame object that was the result<br />

of the initial expression on line 3.<br />

Dynamic Array Creation Some of the co<strong>de</strong> examples in this dissertation<br />

use a notation for dynamically created arrays that is currently not part of the<br />

SOM implementation. However, it is commonly used in Squeak and Pharo<br />

Smalltalk to instantiate arrays in concise one-liners. The syntax uses curly<br />

braces to <strong>de</strong>limit a list of Smalltalk statements. The result value of each of<br />

93

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!