links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Last updated at 5:47 am UTC on 31 December 2018
Slang is a language that rolls up its sleeves, spits on its hands, and goes to work. –Carl Sandburg, poet and biographer (1878-1967)

Slang is a sub-set of Smalltalk language intended to be readily translated to C source code. It is invented so that Squeak VM (written in Smalltalk itself) could be emitted into C code. The article Back to the Future article for information about why Squeak's VM is constructed this way. In short, Squeak VM can be run within Squeak during development and debugging by using Squeak's quick and efficient live programming tools. However, bytecode interpreter is very slow compared to native machine code execution. Therefore, the debugged code is then translated into C and then compiled into (or ported to) machine code for fast execution. See The Squeak Virtual Machine for information about running the VM in Squeak.

The name Slang was coined by Frank Zdybel during an Interval lunch at Swagat in Palo Alto in 1997. (verified by Craig Latta, who was at the lunch). It is unrelated to S-Lang (http://www.s-lang.org/).

Slang Language

Slang is essentially C with a Smalltalk syntax. It does not have blocks (except for a few control structures), message sending or even objects. dispatchOn:in: is mapped to a C switch{} statement specially to make the interpreter efficient. See the methods in classes such as Interpreter are the best sources to learn about Slang code.

Slang Examples

Object subclass: #FooBar
       instanceVariableNames: 'counter'
       classVariablesNames: ''
       poolDictionaries: ''

    ^self malloc: 100

    ^counter + 1
Its C code can be generated with CCodeGenerator object:
FooBarCG := CCodeGenerator new.
FooBarCG addClass: FooBar.  "add more classes like this"
FooBarCG storeCodeOnFile: 'foobar.c' doInlining: true.

Slang Operators

   "&"    "|"    and:    or:    not
   "+"    "-"   "//"    "\\"    min:    max:
  bitAnd: bitOr: bitXor: bitShift:
   "<"    "<="  "="     ">"     ">="    "~="   "=="
  isNil   notNil
  whileTrue:     whileFalse:    to:do:           to:by:do:
  ifTrue:        ifFalse:       ifTrue:ifFalse:  ifFalse:ifTrue:
  at:     at:put:
  <<      >>     bitInvert32    preIncrement     integerValueOf:
  integerObjectOf:              isIntegerObject: 

Slang Statements

Here are some examples of translations
C Code
instanceVariableNames: 'foregroundColor backgroundColor'
sqInt foregroundColor, backgroundColor;
classVariableNames: 'MemorySize'
#define MemorySize 10
return (a+b);
a bitShift: 4
a >> 4;
now := FooBar foo: x bar: y
now = foobar(x,y);
^self bigEndian ifTrue: [16r6502] ifFalse: [16r0265]
return bigEndian() ? 0x6502 : 0x0265;
1 to: 10 by: 2 do: [:i | a at: 1 put: (i*2)]
for(i=1; i = 10; i += 2) { a[i] := (i*2); }
flag whileTrue: [ self check ]
while (flag) { check(); }
  <returnTypeC: 'char *'>
  | newStr |
  var: #newStr type: 'char*'>
  newStr := 'helen'
char *getName(void)
    char*newStr = "hello";
    return newStr;
    <inline: true>

   <inline: false>
   ^width ifNil: [ width := defaultWidth ].
static sqInt
   return width == nilObj ? (width = 10) : width;

See also