Oppolzer - Informatik / Stanford Pascal Compiler


Home       Lebenslauf       Schwerpunkte       Kenntnisse       Seminare       Kunden       Projekte       Produkte       Blog       Stanford Pascal       Kontakt

The Stanford Pascal Compiler / Evolution Steps

Back to Compiler main page

New types: CHAR (n), DECIMAL (n,m) and STRING (n) - aka VARCHAR (n)

Compiler version: 2018.01

Most Pascal compilers support variable length strings in some way. Pascal/VS has a STRING(n) type, which is similar to CHAR(n) VARYING of PL/1 (implemented as a varying string of CHAR with a 2 byte length field). This seemed OK for me, so I wanted to implement such a STRING type, too.

The first task was to allow types to have parameters. I thought about a DECIMAL type, too, and thought it would be best to allow two parameters in general, so that DECIMAL could be handled in the same way.

Then it came to my mind, that CHAR(n) would be nice - simply as an abbreviation for ARRAY [1..n] OF CHAR. This would then be the third type which could be used with parameters.

The method is as follows: as soon as a type with a parameter is encountered - say: CHAR (20) - a type is constructed which is derived from the appropriate standard type (in this case ARRAY [1..n] OF CHAR) and this new type is given an artificial, but unique name, which can technically never be chosen as a normal identifier. And then this new type is inserted in the type identifier list at the top procedure level, as if it were defined in the top procedure. If later the same type is specified again (e.g. another definition using CHAR (20)), the type is already there and all works well. Furthermore, all CHAR(n) types have been made compatible in the meantime in assignment etc. (shorter to longer ones, at least), so the different CHAR(n) types can be mixed without problems.

This IMO is very convenient for the users of the New Stanford Pascal Compiler and makes it almost as comfortable to use as the different PL/1 compilers of its time.

I did something similar to DECIMAL definitions; see next article.

The implementation of STRINGs (real varying character strings) was a much greater effort; this will be covered in some future articles. But here again: all STRING types are compatible, regardless of the length. When strings are assigned, assignments will always work, as long as the field length of the target string variable is sufficient. Strings are never truncated, like in Pascal/VS.

Example program to test some of the new variable types:


program TESTCHAR ( OUTPUT ) ; //************************************************* //$A+ //************************************************* const TESTKONST = 'Bernd ' 'Oppolzer' ; S_VS_1 = X'1b' '&l12D' X'0d0a' ; X = 15 ; Y = 1.2 ; type CHAR25 = array [ 1 .. 25 ] of CHAR ; CHAR30 = array [ 1 .. 30 ] of CHAR ; var CH : CHAR ; CH2 : CHAR ( 25 ) ; CH3 : CHAR ( 14 ) ; CH4 : CHAR ( 25 ) ; CH5 : CHAR25 ; CH6 : CHAR30 ; D1 : DECIMAL ( 7 ) ; D2 : DECIMAL ( 15 , 2 ) ; D3 : DECIMAL ( 7 ) ; D4 : DECIMAL ( 25 , 0 ) ; S1 : STRING ( 254 ) ; S2 : STRING ( 3000 ) ; V1 : VARCHAR ( 254 ) ; V2 : VARCHAR ( 3000 ) ; //************************************ // types with syntax errors: // // IFALSCH : INTEGER ( 2 ) ; // D3 : DECIMAL ; // S4 : STRING ; // V4 : VARCHAR ; // D5 : DECIMAL ( 7 , 13 ) ; // D6 : DECIMAL ( 50 , 0 ) ; // S3 : STRING ( 0 ) ; // V3 : VARCHAR ( 0 ) ; //************************************ TESTCP : -> CHAR ; procedure TESTWRITE ( X : CHAR ( 30 ) ) ; begin (* TESTWRITE *) WRITELN ( 'testwrite: x = ' , X ) ; end (* TESTWRITE *) ; procedure TESTWRITE2 ( X : CHAR30 ) ; begin (* TESTWRITE2 *) WRITELN ( 'testwrite2: x = ' , X ) ; end (* TESTWRITE2 *) ; begin (* HAUPTPROGRAMM *) CH := '''' ; WRITELN ( CH ) ; WRITELN ( 'sizeof string const = ' , SIZEOF ( S_VS_1 ) ) ; WRITELN ( 'const = ' , S_VS_1 ) ; WRITELN ( 'sizeof string const = ' , SIZEOF ( TESTKONST ) ) ; WRITELN ( 'const = ' , TESTKONST ) ; WRITELN ( 'sizeof integer const = ' , SIZEOF ( X ) , X ) ; WRITELN ( 'sizeof real const = ' , SIZEOF ( Y ) , Y ) ; WRITELN ( 'Tests neue char-Datentypen:' ) ; CH := 'A' ; WRITELN ( 'test1: ch = ' , CH ) ; CH3 := CH ; WRITELN ( 'test1: ch3 = ' , CH3 ) ; CH3 := TESTKONST ; CH2 := CH3 ; D2 := 1234.56 ; S1 := 'das ist ein String' ; CH5 := CH2 ; TESTWRITE ( CH2 ) ; TESTWRITE ( CH5 ) ; CH5 := 'das ist ein String' ; CH2 := CH5 ; TESTWRITE2 ( CH5 ) ; WRITELN ( 'test1: ch2 = ' , CH2 ) ; end (* HAUPTPROGRAMM *) .

Back to Compiler main page