|
|
|
The basic syntax of QDL is similar to C++. It's got statements, it's got blocks, it's got variables, it's got functions... all the standard stuff you'd expect to find in a general-purpose language. The language is not case sensitive, so the members of programming teams can now quit quibbling over the case of identifiers in their shared code. And it's free-form, so you get a lot of freedom to express your bizarre programming style. I'm not sure how to start describing the syntax, so why don't I just give you a code snippet to gloss over:
Uses System.Streams.Console;
Function Main (Args: @ String): Integer(0) Do
Index: Integer;
StdOut.Print "Hello World.\n";
StdOut.Print "Here is a list of the whole numbers from 1 to 10.\n";
For Index = 1; Index <= 10; Index++ Do {
StdOut.Print Index, " ";
}
End;
Output:
Hello World. Here is the number ten displayed in three different formats: 10 0xA 1010b
Now let's dissect this program bit-by-bit.
Uses System.Streams.Console;
This line is comparable to C's #include preprocessor directive. Uses tells the compiler that the module wants to use the namespace called System.Streams.Console; the compiler maintains a database of classes and namespaces so that it knows where to find the one you ask for. Uses can also be told to use the declarations in a specific file; however, in this case it pays attention only to the declarations in the other file; it does not actually behave as though the other file was included verbatim. Thus, Uses is closer to Pascal's uses than C's #include.
Note: The QDL statement Compile is equivalent to the C #include statement: it reads in the file specified in full, compiling it as if it were part of the current file.
Most lines, like this one, end with a semicolon (;); the semicolon typically marks the end of a statement. Since QDL is a free-form language, you can stretch out a statement into multiple lines. For example, this line could have been expanded to several lines:
Uses System.Streams .Console;
For this reason, the semicolon is needed to tell the compiler when the statement is really over.
Function Main (Args: @ String): Integer(0) Do
This line declares a function called Main with no arguments, and returning an Integer. As in C, Main is a special function name that determines the program's entry point. Also, as in C, exiting the Main function terminates the program.
First, some terminology. Procedural languages often differ on the names given to a program's subroutines. Pascal has Procedures and Functions, BASIC has Subs and Functions, and C has "functions" (although the language itself really doesn't call its subroutines anything, since there is no function keyword.) It took some deliberation to decide on this, but QDL subroutines are called Functions regardless of whether they return a value.
The way arguments are declared is similar to Pascal, and consists of zero or more variable specifications. A variable specification is a comma-separated list of identifiers (variable names), followed by a colon (:), followed by a data type specification. The data type specification requires quite a bit of explanation in itself, so I won't elaborate here. Suffice it to say that Args: @ String declares a variable called Args that is a reference to a string. You can use this to access the program's complete command line.
The return specification (in this case, : Integer (0)) consists of a colon (:) followed by a data type specification. In this case, the function returns an Integer. The (0) afterward gives the compiler a default return value. This is the value that is returned if you don't give another value to return later. Thus, the compiler doesn't complain that there is no return value given in the body of the function.
Do and End; delimit the body of the function. Notice that there is no semicolon after Do--that's because the Function statement doesn't techically end until End; is reached.
The line
Index: Integer;
declares an integer variable named Index. This variable is used later to hold numbers from 1 to 10.
StdOut.Print "Hello World.\n"; StdOut.Print "Here is a list of the whole numbers from 1 to 10.\n";
These two lines call the function named StdOut.Print. StdOut.Print outputs text to the standard output stream--that is, the screen. After StdOut.Print is an argument to the function, which tells it just what to print. Notice that, as in Basic, there are no brackets around the argument to the function; when a function has no return value, or when the return value is disregarded, brackets are unnecessary.
Inside the quotes, the sequence \n has a special meaning in QDL, just like in C: it represents a newline character, which causes printing thereafter to start on the next line.
For Index = 1; Index <= 10; Index++ Do {
This is the beginning of a For loop. It's a bit complicated to explain how it works just yet, but hopefully most of the people reading this already know. Basically, it executes the contents of the braces ({ }) over and over, with a new value of Index each time. Index starts at 1, and the loop exits when Index exceeds 10.
The contents of the braces are:
StdOut.Print Index, " ";
Which just prints the current value of Index, then a blank space (" ").
The closing brace, (}), is a counterpart to the opening brace ({), and marks the end of the For loop. Like in C, you don't need the braces if your loop contains only one statement, but I put them in anyway... just to let the C people know, yes, there are braces in QDL.
I know it will be difficult for some of you C coders to write Do...End; instead of { } to delimit your functions. Give it a rest; it's not such a hardship.
Braces are actually used for most things--array initializations, loops, if's, and so on use braces. There are just three statements that use that End thing instead: Function (and function-like constructs, such as operator overloads), Class, and Implement.
Oh, and you Pascal programmers: I know it will be difficult for you to write { } sometimes instead of do...end. Give it a rest; it's not such a hardship. And don't tell me you'll get blocks of code confused with comments either.
And then there're you language purists: you want consistency, don't you? I know what you're thinking: I should either use braces all the time, or End all the time. There is a specific reason for this inconsistency, as explained two sections further (Basic Elements).
This concludes my highly incomplete syntax tutorial. Now that you have the basic idea, check out the various reference sections for information about other parts of the language. Click Next for a handy language comparison.
| Table of Contents | Qwertie's Site/Mirror |