Hello World (occam)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Alice ML | Amiga E | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | C++ | C# | Dylan | E | Forth | FORTRAN | Fortress | Go | Groovy | IBM PC bootstrap | Inform 7 | JavaScript | LaTeX | Logo | LOLCODE | Lua | MATLAB | OCaml/F Sharp | occam | Oz | Perl | PHP | PIR | PLI | Prolog | Python | Rexx | Ruby | Scheme | sh | SQL | Standard ML | Tcl | Tcl Tk | Visual Basic

These simple occam programs print a string to the console, then print the integers 1 through 10 using a simple for loop. The first of the two programs demonstrates the use of the classic INMOS hostio library for writing to the screen, and follows the standard occam 2.1 syntax. The second program demonstrates the occam-π syntax implemented in the modern KRoC occam compiler.

[edit] Hello world in classic occam

One of the unusual features of occam is that it requires blocks of code to be explicitly identified as sequentially executed (SEQ) or concurrently executed (PAR). This program is purely sequential. The first two lines import some definitions for the hostio library, and instruct the compiler to link the program with the shared hostio library. The third line defines a procedure with two arguments, both channels which use the SP protocol (defined by the hostio library). These channels are necessary to perform I/O. The so functions are defined by hostio, and permit output of various types of data.

#INCLUDE "hostio.inc"
#USE "hostio.lib"
PROC hello.world (CHAN OF SP fs, ts)
    so.write.string.nl(fs, ts, "Hello World!")
    SEQ i = 1 FOR 10
        so.write.int(fs, ts, i, 0)
        so.write.nl(fs, ts)

[edit] Hello world in occam-π

The modern KRoC occam compiler deprecates the use of hostio in favor of a more POSIX-like approach to I/O. I/O procedures are imported from the oddly named course.lib shared library (the name is a relic of occam-π's academic roots).

#USE "course.lib"
PROC hello.world (CHAN BYTE scr!)
    out.string("Hello World!*n", 0, scr)
    SEQ i = 1 FOR 10
        out.int(i, 0, scr)
        scr ! '*n'

Note that the course.lib I/O procedures use channels that follow the BYTE protocol, rather than the SP protocol used by hostio.lib. As a result, individual characters can be written to the screen via direct output through the scr channel. Thus,

scr ! '*n'

sends the newline character to the screen (occam uses the * character as its escape character, rather than the more common \).

[edit] Running

These programs can be compiled using the Kent Retargetable occam Compiler. On UNIX, you can build and run the samples using the following scripts:

kroc --octran-opts="--tlp-fsts" -lhostio -lconvert -o hello_world-classic hello_world-classic.occ
kroc -lcourse -o hello_world-kroc hello_world-kroc.occ
Download code