This chapter describes the GAP programming language. It should allow you in principle to predict the result of each and every input. In order to know what we are talking about, we first have to look more closely at the process of interpretation and the various representations of data involved.
First we have the input to GAP, given as a string of characters. How those characters enter GAP is operating system dependent, e.g., they might be entered at a terminal, pasted with a mouse into a window, or read from a file. The mechanism does not matter. This representation of expressions by characters is called the external representation of the expression. Every expression has at least one external representation that can be entered to get exactly this expression.
The input, i.e., the external representation, is transformed in a process called reading to an internal representation. At this point the input is analyzed and inputs that are not legal external representations, according to the rules given below, are rejected as errors. Those rules are usually called the syntax of a programming language.
The internal representation created by reading is called either an expression or a statement. Later we will distinguish between those two terms, however now we will use them interchangeably. The exact form of the internal representation does not matter. It could be a string of characters equal to the external representation, in which case the reading would only need to check for errors. It could be a series of machine instructions for the processor on which GAP is running, in which case the reading would more appropriately be called compilation. It is in fact a tree--like structure.
After the input has been read it is again transformed in a process called evaluation or execution. Later we will distinguish between those two terms too, but for the moment we will use them interchangeably. The name hints at the nature of this process, it replaces an expression with the value of the expression. This works recursively, i.e., to evaluate an expression first the subexpressions are evaluated and then the value of the expression is computed according to rules given below from those values. Those rules are usually called the semantics of a programming language.
The result of the evaluation is, not surprisingly, called a value. The set of values is of course a much smaller set than the set of expressions; for every value there are several expressions that will evaluate to this value. Again the form in which such a value is represented internally does not matter. It is in fact a tree--like structure again.
The last process is called printing. It takes the value produced by the evaluation and creates an external representation, i.e., a string of characters again. What you do with this external representation is up to you. You can look at it, paste it with the mouse into another window, or write it to a file.
Lets look at an example to make this more clear. Suppose you type in the following string of 8 characters
1 + 2 * 3;
GAP takes this external representation and creates a tree like internal representation, which we can picture as follows
+ / \ 1 * / \ 2 3
This expression is then evaluated. To do this GAP first evaluates the
right subexpression 2*3
. Again to do this GAP first evaluates its
subexpressions 2 and 3. However they are so simple that they are their
own value, we say that they are self--evaluating. After this has been
done, the rule for *
tells us that the value is the product of the
values of the two subexpressions, which in this case is clearly 6.
Combining this with the value of the left operand of the +
, which is
self--evaluating too gives us the value of the whole expression 7. This
is then printed, i.e., converted into the external representation
consisting of the single character 7
.
In this fashion we can predict the result of every input when we know the syntactic rules that govern the process of reading and the semantic rules that tell us for every expression how its value is computed in terms of the values of the subexpressions. The syntactic rules are given in sections Lexical Structure, Symbols, Whitespaces, Keywords, Identifiers, and The Syntax in BNF, the semantic rules are given in sections Expressions, Variables, Function Calls, Comparisons, Operations, Statements, Assignments, Procedure Calls, If, While, Repeat, For, Functions, and the chapters describing the individual data types.
GAP 3.4.4