1 | unit UTargetCode;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Contnrs;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
51 | implementation
|
---|
52 |
|
---|
53 | { TTargetExecutor }
|
---|
54 |
|
---|
55 | constructor TTargetExecutor.Create;
|
---|
56 | begin
|
---|
57 | Variables := TStringList.Create;
|
---|
58 | end;
|
---|
59 |
|
---|
60 | destructor TTargetExecutor.Destroy;
|
---|
61 | begin
|
---|
62 | Variables.Free;
|
---|
63 | inherited Destroy;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | procedure TTargetExecutor.Execute(TargetCode: TTargetCode);
|
---|
67 | var
|
---|
68 | IP: Integer;
|
---|
69 | Instruction: TTargetInstruction;
|
---|
70 | begin
|
---|
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 | }
|
---|
89 | end;
|
---|
90 |
|
---|
91 | { TTargetCode }
|
---|
92 |
|
---|
93 | constructor TTargetCode.Create;
|
---|
94 | begin
|
---|
95 | Instructions := TTargetInstructions.Create;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | destructor TTargetCode.Destroy;
|
---|
99 | begin
|
---|
100 | Instructions.Free;
|
---|
101 | inherited Destroy;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | end.
|
---|
105 |
|
---|