source: trunk/Compiler/Modules/Interpretter/ModuleInterpretter.pas

Last change on this file was 75, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File size: 5.7 KB
Line 
1unit ModuleInterpretter;
2
3interface
4
5uses
6 Classes, SysUtils, Target, Executor, SourceCodePascal, Dialogs,
7 Generics.Collections, ModularSystem;
8
9type
10 { TExecutorInterpretter }
11
12 TExecutorInterpretter = class(TExecutor)
13 private
14 //procedure SystemAdd(FunctionCall: TFunctionCall);
15 //procedure SystemSub(FunctionCall: TFunctionCall);
16 //procedure SystemMove(FunctionCall: TFunctionCall);
17 //procedure SystemFillChar(FunctionCall: TFunctionCall);
18
19 procedure RunCommand(Command: TCommand);
20 procedure RunBeginEnd(BeginEnd: TBeginEnd);
21 procedure RunWhileDo(WhileDo: TWhileDo);
22 procedure RunRepeatUntil(RepeatUntil: TRepeatUntil);
23 procedure RunIfThenElse(IfThenElse: TIfThenElse);
24 procedure RunAssignment(Assignment: TAssignment);
25 procedure RunCaseOfEnd(CaseOfEnd: TCaseOfEnd);
26 function RunFunction(FunctionCall: TFunctionCall): TValue;
27 procedure RunForToDo(ForToDo: TForToDo);
28 function Evaluate(Expression: TExpression): TValue;
29 public
30 Variables: TObjectList<TVariable>;
31 procedure Run; override;
32 constructor Create;
33 destructor Destroy; override;
34 end;
35
36 { TModuleInterpretter }
37
38 TModuleInterpretter = class(TModule)
39 constructor Create(AOwner: TComponent); override;
40 procedure DoInstall; override;
41 procedure DoUninstall; override;
42 end;
43
44implementation
45
46uses
47 Compiler, CompilerAPI;
48
49resourcestring
50 SUnknownCommandType = 'Unknown command type';
51 SUnknownOperator = 'Unknown operator "%s"';
52
53{ TModuleInterpretter }
54
55constructor TModuleInterpretter.Create(AOwner: TComponent);
56begin
57 inherited;
58 Identification := 'Interpretter';
59 Title := 'Interpretter';
60 Version := '0.1';
61 Dependencies.Add('Pascal');
62end;
63
64procedure TModuleInterpretter.DoInstall;
65begin
66 inherited;
67 //TCompilerAPI(API).RegisterConvertor(TCovertor);
68end;
69
70procedure TModuleInterpretter.DoUninstall;
71begin
72 inherited;
73end;
74
75{ TExecutorInterpretter }
76
77procedure TExecutorInterpretter.RunCommand(Command: TCommand);
78begin
79 if Command is TBeginEnd then RunBeginEnd(TBeginEnd(Command))
80 else if Command is TWhileDo then RunWhileDo(TWhileDo(Command))
81 else if Command is TRepeatUntil then RunRepeatUntil(TRepeatUntil(Command))
82 else if Command is TIfThenElse then RunIfThenElse(TIfThenElse(Command))
83 else if Command is TAssignment then RunAssignment(TAssignment(Command))
84 else if Command is TCaseOfEnd then RunCaseOfEnd(TCaseOfEnd(Command))
85 else if Command is TFunctionCall then RunFunction(TFunctionCall(Command))
86 else if Command is TForToDo then RunForToDo(TForToDo(Command))
87 else raise Exception.Create(SUnknownCommandType);
88end;
89
90procedure TExecutorInterpretter.RunBeginEnd(BeginEnd: TBeginEnd);
91var
92 I: Integer;
93begin
94 for I := 0 to BeginEnd.Commands.Count - 1 do
95 RunCommand(TCommand(BeginEnd.Commands[I]))
96end;
97
98procedure TExecutorInterpretter.RunWhileDo(WhileDo: TWhileDo);
99begin
100 while Evaluate(WhileDo.Condition) do RunBeginEnd(WhileDo.CommonBlock.Code);
101end;
102
103procedure TExecutorInterpretter.RunRepeatUntil(RepeatUntil: TRepeatUntil);
104begin
105 repeat
106 RunBeginEnd(RepeatUntil.CommonBlock.Code);
107 until Evaluate(RepeatUntil.Condition);
108end;
109
110procedure TExecutorInterpretter.RunIfThenElse(IfThenElse: TIfThenElse);
111begin
112 if Evaluate(IfThenElse.Condition) then RunCommand(IfThenElse.Command)
113 else RunCommand(IfThenElse.ElseCommand);
114end;
115
116procedure TExecutorInterpretter.RunAssignment(Assignment: TAssignment);
117begin
118 Assignment.Target.Value := Evaluate(Assignment.Source);
119end;
120
121procedure TExecutorInterpretter.RunCaseOfEnd(CaseOfEnd: TCaseOfEnd);
122var
123 I: Integer;
124begin
125 (* I := 0;
126 while (I < CaseOfEnd.Branches.Count) and
127 if TCaseOfEndBranche(CaseOfEnd.Branches[I]).Constant =
128 Evaluate(CaseOfEnd.Expression) do
129 Inc(I);
130 if (I < CaseOfEnd.Branches.Count) then
131 RunCommand(TCaseOfEndBranche(CaseOfEnd.Branches[I]).Command)
132 else RunCommand(CaseOfEnd.ElseCommand); *)
133end;
134
135function TExecutorInterpretter.RunFunction(FunctionCall: TFunctionCall): TValue;
136begin
137 RunBeginEnd(FunctionCall.FunctionRef.Code);
138end;
139
140procedure TExecutorInterpretter.RunForToDo(ForToDo: TForToDo);
141var
142 I: Integer;
143begin
144(* for I := 0 to ForToDo.Start to ForToDo.Stop do begin
145 ForToDo.ControlVariable.;
146 RunCommand(ForToDo.Command);
147 end; *)
148end;
149
150function TExecutorInterpretter.Evaluate(Expression: TExpression): TValue;
151var
152 I: Integer;
153begin
154 with Expression do
155 case NodeType of
156 ntConstant: Result := Constant.Value;
157 ntFunction: Result := RunFunction(FunctionCall);
158 ntOperator: begin
159 if OperatorName = '>' then
160 Result := Evaluate(TExpression(SubItems.First)) > Evaluate(TExpression(SubItems.Last))
161 else if OperatorName = '<' then
162 Result := Evaluate(TExpression(SubItems.First)) < Evaluate(TExpression(SubItems.Last))
163 else if OperatorName = '+' then
164 Result := Evaluate(TExpression(SubItems.First)) + Evaluate(TExpression(SubItems.Last))
165 else if OperatorName = '-' then
166 Result := Evaluate(TExpression(SubItems.First)) - Evaluate(TExpression(SubItems.Last))
167 else if OperatorName = '*' then
168 Result := Evaluate(TExpression(SubItems.First)) * Evaluate(TExpression(SubItems.Last))
169 else if OperatorName = 'div' then
170 Result := Evaluate(TExpression(SubItems.First)) div Evaluate(TExpression(SubItems.Last))
171 else raise Exception.CreateFmt(SUnknownOperator, [OperatorName]);
172 end;
173 ntVariable: Result := Variable.Value;
174 ntValue: Result := Value;
175 end;
176end;
177
178procedure TExecutorInterpretter.Run;
179begin
180 with TModuleProgram(TCompiler(TTarget(Target).Compiler).Analyzer.ProgramCode.MainModule) do begin
181 RunBeginEnd(Body.Code);
182 end;
183end;
184
185constructor TExecutorInterpretter.Create;
186begin
187 Variables := TVariables.Create;
188end;
189
190destructor TExecutorInterpretter.Destroy;
191begin
192 FreeAndnil(Variables);
193 inherited;
194end;
195
196end.
197
Note: See TracBrowser for help on using the repository browser.