1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
program calc1;
{$H+}
uses
typinfo, sysutils, character;
type
TokenType = (TT_Integer, TT_Plus, TT_Minus, TT_Asterisk, TT_Slash, TT_Eof,
TT_LParen, TT_RParen);
Token = class
TokenType: TokenType;
constructor Create(Type_: TokenType);
function ToStr: String; virtual;
end;
TokenInteger = class(Token)
Val: Integer;
constructor Create(Value_: Integer);
function ToStr: String; override;
end;
TokenPlus = class(Token)
constructor Create;
end;
TokenMinus = class(Token)
constructor Create;
end;
TokenEof = class(Token)
constructor Create;
end;
TLexer = class
Text: AnsiString;
CurPos: Integer;
constructor Create(Text_: AnsiString);
procedure Error;
procedure SkipWhitespace;
function AtEnd: Boolean;
function CurChar: Char;
function GetInteger: Integer;
function GetNextToken: Token;
end;
TInterpreter = class
Lexer: TLexer;
CurrentToken: Token;
constructor Create(Lexer_: TLexer);
procedure Error;
procedure Eat(T: TokenType);
function Factor: Integer;
function Expr: Integer;
function Term: Integer;
end;
constructor Token.Create(Type_: TokenType);
begin
inherited Create;
TokenType := Type_;
end;
function Token.ToStr: String;
begin
Result := GetEnumName(TypeInfo(TokenType), Ord(Self.TokenType));
end;
constructor TokenInteger.Create(Value_: Integer);
begin
inherited Create(TT_Integer);
Val := Value_;
end;
function TokenInteger.ToStr: String;
begin
Result := inherited;
Result := Result + '(' + IntToStr(Val) + ')';
end;
constructor TokenPlus.Create;
begin
inherited Create(TT_Plus);
end;
constructor TokenMinus.Create;
begin
inherited Create(TT_Minus);
end;
constructor TokenEof.Create;
begin
inherited Create(TT_Eof);
end;
constructor TLexer.Create(Text_: String);
begin
inherited Create;
Text := Text_;
CurPos := 1;
end;
procedure TLexer.Error;
begin
Raise Exception.Create('invalid input');
end;
procedure TLexer.SkipWhitespace;
begin
while (not AtEnd) and IsWhiteSpace(CurChar) do
Inc(CurPos);
end;
function TLexer.AtEnd: Boolean;
begin
Result := CurPos > Length(Text);
end;
function TLexer.CurChar: Char;
begin
Result := Text[CurPos];
end;
function TLexer.GetInteger: Integer;
var
Start: Integer;
begin
Start := CurPos;
while (not AtEnd) and IsDigit(CurChar) do
Inc(CurPos);
Result := StrToInt(Copy(Text, Start, CurPos - Start));
end;
function TLexer.GetNextToken: Token;
begin
SkipWhitespace;
if AtEnd then
begin
Result := TokenEof.Create;
Exit;
end;
if IsDigit(CurChar) then
begin
Result := TokenInteger.Create(GetInteger);
end
else if CurChar = '+' then
begin
Result := TokenPlus.Create;
Inc(CurPos);
end
else if CurChar = '-' then
begin
Result := TokenMinus.Create;
Inc(CurPos);
end
else if CurChar = '*' then
begin
Result := Token.Create(TT_Asterisk);
Inc(CurPos);
end
else if CurChar = '/' then
begin
Result := Token.Create(TT_Slash);
Inc(CurPos);
end
else if CurChar = '(' then
begin
Result := Token.Create(TT_LParen);
Inc(CurPos);
end
else if CurChar = ')' then
begin
Result := Token.Create(TT_RParen);
Inc(CurPos);
end
else
Error;
end;
constructor TInterpreter.Create(Lexer_: TLexer);
begin
Lexer := Lexer_;
CurrentToken := Lexer.GetNextToken;
end;
procedure TInterpreter.Error;
begin
Raise Exception.Create('syntax error');
end;
procedure TInterpreter.Eat(T: TokenType);
begin
if CurrentToken.TokenType = T then
begin
CurrentToken := Lexer.GetNextToken;
end
else
Error;
end;
function TInterpreter.Factor: Integer;
var
T: Token;
begin
T := CurrentToken;
if T.TokenType = TT_Integer then
begin
Eat(TT_Integer);
Result := TokenInteger(T).Val;
end
else if T.TokenType = TT_LParen then
begin
Eat(TT_LParen);
Result := Expr;
Eat(TT_RParen);
end
else
Error;
end;
function TInterpreter.Expr: Integer;
var
Tok: Token;
begin
// expr : term ((PLUS|MINUS) term)*
// term : factor ((MUL|DIV) factor)*
// factor : INTEGER | LPAREN expr RPAREN
Result := Term;
while CurrentToken.TokenType in [TT_Plus, TT_Minus] do
begin
Tok := CurrentToken;
if Tok.TokenType = TT_Plus then
begin
Eat(TT_Plus);
Result := Round(Result + Term);
end
else if Tok.TokenType = TT_Minus then
begin
Eat(TT_Minus);
Result := Round(Result - Term);
end;
end;
end;
function TInterpreter.Term: Integer;
var
Tok: Token;
begin
Result := Factor;
while CurrentToken.TokenType in [TT_Asterisk, TT_Slash] do
begin
Tok := CurrentToken;
if Tok.TokenType = TT_Asterisk then
begin
Eat(TT_Asterisk);
Result := Result * Factor;
end
else if Tok.TokenType = TT_Slash then
begin
Eat(TT_Slash);
Result := Round(Result / Factor);
end;
end;
end;
var
Lexer: TLexer;
Interp: TInterpreter;
Line: String;
I: Integer;
procedure InterpString(S: String);
begin
Lexer := TLexer.Create(S);
Interp := TInterpreter.Create(Lexer);
WriteLn(Interp.Expr);
FreeAndNil(Interp);
end;
procedure InputLoop;
begin
while True do
begin
Write('calc> ');
if Eof(Input) then break;
ReadLn(Line);
if Length(Line) = 0 then continue;
InterpString(Line);
end;
end;
begin
if ParamCount > 0 then
begin
Line := '';
for I := 1 to ParamCount do
begin
Line := Line + ParamStr(I);
end;
InterpString(Line);
end
else
InputLoop;
end.
|