One of the common gripes about HiSoft Pascal Version 4 for the Timex/Sinclair 2068 is its lack of built-in operators for handling a dimensioned array of type CHAR, henceforth referred to as a string. The crux of the problem is that if a string is not completely filled by data or spaces, the remainder of the string is left filled with whatever garbage is in memory at the time. When a WRITE or WRITELN is called, the procedure attempts to print the entire contents of the string, which includes the garbage left in the unused portion of the string, plus all the data. This results in garbage characters being printed on the screen or printer if the system does not simply crash.(1)
Parser is a program which demonstrates how a record type — called “word” in this example — containing a string and an internal counter, which is used to keep track of the number of printable characters the string contains, can be constructed and used to solve this problem. Reading and writing information to and from this “word” type; however; also requires special modified versions of the procedures READ() and WRITE(), which in this program are called StringRead() and StringWrite().(2)
(1) HiSoft has released several updates of its ZX Spectrum Pascal program since version 4 was converted and released for the Timex/Sinclair 2068. The latest ZX Spectrum version, (version 1.7M), features a 51 column mode plus the string handling capabilities we are trying to emulate here.
(2) For those of you who have additional questions, please feel free to write me at 1402-1545 Alta Vista Drive, Ottawa, Ontario, Canada K1G 3P4, or by calling Sir Clive’s Castle BBS (8/1/N, 300/1200 baud) at (613)745-8838 and leaving your questions in the sysop’s message area. The source code for this program is also available from the BBS. Download file PARSER.PS.
Program Listing
1000 PROGRAM PARSER;
1010
1020
1030 {
1040 The object of this program is to accept a
1050 sentence from the user then to break the
1060 sentence into its component words and to
1070 display each word on a separate line.
1080
1090 Program by David Solly.
1100
1110 }
1120
1130
1140
1150 CONST
1160
1170 maxword = 15;
1180 maxsentence = 15;
1190 space = CHR(32);
1200 first = 1;
1210
1220
1230
1240 TYPE
1250
1260 string = PACKED ARRAY[1..maxword] OF CHAR;
1270
1280 word = RECORD
1290
1300 body : string;
1310 length : INTEGER
1311
1320 END;
1330
1340
1350 VAR
1360
1370 sentence : ARRAY[1..maxsentence] OF word;
1380 row, col, nextcol, count : INTEGER;
1390 demarker : BOOLEAN;
1400
1410
1420
1430 PROCEDURE SpaceTrap;
1440
1450 {
1460 Insures that there is ony 1 space between
1470 words
1480 }
1490
1500 BEGIN
1510
1520 REPEAT
1530
1540 READ(sentence[row].body[first])
1550
1560 UNTIL sentence[row].body[first] <> space
1565
1570 END;
1580
1590
1600
1610 PROCEDURE StringWrite(VAR phrase : word);
1620
1630 {
1640 Writes only the required length of each
1650 character string.
1660
1670 This is required when using 32 col. mode.
1680 }
1690
1700 VAR letter : INTEGER;
1710
1720 BEGIN
1730
1740 FOR letter := first TO phrase.length DO
1745
1750 WRITE(phrase.body[letter])
1755
1760 END; {Procedure StringWrite}
1770
1780
1790
1800 PROCEDURE StringRead;
1810
1820
1830 VAR I : INTEGER;
1840
1850 BEGIN
1860
1870 {
1880 Intitialize the variables
1890 }
1900
1910 count := 1;
1920 row := first;
1930 col := first;
1940 nextcol := col + 1;
1950 demarker := FALSE;
1960
1970 FOR I := first TO maxsentence DO
1980 sentence[I].length := 1;
1990
2000 WRITE('Type a sentence > ');
2010 READLN; {Clears the buffer of EOLN}
2020
2030 WHILE (NOT EOLN) AND (row < maxsentence) DO
2040
2050 BEGIN
2060
2070
2080
2090 READ(sentence[row].body[col]);
2100 IF sentence[row].body[first] = space THEN SpaceTrap;
2110 IF sentence[row].body[col] = space THEN
2120 demarker := TRUE;
2130
2140 IF (NOT demarker) AND (nextcol < maxword) THEN
2150
2160 BEGIN
2170
2180 col := col + 1;
2190 nextcol := nextcol + 1
2200
2210 END
2220
2230 ELSE
2231
2232 BEGIN
2240
2250 sentence[row].length := col;
2260 count := count + 1;
2270 row := row + 1;
2280 col := first;
2290 nextcol := col + 1;
2300 demarker := FALSE
2310
2320 END; {If...then...else}
2330
2340 IF EOLN THEN sentence[row].length := col - 1
2341
2342 {Accounts for the last word entered less the EOLN marker.}
2343
2350 END {While loop}
2360
2370 END; {Procedure StringRead}
2380
2390
2400
2410 PROCEDURE PrintItOut;
2420
2430 VAR
2440
2450 subsequent : INTEGER;
2460
2470 BEGIN
2480
2490 subsequent := first + 1;
2500
2510 WRITE('Parsing > ');
2520 StringWrite(sentence[first]);
2530 WRITELN;
2540
2550
2560
2570 IF count >= subsequent THEN
2580
2590 BEGIN
2600
2610 FOR row := subsequent TO count DO
2620
2630 BEGIN
2640
2650 WRITE(' ');
2660 StringWrite(sentence[row]);
2670 WRITELN
2680
2690 END
2700 END
2710
2720 END; {Procedure PrintItOut}
2730
2740
2750
2760 PROCEDURE SongAndDance;
2770
2780 BEGIN
2790
2800 PAGE;
2810 WRITELN(' Parser');
2811 WRITELN;
2820 WRITELN(' Program By David Solly');
2830 WRITELN;
2840 WRITELN(' The object of this program');
2845 WRITELN('is to accept a sentence from');
2850 WRITELN('the user then to break the');
2860 WRITELN('sentence down into its')
2865 WRITELN('component words and to display');
2870 WRITELN('each word on a separate line.');
2880 WRITELN;
2890 WRITELN;
2900
2910 END; {Procedure SongAndDance}
2920
2930
2940
2950 BEGIN {Main Program}
2960
2970 SongAndDance;
2980 StringRead;
2990 WRITELN;
3000 PrintItOut;
3010 WRITELN;
3020 WRITELN('End of Demonstration.')
3030
3040 END. {Main Program}
Ottawa, Canada, March 25, 1990.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.