1 | unit UMainForm;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
7 | Dialogs, StdCtrls;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TMemoryType = (mtProgram, mtData, mtEEPROM);
|
---|
11 |
|
---|
12 | TModuleType = (mdProgram, mdUnit, mdLibrary, mdPackage);
|
---|
13 |
|
---|
14 | TInstruction = (inNone, inJump, inConditionalJump, inExpressionEvaluation,
|
---|
15 | inReturn);
|
---|
16 |
|
---|
17 | TNodeType = (ntNone, ntVariable, nTFunction, ntConstant, ntOperator);
|
---|
18 |
|
---|
19 | TValue = array of Byte;
|
---|
20 |
|
---|
21 | TCompiler = class;
|
---|
22 | TCommonBlock = class;
|
---|
23 | TTypeList = class;
|
---|
24 | TConstantList = class;
|
---|
25 | TVariableList = class;
|
---|
26 | TFunctionList = class;
|
---|
27 | TExpression = class;
|
---|
28 | TOperationList = class;
|
---|
29 | TFunction = class;
|
---|
30 |
|
---|
31 | TDevice = class
|
---|
32 | Family: string;
|
---|
33 | Memory: array[TMemoryType] of Integer;
|
---|
34 | end;
|
---|
35 |
|
---|
36 | TCommonBlock = class
|
---|
37 | Name: string;
|
---|
38 | Parent: TCommonBlock;
|
---|
39 | Constants: TConstantList;
|
---|
40 | Types: TTypeList;
|
---|
41 | Variables: TVariableList;
|
---|
42 | Methods: TFunctionList;
|
---|
43 | Operations: TOperationList;
|
---|
44 | procedure AllocateMemory;
|
---|
45 | constructor Create; virtual;
|
---|
46 | destructor Destroy; override;
|
---|
47 | procedure ParseDefinitions(Compiler: TCompiler);
|
---|
48 | function ParseExpression(Compiler: TCompiler): TExpression;
|
---|
49 | procedure ParseProgramCode(Compiler: TCompiler);
|
---|
50 | procedure ParseOperation(Compiler: TCompiler);
|
---|
51 | procedure GenerateAssembler(Compiler: TCompiler; LabelPrefix: string);
|
---|
52 | procedure CheckReferences;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | TType = class
|
---|
56 | Parent: TTypeList;
|
---|
57 | Name: string;
|
---|
58 | Size: Integer;
|
---|
59 | UsedType: TType;
|
---|
60 | procedure Parse(Compiler: TCompiler);
|
---|
61 | end;
|
---|
62 |
|
---|
63 | TTypeList = class(TList)
|
---|
64 | Parent: TCommonBlock;
|
---|
65 | procedure Parse(Compiler: TCompiler);
|
---|
66 | function Search(Name: string): TType;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | TConstant = class
|
---|
70 | Name: string;
|
---|
71 | ValueType: TType;
|
---|
72 | Value: TValue;
|
---|
73 | procedure Parse(Compiler: TCompiler);
|
---|
74 | end;
|
---|
75 |
|
---|
76 | TConstantList = class(TList)
|
---|
77 | Parent: TCommonBlock;
|
---|
78 | function Search(Name: string): TConstant;
|
---|
79 | procedure Parse(Compiler: TCompiler);
|
---|
80 | end;
|
---|
81 |
|
---|
82 | TVariable = class
|
---|
83 | Name: string;
|
---|
84 | ValueType: TType;
|
---|
85 | Value: TValue;
|
---|
86 | procedure Parse(Compiler: TCompiler);
|
---|
87 | end;
|
---|
88 |
|
---|
89 | TVariableList = class(TList)
|
---|
90 | Parent: TCommonBlock;
|
---|
91 | procedure Parse(Compiler: TCompiler);
|
---|
92 | function Search(Name: string): TVariable;
|
---|
93 | end;
|
---|
94 |
|
---|
95 | TExpression = class
|
---|
96 | NodeType: TNodeType;
|
---|
97 | Variable: TVariable;
|
---|
98 | Method: TFunction;
|
---|
99 | Value: TValue;
|
---|
100 | OperatorName: string;
|
---|
101 | SubItems: TList; // TList<TExpression>
|
---|
102 | Associated: Boolean;
|
---|
103 | constructor Create;
|
---|
104 | destructor Destroy; override;
|
---|
105 | procedure GenerateAssembler(Compiler: TCompiler; LabelPrefix: string);
|
---|
106 | end;
|
---|
107 |
|
---|
108 | TOperation = class
|
---|
109 | Instruction: TInstruction;
|
---|
110 | ExpressionTree: TExpression;
|
---|
111 | GotoAddress: Integer;
|
---|
112 | Negative: Boolean;
|
---|
113 | Referenced: Boolean;
|
---|
114 | end;
|
---|
115 |
|
---|
116 | TOperationList = class(TList)
|
---|
117 |
|
---|
118 | end;
|
---|
119 |
|
---|
120 | TFunction = class(TCommonBlock)
|
---|
121 | Parameters: TList; // TList<TParameter>
|
---|
122 | ResultType: TType;
|
---|
123 | constructor Create; override;
|
---|
124 | destructor Destroy; override;
|
---|
125 | procedure Parse(Compiler: TCompiler);
|
---|
126 | end;
|
---|
127 |
|
---|
128 | TFunctionList = class(TList)
|
---|
129 | Parent: TCommonBlock;
|
---|
130 | procedure Parse(Compiler: TCompiler);
|
---|
131 | function Search(Name: string): TFunction;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | TModule = class(TCommonBlock)
|
---|
135 | ModuleType: TModuleType;
|
---|
136 | UsedModules: TList; // TList<TModule>
|
---|
137 | constructor Create; override;
|
---|
138 | destructor Destroy; override;
|
---|
139 | procedure Parse(Compiler: TCompiler);
|
---|
140 | private
|
---|
141 | procedure ParseUnit(Compiler: TCompiler);
|
---|
142 | procedure ParseProgram(Compiler: TCompiler);
|
---|
143 | procedure Clear;
|
---|
144 | end;
|
---|
145 |
|
---|
146 | TProgram = class
|
---|
147 | Device: TDevice;
|
---|
148 | Modules: TList; // TList<TModule>
|
---|
149 | constructor Create;
|
---|
150 | destructor Destroy; override;
|
---|
151 | procedure Parse(Compiler: TCompiler);
|
---|
152 | procedure AllocateMemory;
|
---|
153 | procedure GenerateAssembler(Compiler: TCompiler);
|
---|
154 | end;
|
---|
155 |
|
---|
156 | TAssemblerLine = class
|
---|
157 | LabelName: string;
|
---|
158 | Instruction: string;
|
---|
159 | Operand1: string;
|
---|
160 | Operand2: string;
|
---|
161 | SourceCode: string;
|
---|
162 | function AsString: string;
|
---|
163 | end;
|
---|
164 |
|
---|
165 | TCompiler = class
|
---|
166 | private
|
---|
167 | SourceCode: TStringList;
|
---|
168 | CodePosition: Integer;
|
---|
169 | AssemblyCode: TList; // TList<TAssemblerLine>
|
---|
170 | ProgramCode: TProgram;
|
---|
171 | procedure ErrorMessage(Text: string);
|
---|
172 | function IsAlphanumeric(Character: Char): Boolean;
|
---|
173 | function NextCode(Shift: Boolean = False): string;
|
---|
174 | function ReadCode: string;
|
---|
175 | procedure Expect(Code: string);
|
---|
176 | function IsWhiteSpace(Character: Char): Boolean;
|
---|
177 | function IsAlphabetic(Character: Char): Boolean;
|
---|
178 | function IsIdentificator(Text: string): Boolean;
|
---|
179 | function IsKeyword(Text: string): Boolean;
|
---|
180 | function IsOperator(Text: string): Boolean;
|
---|
181 | procedure GenerateAssemblyCode;
|
---|
182 | procedure AddInstruction(LabelName, Instruction, Operand1, Operand2: string);
|
---|
183 | public
|
---|
184 | constructor Create;
|
---|
185 | procedure Compile(SourceCode: TStrings);
|
---|
186 | destructor Destroy; override;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | TMainForm = class(TForm)
|
---|
190 | Memo1: TMemo;
|
---|
191 | Button1: TButton;
|
---|
192 | Memo2: TMemo;
|
---|
193 | Memo3: TMemo;
|
---|
194 | procedure FormShow(Sender: TObject);
|
---|
195 | procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
196 | procedure Button1Click(Sender: TObject);
|
---|
197 | procedure FormCreate(Sender: TObject);
|
---|
198 | procedure FormDestroy(Sender: TObject);
|
---|
199 | private
|
---|
200 | { Private declarations }
|
---|
201 | public
|
---|
202 | Compiler: TCompiler;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | const
|
---|
206 | KeyWords: array[0..37] of string = ('program', 'unit', 'uses', 'begin', 'end',
|
---|
207 | 'type', 'const', 'var', 'array', 'record', 'absolute', 'virtual', 'class',
|
---|
208 | 'set', 'private', 'public', 'interface', 'implementation', 'finalization',
|
---|
209 | 'initialization', 'for', 'while', 'if', 'case', 'of', 'pointer',
|
---|
210 | 'object', 'packed', 'procedure', 'function', 'to', 'do', 'downto', 'repeat',
|
---|
211 | 'until', 'then', 'asm', 'else');
|
---|
212 | Operators: array[0..22] of string = ('@', 'not', '*', 'and', '/', 'shl',
|
---|
213 | 'shr', 'as', 'div', 'mod', 'or', 'xor', '-', '+', '=', '>', '<', '<>', '<=',
|
---|
214 | '>=', 'is', 'in', ':=');
|
---|
215 |
|
---|
216 | var
|
---|
217 | MainForm: TMainForm;
|
---|
218 |
|
---|
219 | implementation
|
---|
220 |
|
---|
221 | {$R *.dfm}
|
---|
222 |
|
---|
223 | procedure TMainForm.Button1Click(Sender: TObject);
|
---|
224 | var
|
---|
225 | I: Integer;
|
---|
226 | begin
|
---|
227 | Compiler.Compile(Memo1.Lines);
|
---|
228 | Memo2.Clear;
|
---|
229 | for I := 0 to Compiler.AssemblyCode.Count - 1 do
|
---|
230 | Memo2.Lines.Add(TAssemblerLine(Compiler.AssemblyCode[I]).AsString);
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
234 | begin
|
---|
235 | Memo1.Lines.SaveToFile('Example.pas');
|
---|
236 | end;
|
---|
237 |
|
---|
238 | procedure TMainForm.FormCreate(Sender: TObject);
|
---|
239 | begin
|
---|
240 | Compiler := TCompiler.Create;
|
---|
241 | end;
|
---|
242 |
|
---|
243 | procedure TMainForm.FormDestroy(Sender: TObject);
|
---|
244 | begin
|
---|
245 | Compiler.Destroy;
|
---|
246 | end;
|
---|
247 |
|
---|
248 | procedure TMainForm.FormShow(Sender: TObject);
|
---|
249 | var
|
---|
250 | I: Integer;
|
---|
251 | begin
|
---|
252 | Memo1.Lines.LoadFromFile('Example.pas');
|
---|
253 | Compiler.Compile(Memo1.Lines);
|
---|
254 | Memo2.Clear;
|
---|
255 | for I := 0 to Compiler.AssemblyCode.Count - 1 do
|
---|
256 | Memo2.Lines.Add(TAssemblerLine(Compiler.AssemblyCode[I]).AsString);
|
---|
257 |
|
---|
258 | end;
|
---|
259 |
|
---|
260 | { TCompiler }
|
---|
261 |
|
---|
262 | procedure TCompiler.AddInstruction(LabelName, Instruction, Operand1,
|
---|
263 | Operand2: string);
|
---|
264 | var
|
---|
265 | NewLine: TAssemblerLine;
|
---|
266 | begin
|
---|
267 | NewLine := TAssemblerLine.Create;
|
---|
268 | AssemblyCode.Add(NewLine);
|
---|
269 | NewLine.LabelName := LabelName;
|
---|
270 | NewLine.Instruction := Instruction;
|
---|
271 | NewLine.Operand1 := Operand1;
|
---|
272 | NewLine.Operand2 := Operand2;
|
---|
273 | end;
|
---|
274 |
|
---|
275 | procedure TCompiler.Compile(SourceCode: TStrings);
|
---|
276 | begin
|
---|
277 | MainForm.Memo3.Clear;
|
---|
278 | Self.SourceCode.Assign(SourceCode);
|
---|
279 | CodePosition := 1;
|
---|
280 | ProgramCode.Parse(Self);
|
---|
281 | ProgramCode.AllocateMemory;
|
---|
282 | AssemblyCode.Clear;
|
---|
283 | GenerateAssemblyCode;
|
---|
284 | end;
|
---|
285 |
|
---|
286 | constructor TCompiler.Create;
|
---|
287 | begin
|
---|
288 | SourceCode := TStringList.Create;
|
---|
289 | AssemblyCode := TList.Create;
|
---|
290 | ProgramCode := TProgram.Create;
|
---|
291 | end;
|
---|
292 |
|
---|
293 | destructor TCompiler.Destroy;
|
---|
294 | begin
|
---|
295 | ProgramCode.Free;
|
---|
296 | AssemblyCode.Free;
|
---|
297 | SourceCode.Free;
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TCompiler.ErrorMessage(Text: string);
|
---|
301 | begin
|
---|
302 | //ShowMessage(Text);
|
---|
303 | MainForm.Memo3.Lines.Add(Text);
|
---|
304 | end;
|
---|
305 |
|
---|
306 | procedure TCompiler.Expect(Code: string);
|
---|
307 | begin
|
---|
308 | if NextCode <> Code then begin
|
---|
309 | ErrorMessage('Expected ' + Code + ' but ' + NextCode + ' found.');
|
---|
310 | end;
|
---|
311 | ReadCode;
|
---|
312 | end;
|
---|
313 |
|
---|
314 | procedure TCompiler.GenerateAssemblyCode;
|
---|
315 | begin
|
---|
316 | ProgramCode.GenerateAssembler(Self);
|
---|
317 | end;
|
---|
318 |
|
---|
319 | function TCompiler.IsAlphabetic(Character: Char): Boolean;
|
---|
320 | begin
|
---|
321 | Result := (Character in ['a'..'z']) or (Character in ['A'..'Z']);
|
---|
322 | end;
|
---|
323 |
|
---|
324 | function TCompiler.IsAlphanumeric(Character: Char): Boolean;
|
---|
325 | begin
|
---|
326 | Result := IsAlphabetic(Character) or (Character in ['0'..'9']);
|
---|
327 | end;
|
---|
328 |
|
---|
329 | function TCompiler.IsKeyword(Text: string): Boolean;
|
---|
330 | var
|
---|
331 | I: Integer;
|
---|
332 | begin
|
---|
333 | Result := False;
|
---|
334 | for I := 0 to High(Keywords) do
|
---|
335 | if Keywords[I] = Text then
|
---|
336 | Result := True;
|
---|
337 | end;
|
---|
338 |
|
---|
339 | function TCompiler.IsOperator(Text: string): Boolean;
|
---|
340 | var
|
---|
341 | I: Integer;
|
---|
342 | begin
|
---|
343 | Result := False;
|
---|
344 | for I := 0 to High(Operators) do
|
---|
345 | if Operators[I] = Text then
|
---|
346 | Result := True;
|
---|
347 | end;
|
---|
348 |
|
---|
349 | function TCompiler.IsIdentificator(Text: string): Boolean;
|
---|
350 | var
|
---|
351 | I: Integer;
|
---|
352 | begin
|
---|
353 | Result := True;
|
---|
354 | if Length(Text) = 0 then Result := False;
|
---|
355 | if IsKeyWord(Text) then Result := False;
|
---|
356 | if Length(Text) > 0 then
|
---|
357 | if not (Text[1] in ['a'..'z', 'A'..'Z', '%', '_']) then
|
---|
358 | Result := False;
|
---|
359 | for I := 2 to Length(Text) do
|
---|
360 | if not (Text[i] in ['a'..'z', 'A'..'Z', '0'..'9', '_']) then
|
---|
361 | Result := False;
|
---|
362 | end;
|
---|
363 |
|
---|
364 | function TCompiler.IsWhiteSpace(Character: Char): Boolean;
|
---|
365 | begin
|
---|
366 | Result := (Character = ' ') or (Character = #13) or (Character = #10);
|
---|
367 | end;
|
---|
368 |
|
---|
369 | function TCompiler.NextCode(Shift: Boolean = False): string;
|
---|
370 | var
|
---|
371 | I: Integer;
|
---|
372 | // II: Integer;
|
---|
373 | J: Integer;
|
---|
374 | const
|
---|
375 | SpecChar: set of char = [';', '.', ',', ':', '(', ')', '[', ']', '+', '-', '/', '*',
|
---|
376 | '^', '=', '<' , '>' , '@'];
|
---|
377 | DoubleSpecChar : array[1..7] of string = (':=', '..', '<=', '>=', '<>', '+=', '-=');
|
---|
378 | begin
|
---|
379 | Result := '';
|
---|
380 | J := CodePosition;
|
---|
381 | I := CodePosition;
|
---|
382 | with SourceCode do
|
---|
383 | while Result = '' do begin
|
---|
384 | while IsWhiteSpace(Text[I]) do Inc(I);
|
---|
385 | J := I;
|
---|
386 | if Copy(Text, J, 1) = '//' then begin
|
---|
387 | while (Text[I] <> #13) and (Text[I] <> #10) do Inc(I);
|
---|
388 | Result := '';
|
---|
389 | end else
|
---|
390 | if Copy(Text, J, 1) = '{' then begin
|
---|
391 | while (Text[I] <> '}') do Inc(I);
|
---|
392 | Result := '';
|
---|
393 | end else
|
---|
394 | if Copy(Text, J, 1) = '(*' then begin
|
---|
395 | while not((Text[I] = '*') and (Text[I + 1] = ')')) do Inc(I);
|
---|
396 | Result := '';
|
---|
397 | end else
|
---|
398 | if Text[J] = '''' then begin
|
---|
399 | I := J + 1;
|
---|
400 | while not ((Text[I] = '''') and (Text[I + 1] <> '''')) do Inc(I);
|
---|
401 | Inc(I);
|
---|
402 | Result := Copy(Text, J, I - J );
|
---|
403 | end else
|
---|
404 | if (Text[J] in SpecChar) then begin
|
---|
405 | if (Text[J + 1] in SpecChar) then begin
|
---|
406 | for I := 0 to High(DoubleSpecChar) do
|
---|
407 | if Copy(Text, J, 2) = DoubleSpecChar[I] then begin
|
---|
408 | Result := Copy(Text, J, 2);
|
---|
409 | Inc(J, 2);
|
---|
410 | Break;
|
---|
411 | end;
|
---|
412 | I := J;
|
---|
413 | end;
|
---|
414 | if Result = '' then begin
|
---|
415 | Result := Text[J];
|
---|
416 | Inc(I);
|
---|
417 | end;
|
---|
418 | end else begin
|
---|
419 | while IsAlphanumeric(Text[I]) do Inc(I);
|
---|
420 | Result := LowerCase(Copy(Text, J, I - J));
|
---|
421 | end;
|
---|
422 | J := I;
|
---|
423 | end;
|
---|
424 | if Shift then CodePosition := J;
|
---|
425 | end;
|
---|
426 |
|
---|
427 | procedure TModule.ParseUnit(Compiler: TCompiler);
|
---|
428 | begin
|
---|
429 | with Compiler do begin
|
---|
430 | Expect('unit');
|
---|
431 | with TModule(ProgramCode.Modules[0]) do begin
|
---|
432 | Name := ReadCode;
|
---|
433 | ModuleType := mdUnit;
|
---|
434 | end;
|
---|
435 | Expect(';');
|
---|
436 | //ParseInterface;
|
---|
437 | //ParseImplementation;
|
---|
438 | end;
|
---|
439 | end;
|
---|
440 |
|
---|
441 | function TCompiler.ReadCode: string;
|
---|
442 | begin
|
---|
443 | Result := NextCode(True);
|
---|
444 | end;
|
---|
445 |
|
---|
446 | { TFunction }
|
---|
447 |
|
---|
448 | constructor TFunction.Create;
|
---|
449 | begin
|
---|
450 | inherited;
|
---|
451 | Parameters := TList.Create;
|
---|
452 | ResultType := TType.Create;
|
---|
453 | end;
|
---|
454 |
|
---|
455 | destructor TFunction.Destroy;
|
---|
456 | begin
|
---|
457 | Parameters.Free;
|
---|
458 | ResultType.Free;
|
---|
459 | inherited;
|
---|
460 | end;
|
---|
461 |
|
---|
462 | procedure TFunction.Parse(Compiler: TCompiler);
|
---|
463 | begin
|
---|
464 | with Compiler do begin
|
---|
465 | if NextCode = 'var' then TVariableList(Variables).Parse(Compiler)
|
---|
466 | else if NextCode = 'const' then TConstantList(Constants).Parse(Compiler)
|
---|
467 | else if NextCode = 'type' then TTypeList(Types).Parse(Compiler)
|
---|
468 | else ProgramCode.Parse(Compiler);
|
---|
469 | end;
|
---|
470 | end;
|
---|
471 |
|
---|
472 | { TProgram }
|
---|
473 |
|
---|
474 | procedure TProgram.AllocateMemory;
|
---|
475 | var
|
---|
476 | I: Integer;
|
---|
477 | begin
|
---|
478 | for I := 0 to Modules.Count - 1 do
|
---|
479 | TModule(Modules[I]).AllocateMemory;
|
---|
480 | end;
|
---|
481 |
|
---|
482 | constructor TProgram.Create;
|
---|
483 | begin
|
---|
484 | Device := TDevice.Create;
|
---|
485 | Modules := TList.Create;
|
---|
486 | end;
|
---|
487 |
|
---|
488 | destructor TProgram.Destroy;
|
---|
489 | begin
|
---|
490 |
|
---|
491 | end;
|
---|
492 |
|
---|
493 | procedure TProgram.GenerateAssembler(Compiler: TCompiler);
|
---|
494 | var
|
---|
495 | I: Integer;
|
---|
496 | begin
|
---|
497 | for I := 0 to Modules.Count - 1 do
|
---|
498 | TModule(Modules[I]).GenerateAssembler(Compiler, '');
|
---|
499 | end;
|
---|
500 |
|
---|
501 | procedure TProgram.Parse(Compiler: TCompiler);
|
---|
502 | var
|
---|
503 | I: Integer;
|
---|
504 | begin
|
---|
505 | for I := 0 to Modules.Count - 1 do
|
---|
506 | TModule(Modules[I]).Clear;
|
---|
507 | Modules.Clear;
|
---|
508 | with TModule(Modules[Modules.Add(TModule.Create)]) do begin
|
---|
509 | Name := 'main';
|
---|
510 | with TType(Types[Types.Add(TType.Create)]) do begin
|
---|
511 | Name := 'byte';
|
---|
512 | Size := 1;
|
---|
513 | UsedType := nil;
|
---|
514 | end;
|
---|
515 | end;
|
---|
516 | TModule(Modules[0]).Parse(Compiler);
|
---|
517 | end;
|
---|
518 |
|
---|
519 | { TConstant }
|
---|
520 |
|
---|
521 | procedure TConstant.Parse(Compiler: TCompiler);
|
---|
522 | begin
|
---|
523 |
|
---|
524 | end;
|
---|
525 |
|
---|
526 | { TConstantList }
|
---|
527 |
|
---|
528 | procedure TConstantList.Parse(Compiler: TCompiler);
|
---|
529 | begin
|
---|
530 | // Compiler.Expect('const');
|
---|
531 | // while Compiler.IsIdentificator(Compiler.NextCode) do
|
---|
532 | // TConstant(Items[Add(TConstant.Create)]).Parse(Compiler);
|
---|
533 | end;
|
---|
534 |
|
---|
535 | function TConstantList.Search(Name: string): TConstant;
|
---|
536 | var
|
---|
537 | I: Integer;
|
---|
538 | begin
|
---|
539 | I := 0;
|
---|
540 | while (I < Count) and (TConstant(Items[I]).Name <> Name) do Inc(I);
|
---|
541 | if I < Count then Result := Items[I] else begin
|
---|
542 | if Assigned(Parent.Parent) then Result := Parent.Parent.Constants.Search(Name)
|
---|
543 | else begin
|
---|
544 | Result := nil;
|
---|
545 | end;
|
---|
546 | end;
|
---|
547 | end;
|
---|
548 |
|
---|
549 | { TModule }
|
---|
550 |
|
---|
551 | procedure TModule.Clear;
|
---|
552 | begin
|
---|
553 | Variables.Clear;
|
---|
554 | Constants.Clear;
|
---|
555 | Methods.Clear;
|
---|
556 | Operations.Clear;
|
---|
557 | end;
|
---|
558 |
|
---|
559 | constructor TModule.Create;
|
---|
560 | begin
|
---|
561 | inherited;
|
---|
562 | UsedModules := TList.Create;
|
---|
563 | end;
|
---|
564 |
|
---|
565 | destructor TModule.Destroy;
|
---|
566 | begin
|
---|
567 | UsedModules.Destroy;
|
---|
568 | inherited;
|
---|
569 | end;
|
---|
570 |
|
---|
571 | procedure TModule.ParseProgram(Compiler: TCompiler);
|
---|
572 | var
|
---|
573 | Identifier: string;
|
---|
574 | begin
|
---|
575 | with Compiler do begin
|
---|
576 | if NextCode = 'program' then begin
|
---|
577 | Expect('program');
|
---|
578 | Name := ReadCode;
|
---|
579 | ModuleType := mdProgram;
|
---|
580 | Expect(';');
|
---|
581 | end else Name := '';
|
---|
582 |
|
---|
583 | // Uses section
|
---|
584 | if NextCode = 'uses' then begin
|
---|
585 | Identifier := ReadCode;
|
---|
586 | while NextCode = ',' do begin
|
---|
587 | Identifier := ReadCode;
|
---|
588 |
|
---|
589 | end;
|
---|
590 | end;
|
---|
591 | ParseDefinitions(Compiler);
|
---|
592 | end;
|
---|
593 | end;
|
---|
594 |
|
---|
595 | procedure TModule.Parse(Compiler: TCompiler);
|
---|
596 | begin
|
---|
597 | with Compiler do begin
|
---|
598 | if NextCode = 'program' then ParseProgram(Compiler)
|
---|
599 | else if NextCode = 'unit' then ParseUnit(Compiler)
|
---|
600 | else ParseProgram(Compiler);
|
---|
601 | end;
|
---|
602 | end;
|
---|
603 |
|
---|
604 | procedure TCommonBlock.AllocateMemory;
|
---|
605 | begin
|
---|
606 | // for I := 0 to Variables - 1 do
|
---|
607 |
|
---|
608 | end;
|
---|
609 |
|
---|
610 | procedure TCommonBlock.CheckReferences;
|
---|
611 | var
|
---|
612 | I: Integer;
|
---|
613 | begin
|
---|
614 | for I := 0 to Operations.Count - 1 do
|
---|
615 | with TOperation(Operations[I]) do begin
|
---|
616 | if (Instruction = inJump) or (Instruction = inConditionalJump) then
|
---|
617 | TOperation(Operations[GotoAddress]).Referenced := True;
|
---|
618 | end;
|
---|
619 | end;
|
---|
620 |
|
---|
621 | constructor TCommonBlock.Create;
|
---|
622 | begin
|
---|
623 | Constants := TConstantList.Create;
|
---|
624 | Constants.Parent := Self;
|
---|
625 | Types := TTypeList.Create;
|
---|
626 | Types.Parent := Self;
|
---|
627 | Variables := TVariableList.Create;
|
---|
628 | Variables.Parent := Self;
|
---|
629 | Methods := TFunctionList.Create;
|
---|
630 | Methods.Parent := Self;
|
---|
631 | Operations := TOperationList.Create;
|
---|
632 | end;
|
---|
633 |
|
---|
634 | destructor TCommonBlock.Destroy;
|
---|
635 | begin
|
---|
636 | Constants.Destroy;
|
---|
637 | Types.Destroy;
|
---|
638 | Variables.Destroy;
|
---|
639 | Methods.Destroy;
|
---|
640 | Operations.Destroy;
|
---|
641 | inherited;
|
---|
642 | end;
|
---|
643 |
|
---|
644 | procedure TCommonBlock.GenerateAssembler(Compiler: TCompiler; LabelPrefix: string);
|
---|
645 | var
|
---|
646 | I: Integer;
|
---|
647 | LabelName: string;
|
---|
648 | begin
|
---|
649 | with Compiler do
|
---|
650 | for I := 0 to Operations.Count - 1 do
|
---|
651 | with TOperation(Operations[I]) do begin
|
---|
652 | if Referenced then LabelName := Name + '_L' + IntToStr(I)
|
---|
653 | else LabelName := '';
|
---|
654 | case Instruction of
|
---|
655 | inJump: begin
|
---|
656 | AddInstruction(LabelName, 'JMP', Name + '_L' + IntToStr(GotoAddress), '');
|
---|
657 | end;
|
---|
658 | inConditionalJump: begin
|
---|
659 | ExpressionTree.GenerateAssembler(Compiler, LabelPrefix + '_L' + IntToStr(GotoAddress));
|
---|
660 | AddInstruction(LabelName, 'BRCS', Name + '_L' + IntToStr(GotoAddress), '');
|
---|
661 | end;
|
---|
662 | inExpressionEvaluation: begin
|
---|
663 | if LabelName <> '' then AddInstruction(LabelName, '', '', '');
|
---|
664 | ExpressionTree.GenerateAssembler(Compiler, Name + '_L' + IntToStr(GotoAddress));
|
---|
665 | end;
|
---|
666 | inReturn:
|
---|
667 | AddInstruction(LabelName, 'RET', '', '');
|
---|
668 | end;
|
---|
669 | end;
|
---|
670 | end;
|
---|
671 |
|
---|
672 | procedure TCommonBlock.ParseDefinitions(Compiler: TCompiler);
|
---|
673 | begin
|
---|
674 | with Compiler do begin
|
---|
675 | while NextCode <> '.' do begin
|
---|
676 | if NextCode = 'var' then TVariableList(Variables).Parse(Compiler)
|
---|
677 | else if NextCode = 'const' then TConstantList(Constants).Parse(Compiler)
|
---|
678 | else if NextCode = 'type' then TTypeList(Types).Parse(Compiler)
|
---|
679 | else begin
|
---|
680 | ParseProgramCode(Compiler);
|
---|
681 | Break;
|
---|
682 | end;
|
---|
683 | end;
|
---|
684 | end;
|
---|
685 | end;
|
---|
686 |
|
---|
687 | function TCommonBlock.ParseExpression(Compiler: TCompiler): TExpression;
|
---|
688 | var
|
---|
689 | Identifier: string;
|
---|
690 | NewVariable: TVariable;
|
---|
691 | Method: TFunction;
|
---|
692 | Constant: TConstant;
|
---|
693 | // Brackets: Integer;
|
---|
694 | Expressions: TList; // TList<TExpression>;
|
---|
695 | I: Integer;
|
---|
696 | II: Integer;
|
---|
697 | begin
|
---|
698 | Expressions := TList.Create;
|
---|
699 | Expressions.Add(TExpression.Create);
|
---|
700 | with Compiler do begin
|
---|
701 | while ((NextCode <> ';') and (NextCode <> ',') and (not IsKeyWord(NextCode))) and
|
---|
702 | not (((NextCode = ')') or (NextCode = ']'))) do begin
|
---|
703 | Identifier := ReadCode;
|
---|
704 | if Identifier = '(' then begin
|
---|
705 | with TExpression(Expressions[Expressions.Count - 1]) do begin
|
---|
706 | SubItems[1] := ParseExpression(Compiler);
|
---|
707 | end;
|
---|
708 | with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
|
---|
709 | SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
|
---|
710 | end;
|
---|
711 | Expect(')');
|
---|
712 | end else
|
---|
713 | if IsOperator(Identifier) then begin
|
---|
714 | TExpression(Expressions[Expressions.Count - 1]).OperatorName := Identifier;
|
---|
715 | TExpression(Expressions[Expressions.Count - 1]).NodeType := ntOperator;
|
---|
716 | end else
|
---|
717 | if IsIdentificator(Identifier) then begin
|
---|
718 | NewVariable := Variables.Search(Identifier);
|
---|
719 | if Assigned(NewVariable) then begin
|
---|
720 | with TExpression(Expressions[Expressions.Count - 1]) do begin
|
---|
721 | SubItems[1] := TExpression.Create;
|
---|
722 | TExpression(SubItems[1]).NodeType := ntVariable;
|
---|
723 | TExpression(SubItems[1]).Variable := NewVariable;
|
---|
724 | end;
|
---|
725 | with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
|
---|
726 | SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
|
---|
727 | end;
|
---|
728 | end else begin
|
---|
729 | Method := Methods.Search(Identifier);
|
---|
730 | if Assigned(Method) then begin
|
---|
731 | with TExpression(Expressions[Expressions.Count - 1]) do begin
|
---|
732 | SubItems[1] := TExpression.Create;
|
---|
733 | if NextCode = '(' then // Method with parameters
|
---|
734 | with TExpression(SubItems[1]) do begin
|
---|
735 | Expect('(');
|
---|
736 | SubItems.Add(ParseExpression(Compiler));
|
---|
737 | while NextCode = ',' do begin
|
---|
738 | Expect(',');
|
---|
739 | SubItems.Add(ParseExpression(Compiler));
|
---|
740 | end;
|
---|
741 | Expect(')');
|
---|
742 | end;
|
---|
743 | TExpression(SubItems[1]).NodeType := nTFunction;
|
---|
744 | TExpression(SubItems[1]).Method := Method;
|
---|
745 | end;
|
---|
746 | with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
|
---|
747 | SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
|
---|
748 | end;
|
---|
749 | end else begin
|
---|
750 | Constant := Constants.Search(Identifier);
|
---|
751 | if Assigned(Constant) then begin
|
---|
752 | with TExpression(Expressions[Expressions.Count - 1]) do begin
|
---|
753 | SubItems[1] := TExpression.Create;
|
---|
754 | TExpression(SubItems[1]).NodeType := ntConstant;
|
---|
755 | TExpression(SubItems[1]).Value := Constant.Value;
|
---|
756 | end;
|
---|
757 | with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
|
---|
758 | SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
|
---|
759 | end;
|
---|
760 | end else begin
|
---|
761 | ErrorMessage('Neznámý identifikátor: ' + Identifier);
|
---|
762 | end;
|
---|
763 | end;
|
---|
764 | end;
|
---|
765 | end else
|
---|
766 | begin
|
---|
767 | with TExpression(Expressions[Expressions.Count - 1]) do begin
|
---|
768 | SubItems[1] := TExpression.Create;
|
---|
769 | TExpression(SubItems[1]).NodeType := ntConstant;
|
---|
770 |
|
---|
771 | if Identifier[1] = '''' then begin
|
---|
772 | SetLength(TExpression(SubItems[1]).Value, Length(Identifier));
|
---|
773 | for I := 1 to Length(Identifier) do TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]);
|
---|
774 | end else begin
|
---|
775 | SetLength(TExpression(SubItems[1]).Value, 1);
|
---|
776 | TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);
|
---|
777 | end;
|
---|
778 | end;
|
---|
779 | with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin
|
---|
780 | SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1];
|
---|
781 | end;
|
---|
782 | end;
|
---|
783 | end;
|
---|
784 |
|
---|
785 | // Build expression tree
|
---|
786 | for II := 0 to High(Operators) do begin
|
---|
787 | I := 1;
|
---|
788 | while (I < Expressions.Count - 1) do begin
|
---|
789 | if not TExpression(Expressions[I]).Associated and
|
---|
790 | (TExpression(Expressions[I]).OperatorName = Operators[II]) then begin
|
---|
791 | TExpression(Expressions[I]).Associated := True;
|
---|
792 | TExpression(Expressions[I - 1]).SubItems[1] := Expressions[I];
|
---|
793 | TExpression(Expressions[I + 1]).SubItems[0] := Expressions[I];
|
---|
794 | Expressions.Delete(I);
|
---|
795 | end else Inc(I);
|
---|
796 | end;
|
---|
797 | end;
|
---|
798 | end;
|
---|
799 | Result := TExpression(Expressions[0]).SubItems[1];
|
---|
800 | TExpression(Expressions[0]).Destroy;
|
---|
801 | TExpression(Expressions[1]).Destroy;
|
---|
802 | Expressions.Destroy;
|
---|
803 | end;
|
---|
804 |
|
---|
805 | procedure TCommonBlock.ParseOperation(Compiler: TCompiler);
|
---|
806 | var
|
---|
807 | Identifier: string;
|
---|
808 | Variable: TVariable;
|
---|
809 | Method: TFunction;
|
---|
810 | First: TOperation;
|
---|
811 | Second: TOperation;
|
---|
812 | StartIndex: Integer;
|
---|
813 | LoopVaraible: TVariable;
|
---|
814 | begin
|
---|
815 | with Compiler do begin
|
---|
816 | if NextCode = 'begin' then ParseProgramCode(Compiler)
|
---|
817 | else if NextCode = 'if' then begin
|
---|
818 | Expect('if');
|
---|
819 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
820 | Instruction := inConditionalJump;
|
---|
821 | ExpressionTree := ParseExpression(Compiler);
|
---|
822 | Negative := True;
|
---|
823 | end;
|
---|
824 | First := Operations[Operations.Count - 1];
|
---|
825 | Expect('then');
|
---|
826 | ParseOperation(Compiler);
|
---|
827 | if NextCode = 'else' then begin
|
---|
828 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
829 | Instruction := inJump;
|
---|
830 | end;
|
---|
831 | Second := Operations[Operations.Count - 1];
|
---|
832 | First.GotoAddress := Operations.Count;
|
---|
833 | Expect('else');
|
---|
834 | ParseOperation(Compiler);
|
---|
835 | Second.GotoAddress := Operations.Count;
|
---|
836 | end else First.GotoAddress := Operations.Count;
|
---|
837 | end
|
---|
838 | else if NextCode = 'repeat' then begin
|
---|
839 | Expect('repeat');
|
---|
840 | StartIndex := Operations.Count;
|
---|
841 | ParseOperation(Compiler);
|
---|
842 | Expect('until');
|
---|
843 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
844 | Instruction := inConditionalJump;
|
---|
845 | ExpressionTree := ParseExpression(Compiler);
|
---|
846 | GotoAddress := StartIndex;
|
---|
847 | end;
|
---|
848 | end
|
---|
849 | else if NextCode = 'while' then begin
|
---|
850 | Expect('while');
|
---|
851 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
852 | Instruction := inConditionalJump;
|
---|
853 | ExpressionTree := ParseExpression(Compiler);
|
---|
854 | end;
|
---|
855 | First := Operations[Operations.Count - 1];
|
---|
856 | StartIndex := Operations.Count - 1;
|
---|
857 | Expect('do');
|
---|
858 | ParseOperation(Compiler);
|
---|
859 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
860 | Instruction := inJump;
|
---|
861 | GotoAddress := StartIndex;
|
---|
862 | end;
|
---|
863 | First.GotoAddress := Operations.Count;
|
---|
864 | end
|
---|
865 | else if NextCode = 'for' then begin
|
---|
866 | Expect('for');
|
---|
867 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
868 | Instruction := inExpressionEvaluation;
|
---|
869 | ExpressionTree := ParseExpression(Compiler);
|
---|
870 | if (ExpressionTree.NodeType <> ntOperator) and
|
---|
871 | (ExpressionTree.OperatorName <> ':=') then ErrorMessage('Expected assigment in for loop');
|
---|
872 | if TExpression(TExpression(ExpressionTree).SubItems[0]).NodeType <> ntVariable then
|
---|
873 | ErrorMessage('Index in FOR loop have to be variable');
|
---|
874 | LoopVaraible := TExpression(TExpression(ExpressionTree).SubItems[0]).Variable;
|
---|
875 | end;
|
---|
876 | Expect('to');
|
---|
877 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
878 | Instruction := inExpressionEvaluation;
|
---|
879 | ExpressionTree := TExpression.Create;
|
---|
880 | with ExpressionTree do begin
|
---|
881 | NodeType := ntOperator;
|
---|
882 | OperatorName := '=';
|
---|
883 | SubItems[0] := TExpression.Create;
|
---|
884 | with TExpression(SubItems[0]) do begin
|
---|
885 | NodeType := ntVariable;
|
---|
886 | Variable := LoopVaraible;
|
---|
887 | end;
|
---|
888 | SubItems[1] := ParseExpression(Compiler);
|
---|
889 | end;
|
---|
890 | Negative := True;
|
---|
891 | end;
|
---|
892 | First := Operations[Operations.Count - 1];
|
---|
893 | StartIndex := Operations.Count - 1;
|
---|
894 | Expect('do');
|
---|
895 | ParseOperation(Compiler);
|
---|
896 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
897 | Instruction := inExpressionEvaluation;
|
---|
898 | ExpressionTree := TExpression.Create;
|
---|
899 | with ExpressionTree do begin
|
---|
900 | NodeType := ntOperator;
|
---|
901 | OperatorName := ':=';
|
---|
902 | SubItems[0] := TExpression.Create;
|
---|
903 | with TExpression(SubItems[0]) do begin
|
---|
904 | NodeType := ntVariable;
|
---|
905 | Variable := LoopVaraible;
|
---|
906 | end;
|
---|
907 | SubItems[1] := TExpression.Create;
|
---|
908 | with TExpression(SubItems[1]) do begin
|
---|
909 | NodeType := ntOperator;
|
---|
910 | OperatorName := '+';
|
---|
911 | SubItems[0] := TExpression.Create;
|
---|
912 | with TExpression(SubItems[0]) do begin
|
---|
913 | NodeType := ntVariable;
|
---|
914 | Variable := LoopVaraible;
|
---|
915 | end;
|
---|
916 | SubItems[1] := TExpression.Create;
|
---|
917 | with TExpression(SubItems[1]) do begin
|
---|
918 | NodeType := ntConstant;
|
---|
919 | SetLength(Value, 1);
|
---|
920 | Value[0] := 1;
|
---|
921 | end;
|
---|
922 | end;
|
---|
923 | end;
|
---|
924 | end;
|
---|
925 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
926 | Instruction := inJump;
|
---|
927 | GotoAddress := StartIndex;
|
---|
928 | end;
|
---|
929 | First.GotoAddress := Operations.Count;
|
---|
930 | end
|
---|
931 | else begin
|
---|
932 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
933 | Instruction := inExpressionEvaluation;
|
---|
934 | ExpressionTree := ParseExpression(Compiler);
|
---|
935 | end;
|
---|
936 | end;
|
---|
937 | end;
|
---|
938 | end;
|
---|
939 |
|
---|
940 | procedure TCommonBlock.ParseProgramCode(Compiler: TCompiler);
|
---|
941 | begin
|
---|
942 | with Compiler do begin
|
---|
943 | Expect('begin');
|
---|
944 | while NextCode <> 'end' do begin
|
---|
945 | ParseOperation(Compiler);
|
---|
946 | Expect(';');
|
---|
947 | end;
|
---|
948 | Expect('end');
|
---|
949 | with TOperation(Operations[Operations.Add(TOperation.Create)]) do begin
|
---|
950 | Instruction := inReturn;
|
---|
951 | end;
|
---|
952 | end;
|
---|
953 | CheckReferences;
|
---|
954 | end;
|
---|
955 |
|
---|
956 | { TTypeList }
|
---|
957 |
|
---|
958 | procedure TTypeList.Parse(Compiler: TCompiler);
|
---|
959 | begin
|
---|
960 | with Compiler do begin
|
---|
961 | Expect('type');
|
---|
962 | while IsIdentificator(NextCode) do
|
---|
963 | with TType(Items[Add(TType.Create)]) do begin
|
---|
964 | Parent := Self;
|
---|
965 | Parse(Compiler);
|
---|
966 | end;
|
---|
967 | end;
|
---|
968 | end;
|
---|
969 |
|
---|
970 | function TTypeList.Search(Name: string): TType;
|
---|
971 | var
|
---|
972 | I: Integer;
|
---|
973 | begin
|
---|
974 | I := 0;
|
---|
975 | while (I < Count) and (TType(Items[I]).Name <> Name) do Inc(I);
|
---|
976 | if I < Count then Result := Items[I] else begin
|
---|
977 | if Assigned(Parent.Parent) then Result := Parent.Parent.Types.Search(Name)
|
---|
978 | else begin
|
---|
979 | Result := nil;
|
---|
980 | end;
|
---|
981 | end;
|
---|
982 | end;
|
---|
983 |
|
---|
984 | { TVariableList }
|
---|
985 |
|
---|
986 | procedure TVariableList.Parse(Compiler: TCompiler);
|
---|
987 | var
|
---|
988 | Identifiers: TStringList;
|
---|
989 | NewValueType: TType;
|
---|
990 | TypeName: string;
|
---|
991 | VariableName: string;
|
---|
992 | Variable: TVariable;
|
---|
993 | I: Integer;
|
---|
994 | begin
|
---|
995 | Identifiers := TStringList.Create;
|
---|
996 | with Compiler do begin
|
---|
997 | Expect('var');
|
---|
998 | while IsIdentificator(NextCode) do begin
|
---|
999 | VariableName := ReadCode;
|
---|
1000 | Variable := Search(VariableName);
|
---|
1001 | if not Assigned(Variable) then begin
|
---|
1002 | Identifiers.Add(VariableName);
|
---|
1003 | while NextCode = ',' do begin
|
---|
1004 | Expect(',');
|
---|
1005 | Identifiers.Add(ReadCode);
|
---|
1006 | end;
|
---|
1007 | end else ErrorMessage('Pøedefinování existující promìnné.');
|
---|
1008 | Expect(':');
|
---|
1009 | TypeName := ReadCode;
|
---|
1010 | NewValueType := Parent.Types.Search(TypeName);
|
---|
1011 | if NewValueType = nil then ErrorMessage('Typ ' + TypeName + ' nebyl definován.')
|
---|
1012 | else for I := 0 to Identifiers.Count - 1 do
|
---|
1013 | with TVariable(Items[Add(TVariable.Create)]) do begin
|
---|
1014 | Name := Identifiers[I];
|
---|
1015 | ValueType := NewValueType;
|
---|
1016 | end;
|
---|
1017 | Expect(';');
|
---|
1018 | end;
|
---|
1019 | end;
|
---|
1020 | Identifiers.Destroy;
|
---|
1021 | end;
|
---|
1022 |
|
---|
1023 | function TVariableList.Search(Name: string): TVariable;
|
---|
1024 | var
|
---|
1025 | I: Integer;
|
---|
1026 | begin
|
---|
1027 | I := 0;
|
---|
1028 | while (I < Count) and (TVariable(Items[I]).Name <> Name) do Inc(I);
|
---|
1029 | if I < Count then Result := Items[I] else begin
|
---|
1030 | if Assigned(Parent.Parent) then Result := Parent.Parent.Variables.Search(Name)
|
---|
1031 | else begin
|
---|
1032 | Result := nil;
|
---|
1033 | end;
|
---|
1034 | end;
|
---|
1035 | end;
|
---|
1036 |
|
---|
1037 | { TVariable }
|
---|
1038 |
|
---|
1039 | procedure TVariable.Parse(Compiler: TCompiler);
|
---|
1040 | begin
|
---|
1041 | end;
|
---|
1042 |
|
---|
1043 | { TType }
|
---|
1044 |
|
---|
1045 | procedure TType.Parse(Compiler: TCompiler);
|
---|
1046 | begin
|
---|
1047 | with Compiler do begin
|
---|
1048 | Name := NextCode;
|
---|
1049 | Expect('=');
|
---|
1050 | UsedType := Parent.Search(NextCode);
|
---|
1051 | end;
|
---|
1052 | end;
|
---|
1053 |
|
---|
1054 | { TFunctionList }
|
---|
1055 |
|
---|
1056 | procedure TFunctionList.Parse(Compiler: TCompiler);
|
---|
1057 | begin
|
---|
1058 |
|
---|
1059 | end;
|
---|
1060 |
|
---|
1061 | function TFunctionList.Search(Name: string): TFunction;
|
---|
1062 | var
|
---|
1063 | I: Integer;
|
---|
1064 | begin
|
---|
1065 | I := 0;
|
---|
1066 | while (I < Count) and (TFunction(Items[I]).Name <> Name) do Inc(I);
|
---|
1067 | if I < Count then Result := Items[I] else begin
|
---|
1068 | if Assigned(Parent.Parent) then Result := Parent.Parent.Methods.Search(Name)
|
---|
1069 | else begin
|
---|
1070 | Result := nil;
|
---|
1071 | end;
|
---|
1072 | end;
|
---|
1073 | end;
|
---|
1074 |
|
---|
1075 | { TExpression }
|
---|
1076 |
|
---|
1077 | constructor TExpression.Create;
|
---|
1078 | begin
|
---|
1079 | SubItems := TList.Create;
|
---|
1080 | SubItems.Count := 2;
|
---|
1081 | end;
|
---|
1082 |
|
---|
1083 | destructor TExpression.Destroy;
|
---|
1084 | begin
|
---|
1085 | SubItems.Destroy;
|
---|
1086 | inherited;
|
---|
1087 | end;
|
---|
1088 |
|
---|
1089 | procedure TExpression.GenerateAssembler(Compiler: TCompiler; LabelPrefix: string);
|
---|
1090 | var
|
---|
1091 | I: Integer;
|
---|
1092 | begin
|
---|
1093 | with Compiler do
|
---|
1094 | case NodeType of
|
---|
1095 | ntNone: ;
|
---|
1096 | ntVariable: if Assigned(Variable) then AddInstruction('', 'GETVAR', Variable.Name, '');
|
---|
1097 | nTFunction: AddInstruction('', 'CALL', Method.Name, '');
|
---|
1098 | ntConstant: AddInstruction('', 'CONST', '', '');
|
---|
1099 | ntOperator: begin
|
---|
1100 | for I := 0 to SubItems.Count - 1 do
|
---|
1101 | TExpression(SubItems[I]).GenerateAssembler(Compiler, LabelPrefix);
|
---|
1102 | if OperatorName = '+' then AddInstruction('', 'ADD', '', '')
|
---|
1103 | else if OperatorName = '-' then AddInstruction('', 'SUB', '', '')
|
---|
1104 | else if OperatorName = '*' then AddInstruction('', 'MUL', '', '')
|
---|
1105 | else if OperatorName = '/' then AddInstruction('', 'DIV', '', '')
|
---|
1106 | else if OperatorName = 'div' then AddInstruction('', 'DIV', '', '')
|
---|
1107 | else if OperatorName = 'mod' then AddInstruction('', 'MOD', '', '')
|
---|
1108 | else if OperatorName = 'xor' then AddInstruction('', 'XOR', '', '')
|
---|
1109 | else if OperatorName = 'or' then AddInstruction('', 'OR', '', '')
|
---|
1110 | else if OperatorName = 'and' then AddInstruction('', 'AND', '', '')
|
---|
1111 | else if OperatorName = 'not' then AddInstruction('', 'NEG', '', '')
|
---|
1112 | else if OperatorName = ':=' then AddInstruction('', 'ST', '', '')
|
---|
1113 | else if OperatorName = '>' then AddInstruction('', 'CP', '', '')
|
---|
1114 | else if OperatorName = '>=' then AddInstruction('', 'CP', '', '')
|
---|
1115 | else if OperatorName = '<' then AddInstruction('', 'CP', '', '')
|
---|
1116 | else if OperatorName = '<=' then AddInstruction('', 'CP', '', '')
|
---|
1117 | else if OperatorName = '=' then AddInstruction('', 'TST', '', '')
|
---|
1118 | else if OperatorName = '<>' then AddInstruction('', 'CP', '', '');
|
---|
1119 | end;
|
---|
1120 | end;
|
---|
1121 | end;
|
---|
1122 |
|
---|
1123 | { TAssemblerLine }
|
---|
1124 |
|
---|
1125 | function TAssemblerLine.AsString: string;
|
---|
1126 | begin
|
---|
1127 | if LabelName = '' then LabelName := #9 else
|
---|
1128 | LabelName := LabelName + ':'#9;
|
---|
1129 | if Operand2 <> '' then Operand1 := Operand1 + ', ';
|
---|
1130 |
|
---|
1131 | Result := LabelName + Instruction + ' ' + Operand1 + Operand2;
|
---|
1132 | end;
|
---|
1133 |
|
---|
1134 | end.
|
---|