source: branches/xpascal/Source.pas

Last change on this file was 236, checked in by chronos, 11 months ago
  • Fixed: Var function parameters processed correctly for both user defined and internal functions.
File size: 35.4 KB
Line 
1unit Source;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, SourceNode;
7
8type
9 TExpressions = class;
10 TFunctions = class;
11 TBeginEnd = class;
12 TBlock = class;
13
14 { TValue }
15
16 TValue = class
17 function Clone: TValue; virtual;
18 end;
19
20 { TValueString }
21
22 TValueString = class(TValue)
23 Value: string;
24 function Clone: TValue; override;
25 end;
26
27 { TValueInteger }
28
29 TValueInteger = class(TValue)
30 Value: Integer;
31 function Clone: TValue; override;
32 end;
33
34 { TValueBoolean }
35
36 TValueBoolean = class(TValue)
37 Value: Boolean;
38 function Clone: TValue; override;
39 end;
40
41 TValueClass = class of TValue;
42
43 { TType }
44
45 TType = class(TSourceNode)
46 protected
47 function GetFieldsCount: Integer; override;
48 public
49 Name: string;
50 Functions: TFunctions;
51 ValueClass: TValueClass;
52 procedure GetValue(Index: Integer; out Value); override;
53 function GetField(Index: Integer): TField; override;
54 procedure SetValue(Index: Integer; var Value); override;
55 constructor Create;
56 destructor Destroy; override;
57 end;
58
59 { TTypes }
60
61 TTypes = class(TSourceNodeList<TType>)
62 function SearchByName(Name: string): TType;
63 function AddNew(Name: string): TType;
64 end;
65
66 { TVariable }
67
68 TVariable = class(TSourceNode)
69 protected
70 function GetFieldsCount: Integer; override;
71 public
72 Name: string;
73 TypeRef: TType;
74 Internal: Boolean;
75 procedure GetValue(Index: Integer; out Value); override;
76 function GetField(Index: Integer): TField; override;
77 procedure SetValue(Index: Integer; var Value); override;
78 end;
79
80 { TVariables }
81
82 TVariables = class(TSourceNodeList<TVariable>)
83 function SearchByName(Name: string): TVariable;
84 end;
85
86 { TConstant }
87
88 TConstant = class(TSourceNode)
89 protected
90 function GetFieldsCount: Integer; override;
91 public
92 Name: string;
93 TypeRef: TType;
94 Value: TValue;
95 procedure GetValue(Index: Integer; out Value); override;
96 function GetField(Index: Integer): TField; override;
97 procedure SetValue(Index: Integer; var Value); override;
98 destructor Destroy; override;
99 end;
100
101 { TConstants }
102
103 TConstants = class(TSourceNodeList<TConstant>)
104 function SearchByName(Name: string): TConstant;
105 function AddNew(Name: string): TConstant;
106 end;
107
108 TFunctionParamKind = (pkNormal, pkVar, pkConst);
109
110 TFunctionParameter = class(TSourceNode)
111 Name: string;
112 TypeRef: TType;
113 Kind: TFunctionParamKind;
114 end;
115
116 { TFunctionParameters }
117
118 TFunctionParameters = class(TSourceNodeList<TFunctionParameter>)
119 function SearchByName(Name: string): TFunctionParameter;
120 function AddNew(Name: string; TypeRef: TType): TFunctionParameter;
121 end;
122
123 { TFunction }
124
125 TFunction = class(TSourceNode)
126 protected
127 function GetFieldsCount: Integer; override;
128 public
129 Name: string;
130 InternalName: string;
131 Params: TFunctionParameters;
132 ResultType: TType;
133 Block: TBlock;
134 ParentType: TType;
135 procedure InitVariables;
136 procedure GetValue(Index: Integer; out Value); override;
137 function GetField(Index: Integer): TField; override;
138 procedure SetValue(Index: Integer; var Value); override;
139 constructor Create;
140 destructor Destroy; override;
141 end;
142
143 { TFunctions }
144
145 TFunctions = class(TSourceNodeList<TFunction>)
146 ParentType: TType;
147 function SearchByName(Name: string): TFunction;
148 function AddNew(Name: string): TFunction;
149 end;
150
151 { TProcedure }
152
153 TProcedure = class(TSourceNode)
154 protected
155 function GetFieldsCount: Integer; override;
156 public
157 Name: string;
158 InternalName: string;
159 Params: TFunctionParameters;
160 Block: TBlock;
161 ParentType: TType;
162 procedure InitVariables;
163 procedure GetValue(Index: Integer; out Value); override;
164 function GetField(Index: Integer): TField; override;
165 procedure SetValue(Index: Integer; var Value); override;
166 constructor Create;
167 destructor Destroy; override;
168 end;
169
170 { TProcedures }
171
172 TProcedures = class(TSourceNodeList<TProcedure>)
173 ParentType: TType;
174 function SearchByName(Name: string): TProcedure;
175 function AddNew(Name: string): TProcedure;
176 end;
177
178 TCommand = class(TSourceNode)
179 end;
180
181 TCommands = class(TSourceNodeList<TCommand>)
182 end;
183
184 TEmptyCommand = class(TCommand)
185 end;
186
187 { TFunctionCall }
188
189 TFunctionCall = class(TCommand)
190 protected
191 function GetFieldsCount: Integer; override;
192 public
193 FunctionDef: TFunction;
194 Params: TExpressions;
195 procedure GetValue(Index: Integer; out Value); override;
196 function GetField(Index: Integer): TField; override;
197 procedure SetValue(Index: Integer; var Value); override;
198 constructor Create;
199 destructor Destroy; override;
200 end;
201
202 { TProcedureCall }
203
204 TProcedureCall = class(TCommand)
205 protected
206 function GetFieldsCount: Integer; override;
207 public
208 ProcedureDef: TProcedure;
209 Params: TExpressions;
210 procedure GetValue(Index: Integer; out Value); override;
211 function GetField(Index: Integer): TField; override;
212 procedure SetValue(Index: Integer; var Value); override;
213 constructor Create;
214 destructor Destroy; override;
215 end;
216
217 { TBeginEnd }
218
219 TBeginEnd = class(TCommand)
220 protected
221 function GetFieldsCount: Integer; override;
222 public
223 Commands: TCommands;
224 procedure GetValue(Index: Integer; out Value); override;
225 function GetField(Index: Integer): TField; override;
226 procedure SetValue(Index: Integer; var Value); override;
227 procedure Clear;
228 constructor Create;
229 destructor Destroy; override;
230 end;
231
232 TExpressionOperator = (eoNone, eoAdd, eoSub, eoMultiply, eoDivide, eoIntDivide,
233 eoModulo, eoAnd, eoXor, eoOr, eoShl, eoShr, eoEqual, eoNotEqual, eoLesser,
234 eoHigher, eoLesserOrEqual, eoHigherOrEqual, eoNot);
235
236 { TExpression }
237
238 TExpression = class(TSourceNode)
239 function GetType: TType; virtual;
240 end;
241
242 { TExpressionOperation }
243
244 TExpressionOperation = class(TExpression)
245 protected
246 function GetFieldsCount: Integer; override;
247 public
248 TypeRef: TType;
249 FunctionRef: TFunction;
250 Operation: TExpressionOperator;
251 Items: TExpressions;
252 function GetFunctionName: string;
253 procedure GetValue(Index: Integer; out Value); override;
254 function GetField(Index: Integer): TField; override;
255 procedure SetValue(Index: Integer; var Value); override;
256 constructor Create;
257 destructor Destroy; override;
258 function GetType: TType; override;
259 end;
260
261 TExpressionOperandType = (otVariableRef, otConstantRef, otConstantDirect,
262 otFunctionCall);
263
264 { TExpressionOperand }
265
266 TExpressionOperand = class(TExpression)
267 protected
268 function GetFieldsCount: Integer; override;
269 public
270 OperandType: TExpressionOperandType;
271 VariableRef: TVariable;
272 ConstantRef: TConstant;
273 ConstantDirect: TConstant;
274 FunctionCall: TFunctionCall;
275 procedure GetValue(Index: Integer; out Value); override;
276 function GetField(Index: Integer): TField; override;
277 procedure SetValue(Index: Integer; var Value); override;
278 function GetType: TType; override;
279 constructor Create;
280 destructor Destroy; override;
281 end;
282
283 { TExpressionBrackets }
284
285 TExpressionBrackets = class(TExpression)
286 Expression: TExpression;
287 procedure GetValue(Index: Integer; out Value); override;
288 function GetField(Index: Integer): TField; override;
289 procedure SetValue(Index: Integer; var Value); override;
290 function GetType: TType; override;
291 destructor Destroy; override;
292 end;
293
294 TExpressions = class(TSourceNodeList<TExpression>)
295 end;
296
297 { TAssignment }
298
299 TAssignment = class(TCommand)
300 protected
301 function GetFieldsCount: Integer; override;
302 public
303 Variable: TVariable;
304 Expression: TExpression;
305 procedure GetValue(Index: Integer; out Value); override;
306 function GetField(Index: Integer): TField; override;
307 procedure SetValue(Index: Integer; var Value); override;
308 constructor Create;
309 destructor Destroy; override;
310 end;
311
312 { TReturn }
313
314 TReturn = class(TCommand)
315 protected
316 function GetFieldsCount: Integer; override;
317 public
318 Expression: TExpression;
319 procedure GetValue(Index: Integer; out Value); override;
320 function GetField(Index: Integer): TField; override;
321 procedure SetValue(Index: Integer; var Value); override;
322 constructor Create;
323 destructor Destroy; override;
324 end;
325
326 { TIfThenElse }
327
328 TIfThenElse = class(TCommand)
329 protected
330 function GetFieldsCount: Integer; override;
331 public
332 Expression: TExpression;
333 CommandThen: TCommand;
334 CommandElse: TCommand;
335 procedure GetValue(Index: Integer; out Value); override;
336 function GetField(Index: Integer): TField; override;
337 procedure SetValue(Index: Integer; var Value); override;
338 constructor Create;
339 destructor Destroy; override;
340 end;
341
342 TLoop = class(TCommand)
343 DoBreak: Boolean;
344 DoContinue: Boolean;
345 end;
346
347 { TWhileDo }
348
349 TWhileDo = class(TLoop)
350 protected
351 function GetFieldsCount: Integer; override;
352 public
353 Expression: TExpression;
354 Command: TCommand;
355 procedure GetValue(Index: Integer; out Value); override;
356 function GetField(Index: Integer): TField; override;
357 procedure SetValue(Index: Integer; var Value); override;
358 constructor Create;
359 destructor Destroy; override;
360 end;
361
362 { TRepeatUntil }
363
364 TRepeatUntil = class(TLoop)
365 protected
366 function GetFieldsCount: Integer; override;
367 public
368 Expression: TExpression;
369 Commands: TCommands;
370 procedure GetValue(Index: Integer; out Value); override;
371 function GetField(Index: Integer): TField; override;
372 procedure SetValue(Index: Integer; var Value); override;
373 constructor Create;
374 destructor Destroy; override;
375 end;
376
377 TBreak = class(TCommand)
378 end;
379
380 TContinue = class(TCommand)
381 end;
382
383 { TForToDo }
384
385 TForToDo = class(TLoop)
386 protected
387 function GetFieldsCount: Integer; override;
388 public
389 VariableRef: TVariable;
390 ExpressionFrom: TExpression;
391 ExpressionTo: TExpression;
392 Command: TCommand;
393 procedure GetValue(Index: Integer; out Value); override;
394 function GetField(Index: Integer): TField; override;
395 procedure SetValue(Index: Integer; var Value); override;
396 constructor Create;
397 destructor Destroy; override;
398 end;
399
400 { TBlock }
401
402 TBlock = class(TSourceNode)
403 protected
404 function GetFieldsCount: Integer; override;
405 public
406 ParentBlock: TBlock;
407 Variables: TVariables;
408 Constants: TConstants;
409 Functions: TFunctions;
410 Procedures: TProcedures;
411 Types: TTypes;
412 BeginEnd: TBeginEnd;
413 procedure GetValue(Index: Integer; out Value); override;
414 function GetField(Index: Integer): TField; override;
415 procedure SetValue(Index: Integer; var Value); override;
416 procedure Clear;
417 function GetType(Name: string): TType;
418 function GetConstant(Name: string): TConstant;
419 function GetVariable(Name: string): TVariable;
420 function GetFunction(Name: string): TFunction;
421 function GetProcedure(Name: string): TProcedure;
422 constructor Create;
423 destructor Destroy; override;
424 end;
425
426 { TProgram }
427
428 TProgram = class(TSourceNode)
429 protected
430 function GetFieldsCount: Integer; override;
431 public
432 Name: string;
433 SystemBlock: TBlock;
434 Block: TBlock;
435 procedure GetValue(Index: Integer; out Value); override;
436 function GetField(Index: Integer): TField; override;
437 procedure SetValue(Index: Integer; var Value); override;
438 procedure Clear;
439 constructor Create;
440 destructor Destroy; override;
441 end;
442
443 const
444 ExpressionOperatorText: array[TExpressionOperator] of string = ('', '+',
445 '-', '*', '/', 'div', 'mod', 'and', 'xor', 'or', 'shl',
446 'shr', '=', '<>', '<', '>', '<=','>=', 'not');
447 ExpressionOperatorFuncText: array[TExpressionOperator] of string = ('', '_Add',
448 '_Sub', '_Mul', '_Div', '_IntDiv', '_Mod', '_And', '_Xor', '_Or', '_Shl',
449 '_Shr', '_Equal', '_NotEqual', '_Lesser', '_Higher', '_LesserOrEqual',
450 '_HigherOrEqual', '_Not');
451
452 function GetOperatorByName(Name: string): TExpressionOperator;
453
454
455implementation
456
457resourcestring
458 SIndexError = 'Index error';
459
460function GetOperatorByName(Name: string): TExpressionOperator;
461var
462 I: TExpressionOperator;
463begin
464 Result := eoNone;
465 for I := Succ(Low(TExpressionOperator)) to High(TExpressionOperator) do begin
466 if ExpressionOperatorText[I] = Name then begin
467 Result := I;
468 Break;
469 end;
470 end;
471end;
472
473{ TProcedureCall }
474
475function TProcedureCall.GetFieldsCount: Integer;
476begin
477 Result := 2;
478end;
479
480procedure TProcedureCall.GetValue(Index: Integer; out Value);
481begin
482 if Index = 0 then TProcedure(Value) := ProcedureDef
483 else if Index = 1 then TExpressions(Value) := Params
484 else inherited;
485end;
486
487function TProcedureCall.GetField(Index: Integer): TField;
488begin
489 if Index = 0 then Result := TField.Create(dtObject, 'Procedure')
490 else if Index = 1 then Result := TField.Create(dtObject, 'Parameters')
491 else inherited;
492end;
493
494procedure TProcedureCall.SetValue(Index: Integer; var Value);
495begin
496 if Index = 0 then ProcedureDef := TProcedure(Value)
497 else if Index = 1 then Params := TExpressions(Value)
498 else inherited;
499end;
500
501constructor TProcedureCall.Create;
502begin
503 Params := TExpressions.Create;
504end;
505
506destructor TProcedureCall.Destroy;
507begin
508 FreeAndNil(Params);
509 inherited;
510end;
511
512{ TProcedure }
513
514function TProcedures.SearchByName(Name: string): TProcedure;
515var
516 I: Integer;
517begin
518 I := 0;
519 while (I < Count) and (TProcedure(Items[I]).Name <> Name) do Inc(I);
520 if I < Count then Result := TProcedure(Items[I])
521 else Result := nil;
522end;
523
524function TProcedures.AddNew(Name: string): TProcedure;
525begin
526 Result := TProcedure.Create;
527 Result.Name := Name;
528 Result.ParentType := ParentType;
529 Add(Result);
530end;
531
532function TProcedure.GetFieldsCount: Integer;
533begin
534 Result := 3;
535end;
536
537procedure TProcedure.InitVariables;
538var
539 I: Integer;
540 Variable: TVariable;
541begin
542 for I := 0 to Params.Count - 1 do begin
543 Variable := TVariable.Create;
544 Variable.Name := Params[I].Name;
545 Variable.TypeRef := Params[I].TypeRef;
546 Variable.Internal := True;
547 Block.Variables.Add(Variable);
548 end;
549end;
550
551procedure TProcedure.GetValue(Index: Integer; out Value);
552begin
553 if Index = 0 then TBlock(Value) := Block
554 else if Index = 1 then TFunctionParameters(Value) := Params
555 else if Index = 2 then string(Value) := Name
556 else inherited;
557end;
558
559function TProcedure.GetField(Index: Integer): TField;
560begin
561 if Index = 0 then Result := TField.Create(dtObject, 'Block')
562 else if Index = 1 then Result := TField.Create(dtList, 'Parameters')
563 else if Index = 2 then Result := TField.Create(dtString, 'Name')
564 else inherited;
565end;
566
567procedure TProcedure.SetValue(Index: Integer; var Value);
568begin
569 if Index = 0 then Block := TBlock(Value)
570 else if Index = 1 then Params := TFunctionParameters(Value)
571 else if Index = 2 then Name := string(Value)
572 else inherited;
573end;
574
575constructor TProcedure.Create;
576begin
577 Params := TFunctionParameters.Create;
578 Block := TBlock.Create;
579end;
580
581destructor TProcedure.Destroy;
582begin
583 FreeAndNil(Block);
584 FreeAndNil(Params);
585 inherited;
586end;
587
588{ TExpressionBrackets }
589
590procedure TExpressionBrackets.GetValue(Index: Integer; out Value);
591begin
592 if Index = 0 then begin
593 TExpression(Value) := Expression;
594 end
595 else inherited;
596end;
597
598function TExpressionBrackets.GetField(Index: Integer): TField;
599begin
600 if Index = 0 then Result := TField.Create(dtObject, 'Expression')
601 else inherited;
602end;
603
604procedure TExpressionBrackets.SetValue(Index: Integer; var Value);
605begin
606 if Index = 0 then begin
607 Expression := TExpression(Value);
608 end
609 else inherited;
610end;
611
612function TExpressionBrackets.GetType: TType;
613begin
614 Result := Expression.GetType;
615end;
616
617destructor TExpressionBrackets.Destroy;
618begin
619 FreeAndNil(Expression);
620 inherited;
621end;
622
623{ TReturn }
624
625function TReturn.GetFieldsCount: Integer;
626begin
627 Result := 1;
628end;
629
630procedure TReturn.GetValue(Index: Integer; out Value);
631begin
632 if Index = 0 then TExpression(Value) := Expression
633 else inherited;
634end;
635
636function TReturn.GetField(Index: Integer): TField;
637begin
638 if Index = 0 then Result := TField.Create(dtObject, 'Expression')
639 else inherited;
640end;
641
642procedure TReturn.SetValue(Index: Integer; var Value);
643begin
644 if Index = 0 then Expression := TExpression(Value)
645 else inherited;
646end;
647
648constructor TReturn.Create;
649begin
650 Expression := TExpression.Create;
651end;
652
653destructor TReturn.Destroy;
654begin
655 FreeAndNil(Expression);
656 inherited;
657end;
658
659{ TVariable }
660
661procedure TVariable.GetValue(Index: Integer; out Value);
662begin
663 if Index = 0 then string(Value) := Name
664 else if Index = 1 then TType(Value) := TypeRef
665 else inherited;
666end;
667
668function TVariable.GetField(Index: Integer): TField;
669begin
670 if Index = 0 then Result := TField.Create(dtString, 'Name')
671 else if Index = 1 then Result := TField.Create(dtObject, 'Type')
672 else inherited;
673end;
674
675function TVariable.GetFieldsCount: Integer;
676begin
677 Result := 2;
678end;
679
680procedure TVariable.SetValue(Index: Integer; var Value);
681begin
682 if Index = 0 then Name := string(Value)
683 else if Index = 1 then TypeRef := TType(Value)
684 else inherited;
685end;
686
687{ TConstant }
688
689procedure TConstant.GetValue(Index: Integer; out Value);
690begin
691 if Index = 0 then string(Value) := Name
692 else if Index = 1 then TType(Value) := TypeRef
693 else inherited;
694end;
695
696function TConstant.GetField(Index: Integer): TField;
697begin
698 if Index = 0 then Result := TField.Create(dtString, 'Name')
699 else if Index = 1 then Result := TField.Create(dtObject, 'Type')
700 else inherited;
701end;
702
703function TConstant.GetFieldsCount: Integer;
704begin
705 Result := 2;
706end;
707
708procedure TConstant.SetValue(Index: Integer; var Value);
709begin
710 if Index = 0 then Name := string(Value)
711 else if Index = 1 then TypeRef := TType(Value)
712 else inherited;
713end;
714
715destructor TConstant.Destroy;
716begin
717 FreeAndNil(Value);
718 inherited;
719end;
720
721{ TRepeatUntil }
722
723procedure TRepeatUntil.GetValue(Index: Integer; out Value);
724begin
725 if Index = 0 then TExpression(Value) := Expression
726 else if Index = 1 then TCommands(Value) := Commands
727 else inherited;
728end;
729
730function TRepeatUntil.GetField(Index: Integer): TField;
731begin
732 if Index = 0 then Result := TField.Create(dtObject, 'Expression')
733 else if Index = 1 then Result := TField.Create(dtList, 'Commands')
734 else inherited;
735end;
736
737function TRepeatUntil.GetFieldsCount: Integer;
738begin
739 Result := 2;
740end;
741
742procedure TRepeatUntil.SetValue(Index: Integer; var Value);
743begin
744 if Index = 0 then Expression := TExpression(Value)
745 else if Index = 1 then Commands := TCommands(Value)
746 else inherited;
747end;
748
749constructor TRepeatUntil.Create;
750begin
751 Expression := TExpression.Create;
752 Commands := TCommands.Create;
753end;
754
755destructor TRepeatUntil.Destroy;
756begin
757 FreeAndNil(Expression);
758 FreeAndNil(Commands);
759 inherited;
760end;
761
762{ TValueBoolean }
763
764function TValueBoolean.Clone: TValue;
765begin
766 Result := TValueBoolean.Create;
767 TValueBoolean(Result).Value := Value;
768end;
769
770{ TValueInteger }
771
772function TValueInteger.Clone: TValue;
773begin
774 Result := TValueInteger.Create;
775 TValueInteger(Result).Value := Value;
776end;
777
778{ TValueString }
779
780function TValueString.Clone: TValue;
781begin
782 Result := TValueString.Create;
783 TValueString(Result).Value := Value;
784end;
785
786{ TValue }
787
788function TValue.Clone: TValue;
789begin
790 Result := nil;
791end;
792
793{ TForToDo }
794
795procedure TForToDo.GetValue(Index: Integer; out Value);
796begin
797 if Index = 0 then TVariable(Value) := VariableRef
798 else if Index = 1 then TExpression(Value) := ExpressionFrom
799 else if Index = 2 then TExpression(Value) := ExpressionTo
800 else if Index = 3 then TCommand(Value) := Command
801 else inherited;
802end;
803
804function TForToDo.GetField(Index: Integer): TField;
805begin
806 if Index = 0 then Result := TField.Create(dtObject, 'Variable')
807 else if Index = 1 then Result := TField.Create(dtObject, 'To')
808 else if Index = 2 then Result := TField.Create(dtObject, 'From')
809 else if Index = 3 then Result := TField.Create(dtObject, 'Do')
810 else inherited;
811end;
812
813function TForToDo.GetFieldsCount: Integer;
814begin
815 Result := 4;
816end;
817
818procedure TForToDo.SetValue(Index: Integer; var Value);
819begin
820 if Index = 0 then VariableRef := TVariable(Value)
821 else if Index = 1 then ExpressionFrom := TExpression(Value)
822 else if Index = 2 then ExpressionTo := TExpression(Value)
823 else if Index = 3 then Command := TCommand(Value)
824 else inherited;
825end;
826
827constructor TForToDo.Create;
828begin
829 ExpressionFrom := TExpression.Create;
830 ExpressionTo := TExpression.Create;
831 Command := TEmptyCommand.Create;
832end;
833
834destructor TForToDo.Destroy;
835begin
836 FreeAndNil(Command);
837 FreeAndNil(ExpressionTo);
838 FreeAndNil(ExpressionFrom);
839 inherited;
840end;
841
842{ TExpression }
843
844function TExpression.GetType: TType;
845begin
846 Result := nil;
847end;
848
849{ TExpressionOperand }
850
851procedure TExpressionOperand.GetValue(Index: Integer; out Value);
852begin
853 if Index = 0 then begin
854 case OperandType of
855 otConstantDirect: TConstant(Value) := ConstantDirect;
856 otConstantRef: TConstant(Value) := ConstantRef;
857 otFunctionCall: TFunctionCall(Value) := FunctionCall;
858 otVariableRef: TVariable(Value) := VariableRef;
859 end;
860 end
861 else inherited;
862end;
863
864function TExpressionOperand.GetField(Index: Integer): TField;
865begin
866 if Index = 0 then Result := TField.Create(dtObject, 'Value')
867 else inherited;
868end;
869
870function TExpressionOperand.GetFieldsCount: Integer;
871begin
872 Result := 1;
873end;
874
875procedure TExpressionOperand.SetValue(Index: Integer; var Value);
876begin
877 if Index = 0 then begin
878 case OperandType of
879 otConstantDirect: ConstantDirect := TConstant(Value);
880 otConstantRef: ConstantRef := TConstant(Value);
881 otFunctionCall: FunctionCall := TFunctionCall(Value);
882 otVariableRef: VariableRef := TVariable(Value);
883 end;
884 end
885 else inherited;
886end;
887
888function TExpressionOperand.GetType: TType;
889begin
890 if OperandType = otFunctionCall then Result := FunctionCall.FunctionDef.ResultType
891 else if OperandType = otConstantRef then Result := ConstantRef.TypeRef
892 else if OperandType = otConstantDirect then Result := ConstantDirect.TypeRef
893 else if OperandType = otVariableRef then Result := VariableRef.TypeRef
894 else raise Exception.Create('Unsupported operand type');
895end;
896
897constructor TExpressionOperand.Create;
898begin
899end;
900
901destructor TExpressionOperand.Destroy;
902begin
903 if Assigned(ConstantDirect) then FreeAndNil(ConstantDirect);
904 if Assigned(FunctionCall) then FreeAndNil(FunctionCall);
905end;
906
907{ TFunctionParameters }
908
909function TFunctionParameters.SearchByName(Name: string): TFunctionParameter;
910var
911 I: Integer;
912begin
913 I := 0;
914 while (I < Count) and (TFunctionParameter(Items[I]).Name <> Name) do Inc(I);
915 if I < Count then Result := TFunctionParameter(Items[I])
916 else Result := nil;
917end;
918
919function TFunctionParameters.AddNew(Name: string; TypeRef: TType): TFunctionParameter;
920begin
921 Result := TFunctionParameter.Create;
922 Result.Name := Name;
923 Result.TypeRef := TypeRef;
924 Add(Result);
925end;
926
927{ TFunction }
928
929procedure TFunction.GetValue(Index: Integer; out Value);
930begin
931 if Index = 0 then TBlock(Value) := Block
932 else if Index = 1 then TFunctionParameters(Value) := Params
933 else if Index = 2 then string(Value) := Name
934 else if Index = 3 then TType(Value) := ResultType
935 else inherited;
936end;
937
938function TFunction.GetField(Index: Integer): TField;
939begin
940 if Index = 0 then Result := TField.Create(dtObject, 'Block')
941 else if Index = 1 then Result := TField.Create(dtList, 'Parameters')
942 else if Index = 2 then Result := TField.Create(dtString, 'Name')
943 else if Index = 3 then Result := TField.Create(dtObject, 'ResultType')
944 else inherited;
945end;
946
947function TFunction.GetFieldsCount: Integer;
948begin
949 Result := 4;
950end;
951
952procedure TFunction.InitVariables;
953var
954 I: Integer;
955 Variable: TVariable;
956begin
957 for I := 0 to Params.Count - 1 do begin
958 Variable := TVariable.Create;
959 Variable.Name := Params[I].Name;
960 Variable.TypeRef := Params[I].TypeRef;
961 Variable.Internal := True;
962 Block.Variables.Add(Variable);
963 end;
964
965 Variable := TVariable.Create;
966 Variable.Name := 'Result';
967 Variable.TypeRef := ResultType;
968 Variable.Internal := True;
969 Block.Variables.Add(Variable);
970end;
971
972procedure TFunction.SetValue(Index: Integer; var Value);
973begin
974 if Index = 0 then Block := TBlock(Value)
975 else if Index = 1 then Params := TFunctionParameters(Value)
976 else if Index = 2 then Name := string(Value)
977 else if Index = 3 then ResultType := TType(Value)
978 else inherited;
979end;
980
981constructor TFunction.Create;
982begin
983 Params := TFunctionParameters.Create;
984 Block := TBlock.Create;
985end;
986
987destructor TFunction.Destroy;
988begin
989 FreeAndNil(Block);
990 FreeAndNil(Params);
991 inherited;
992end;
993
994{ TType }
995
996procedure TType.GetValue(Index: Integer; out Value);
997begin
998 if Index = 0 then string(Value) := Name
999 else inherited;
1000end;
1001
1002function TType.GetField(Index: Integer): TField;
1003begin
1004 if Index = 0 then Result := TField.Create(dtString, 'Name')
1005 else inherited;
1006end;
1007
1008function TType.GetFieldsCount: Integer;
1009begin
1010 Result := 1;
1011end;
1012
1013procedure TType.SetValue(Index: Integer; var Value);
1014begin
1015 if Index = 0 then Name := string(Value)
1016 else inherited;
1017end;
1018
1019constructor TType.Create;
1020begin
1021 Functions := TFunctions.Create;
1022 Functions.ParentType := Self;
1023end;
1024
1025destructor TType.Destroy;
1026begin
1027 FreeAndNil(Functions);
1028 inherited;
1029end;
1030
1031{ TTypes }
1032
1033function TTypes.SearchByName(Name: string): TType;
1034var
1035 I: Integer;
1036begin
1037 I := 0;
1038 while (I < Count) and (TType(Items[I]).Name <> Name) do Inc(I);
1039 if I < Count then Result := TType(Items[I])
1040 else Result := nil;
1041end;
1042
1043function TTypes.AddNew(Name: string): TType;
1044begin
1045 Result := TType.Create;
1046 Result.Name := Name;
1047 Add(Result);
1048end;
1049
1050{ TExpressionOperation }
1051
1052procedure TExpressionOperation.GetValue(Index: Integer; out Value);
1053begin
1054 TObject(Value) := Items[Index];
1055end;
1056
1057function TExpressionOperation.GetField(Index: Integer): TField;
1058begin
1059 if Index < Items.Count then Result := TField.Create(dtObject, 'Expression')
1060 else inherited;
1061end;
1062
1063function TExpressionOperation.GetFunctionName: string;
1064begin
1065 Result := ExpressionOperatorFuncText[Operation];
1066end;
1067
1068function TExpressionOperation.GetFieldsCount: Integer;
1069begin
1070 Result := Items.Count;
1071end;
1072
1073procedure TExpressionOperation.SetValue(Index: Integer; var Value);
1074begin
1075 Items[Index] := TExpression(Value);
1076end;
1077
1078constructor TExpressionOperation.Create;
1079begin
1080 Items := TExpressions.Create;
1081end;
1082
1083destructor TExpressionOperation.Destroy;
1084begin
1085 FreeAndNil(Items);
1086 inherited;
1087end;
1088
1089function TExpressionOperation.GetType: TType;
1090begin
1091 Result := TypeRef;
1092end;
1093
1094{ TAssignment }
1095
1096procedure TAssignment.GetValue(Index: Integer; out Value);
1097begin
1098 if Index = 0 then TExpression(Value) := Expression
1099 else if Index = 1 then TVariable(Value) := Variable
1100 else inherited;
1101end;
1102
1103function TAssignment.GetField(Index: Integer): TField;
1104begin
1105 if Index = 0 then Result := TField.Create(dtObject, 'Expression')
1106 else if Index = 1 then Result := TField.Create(dtObject, 'Variable')
1107 else inherited;
1108end;
1109
1110function TAssignment.GetFieldsCount: Integer;
1111begin
1112 Result := 2;
1113end;
1114
1115procedure TAssignment.SetValue(Index: Integer; var Value);
1116begin
1117 if Index = 0 then Expression := TExpression(Value)
1118 else if Index = 1 then Variable := TVariable(Value)
1119 else inherited;
1120end;
1121
1122constructor TAssignment.Create;
1123begin
1124 Variable := nil;
1125 Expression := TExpression.Create;
1126end;
1127
1128destructor TAssignment.Destroy;
1129begin
1130 Variable := nil;
1131 FreeAndNil(Expression);
1132 inherited;
1133end;
1134
1135{ TIfThenElse }
1136
1137procedure TIfThenElse.GetValue(Index: Integer; out Value);
1138begin
1139 if Index = 0 then TExpression(Value) := Expression
1140 else if Index = 1 then TCommand(Value) := CommandElse
1141 else if Index = 2 then TCommand(Value) := CommandThen
1142 else inherited;
1143end;
1144
1145function TIfThenElse.GetField(Index: Integer): TField;
1146begin
1147 if Index = 0 then Result := TField.Create(dtObject, 'Expression')
1148 else if Index = 1 then Result := TField.Create(dtObject, 'Else')
1149 else if Index = 2 then Result := TField.Create(dtObject, 'Then')
1150 else inherited;
1151end;
1152
1153function TIfThenElse.GetFieldsCount: Integer;
1154begin
1155 Result := 3;
1156end;
1157
1158procedure TIfThenElse.SetValue(Index: Integer; var Value);
1159begin
1160 if Index = 0 then Expression := TExpression(Value)
1161 else if Index = 1 then CommandElse := TCommand(Value)
1162 else if Index = 2 then CommandThen := TCommand(Value)
1163 else inherited;
1164end;
1165
1166constructor TIfThenElse.Create;
1167begin
1168 Expression := TExpression.Create;
1169 CommandThen := TEmptyCommand.Create;
1170 CommandElse := TEmptyCommand.Create;
1171end;
1172
1173destructor TIfThenElse.Destroy;
1174begin
1175 FreeAndNil(Expression);
1176 FreeAndNil(CommandThen);
1177 FreeAndNil(CommandElse);
1178 inherited;
1179end;
1180
1181{ TWhileDo }
1182
1183procedure TWhileDo.GetValue(Index: Integer; out Value);
1184begin
1185 if Index = 0 then TExpression(Value) := Expression
1186 else if Index = 1 then TCommand(Value) := Command
1187 else inherited;
1188end;
1189
1190function TWhileDo.GetField(Index: Integer): TField;
1191begin
1192 if Index = 0 then Result := TField.Create(dtObject, 'Expression')
1193 else if Index = 1 then Result := TField.Create(dtObject, 'Do')
1194 else inherited;
1195end;
1196
1197function TWhileDo.GetFieldsCount: Integer;
1198begin
1199 Result := 2;
1200end;
1201
1202procedure TWhileDo.SetValue(Index: Integer; var Value);
1203begin
1204 if Index = 0 then Expression := TExpression(Value)
1205 else if Index = 1 then Command := TCommand(Value)
1206 else inherited;
1207end;
1208
1209constructor TWhileDo.Create;
1210begin
1211 Expression := TExpression.Create;
1212 Command := TEmptyCommand.Create;
1213end;
1214
1215destructor TWhileDo.Destroy;
1216begin
1217 FreeAndNil(Expression);
1218 FreeAndNil(Command);
1219 inherited;
1220end;
1221
1222{ TFunctionCall }
1223
1224procedure TFunctionCall.GetValue(Index: Integer; out Value);
1225begin
1226 if Index = 0 then TFunction(Value) := FunctionDef
1227 else if Index = 1 then TExpressions(Value) := Params
1228 else inherited;
1229end;
1230
1231function TFunctionCall.GetField(Index: Integer): TField;
1232begin
1233 if Index = 0 then Result := TField.Create(dtObject, 'Function')
1234 else if Index = 1 then Result := TField.Create(dtList, 'Parameters')
1235 else inherited;
1236end;
1237
1238function TFunctionCall.GetFieldsCount: Integer;
1239begin
1240 Result := 2;
1241end;
1242
1243procedure TFunctionCall.SetValue(Index: Integer; var Value);
1244begin
1245 if Index = 0 then FunctionDef := TFunction(Value)
1246 else if Index = 1 then Params := TExpressions(Value)
1247 else inherited;
1248end;
1249
1250constructor TFunctionCall.Create;
1251begin
1252 Params := TExpressions.Create;
1253end;
1254
1255destructor TFunctionCall.Destroy;
1256begin
1257 FreeAndNil(Params);
1258 inherited;
1259end;
1260
1261{ TFunctions }
1262
1263function TFunctions.SearchByName(Name: string): TFunction;
1264var
1265 I: Integer;
1266begin
1267 I := 0;
1268 while (I < Count) and (TFunction(Items[I]).Name <> Name) do Inc(I);
1269 if I < Count then Result := TFunction(Items[I])
1270 else Result := nil;
1271end;
1272
1273function TFunctions.AddNew(Name: string): TFunction;
1274begin
1275 Result := TFunction.Create;
1276 Result.Name := Name;
1277 Result.ParentType := ParentType;
1278 Add(Result);
1279end;
1280
1281{ TConstants }
1282
1283function TConstants.SearchByName(Name: string): TConstant;
1284var
1285 I: Integer;
1286begin
1287 I := 0;
1288 while (I < Count) and (TConstant(Items[I]).Name <> Name) do Inc(I);
1289 if I < Count then Result := TConstant(Items[I])
1290 else Result := nil;
1291end;
1292
1293function TConstants.AddNew(Name: string): TConstant;
1294begin
1295 Result := TConstant.Create;
1296 Result.Name := Name;
1297 Add(Result);
1298end;
1299
1300{ TVariables }
1301
1302function TVariables.SearchByName(Name: string): TVariable;
1303var
1304 I: Integer;
1305begin
1306 I := 0;
1307 while (I < Count) and (TVariable(Items[I]).Name <> Name) do Inc(I);
1308 if I < Count then Result := TVariable(Items[I])
1309 else Result := nil;
1310end;
1311
1312{ TBlock }
1313
1314procedure TBlock.GetValue(Index: Integer; out Value);
1315begin
1316 if Index = 0 then TBeginEnd(Value) := BeginEnd
1317 else if Index = 1 then TTypes(Value) := Types
1318 else if Index = 2 then TVariables(Value) := Variables
1319 else if Index = 3 then TConstants(Value) := Constants
1320 else if Index = 4 then TFunctions(Value) := Functions
1321 else if Index = 5 then TProcedures(Value) := Procedures
1322 else inherited;
1323end;
1324
1325function TBlock.GetField(Index: Integer): TField;
1326begin
1327 if Index = 0 then Result := TField.Create(dtObject, 'BeginEnd')
1328 else if Index = 1 then Result := TField.Create(dtList, 'Types')
1329 else if Index = 2 then Result := TField.Create(dtList, 'Variables')
1330 else if Index = 3 then Result := TField.Create(dtList, 'Constants')
1331 else if Index = 4 then Result := TField.Create(dtList, 'Functions')
1332 else if Index = 5 then Result := TField.Create(dtList, 'Procedures')
1333 else inherited;
1334end;
1335
1336function TBlock.GetFieldsCount: Integer;
1337begin
1338 Result := 6;
1339end;
1340
1341procedure TBlock.SetValue(Index: Integer; var Value);
1342begin
1343 if Index = 0 then BeginEnd := TBeginEnd(Value)
1344 else if Index = 1 then Types := TTypes(Value)
1345 else if Index = 2 then Variables := TVariables(Value)
1346 else if Index = 3 then Constants := TConstants(Value)
1347 else if Index = 4 then Functions := TFunctions(Value)
1348 else if Index = 5 then Procedures := TProcedures(Value)
1349 else inherited;
1350end;
1351
1352procedure TBlock.Clear;
1353begin
1354 Functions.Clear;
1355 Procedures.Clear;
1356 Constants.Clear;
1357 Variables.Clear;
1358 Types.Clear;
1359end;
1360
1361function TBlock.GetType(Name: string): TType;
1362begin
1363 Result := Types.SearchByName(Name);
1364 if not Assigned(Result) and Assigned(ParentBlock) then
1365 Result := ParentBlock.GetType(Name);
1366end;
1367
1368function TBlock.GetConstant(Name: string): TConstant;
1369begin
1370 Result := Constants.SearchByName(Name);
1371 if not Assigned(Result) and Assigned(ParentBlock) then
1372 Result := ParentBlock.GetConstant(Name);
1373end;
1374
1375function TBlock.GetVariable(Name: string): TVariable;
1376begin
1377 Result := Variables.SearchByName(Name);
1378 if not Assigned(Result) and Assigned(ParentBlock) then
1379 Result := ParentBlock.GetVariable(Name);
1380end;
1381
1382function TBlock.GetFunction(Name: string): TFunction;
1383begin
1384 Result := Functions.SearchByName(Name);
1385 if not Assigned(Result) and Assigned(ParentBlock) then
1386 Result := ParentBlock.GetFunction(Name);
1387end;
1388
1389function TBlock.GetProcedure(Name: string): TProcedure;
1390begin
1391 Result := Procedures.SearchByName(Name);
1392 if not Assigned(Result) and Assigned(ParentBlock) then
1393 Result := ParentBlock.GetProcedure(Name);
1394end;
1395
1396constructor TBlock.Create;
1397begin
1398 Constants := TConstants.Create;
1399 Constants.Parent := Self;
1400 Variables := TVariables.Create;
1401 Variables.Parent := Self;
1402 Functions := TFunctions.Create;
1403 Functions.Parent := Self;
1404 Procedures := TProcedures.Create;
1405 Procedures.Parent := Self;
1406 Types := TTypes.Create;
1407 Types.Parent := Self;
1408 BeginEnd := TBeginEnd.Create;
1409 BeginEnd.Parent := Self;
1410end;
1411
1412destructor TBlock.Destroy;
1413begin
1414 FreeAndNil(BeginEnd);
1415 FreeAndNil(Types);
1416 FreeAndNil(Variables);
1417 FreeAndNil(Constants);
1418 FreeAndNil(Functions);
1419 FreeAndNil(Procedures);
1420 inherited;
1421end;
1422
1423{ TBeginEnd }
1424
1425procedure TBeginEnd.GetValue(Index: Integer; out Value);
1426begin
1427 if Index = 0 then TCommands(Value) := Commands
1428 else inherited;
1429end;
1430
1431function TBeginEnd.GetField(Index: Integer): TField;
1432begin
1433 if Index = 0 then Result := TField.Create(dtList, 'Commands')
1434 else inherited;
1435end;
1436
1437function TBeginEnd.GetFieldsCount: Integer;
1438begin
1439 Result := 1;
1440end;
1441
1442procedure TBeginEnd.SetValue(Index: Integer; var Value);
1443begin
1444 if Index = 0 then Commands := TCommands(Value)
1445 else inherited;
1446end;
1447
1448procedure TBeginEnd.Clear;
1449begin
1450 Commands.Clear;
1451end;
1452
1453constructor TBeginEnd.Create;
1454begin
1455 Commands := TCommands.Create;
1456end;
1457
1458destructor TBeginEnd.Destroy;
1459begin
1460 FreeAndNil(Commands);
1461 inherited;
1462end;
1463
1464{ TProgram }
1465
1466procedure TProgram.GetValue(Index: Integer; out Value);
1467begin
1468 if Index = 0 then string(Value) := Name
1469 else if Index = 1 then TBlock(Value) := Block
1470 else raise Exception.Create(SIndexError);
1471end;
1472
1473function TProgram.GetField(Index: Integer): TField;
1474begin
1475 if Index = 0 then Result := TField.Create(dtString, 'Name')
1476 else if Index = 1 then Result := TField.Create(dtObject, 'Block')
1477 else inherited;
1478end;
1479
1480function TProgram.GetFieldsCount: Integer;
1481begin
1482 Result := 2;
1483end;
1484
1485procedure TProgram.SetValue(Index: Integer; var Value);
1486begin
1487 if Index = 0 then Name := string(Value)
1488 else if Index = 0 then Block := TBlock(Value)
1489 else raise Exception.Create(SIndexError);
1490end;
1491
1492procedure TProgram.Clear;
1493begin
1494 Name := '';
1495end;
1496
1497constructor TProgram.Create;
1498begin
1499 SystemBlock := TBlock.Create;
1500 Block := TBlock.Create;
1501end;
1502
1503destructor TProgram.Destroy;
1504begin
1505 FreeAndNil(Block);
1506 FreeAndNil(SystemBlock);
1507 inherited;
1508end;
1509
1510end.
1511
Note: See TracBrowser for help on using the repository browser.