source: branches/easy compiler/UTargetCode.pas

Last change on this file was 139, checked in by chronos, 7 years ago
  • Added: Experimental simple compiler.
File size: 2.2 KB
Line 
1unit UTargetCode;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Contnrs;
9
10type
11 TTargetInstructionKind = (tiNop, tiFunction);
12 TTargetInstruction = class
13 Kind: TTargetInstructionKind;
14 Name: string;
15 Param1: string;
16 Param2: string;
17 end;
18
19 TTargetInstructions = class(TObjectList)
20 end;
21
22 TTargetInstructionFunction = class(TTargetInstruction)
23
24 end;
25
26 { TTargetCode }
27
28 TTargetCode = class
29 Instructions: TTargetInstructions;
30 constructor Create;
31 destructor Destroy; override;
32 end;
33
34 TOutputEvent = procedure (Text: string) of object;
35
36 { TTargetExecutor }
37
38 TTargetExecutor = class
39 private
40 FOnOutput: TOutputEvent;
41 Variables: TStringList;
42 public
43 constructor Create;
44 destructor Destroy; override;
45 procedure Execute(TargetCode: TTargetCode);
46 property OnOutput: TOutputEvent read FOnOutput write FOnOutput;
47 end;
48
49
50
51implementation
52
53{ TTargetExecutor }
54
55constructor TTargetExecutor.Create;
56begin
57 Variables := TStringList.Create;
58end;
59
60destructor TTargetExecutor.Destroy;
61begin
62 Variables.Free;
63 inherited Destroy;
64end;
65
66procedure TTargetExecutor.Execute(TargetCode: TTargetCode);
67var
68 IP: Integer;
69 Instruction: TTargetInstruction;
70begin
71{ IP := 0;
72 while IP < TargetCode.Instructions.Count do begin
73 Instruction := TTargetInstruction(Targetcode.Instructions[IP]);
74 if Instruction.Kind = tiNop then
75 else if Instruction.Kind = tiFunction then begin
76 if Instruction.Name = 'print' then begin
77 if Assigned(FOnOutput) then begin
78 if TTargetInstructionFunction(Instruction).;
79 FOnOutput(Variables[Instruction.Param1] + LineEnding);
80 end;
81 end else
82 if Instruction.Name = 'assign' then begin
83 // Variables.Add(Instruction.Param1 + Variables.Delimited + Instruction.Param2);
84 end else raise Exception.Create('Unsupported function');
85 end else raise Exception.Create('Unsupported instruction');
86 Inc(IP);
87 end;
88}
89end;
90
91{ TTargetCode }
92
93constructor TTargetCode.Create;
94begin
95 Instructions := TTargetInstructions.Create;
96end;
97
98destructor TTargetCode.Destroy;
99begin
100 Instructions.Free;
101 inherited Destroy;
102end;
103
104end.
105
Note: See TracBrowser for help on using the repository browser.