Introduction to Som language
We are going to use Som as an executable pseudo code for studying
programs. Som is a simple language (and hence it is at a lower
level than a present day language such as Java). Som contains
only one data type, ie. integer (32 bits). It has only one kind
of data structure, an array. The purpose of introducing a new
language is that it is simpler than other language used to illustrate
an implementation of data structure (such as C or Java). You can
understand the step-by-step actions to manipulate data structure
easily. You can then, write a Java program to do it.
Som lacks many essential features to be a practical programming
language but it is good enough for our purpose in this course.
Reserved words: to,
if, else, while, for
Operators:
arithmatic: +, -, *, / (integer division),
% (mod)
logic: ==,
!=, <, <=, >, >=, !
bitwise: &
(bit-and), | (bit-or), ^ (bit-xor), << (shift left), >>
(shift right)
Library functions: and
(logic), or (logic), print, printc (print character), space, nl
(newline)
To define a function:
to fun_name
parameters | locals = body
"|" is used to separate parameter list from local variable list.
The body of function is a block of code. Som uses indentation to group
expressions into a block (similar to {} in C or Java). The evaluation
of an expression is from left to right. There is no precedency
among the operators. Use parenthesis to make certain of the
evaluation order.
It is best to see examples. Here is a function to sum an
array.
ax = array 100
to suma x | s i =
s = 0
for i 0 99
s = s + x[i]
s
to factorial n =
if n == 0
1
else
n * factorial
(n-1)
How to use Som compiler
The compiler for Som has three modes: interactive, batch and
execution. (We use Som version 5.0 for illustration). It runs
from a "command" window. Do >run cmd from Start menu of Windows.
interactive
>som5
...
>print 1 + 2
3>
>to sq x = x * x
+sq
>print sq 4
16>^C
Control-C stops the interactive compiler. The batch mode reads a
source file in Som and compiles it into an object (executable) and a
listing file.
>som5 do-list.txt
produces do-list.obj and do-list.lst
Finally, the execution mode, it reads an object file and runs it.
>som5 -x do-list.obj
You can download Som compiler and read more
from Som
homepage.
End