source: branches/easy compiler/USourceCode.pas

Last change on this file was 149, checked in by chronos, 6 years ago
  • Fixed: Now arrays of string and integer are supported and executed correctly by executor.
  • Added: IfNotEqual command.
File size: 12.5 KB
Line 
1unit USourceCode;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, Contnrs;
9
10type
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
233implementation
234
235{ TCommandIfNotEqual }
236
237destructor TCommandIfNotEqual.Destroy;
238begin
239 Reference1.Free;
240 Reference2.Free;
241 inherited Destroy;
242end;
243
244{ TSourceType }
245
246function TSourceType.GetValueType: TSourceValueClass;
247begin
248 Result := nil;
249end;
250
251{ TSourceTypeInteger }
252
253function TSourceTypeInteger.GetValueType: TSourceValueClass;
254begin
255 Result := TSourceValueInteger;
256end;
257
258{ TSourceTypeString }
259
260function TSourceTypeString.GetValueType: TSourceValueClass;
261begin
262 Result := TSourceValueString;
263end;
264
265{ TSourceTypeArray }
266
267function TSourceTypeArray.GetValueType: TSourceValueClass;
268begin
269 Result := TSourceValueArray;
270end;
271
272{ TSourceValueArray }
273
274procedure TSourceValueArray.Assign(Source: TSourceValue);
275var
276 I: Integer;
277 Value: TSourceValue;
278begin
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');
290end;
291
292constructor TSourceValueArray.Create;
293begin
294 Items := TSourceValues.Create;
295end;
296
297destructor TSourceValueArray.Destroy;
298begin
299 Items.Free;
300 inherited Destroy;
301end;
302
303{ TCommandIfEqual }
304
305destructor TCommandIfEqual.Destroy;
306begin
307 Reference1.Free;
308 Reference2.Free;
309 inherited Destroy;
310end;
311
312{ TCommandRepeat }
313
314destructor TCommandRepeat.Destroy;
315begin
316 Command.Free;
317 inherited Destroy;
318end;
319
320{ TSourceConstant }
321
322destructor TSourceConstant.Destroy;
323begin
324 Value.Free;
325 inherited Destroy;
326end;
327
328{ TCommandFunctionCall }
329
330constructor TCommandFunctionCall.Create;
331begin
332 Parameters := TSourceReferences.Create;
333end;
334
335destructor TCommandFunctionCall.Destroy;
336begin
337 Parameters.Free;
338 inherited Destroy;
339end;
340
341{ TCommandBeginEnd }
342
343constructor TCommandBeginEnd.Create;
344begin
345 Commands := TSourceCommands.Create;
346end;
347
348destructor TCommandBeginEnd.Destroy;
349begin
350 Commands.Free;
351 inherited Destroy;
352end;
353
354{ TSourceTypes }
355
356function TSourceTypes.AddNew(Name: string; ClassType: TSourceTypeClass): TSourceType;
357begin
358 Result := ClassType.Create;
359 Result.Name := Name;
360 Add(Result);
361end;
362
363function TSourceTypes.Search(Name: string): TSourceType;
364var
365 Item: TSourceType;
366begin
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);
375end;
376
377{ TSourceValue }
378
379procedure TSourceValue.Assign(Source: TSourceValue);
380begin
381 raise Exception.Create('Value assign not implemented');
382end;
383
384constructor TSourceValue.Create;
385begin
386end;
387
388{ TSourceValueInteger }
389
390procedure TSourceValueInteger.Assign(Source: TSourceValue);
391begin
392 if Source is TSourceValueInteger then
393 Value := TSourceValueInteger(Source).Value
394 else raise Exception.Create('Type for assignment not matches');
395end;
396
397{ TSourceValueString }
398
399procedure TSourceValueString.Assign(Source: TSourceValue);
400begin
401 if Source is TSourceValueString then
402 Value := TSourceValueString(Source).Value
403 else raise Exception.Create('Type for assignment not matches');
404end;
405
406{ TSourceFunctions }
407
408function TSourceFunctions.AddNew(Name: string): TSourceFunction;
409begin
410 Result := TSourceFunction.Create;
411 Result.Name := Name;
412 Add(Result);
413end;
414
415function TSourceFunctions.Search(Name: string): TSourceFunction;
416var
417 Item: TSourceFunction;
418begin
419 Result := nil;
420 for Item in Self do
421 if Item.Name = Name then begin
422 Result := Item;
423 Break;
424 end;
425end;
426
427{ TSourceFunction }
428
429procedure TSourceFunction.AddParameter(Name: string; Kind: TSourceParameterKind
430 );
431var
432 Parameter: TSourceFunctionParameter;
433begin
434 Parameter := TSourceFunctionParameter.Create;
435 Parameter.Name := Name;
436 Parameter.Kind := Kind;
437 Parameters.Add(Parameter);
438end;
439
440constructor TSourceFunction.Create;
441begin
442 Parameters := TSourceFunctionParameters.Create;
443end;
444
445destructor TSourceFunction.Destroy;
446begin
447 Parameters.Free;
448 inherited Destroy;
449end;
450
451{ TSourceVariables }
452
453function TSourceVariables.AddNew(Name: string;ValueType: TSourceType): TSourceVariable;
454begin
455 Result := TSourceVariable.Create;
456 Result.Name := Name;
457 Result.ValueType := ValueType;
458 Add(Result);
459end;
460
461function TSourceVariables.Search(Name: string): TSourceVariable;
462var
463 Item: TSourceVariable;
464begin
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);
473end;
474
475{ TSourceConstants }
476
477function TSourceConstants.AddNewString(Value: string; Name: string
478 ): TSourceConstant;
479begin
480 Result := TSourceConstant.Create;
481 Result.Value := TSourceValueString.Create;
482 TSourceValueString(Result.Value).Value := Value;
483 Result.Name := '';
484 Add(Result);
485end;
486
487function TSourceConstants.AddNewInteger(Value: Integer; Name: string
488 ): TSourceConstant;
489begin
490 Result := TSourceConstant.Create;
491 Result.Value := TSourceValueInteger.Create;
492 TSourceValueInteger(Result.Value).Value := Value;
493 Result.Name := '';
494 Add(Result);
495end;
496
497function TSourceConstants.Search(Name: string): TSourceConstant;
498var
499 Item: TSourceConstant;
500begin
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);
509end;
510
511{ TCommandFunctionCall }
512
513{ TSourceCode }
514
515procedure TSourceCode.InitFunctions;
516var
517 Funct: TSourceFunction;
518 Typ: TSourceType;
519begin
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);
555end;
556
557constructor TSourceCode.Create;
558begin
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;
566end;
567
568destructor TSourceCode.Destroy;
569begin
570 Main.Free;
571 Functions.Free;
572 Variables.Free;
573 Constants.Free;
574 Types.Free;
575 inherited Destroy;
576end;
577
578
579end.
580
Note: See TracBrowser for help on using the repository browser.