You may recall that Chapter 1 began with the typical first program most people write when learning a new language, the "Hello, world!" program. It is only fitting for this chapter to present that same program when discussing the second language of this book. Example 9-1 provides the basic "Hello, world!" program written in the HLA compile-time language.
Example 9-1. The CTL "Hello, world!" program
program ctlHelloWorld; begin ctlHelloWorld; #print( "Hello, World of HLA/CTL" ) end ctlHelloWorld;
The only CTL statement in this program is the #print
statement. The remaining lines are needed just to keep the compiler happy (though we could have reduced the overhead to two lines by using a unit
rather than a program
declaration).
The #print
statement displays the textual representation of its argument list during the compilation of an HLA program. Therefore, if you compile the program above with the command hla ctlHW.hla
, the HLA compiler will immediately print the text:
Hello, World of HLA/CTL
Note that there is a big difference between the following two statements in an HLA source file:
#print( "Hello World" ) stdout.puts( "Hello World" nl );
The first statement prints Hello World
(and a new line) during the compilation process. This first statement does not have any effect on the executable program. The second line doesn't affect the compilation process (other than the emission of code to the executable file). However, when you run the executable file, the second statement prints the string Hello World
followed by a newline sequence.
The HLA/CTL #print
statement uses the following basic syntax:
#print( list_of_comma_separated_constants
)
Note that a semicolon does not terminate this statement. Semicolons terminate runtime statements; they generally do not terminate compile-time statements (there is one big exception, as you will see a little later).
The #print
statement must have at least one operand; if multiple operands appear in the parameter list, you must separate each operand with a comma ( just like stdout.put
). If a particular operand is not a string constant, HLA will translate that constant to its corresponding string representation and print that string. Here's an example:
#print( "A string Constant ", 45, ' ', 54.9, ' ', true )
You may specify named symbolic constants and constant expressions. However, all #print
operands must be constants (either literal constants or constants you define in the const
or val
sections), and those constants must be defined before you use them in the #print
statement. For example:
const pi := 3.14159; charConst := 'c'; #print( "PI = ", pi, " CharVal=", charConst )
The HLA #print
statement is particularly invaluable for debugging CTL programs. This statement is also useful for displaying the progress of the compilation and displaying assumptions and default actions that take place during compilation. Other than displaying the text associated with the #print
parameter list, the #print
statement has no effect on the compilation of the program.
The #error
statement allows a single-string constant operand. Like #print
, this statement will display the string to the console during compilation. However, the #error
statement treats the string as an error message and displays the string as part of an HLA error diagnostic. Further, the #error
statement increments the error count, and this will cause HLA to stop the compilation (without assembling or linking) after processing the current source file. You would normally use the #error
statement to display an error message during compilation if your CTL code discovers something that prevents it from creating valid code. For example:
#error( "Statement must have exactly one operand" )
Like the #print
statement, the #error
statement does not end with a semicolon. Although #error
allows only a single-string operand, it's very easy to print other values by using the compile-time string concatenation operator and several of the HLA built-in compile-time functions. You'll learn about these a little later in this chapter.