next up previous contents
Next: Control Statements Up: Statements Previous: Procedure and Function

write and writeln Statements

 

The ACK Pascal compiler is structured in such a way that a write, writeln, read and readln statement containing more than one argument is compiled into several write or read statements with only one argument. For readln and writeln these statements are followed by a statement that only reads or writes the end-of-line. So for example the writeln statement:

    writeln('This writeln is compiled into ', 4, ' parts');

is compiled as if it were the following four statements:

    write('This writeln is compiled into ');
    write(4);
    write(' parts');
    writeln;

I only implemented write and writeln statements writing to standard output, so I will only discuss these here.

I wrote Java methods for all the kinds of write statements. These methods are all part of class pcio, which acts as a library. The following table contains all the kinds of write statements in Pascal with one argument, and the corresponding Java method and its arguments. The letters i, r, c, b and f stand respectively for the types integer or int, real, char, boolean and float.

In the Java source, all the Java methods writing an argument of a numeric type, except for the methods writing an argument of type float, call the method writing a string, with their argument converted to type String.

After the parameters are loaded on the operand stack, the method is invoked by the invokestatic instruction that is also used for normal procedure and function calls. As this instruction (and every other instruction to invoke a method) needs the index in the constant_pool[] of a CONSTANT_Methodref_info entry describing the method, these have to be created.

A CONSTANT_Methodref_info entry in the constant_pool[] has to be created for each of the Java methods in the pcio library. This means that for the complete library the following entries in the constant_pool[] have to be created:

In the present compiler all these entries are always added to the constant_pool[]. I did this for convenience reasons. It would be nicer to put the entries for a method in the constant_pool[] only if the method is used.


next up previous contents
Next: Control Statements Up: Statements Previous: Procedure and Function



mark@bottom.xs4all.nl