JRT Pascal User's Guide version 3.0 NOT FOR SALE -19- 4. Data Types Pascal is a language rich in data types. Unlike Basic, which provides only two or three data types, Pascal provides eight: integers, real numbers, Booleans, characters, structured variables, sets, pointers, and dynamic strings. These forms can be combined in records and arrays to form data aggregates that closely relate to the application area. Records and arrays can contain other records and arrays and pointers with no restrictions on nesting or even on recursive definitions. It is these features that set Pascal apart from earlier languages like Cobol, Fortran, PL/I. Pascal recognizes the importance of powerful facilities for describing the data in a program as well as the active statements. 4.1 Integers Integers or whole numbers occupy two bytes. They are represented in twos complement format. The range is -32768 to +32767. Integer literals in the source program and in console or disk input may be entered as hex values. Standard Intel hex format is used. The last character must be an `H'. A leading zero is required if the first digit is A, B, C, D, E, or F. 1ah +0C35H -0ffh 0c00h 1234H 4.2 Real numbers Real numbers have 14 digits and are expressed in floating point format. The exponent range is from -64 to +63. The exponent field is not required in the source program or input, but when present it must be entered in a fixed format. The exponent format is `e+00' or `e-00'. (see NOTES section 2.1.2). Copy compliments of Merle Schnick SECTION 4: Data Types JRT Pascal User's Guide version 3.0 NOT FOR SALE -20- 32.01e+04 1.075 -3.14159 -1234567.8901234E-47 In source programs, the decimal point must be included to distinguish real numbers from integers. (NOTE: This version of Pascal requires that at least one digit is entered to the RIGHT and LEFT of the decimal point. i.e., 4. and .4 will produce errors where 4.0 and 0.4 will not). 4.3 Booleans Boolean variables may have only two values: TRUE or FALSE. Booleans may be used directly in output statements but should NOT be used directly in input statements. 4.4 Char The char data type is one character. Packed char fields are not meaningful on 8-bit microcomputers and are not supported. The ASCII character set is used in JRT Pascal. 4.5 Structured variables Structured variables are records or arrays which are treated as aggregates. For example: a record of one type could be compared directly against a record of another type. Structured variables may be compared (all six operators), assigned, input/output, concatenated, used as parameters and function return values without restriction. In addition to the CONCAT builtin function, the `+' operator indicates concatenation of structured variables or dynamic strings. Structured variables to be compared may have different lengths. The result is determined as if the shorter one is extended by spaces. In assigning structured variables of different lengths, if the receiving field is shorter then truncation will occur. If the receiving field is longer then the remainder of it is padded with spaces. Copy compliments of Merle Schnick SECTION 4: Data Types JRT Pascal User's Guide version 3.0 NOT FOR SALE -21- Arrays of type char constitute fixed length strings. Unlike dynamic strings, these have no (hidden) two byte length prefix. Arrays of fixed length strings are useful for many type of text processing. TYPE CHAR100 = ARRAY [1..100] OF CHAR; TABLE = ARRAY [1..40] OF CHAR100; VAR T : TABLE; BEGIN T:= ' '; (*CLEARS ENTIRE TABLE*) T[1,8] := '*'; (*STORE 1 CHARACTER *) T[15] := 'JRT Pascal is the best'; ... END; 4.6 Dynamic strings Dynamic strings are an extension to standard Pascal. A hidden two-byte prefix on the string contains the string's current length in bytes. JRT Pascal dynamic strings may be up to 64k bytes in length. Of course the computer's main storage size restricts the size to a smaller value. Other Pascals limit strings to 255 bytes. The maximum size of a string variable is declared with the variable definition. If no size is specified, the default is 80 bytes. VAR S1 : STRING; S2 : STRING[4000]; S3 : STRING[12]; Dynamic strings may be used in the same way as structured variables: comparisons, assignment, input/output, parameters, and function return values. Copy compliments of Merle Schnick SECTION 4: Data Types JRT Pascal User's Guide version 3.0 NOT FOR SALE -22- NOTE - Dynamic string variables may NOT be used in READ statements directed to files, only to the console. To read string data from files, fixed strings (arrays of characters) must be used. The individual characters of a string may be accessed and updated. If an attempt is made to access an element of a string beyond the current length of the string, a run-time error occurs. S1[4] := 'X'; WRITELN( S2[1500] ); S1[J] := S1[J+1]; S3[1] := UPCASE( S3[1] ); Several builtin procedures and functions are available to enhance string processing. Refer to the sections on builtin functions and on builtin procedure for complete descriptions. name purpose ---- --------- CONCAT concatenate n strings COPY extract portion of string DELETE delete portion of string INSERT insert a string into another LENGTH return current string size POS search string for a pattern 4.7 Sets Set variables occupy 16 bytes. The entire ASCII caracter set may by represented in the 128 bits. LOW_CASE := ['a'..'z']; UP_CASE := ['A'..'Z']; NUMERIC := ['0'..'9']; ALPHAMERIC := LOW_CASE + UP_CASE + NUMERIC; ALPHABETIC := ALPHAMERIC - NUMERIC IF NOT (INPUT_CHAR IN ALPHAMERIC) THEN WRITELN('INVALID INPUT CHAR'); Copy compliments of Merle Schnick SECTION 4: Data Types JRT Pascal User's Guide version 3.0 NOT FOR SALE -23- NOTE - Set variables have no meaningful format in text format input/output. Sets may be input/output to disk files which are opened for binary format processing. 4.8 Pointers Pointers contain the virtual address of dynamic variables created by the NEW procedure and of ghost variables created by the MAP procedure. Pointers are two bytes in size. The value stored in a pointer variable is NOT the actual address of the dynamic variable - it is the virtual address. The actual address of a dynamic variable may be obtained with the ADDR builtin function. ACTUAL_ADDRESS := ADDR( PTR^ ); Note that the actual address of a dynamic variable may change during program execution, but the virtual address is fixed for the life of the variable. 4.9 Dynamic arrays Dynamic arrays are a JRT extension to the Pascal language. Arrays are a widely used device for storing and retrieving logically identical data elements. Often it is not known in advance how many data elements will be processed - thus it is necessary to create arrays to hold the maximum number of elements that ever may be processed. With dynamic arrays, the array's actual size need not be "hard-coded" into the source program. The array size may vary with each run of the program or even at different times within the same run. Copy compliments of Merle Schnick SECTION 4: Data Types JRT Pascal User's Guide version 3.0 NOT FOR SALE -24- In some programs, dynamic arrays can greatly improve storage use efficiency. This implies that the program can operate over a much wider range of situations. IMPORTANT - Dynamic arrays MUST be actual variables - they may NOT be elements of other arrays or fields of record variables. Files of dynamic arrays are not allowed. Declaring dynamic arrays The declarations of dynamic arrays in either the TYPE or VAR sections is identical to static arrays except that the indexes are not specified as subranges. The indexes must be specified as either the reserved word INTEGER or CHAR. No other index declaration is allowed in dynamic arrays. Static and dynamic indexes may not be mixed in the same array declaration. TYPE MATRIX = ARRAY [ INTEGER, INTEGER ] OF REAL; VAR M : MATRIX; TABLE : ARRAY [ CHAR ] OF STRING [20]; INDEX : ARRAY [ INTEGER, CHAR ] OF INTEGER; Allocating and deallocating dynamic arrays A dynamic array may not be referenced until it has been allocated. Doing so would cause a run-time error. Allocation accomplishes two purposes: 1. establish the dynamic arrays currnet lower and upper index bounds for each dimension. 2. allocate storage for the dynamic array in dynamic storage. Current bounds are stored in an array control block (ACB) which also contains an allocation flag, dimension count, and the virtual address of the dynamic array. A builtin procedure performs the allocation operation. ALLOCATE ( dyn_array_variable [ subrange_expr1,... subrange_exp_n ] ); Copy compliments of Merle Schnick SECTION 4: Data Types JRT Pascal User's Guide version 3.0 NOT FOR SALE -25- Note that an ALLOCATE must be used for each array VARIABLE declared, NOT for aaray TYPES. ALLOCATE ( M [1..10, 0..50] ); ALLOCATE ( TABLE ['A'..'M'] ); ALLOCATE ( INDEX [I..I+10, CHAR1..CHAR2] ); The bounds of a dynamic array may be changed by executing another ALLOCATE with different parameters. The data stored in a dynamic array is lost when it is reallocated. Dynamic arrays follow the standard Pascal rules for scope of reference. They remain allocated until they are explicitly deallocated. Since dynamic arrays use storage, they should be deallocated when they are no longer needed. DEALLOCATE ( dyn_array_variable ); Examples: DEALLOCATE ( M ); DEALLOCATE ( TABLE ); DEALLOCATE ( INDEX ); Dynamic arrays declared and allocated within a procedure are not automatically deallocated on the termination of that procedure. Programming Notes: 1. Dynamic arrays may not be referenced as structures. Only elements of dynamic arrays may be referenced in programs. 2. FILLCHAR should not be used to initialize dynamic arrays. 3. Dynamic arrays should always be DEALLOCATED before being reallocated to a different size 4. Full file variables now supported. File variables may be used as reference parameters (indicated by VAR) but should NOT be used as value parameters. (see section 7.) Copy compliments of Merle Schnick SECTION 4: Data Types iables may be used as reference parameters (indicated by VAR) but should NOT be used as value parameters. (see section 7.) Copy compliments of Merle Sch