In our next program we declare variables of the 4 basic types defined in BETA: integer, real, char and boolean. Values are assigned to these variables and they are written out. The first version of our program is shown in Program 2.1.
ORIGIN '~beta/basiclib/betaenv' ---- program: descriptor ---- (*********************************************************** * * SimpleTypes1.bet: A program to show handling of simple types * * Note: this program will not compile * ***********************************************************) (# i,j,k: @integer; x,y,z: @real; c: @char; b1,b2: @boolean; do 111->i; 10->j -> k; i + 3 * j->k ; 3.1415->x; 10e5->y; 'X'->c; true->b1; newline; 'Printing out integers'->putline; ' i = '->puttext; i->putint; newline; ' j = '->puttext; j->putint; newline; ' k = '->puttext; k->putint; newline; newline; 'Printing out reals: '->putline; ' x = '->puttext; x->putreal; newline; ' y = '->puttext; y->putreal; newline; ' z = '->puttext; z->putreal; newline; newline; 'Now for a character, C="'->puttext; C->Put; '" and as a integer: '->puttext; C->putint; newline; newline; 'Printing out booleans: '->putline; ' B1 = '->puttext; b1->putboolean; newline; ' B2 = '->puttext; b2->putboolean; newline; newline; 'Now for something very C-like.'->putline; ' \'A\'+ 3->put: '->puttext; 'A' + 3->put; newline; #)
Again, one notes the initial fragment ORIGIN statement, a comment, and then the program.
The declarations are Pascal-like with the addition of the @ character. In BETA, the same declaration syntax will be used for types, variables, classes and procedures. In this context, the @ denotes a static variable declaration whereas a declaration without the @ corresponds to a type declaration.
The first few statements after the do show arithmetic and assignment. Arithmetic expressions follow convention; the usual operators (+,- ,* , / (or div) and mod) are provided. Assignment goes left to right following the arrow and multiple assignment is allowed.
The next few lines show the syntax of real, character and boolean constants. Note that characters use the same delimiters as text strings.
In the remainder of the program we do output. The procedures used are:
Characters are type compatible with integers and can be used interchangeably in expressions. An example of this is shown at the end of the program.
When we try to compile SimpleTypesWithErrors.bet, we get the following semantic error message:
putreal *****Name is not declared putboolean *****Name is not declared
There is also a complete listing of the program text with the same error messages in SimpleTypesWithErrors.lst to help localize the errors. In this case the error is due to the fact that putreal is not in the standard environment. putreal is in a library that must be included in the program [1]. Likewise putboolean is in the textUtils library.
The Mjølner System comes with a library supporting a wide range of input and output for numbers (integers, based integers, reals, etc.) called numberio. In order to print reals on the screen, this library must be included. A library is included using a fragment INCLUDE statement:
ORIGIN '~beta/basiclib/betaenv'; INCLUDE '~beta/basiclib/numberio'; INCLUDE '~beta/basiclib/textUtils'; ---- program: descriptor ---- (*********************************************************** * * SimpleTypes1.bet: A program to show handling of simple types * ***********************************************************) (# i,j,k: @integer; x,y,z: @real; c: @char; b1,b2: @boolean; do 111->i; 10->j -> k; i + 3 * j->k ; 3.1415->x; 10e5->y; 'X'->c; true->b1; newline; 'Printing out integers'->putline; ' i = '->puttext; i->putint; newline; ' j = '->puttext; j->putint; newline; ' k = '->puttext; k->putint; newline; newline; 'Printing out reals: '->putline; ' x = '->puttext; x->putreal; newline; ' y = '->puttext; y->putreal; newline; ' z = '->puttext; z->putreal; newline; newline; 'Now for a character, C="'->puttext; C->Put; '" and as a integer: '->puttext; C->putint; newline; newline; 'Printing out booleans: '->putline; ' B1 = '->puttext; b1->putboolean; newline; ' B2 = '->puttext; b2->putboolean; newline; newline; 'Now for something very C-like.'->putline; ' \'A\'+ 3->put: '->puttext; 'A' + 3->put; newline; #)
And the results are shown below.
Printing out integers i = 111 j = 10 k = 141 Printing out reals: x = 3.141500 y = 1000000.000000 z = 0.000000 Now for a character, C="X" and as a integer: 88 Printing out booleans: B1 = true B2 = false Now for something very C-like. 'A'+ 3->put: D
Libraries Tutorial | © 1994-2004 Mjølner Informatics |
[Modified: Thursday January 16th 2003 at 10:23]
|