Changeset 50 for trunk/Compiler/Target/Interpretter/UTargetInterpretter.pas
- Timestamp:
- Apr 5, 2012, 2:03:52 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/Target/Interpretter/UTargetInterpretter.pas
r46 r50 6 6 7 7 uses 8 Classes, SysUtils, UTarget, UExecutor ;8 Classes, SysUtils, UTarget, UExecutor, USourceCode, Dialogs; 9 9 10 10 type 11 12 11 { TTargetInterpretter } 13 12 14 13 TTargetInterpretter = class(TTarget) 15 14 constructor Create; override; 15 destructor Destroy; override; 16 16 end; 17 17 18 { TExecutorInterpretter } 19 18 20 TExecutorInterpretter = class(TExecutor) 19 21 private 22 procedure RunCommand(Command: TCommand); 23 procedure RunBeginEnd(BeginEnd: TBeginEnd); 24 procedure RunWhileDo(WhileDo: TWhileDo); 25 procedure RunRepeatUntil(RepeatUntil: TRepeatUntil); 26 procedure RunIfThenElse(IfThenElse: TIfThenElse); 27 procedure RunAssignment(Assignment: TAssignment); 28 procedure RunCaseOfEnd(CaseOfEnd: TCaseOfEnd); 29 procedure RunFunction(FunctionCall: TFunctionCall); 30 procedure RunForToDo(ForToDo: TForToDo); 31 function Evaluate(Expression: TExpression): Boolean; 32 public 33 procedure Run; override; 20 34 end; 21 35 36 22 37 implementation 38 39 uses 40 UCompiler; 41 42 resourcestring 43 SUnknownCommandType = 'Unknown command type'; 44 45 { TExecutorInterpretter } 46 47 procedure TExecutorInterpretter.RunCommand(Command: TCommand); 48 begin 49 if Command is TBeginEnd then RunBeginEnd(TBeginEnd(Command)) 50 else if Command is TWhileDo then RunWhileDo(TWhileDo(Command)) 51 else if Command is TRepeatUntil then RunRepeatUntil(TRepeatUntil(Command)) 52 else if Command is TIfThenElse then RunIfThenElse(TIfThenElse(Command)) 53 else if Command is TAssignment then RunAssignment(TAssignment(Command)) 54 else if Command is TCaseOfEnd then RunCaseOfEnd(TCaseOfEnd(Command)) 55 else if Command is TFunctionCall then RunFunction(TFunctionCall(Command)) 56 else if Command is TForToDo then RunForToDo(TForToDo(Command)) 57 else raise Exception.Create(SUnknownCommandType); 58 end; 59 60 procedure TExecutorInterpretter.RunBeginEnd(BeginEnd: TBeginEnd); 61 var 62 I: Integer; 63 begin 64 for I := 0 to BeginEnd.Commands.Count - 1 do 65 RunCommand(TCommand(BeginEnd.Commands[I])) 66 end; 67 68 procedure TExecutorInterpretter.RunWhileDo(WhileDo: TWhileDo); 69 begin 70 while Evaluate(WhileDo.Condition) do RunBeginEnd(WhileDo.CommonBlock.Code); 71 end; 72 73 procedure TExecutorInterpretter.RunRepeatUntil(RepeatUntil: TRepeatUntil); 74 begin 75 repeat 76 RunBeginEnd(RepeatUntil.CommonBlock.Code); 77 until Evaluate(RepeatUntil.Condition); 78 end; 79 80 procedure TExecutorInterpretter.RunIfThenElse(IfThenElse: TIfThenElse); 81 begin 82 if Evaluate(IfThenElse.Condition) then RunCommand(IfThenElse.Command) 83 else RunCommand(IfThenElse.ElseCommand); 84 end; 85 86 procedure TExecutorInterpretter.RunAssignment(Assignment: TAssignment); 87 begin 88 //Assignment.Target := Assignment.Source; 89 end; 90 91 procedure TExecutorInterpretter.RunCaseOfEnd(CaseOfEnd: TCaseOfEnd); 92 var 93 I: Integer; 94 begin 95 (* I := 0; 96 while (I < CaseOfEnd.Branches.Count) and 97 if TCaseOfEndBranche(CaseOfEnd.Branches[I]).Constant = 98 Evaluate(CaseOfEnd.Expression) do 99 Inc(I); 100 if (I < CaseOfEnd.Branches.Count) then 101 RunCommand(TCaseOfEndBranche(CaseOfEnd.Branches[I]).Command) 102 else RunCommand(CaseOfEnd.ElseCommand); *) 103 end; 104 105 procedure TExecutorInterpretter.RunFunction(FunctionCall: TFunctionCall); 106 begin 107 RunBeginEnd(FunctionCall.FunctionRef.Code); 108 end; 109 110 procedure TExecutorInterpretter.RunForToDo(ForToDo: TForToDo); 111 var 112 I: Integer; 113 begin 114 (* for I := 0 to ForToDo.Start to ForToDo.Stop do begin 115 ForToDo.ControlVariable.; 116 RunCommand(ForToDo.Command); 117 end; *) 118 end; 119 120 function TExecutorInterpretter.Evaluate(Expression: TExpression): Boolean; 121 begin 122 // case Expression.NodeType of 123 // ntConstant: ; 124 // end; 125 end; 126 127 procedure TExecutorInterpretter.Run; 128 begin 129 ShowMessage(TModuleProgram(TCompiler(TTarget(Target).Compiler).Analyzer.ProgramCode.Modules[0]).Name); 130 with TModuleProgram(TCompiler(TTarget(Target).Compiler).Analyzer.ProgramCode.Modules[0]) do begin 131 RunBeginEnd(Body.Code); 132 end; 133 end; 23 134 24 135 { TTargetInterpretter } … … 30 141 Name := 'Interpretter'; 31 142 Executor := TExecutorInterpretter.Create; 143 Executor.Target := Self; 144 end; 145 146 destructor TTargetInterpretter.Destroy; 147 begin 148 FreeAndNil(Executor); 149 inherited Destroy; 32 150 end; 33 151
Note:
See TracChangeset
for help on using the changeset viewer.