1 | unit USourceCode;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Contnrs;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TSourceCommand = class
|
---|
12 | Parent: TSourceCommand;
|
---|
13 | end;
|
---|
14 |
|
---|
15 | TSourceCommands = class(TObjectList)
|
---|
16 | end;
|
---|
17 |
|
---|
18 | TSourceReference = class
|
---|
19 | end;
|
---|
20 |
|
---|
21 | TSourceReferences = class(TObjectList)
|
---|
22 | end;
|
---|
23 |
|
---|
24 | { TSourceValue }
|
---|
25 |
|
---|
26 | TSourceValue = class
|
---|
27 | procedure Assign(Source: TSourceValue); virtual;
|
---|
28 | constructor Create; virtual;
|
---|
29 | end;
|
---|
30 |
|
---|
31 | TSourceValues = class(TObjectList)
|
---|
32 | end;
|
---|
33 |
|
---|
34 | TSourceValueClass = class of TSourceValue;
|
---|
35 |
|
---|
36 | { TSourceType }
|
---|
37 |
|
---|
38 | TSourceType = class
|
---|
39 | Name: string;
|
---|
40 | function GetValueType: TSourceValueClass; virtual;
|
---|
41 | end;
|
---|
42 |
|
---|
43 | TSourceTypeClass = class of TSourceType;
|
---|
44 |
|
---|
45 | { TSourceTypes }
|
---|
46 |
|
---|
47 | TSourceTypes = class(TObjectList)
|
---|
48 | Parent: TSourceTypes;
|
---|
49 | function AddNew(Name: string; ClassType: TSourceTypeClass): TSourceType;
|
---|
50 | function Search(Name: string): TSourceType;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | { TSourceTypeInteger }
|
---|
54 |
|
---|
55 | TSourceTypeInteger = class(TSourceType)
|
---|
56 | function GetValueType: TSourceValueClass; override;
|
---|
57 | end;
|
---|
58 |
|
---|
59 | { TSourceTypeString }
|
---|
60 |
|
---|
61 | TSourceTypeString = class(TSourceType)
|
---|
62 | function GetValueType: TSourceValueClass; override;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | { TSourceTypeArray }
|
---|
66 |
|
---|
67 | TSourceTypeArray = class(TSourceType)
|
---|
68 | ItemType: TSourceType;
|
---|
69 | function GetValueType: TSourceValueClass; override;
|
---|
70 | end;
|
---|
71 |
|
---|
72 |
|
---|
73 | { TSourceVariable }
|
---|
74 |
|
---|
75 | TSourceVariable = class
|
---|
76 | Name: string;
|
---|
77 | ValueType: TSourceType;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | { TSourceVariables }
|
---|
81 |
|
---|
82 | TSourceVariables = class(TObjectList)
|
---|
83 | Parent: TSourceVariables;
|
---|
84 | function AddNew(Name: string; ValueType: TSourceType): TSourceVariable;
|
---|
85 | function Search(Name: string): TSourceVariable;
|
---|
86 | end;
|
---|
87 |
|
---|
88 | TSourceReferenceVariable = class(TSourceReference)
|
---|
89 | Variable: TSourceVariable;
|
---|
90 | end;
|
---|
91 |
|
---|
92 | TSourceReferenceArray = class(TSourceReference)
|
---|
93 | ArrayRef: TSourceVariable;
|
---|
94 | Index: TSourceReference;
|
---|
95 | end;
|
---|
96 |
|
---|
97 | { TSourceValueString }
|
---|
98 |
|
---|
99 | TSourceValueString = class(TSourceValue)
|
---|
100 | Value: string;
|
---|
101 | procedure Assign(Source: TSourceValue); override;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | { TSourceValueInteger }
|
---|
105 |
|
---|
106 | TSourceValueInteger = class(TSourceValue)
|
---|
107 | Value: Integer;
|
---|
108 | procedure Assign(Source: TSourceValue); override;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | { TSourceValueArray }
|
---|
112 |
|
---|
113 | TSourceValueArray = class(TSourceValue)
|
---|
114 | Items: TSourceValues;
|
---|
115 | procedure Assign(Source: TSourceValue); override;
|
---|
116 | constructor Create; override;
|
---|
117 | destructor Destroy; override;
|
---|
118 | end;
|
---|
119 |
|
---|
120 | { TSourceConstant }
|
---|
121 |
|
---|
122 | TSourceConstant = class
|
---|
123 | Name: string;
|
---|
124 | Value: TSourceValue;
|
---|
125 | destructor Destroy; override;
|
---|
126 | end;
|
---|
127 |
|
---|
128 | TSourceParameterKind = (pkString, pkVariable, pkType);
|
---|
129 |
|
---|
130 | TSourceFunctionParameter = class
|
---|
131 | Name: string;
|
---|
132 | Kind: TSourceParameterKind;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | TSourceFunctionParameters = class(TObjectList)
|
---|
136 | end;
|
---|
137 |
|
---|
138 | { TSourceFunction }
|
---|
139 |
|
---|
140 | TSourceFunction = class
|
---|
141 | Name: string;
|
---|
142 | Parameters: TSourceFunctionParameters;
|
---|
143 | procedure AddParameter(Name: string; Kind: TSourceParameterKind);
|
---|
144 | constructor Create;
|
---|
145 | destructor Destroy; override;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | { TSourceFunctions }
|
---|
149 |
|
---|
150 | TSourceFunctions = class(TObjectList)
|
---|
151 | function AddNew(Name: string): TSourceFunction;
|
---|
152 | function Search(Name: string): TSourceFunction;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | { TSourceConstants }
|
---|
156 |
|
---|
157 | TSourceConstants = class(TObjectList)
|
---|
158 | Parent: TSourceConstants;
|
---|
159 | function AddNewString(Value: string; Name: string = ''): TSourceConstant;
|
---|
160 | function AddNewInteger(Value: Integer; Name: string = ''): TSourceConstant;
|
---|
161 | function Search(Name: string): TSourceConstant;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | TSourceReferenceConstant = class(TSourceReference)
|
---|
165 | Constant: TSourceConstant;
|
---|
166 | end;
|
---|
167 |
|
---|
168 | { TCommandFunctionCall }
|
---|
169 |
|
---|
170 | TCommandFunctionCall = class(TSourceCommand)
|
---|
171 | Name: string;
|
---|
172 | Parameters: TSourceReferences;
|
---|
173 | constructor Create;
|
---|
174 | destructor Destroy; override;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | TSourceCode = class;
|
---|
178 |
|
---|
179 | { TCommandBeginEnd }
|
---|
180 |
|
---|
181 | TCommandBeginEnd = class(TSourceCommand)
|
---|
182 | SourceCode: TSourceCode;
|
---|
183 | Commands: TSourceCommands;
|
---|
184 | constructor Create;
|
---|
185 | destructor Destroy; override;
|
---|
186 | end;
|
---|
187 |
|
---|
188 | { TCommandIfEqual }
|
---|
189 |
|
---|
190 | TCommandIfEqual = class(TSourceCommand)
|
---|
191 | Reference1: TSourceReference;
|
---|
192 | Reference2: TSourceReference;
|
---|
193 | destructor Destroy; override;
|
---|
194 | end;
|
---|
195 |
|
---|
196 | { TCommandIfNotEqual }
|
---|
197 |
|
---|
198 | TCommandIfNotEqual = class(TSourceCommand)
|
---|
199 | Reference1: TSourceReference;
|
---|
200 | Reference2: TSourceReference;
|
---|
201 | destructor Destroy; override;
|
---|
202 | end;
|
---|
203 |
|
---|
204 | { TCommandRepeat }
|
---|
205 |
|
---|
206 | TCommandRepeat = class(TSourceCommand)
|
---|
207 | Command: TSourceCommand;
|
---|
208 | destructor Destroy; override;
|
---|
209 | end;
|
---|
210 |
|
---|
211 | TCommandBreak = class(TSourceCommand)
|
---|
212 | end;
|
---|
213 |
|
---|
214 | TCommandContinue = class(TSourceCommand)
|
---|
215 | end;
|
---|
216 |
|
---|
217 | { TSourceCode }
|
---|
218 |
|
---|
219 | TSourceCode = class
|
---|
220 | private
|
---|
221 | procedure InitFunctions;
|
---|
222 | public
|
---|
223 | Functions: TSourceFunctions;
|
---|
224 | Types: TSourceTypes;
|
---|
225 | Variables: TSourceVariables;
|
---|
226 | Constants: TSourceConstants;
|
---|
227 | Main: TCommandBeginEnd;
|
---|
228 | constructor Create;
|
---|
229 | destructor Destroy; override;
|
---|
230 | end;
|
---|
231 |
|
---|
232 |
|
---|
233 | implementation
|
---|
234 |
|
---|
235 | { TCommandIfNotEqual }
|
---|
236 |
|
---|
237 | destructor TCommandIfNotEqual.Destroy;
|
---|
238 | begin
|
---|
239 | Reference1.Free;
|
---|
240 | Reference2.Free;
|
---|
241 | inherited Destroy;
|
---|
242 | end;
|
---|
243 |
|
---|
244 | { TSourceType }
|
---|
245 |
|
---|
246 | function TSourceType.GetValueType: TSourceValueClass;
|
---|
247 | begin
|
---|
248 | Result := nil;
|
---|
249 | end;
|
---|
250 |
|
---|
251 | { TSourceTypeInteger }
|
---|
252 |
|
---|
253 | function TSourceTypeInteger.GetValueType: TSourceValueClass;
|
---|
254 | begin
|
---|
255 | Result := TSourceValueInteger;
|
---|
256 | end;
|
---|
257 |
|
---|
258 | { TSourceTypeString }
|
---|
259 |
|
---|
260 | function TSourceTypeString.GetValueType: TSourceValueClass;
|
---|
261 | begin
|
---|
262 | Result := TSourceValueString;
|
---|
263 | end;
|
---|
264 |
|
---|
265 | { TSourceTypeArray }
|
---|
266 |
|
---|
267 | function TSourceTypeArray.GetValueType: TSourceValueClass;
|
---|
268 | begin
|
---|
269 | Result := TSourceValueArray;
|
---|
270 | end;
|
---|
271 |
|
---|
272 | { TSourceValueArray }
|
---|
273 |
|
---|
274 | procedure TSourceValueArray.Assign(Source: TSourceValue);
|
---|
275 | var
|
---|
276 | I: Integer;
|
---|
277 | Value: TSourceValue;
|
---|
278 | begin
|
---|
279 | if Source is TSourceValueInteger then begin
|
---|
280 | while Items.Count < TSourceValueArray(Source).Items.Count do begin
|
---|
281 | Value := TSourceValue(TSourceValue(TSourceValueArray(Source).Items[Items.Count]).ClassType.Create);
|
---|
282 | Items.Add(Value);
|
---|
283 | end;
|
---|
284 | while Items.Count > TSourceValueArray(Source).Items.Count do begin
|
---|
285 | Items.Delete(Items.Count - 1);
|
---|
286 | end;
|
---|
287 | for I := 0 to Items.Count - 1 do
|
---|
288 | TSourceValue(Items[I]).Assign(TSourceValue(TSourceValueArray(Source).Items[I]));
|
---|
289 | end else raise Exception.Create('Type for assignment not matches');
|
---|
290 | end;
|
---|
291 |
|
---|
292 | constructor TSourceValueArray.Create;
|
---|
293 | begin
|
---|
294 | Items := TSourceValues.Create;
|
---|
295 | end;
|
---|
296 |
|
---|
297 | destructor TSourceValueArray.Destroy;
|
---|
298 | begin
|
---|
299 | Items.Free;
|
---|
300 | inherited Destroy;
|
---|
301 | end;
|
---|
302 |
|
---|
303 | { TCommandIfEqual }
|
---|
304 |
|
---|
305 | destructor TCommandIfEqual.Destroy;
|
---|
306 | begin
|
---|
307 | Reference1.Free;
|
---|
308 | Reference2.Free;
|
---|
309 | inherited Destroy;
|
---|
310 | end;
|
---|
311 |
|
---|
312 | { TCommandRepeat }
|
---|
313 |
|
---|
314 | destructor TCommandRepeat.Destroy;
|
---|
315 | begin
|
---|
316 | Command.Free;
|
---|
317 | inherited Destroy;
|
---|
318 | end;
|
---|
319 |
|
---|
320 | { TSourceConstant }
|
---|
321 |
|
---|
322 | destructor TSourceConstant.Destroy;
|
---|
323 | begin
|
---|
324 | Value.Free;
|
---|
325 | inherited Destroy;
|
---|
326 | end;
|
---|
327 |
|
---|
328 | { TCommandFunctionCall }
|
---|
329 |
|
---|
330 | constructor TCommandFunctionCall.Create;
|
---|
331 | begin
|
---|
332 | Parameters := TSourceReferences.Create;
|
---|
333 | end;
|
---|
334 |
|
---|
335 | destructor TCommandFunctionCall.Destroy;
|
---|
336 | begin
|
---|
337 | Parameters.Free;
|
---|
338 | inherited Destroy;
|
---|
339 | end;
|
---|
340 |
|
---|
341 | { TCommandBeginEnd }
|
---|
342 |
|
---|
343 | constructor TCommandBeginEnd.Create;
|
---|
344 | begin
|
---|
345 | Commands := TSourceCommands.Create;
|
---|
346 | end;
|
---|
347 |
|
---|
348 | destructor TCommandBeginEnd.Destroy;
|
---|
349 | begin
|
---|
350 | Commands.Free;
|
---|
351 | inherited Destroy;
|
---|
352 | end;
|
---|
353 |
|
---|
354 | { TSourceTypes }
|
---|
355 |
|
---|
356 | function TSourceTypes.AddNew(Name: string; ClassType: TSourceTypeClass): TSourceType;
|
---|
357 | begin
|
---|
358 | Result := ClassType.Create;
|
---|
359 | Result.Name := Name;
|
---|
360 | Add(Result);
|
---|
361 | end;
|
---|
362 |
|
---|
363 | function TSourceTypes.Search(Name: string): TSourceType;
|
---|
364 | var
|
---|
365 | Item: TSourceType;
|
---|
366 | begin
|
---|
367 | Result := nil;
|
---|
368 | for Item in Self do
|
---|
369 | if Item.Name = Name then begin
|
---|
370 | Result := Item;
|
---|
371 | Break;
|
---|
372 | end;
|
---|
373 | if not Assigned(Result) and Assigned(Parent) then
|
---|
374 | Result := Parent.Search(Name);
|
---|
375 | end;
|
---|
376 |
|
---|
377 | { TSourceValue }
|
---|
378 |
|
---|
379 | procedure TSourceValue.Assign(Source: TSourceValue);
|
---|
380 | begin
|
---|
381 | raise Exception.Create('Value assign not implemented');
|
---|
382 | end;
|
---|
383 |
|
---|
384 | constructor TSourceValue.Create;
|
---|
385 | begin
|
---|
386 | end;
|
---|
387 |
|
---|
388 | { TSourceValueInteger }
|
---|
389 |
|
---|
390 | procedure TSourceValueInteger.Assign(Source: TSourceValue);
|
---|
391 | begin
|
---|
392 | if Source is TSourceValueInteger then
|
---|
393 | Value := TSourceValueInteger(Source).Value
|
---|
394 | else raise Exception.Create('Type for assignment not matches');
|
---|
395 | end;
|
---|
396 |
|
---|
397 | { TSourceValueString }
|
---|
398 |
|
---|
399 | procedure TSourceValueString.Assign(Source: TSourceValue);
|
---|
400 | begin
|
---|
401 | if Source is TSourceValueString then
|
---|
402 | Value := TSourceValueString(Source).Value
|
---|
403 | else raise Exception.Create('Type for assignment not matches');
|
---|
404 | end;
|
---|
405 |
|
---|
406 | { TSourceFunctions }
|
---|
407 |
|
---|
408 | function TSourceFunctions.AddNew(Name: string): TSourceFunction;
|
---|
409 | begin
|
---|
410 | Result := TSourceFunction.Create;
|
---|
411 | Result.Name := Name;
|
---|
412 | Add(Result);
|
---|
413 | end;
|
---|
414 |
|
---|
415 | function TSourceFunctions.Search(Name: string): TSourceFunction;
|
---|
416 | var
|
---|
417 | Item: TSourceFunction;
|
---|
418 | begin
|
---|
419 | Result := nil;
|
---|
420 | for Item in Self do
|
---|
421 | if Item.Name = Name then begin
|
---|
422 | Result := Item;
|
---|
423 | Break;
|
---|
424 | end;
|
---|
425 | end;
|
---|
426 |
|
---|
427 | { TSourceFunction }
|
---|
428 |
|
---|
429 | procedure TSourceFunction.AddParameter(Name: string; Kind: TSourceParameterKind
|
---|
430 | );
|
---|
431 | var
|
---|
432 | Parameter: TSourceFunctionParameter;
|
---|
433 | begin
|
---|
434 | Parameter := TSourceFunctionParameter.Create;
|
---|
435 | Parameter.Name := Name;
|
---|
436 | Parameter.Kind := Kind;
|
---|
437 | Parameters.Add(Parameter);
|
---|
438 | end;
|
---|
439 |
|
---|
440 | constructor TSourceFunction.Create;
|
---|
441 | begin
|
---|
442 | Parameters := TSourceFunctionParameters.Create;
|
---|
443 | end;
|
---|
444 |
|
---|
445 | destructor TSourceFunction.Destroy;
|
---|
446 | begin
|
---|
447 | Parameters.Free;
|
---|
448 | inherited Destroy;
|
---|
449 | end;
|
---|
450 |
|
---|
451 | { TSourceVariables }
|
---|
452 |
|
---|
453 | function TSourceVariables.AddNew(Name: string;ValueType: TSourceType): TSourceVariable;
|
---|
454 | begin
|
---|
455 | Result := TSourceVariable.Create;
|
---|
456 | Result.Name := Name;
|
---|
457 | Result.ValueType := ValueType;
|
---|
458 | Add(Result);
|
---|
459 | end;
|
---|
460 |
|
---|
461 | function TSourceVariables.Search(Name: string): TSourceVariable;
|
---|
462 | var
|
---|
463 | Item: TSourceVariable;
|
---|
464 | begin
|
---|
465 | Result := nil;
|
---|
466 | for Item in Self do
|
---|
467 | if Item.Name = Name then begin
|
---|
468 | Result := Item;
|
---|
469 | Break;
|
---|
470 | end;
|
---|
471 | if not Assigned(Result) and Assigned(Parent) then
|
---|
472 | Result := Parent.Search(Name);
|
---|
473 | end;
|
---|
474 |
|
---|
475 | { TSourceConstants }
|
---|
476 |
|
---|
477 | function TSourceConstants.AddNewString(Value: string; Name: string
|
---|
478 | ): TSourceConstant;
|
---|
479 | begin
|
---|
480 | Result := TSourceConstant.Create;
|
---|
481 | Result.Value := TSourceValueString.Create;
|
---|
482 | TSourceValueString(Result.Value).Value := Value;
|
---|
483 | Result.Name := '';
|
---|
484 | Add(Result);
|
---|
485 | end;
|
---|
486 |
|
---|
487 | function TSourceConstants.AddNewInteger(Value: Integer; Name: string
|
---|
488 | ): TSourceConstant;
|
---|
489 | begin
|
---|
490 | Result := TSourceConstant.Create;
|
---|
491 | Result.Value := TSourceValueInteger.Create;
|
---|
492 | TSourceValueInteger(Result.Value).Value := Value;
|
---|
493 | Result.Name := '';
|
---|
494 | Add(Result);
|
---|
495 | end;
|
---|
496 |
|
---|
497 | function TSourceConstants.Search(Name: string): TSourceConstant;
|
---|
498 | var
|
---|
499 | Item: TSourceConstant;
|
---|
500 | begin
|
---|
501 | Result := nil;
|
---|
502 | for Item in Self do
|
---|
503 | if Item.Name = Name then begin
|
---|
504 | Result := Item;
|
---|
505 | Break;
|
---|
506 | end;
|
---|
507 | if not Assigned(Result) and Assigned(Parent) then
|
---|
508 | Result := Parent.Search(Name);
|
---|
509 | end;
|
---|
510 |
|
---|
511 | { TCommandFunctionCall }
|
---|
512 |
|
---|
513 | { TSourceCode }
|
---|
514 |
|
---|
515 | procedure TSourceCode.InitFunctions;
|
---|
516 | var
|
---|
517 | Funct: TSourceFunction;
|
---|
518 | Typ: TSourceType;
|
---|
519 | begin
|
---|
520 | Functions.Clear;
|
---|
521 |
|
---|
522 | // Init types
|
---|
523 | Types.AddNew('Integer', TSourceTypeInteger);
|
---|
524 | Types.AddNew('String', TSourceTypeString);
|
---|
525 | Typ := Types.AddNew('StringArray', TSourceTypeArray);
|
---|
526 | TSourceTypeArray(Typ).ItemType := TSourceTypeString.Create;
|
---|
527 | Typ := Types.AddNew('IntegerArray', TSourceTypeArray);
|
---|
528 | TSourceTypeArray(Typ).ItemType := TSourceTypeInteger.Create;
|
---|
529 |
|
---|
530 | // Init functions
|
---|
531 | Funct := Functions.AddNew('print');
|
---|
532 | Funct.AddParameter('Text', pkString);
|
---|
533 |
|
---|
534 | Funct := Functions.AddNew('println');
|
---|
535 | Funct.AddParameter('Text', pkString);
|
---|
536 |
|
---|
537 | Funct := Functions.AddNew('var');
|
---|
538 | Funct.AddParameter('Variable', pkVariable);
|
---|
539 | Funct.AddParameter('Type', pkType);
|
---|
540 |
|
---|
541 | Funct := Functions.AddNew('assign');
|
---|
542 | Funct.AddParameter('Destination', pkVariable);
|
---|
543 | Funct.AddParameter('Source', pkString);
|
---|
544 |
|
---|
545 | Funct := Functions.AddNew('inputln');
|
---|
546 | Funct.AddParameter('Text', pkVariable);
|
---|
547 |
|
---|
548 | Funct := Functions.AddNew('increment');
|
---|
549 | Funct.AddParameter('Variable', pkVariable);
|
---|
550 | Funct.AddParameter('Addition', pkString);
|
---|
551 |
|
---|
552 | Funct := Functions.AddNew('decrement');
|
---|
553 | Funct.AddParameter('Variable', pkVariable);
|
---|
554 | Funct.AddParameter('Addition', pkString);
|
---|
555 | end;
|
---|
556 |
|
---|
557 | constructor TSourceCode.Create;
|
---|
558 | begin
|
---|
559 | Types := TSourceTypes.Create;
|
---|
560 | Variables := TSourceVariables.Create;
|
---|
561 | Constants := TSourceConstants.Create;
|
---|
562 | Functions := TSourceFunctions.Create;
|
---|
563 | Main := TCommandBeginEnd.Create;
|
---|
564 | Main.SourceCode := Self;
|
---|
565 | InitFunctions;
|
---|
566 | end;
|
---|
567 |
|
---|
568 | destructor TSourceCode.Destroy;
|
---|
569 | begin
|
---|
570 | Main.Free;
|
---|
571 | Functions.Free;
|
---|
572 | Variables.Free;
|
---|
573 | Constants.Free;
|
---|
574 | Types.Free;
|
---|
575 | inherited Destroy;
|
---|
576 | end;
|
---|
577 |
|
---|
578 |
|
---|
579 | end.
|
---|
580 |
|
---|