Macro definition  (in Som 1.5)

To reduce the overhead of a function call, a function can be defined as a "macro".  A macro definition is just like a function definition, the difference is that the body of a macro definition is substituted into the call.  Hence, the size of a program with a macro is larger.  The advantage is that it will be executed faster.  The syntax of a macro definition is similar to a function definition, only the keyword is ":" instead of "to".  For example,

: print x = syscall {1 x}

Whenever the macro appears in the program, the macro body is substituted.

to report a b =
  ...
  print a
  ...

will become

to report a b =
  ...
  syscall {1 a}
  ...

Macro is suitable for defining "access function" such as,

(from list-s.txt in Som v2 som-in-som)

to setcar a value = cell[a] = value
to setcdr a value = cell[a+1] = value
to car a = if a == NIL a else cell[a]
to cdr a = if a == NIL a else cell[a+1]

These functions will be executed much faster because there is no overhead associated with "call" "return" such as create/destroy the stack frame.


