How to build Som


Source files

Source code of Som-system comprises of

compile.h     define constants
icname.h      encoding of all s-code
parse.h        function prototypes
token.h        definition of tokens
interp.h        for interpreter
a1.c             main
token.c        tokeniser, generated from lexgen
lex.c            lexical analyser
parse.c        parser, generated from pgen
stmt.c          functions that support parsing
gencode.c   code generator
icode.c        functions that support outputing code
symtab.c     symbol table functions
list.c            list construction functions: car, cdr, cons, list.
interp.c        the s-code interpreter and run-time data structure

How to compile Som-system

Include all the above files in the project.  It has been tested with lcc compiler under lcc-win32 IDE.   There are four sub-directories:
doc        documents
lex          tokeniser generator
parser     parser generator
test         examples and executables

token.c, token.h    are generated by lexgen in "lex" directory.  lexgen takes a text file that specifies lexical to token-name binding, such as  "* tkSTAR"  and produces a lexical analyser (token.c) that recognised them.  

    lexgen < tok-som.txt > token.c     (token.h is implicit)

parse.c, parse.h   are generated by pgen in "parser" directory.  pgen takes a text file that specifies LL(1) grammar of Som-language, such as,

    top -> tkTO fundef | ex #
    ex1 ->  tkIF ex0 ex exelse $doif(); | ...

and produces a parser.  The action routines in the grammar such as "$doif();" are the C functions reside in the "stmt.c" file.  pgen takes "parse.txt" and produces a C parser,

    pgen < parse.txt > parse.c      (parse.h is implicit)

It is not necessary to regenerate token.c  and parse.c  except when you want to change the lexicon and the grammar to modify Som-language for your application.

Examples in "test" directory

bubble.txt        bubble sort  20..1  to 1..20
array.txt           fill in an array 0..9 and print it
hanoi.txt           solve tower of hanoi for 3 disks
matmul.txt        multiply two 4 by 4 matrices
perm.txt          permute the number 0,1,2,3
queen.txt         solve all solutions (92 boards) of 8-queen
quick.txt         quick sort 20..1 to 1..20
sieve.txt           find all prime numbers less than 1000 (168 primes)

Two files "bubble.lst" and "bubble.obj"  show the listing file and the object file (which can be converted to a target machine code).

Sample session

Som can be used in 3 modes:  interactive, batch (produce listing and object files), and mixed.  When start the system loads "lib.a1" which contain very small set of useful functions: print, printc, space, nl, loadfile

interactive

This mode is suitable to try out a program.

C:som-v1\test>som
>print 2 + 3 nl
5
>to sq x = x * x
>print sq 5 nl
25
>

to define a function, the whole function must be completed in one line.

batch mode

This mode is used to run a program.  It will produce a listing file and an object file.

C:som-v1\test>som bubble.txt
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

C:som-v1\test>

It will produce "bubble.lst" and "bubble.obj".

mixed mode  

This mode is used to debug a program.  It will execute the program immediately (not producing any output file) and start the interactive mode.  A user can interrogate global variables and executes all functions in the program.

C:som-v1\test>som -i bubble.txt
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>print data[2] nl
3
>

To show a help message:


C:som-v1\test>som -?
som          for interactive mode
som file     for batch mode, output .lst and .obj
som -i file  for load file and then interact
som -?       for this help

C:som-v1\test>

5 December 2004
Long live King Bhumipol
P. Chongstitvatana