| 1 | unit ModuleInterpretter;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Target, Executor, SourceCodePascal, Dialogs,
|
|---|
| 7 | Generics.Collections, ModularSystem;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 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 |
|
|---|
| 44 | implementation
|
|---|
| 45 |
|
|---|
| 46 | uses
|
|---|
| 47 | Compiler, CompilerAPI;
|
|---|
| 48 |
|
|---|
| 49 | resourcestring
|
|---|
| 50 | SUnknownCommandType = 'Unknown command type';
|
|---|
| 51 | SUnknownOperator = 'Unknown operator "%s"';
|
|---|
| 52 |
|
|---|
| 53 | { TModuleInterpretter }
|
|---|
| 54 |
|
|---|
| 55 | constructor TModuleInterpretter.Create(AOwner: TComponent);
|
|---|
| 56 | begin
|
|---|
| 57 | inherited;
|
|---|
| 58 | Identification := 'Interpretter';
|
|---|
| 59 | Title := 'Interpretter';
|
|---|
| 60 | Version := '0.1';
|
|---|
| 61 | Dependencies.Add('Pascal');
|
|---|
| 62 | end;
|
|---|
| 63 |
|
|---|
| 64 | procedure TModuleInterpretter.DoInstall;
|
|---|
| 65 | begin
|
|---|
| 66 | inherited;
|
|---|
| 67 | //TCompilerAPI(API).RegisterConvertor(TCovertor);
|
|---|
| 68 | end;
|
|---|
| 69 |
|
|---|
| 70 | procedure TModuleInterpretter.DoUninstall;
|
|---|
| 71 | begin
|
|---|
| 72 | inherited;
|
|---|
| 73 | end;
|
|---|
| 74 |
|
|---|
| 75 | { TExecutorInterpretter }
|
|---|
| 76 |
|
|---|
| 77 | procedure TExecutorInterpretter.RunCommand(Command: TCommand);
|
|---|
| 78 | begin
|
|---|
| 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);
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | procedure TExecutorInterpretter.RunBeginEnd(BeginEnd: TBeginEnd);
|
|---|
| 91 | var
|
|---|
| 92 | I: Integer;
|
|---|
| 93 | begin
|
|---|
| 94 | for I := 0 to BeginEnd.Commands.Count - 1 do
|
|---|
| 95 | RunCommand(TCommand(BeginEnd.Commands[I]))
|
|---|
| 96 | end;
|
|---|
| 97 |
|
|---|
| 98 | procedure TExecutorInterpretter.RunWhileDo(WhileDo: TWhileDo);
|
|---|
| 99 | begin
|
|---|
| 100 | while Evaluate(WhileDo.Condition) do RunBeginEnd(WhileDo.CommonBlock.Code);
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | procedure TExecutorInterpretter.RunRepeatUntil(RepeatUntil: TRepeatUntil);
|
|---|
| 104 | begin
|
|---|
| 105 | repeat
|
|---|
| 106 | RunBeginEnd(RepeatUntil.CommonBlock.Code);
|
|---|
| 107 | until Evaluate(RepeatUntil.Condition);
|
|---|
| 108 | end;
|
|---|
| 109 |
|
|---|
| 110 | procedure TExecutorInterpretter.RunIfThenElse(IfThenElse: TIfThenElse);
|
|---|
| 111 | begin
|
|---|
| 112 | if Evaluate(IfThenElse.Condition) then RunCommand(IfThenElse.Command)
|
|---|
| 113 | else RunCommand(IfThenElse.ElseCommand);
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TExecutorInterpretter.RunAssignment(Assignment: TAssignment);
|
|---|
| 117 | begin
|
|---|
| 118 | Assignment.Target.Value := Evaluate(Assignment.Source);
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | procedure TExecutorInterpretter.RunCaseOfEnd(CaseOfEnd: TCaseOfEnd);
|
|---|
| 122 | var
|
|---|
| 123 | I: Integer;
|
|---|
| 124 | begin
|
|---|
| 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); *)
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | function TExecutorInterpretter.RunFunction(FunctionCall: TFunctionCall): TValue;
|
|---|
| 136 | begin
|
|---|
| 137 | RunBeginEnd(FunctionCall.FunctionRef.Code);
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | procedure TExecutorInterpretter.RunForToDo(ForToDo: TForToDo);
|
|---|
| 141 | var
|
|---|
| 142 | I: Integer;
|
|---|
| 143 | begin
|
|---|
| 144 | (* for I := 0 to ForToDo.Start to ForToDo.Stop do begin
|
|---|
| 145 | ForToDo.ControlVariable.;
|
|---|
| 146 | RunCommand(ForToDo.Command);
|
|---|
| 147 | end; *)
|
|---|
| 148 | end;
|
|---|
| 149 |
|
|---|
| 150 | function TExecutorInterpretter.Evaluate(Expression: TExpression): TValue;
|
|---|
| 151 | var
|
|---|
| 152 | I: Integer;
|
|---|
| 153 | begin
|
|---|
| 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;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | procedure TExecutorInterpretter.Run;
|
|---|
| 179 | begin
|
|---|
| 180 | with TModuleProgram(TCompiler(TTarget(Target).Compiler).Analyzer.ProgramCode.MainModule) do begin
|
|---|
| 181 | RunBeginEnd(Body.Code);
|
|---|
| 182 | end;
|
|---|
| 183 | end;
|
|---|
| 184 |
|
|---|
| 185 | constructor TExecutorInterpretter.Create;
|
|---|
| 186 | begin
|
|---|
| 187 | Variables := TVariables.Create;
|
|---|
| 188 | end;
|
|---|
| 189 |
|
|---|
| 190 | destructor TExecutorInterpretter.Destroy;
|
|---|
| 191 | begin
|
|---|
| 192 | FreeAndnil(Variables);
|
|---|
| 193 | inherited;
|
|---|
| 194 | end;
|
|---|
| 195 |
|
|---|
| 196 | end.
|
|---|
| 197 |
|
|---|