Previous | Table of Contents | Next |
The implementation of Icon is based on the concept of a virtual machinean imaginary computer that executes instructions for Icon programs. The Icon compiler translates Icon programs into assembly language for the virtual machine and then converts the assembly language into virtual machine code. This virtual machine code is then executed on a real computer using an interpreter. This implementation method allows Icon to run on many different computer platforms.
Compiling and running Icon programs is easy. It is not necessary to understand Icons virtual machine, but knowing the nature of the implementation may help in understanding what is going on in some situations. This section describes the rudiments of running Icon programs.
How Icon programs are run necessarily varies from platform to platform. On some platforms, Icon is run from the command line. On others, it is run interactively through a visual interface. This chapter describes how Icon is run in a command-line environment. Even for this environment, details depend on the platform. In any event, the user manual for a specific platform is the best guide to running Icon.
6.2.8.1. Basics
The name of a file that contains an Icon source program must end with the suffix .icn, as in hello.icn. The .icn suffix is used by the Icon compiler to distinguish Icon source programs from other kinds of files.
The Icon compiler usually is named icont. To compile hello.icn, all that is needed is
icont hello.icn
The suffix .icn is assumed if none is given, so that this can be written more simply as
icont hello
The result is an executable icode file. The name of the icode file depends on the platform on which Icon is run. On some platforms, notably UNIX, the name is the same as the name of the source file, but without the suffix. On these platforms, the compilation of hello.icn produces an icode file named hello. On other platforms, such as MS-DOS, the icode file has the suffix .icn replaced by .exe, as in hello.exe. For Microsoft Windows, the suffix is .cmd, and so on.
After compilation, entering
hello
runs the program.
An Icon program can be compiled and run in a single step using the x option following the program name. For example,
icont hello x
compiles and executes hello.icn. An icode file also is created, and it can be executed subsequently without recompiling the source program.
There are command-line options for icont. Options must appear before file names on the icont command line. For example,
icont s hello
suppresses informative messages that icont ordinarily produces.
6.2.8.2. Input and Output Redirection
In a command-line environment, most input and output is done using standard input, standard output, and standard error output. Standard input typically is read from the keyboard, whereas standard output and standard error output are written to the console.
Standard input and standard output can be redirected so that files can be used in place of keyboard input. For example,
hello < hello.dat > hello.out
executes hello with hello.dat as standard input and hello.out as standard output. (The directions that the angular brackets point relative to the program name are suggestive of the direction of data flow.)
6.2.8.3. Command-Line Arguments
Arguments on the command line following an icode file name are available to the executing Icon program in the form of a list of strings. This list is the argument to the main procedure. For example, if showargs.icn consists of
procedure main(arguments) every write(!arguments) end
then this program prints the arguments on the command line with which it is executed. Thus,
icont showargs showargs Hello world
writes
Hello world
When x is used, the arguments follow it, as in
icont showargs x Hello world
Arguments are separated by blanks. The treatment of special characters, methods of embedding blanks in arguments, and so forth, vary from platform to platform.
6.2.8.4. Library Modules
The c option to icont stops processing after the production of assembly-language code and produces a pair of ucode files that can be incorporated in other Icon programs. These files constitute a library module, which usually contains one or more procedures. For example,
icont c mylibe
produces ucode files from mylibe.icn. The ucode files have the suffixes .u1 and .u2in this case, mylibe.u1 and mylibe.u2.
Ucode files can be incorporated in a program using the link declaration, as in
link mylibe procedure main()
The ucode file suffixes are not used in link declarations. In the preceding example, any procedures in mylibe.icn are available in the program that links their ucode files.
6.2.8.5. Environment Variables
Environment variables can be used to configure Icon and specify the location of files. For example, the environment variable IPATH can be used to specify the location of library modules. If the library module graphics is in
/usr/icon/ipl/gprogs
and IPATH has that value, then
link graphics
finds it.
Previous | Table of Contents | Next |