Ignore:
Timestamp:
Apr 5, 2012, 2:03:52 PM (13 years ago)
Author:
chronos
Message:
  • Modified: Interpretter execution partial implementation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/Target/Interpretter/UTargetInterpretter.pas

    r46 r50  
    66
    77uses
    8   Classes, SysUtils, UTarget, UExecutor;
     8  Classes, SysUtils, UTarget, UExecutor, USourceCode, Dialogs;
    99
    1010type
    11 
    1211  { TTargetInterpretter }
    1312
    1413  TTargetInterpretter = class(TTarget)
    1514    constructor Create; override;
     15    destructor Destroy; override;
    1616  end;
    1717
     18  { TExecutorInterpretter }
     19
    1820  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;
    2034  end;
    2135
     36
    2237implementation
     38
     39uses
     40  UCompiler;
     41
     42resourcestring
     43  SUnknownCommandType = 'Unknown command type';
     44
     45{ TExecutorInterpretter }
     46
     47procedure TExecutorInterpretter.RunCommand(Command: TCommand);
     48begin
     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);
     58end;
     59
     60procedure TExecutorInterpretter.RunBeginEnd(BeginEnd: TBeginEnd);
     61var
     62  I: Integer;
     63begin
     64  for I := 0 to BeginEnd.Commands.Count - 1 do
     65    RunCommand(TCommand(BeginEnd.Commands[I]))
     66end;
     67
     68procedure TExecutorInterpretter.RunWhileDo(WhileDo: TWhileDo);
     69begin
     70  while Evaluate(WhileDo.Condition) do RunBeginEnd(WhileDo.CommonBlock.Code);
     71end;
     72
     73procedure TExecutorInterpretter.RunRepeatUntil(RepeatUntil: TRepeatUntil);
     74begin
     75  repeat
     76    RunBeginEnd(RepeatUntil.CommonBlock.Code);
     77  until Evaluate(RepeatUntil.Condition);
     78end;
     79
     80procedure TExecutorInterpretter.RunIfThenElse(IfThenElse: TIfThenElse);
     81begin
     82  if Evaluate(IfThenElse.Condition) then RunCommand(IfThenElse.Command)
     83    else RunCommand(IfThenElse.ElseCommand);
     84end;
     85
     86procedure TExecutorInterpretter.RunAssignment(Assignment: TAssignment);
     87begin
     88  //Assignment.Target := Assignment.Source;
     89end;
     90
     91procedure TExecutorInterpretter.RunCaseOfEnd(CaseOfEnd: TCaseOfEnd);
     92var
     93  I: Integer;
     94begin
     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); *)
     103end;
     104
     105procedure TExecutorInterpretter.RunFunction(FunctionCall: TFunctionCall);
     106begin
     107  RunBeginEnd(FunctionCall.FunctionRef.Code);
     108end;
     109
     110procedure TExecutorInterpretter.RunForToDo(ForToDo: TForToDo);
     111var
     112  I: Integer;
     113begin
     114(*  for I := 0 to ForToDo.Start to ForToDo.Stop do begin
     115    ForToDo.ControlVariable.;
     116    RunCommand(ForToDo.Command);
     117  end; *)
     118end;
     119
     120function TExecutorInterpretter.Evaluate(Expression: TExpression): Boolean;
     121begin
     122//  case Expression.NodeType of
     123//    ntConstant: ;
     124//  end;
     125end;
     126
     127procedure TExecutorInterpretter.Run;
     128begin
     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;
     133end;
    23134
    24135{ TTargetInterpretter }
     
    30141  Name := 'Interpretter';
    31142  Executor := TExecutorInterpretter.Create;
     143  Executor.Target := Self;
     144end;
     145
     146destructor TTargetInterpretter.Destroy;
     147begin
     148  FreeAndNil(Executor);
     149  inherited Destroy;
    32150end;
    33151
Note: See TracChangeset for help on using the changeset viewer.