10.3 The PRINT and ERROR Statements


10.3 The #PRINT and #ERROR Statements

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 text. Listing 10-1 provides the basic Hello World program written in the HLA compile time language.

Listing 10-1: The CTL "Hello World" Program.

start example
 program ctlHelloWorld; begin ctlHelloWorld;      #print( "Hello, World of HLA/CTL" ) end ctlHelloWorld; 
end example

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 run-time 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. 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. Example:

 const      pi := 3.14159;      charConst := 'c'; #print( "PI = ", pi, " CharVal=", CharConst ) 

The HLA #print statement is particularly invaluable for debugging CTL programs (because there is no debugger available for CTL code). 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 does not have any affect 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. Example:

 #error( "Statement must have exactly one operand" ) 

Like the #print statement, the #error statement does not end with a semicolon. Although #error only allows a 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.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net