source: branches/interpreter/interpreter2/interpreter.lpr

Last change on this file was 103, checked in by chronos, 8 years ago
  • Moved: All projects moved to subfolders for better maintenance of multiple projects.
File size: 804 bytes
Line 
1program interpreter;
2
3function IsWhiteSpace(C: Char): Boolean;
4begin
5 Result := (C = ' ') or (C = #13) or (C = #10) or (C = #9);
6end;
7
8procedure ShowError(Text: string);
9begin
10 WriteLn(Text);
11 Halt;
12end;
13
14function ReadChar: Char;
15begin
16 if Eof then ShowError('Premature end of source');
17 Read(Result);
18end;
19
20function ReadNext: string;
21var
22 C: Char;
23begin
24 Result := '';
25 C := ReadChar;
26 while IsWhiteSpace(C) do C := ReadChar;
27 while not IsWhiteSpace(C) do begin
28 Result := Result + C;
29 C := ReadChar;
30 end;
31end;
32
33procedure Expect(Text: string);
34var
35 Next: string;
36begin
37 Next := ReadNext;
38 if Next <> Text then
39 ShowError('Expected ' + Text + ' but found ' + Next);
40end;
41
42begin
43 WriteLn('Start');
44 Expect('begin');
45 Expect('end.');
46 WriteLn('Finished');
47end.
48{
49|begin
50|end.
51}
Note: See TracBrowser for help on using the repository browser.