[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

[S8] Extensions to Smalltalk syntax

As we run on top of low level execution machinery that is not opaque[1], there are times we want to express/access something at the lower levels of the execution engine.
S8 compiler support a small number of extensions to smalltalk syntax to access objects in native layers.

SyntaxEffectHow/where it is used?
{' javascript '} Inline the javascript code in the generated code. It is used to implement basic operations that are provided by javascript language or can be expressed by minimal variations of it. Most times are used to implement basic operations like Primitive ops in smalltalk.Use references tool with searchSource: for {'
object#propertyNameGenerate the code to direct access the property(propertyName) of the object. It is equivalent to dot op in javascript. The result can be undefined. It is reccomended to be written as
object basicAt: propertyName
(or basicAt:ifAbsent:)
See implementors of accessors to NativeObject properties e.g. HTMLElement>>#scrollTop
#{aName}Lookup for object named aName will be realized in javascript. The result can be undefined.
Note: in case the name is lowercase, the S8 compiler do not force the existence of the target, so if the name is not found in the executing context lookup will continue at the javascript upper contexts (e.g. global object).
See implementation of Object>>#halt
# aMessageThe use of # for receiver means the receiver is missing, and the function activated for the message will be look up in global context. For example
# gc
generates javascript code
gc()
See implementation of Object>>#print:
receiver #message: arg1 with: arg: 2 and: arg3Will call function "message" (selector up to first colon, ignoring remaining parts of the selector) with the arguments. In the example it will generate javascript code
receiver.message(arg1,arg2,arg3)
See implementation of Number>>#isNumber

[1] Information hiding is a facility imposed to let us not be distracted by underlying mechanisms WHEN (and only when) we do not want to. Putting the vellum out must be mediated by an immediate gesture.

Annotations

The S8 parser (SmalltalkParser) support annotations for parse nodes.
The annotations can be detected during code generation (Encoder) to customize or access features on the native language.
The parser accept annotations after varables (and other relevant nodes, e.g. return expressions) if followed by two ":" characters.
E.g. in a definition of message arguments, locals or returns; you can write a method as:
! Object methodsFor: #test !
sample
    | k m |
    k::',m' := self twoReturnedValues.
    ^k::',Mat@m'! !

The encoder must handle/support the syntax of annotations.
The default action is to ignore annotations (defaulting to normal S8 encoding).
The sample method defined here is encoded by PythonEncoder to support multiple values returned by python functions. The k::',m' := ... assignment is encoded as asignment of locals (k & m) from return values returned by calling #twoReturnedValues
The ^k::',Mat@m' expression returns two variables, k and m wrapped as an instance of Mat (sending #@ for instantiation).