Ignore:
Timestamp:
Jul 13, 2012, 2:10:18 PM (12 years ago)
Author:
chronos
Message:
  • Modified: Slightly enhanced unfinished interpretter.
File:
1 edited

Legend:

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

    r52 r56  
    3232    procedure RunAssignment(Assignment: TAssignment);
    3333    procedure RunCaseOfEnd(CaseOfEnd: TCaseOfEnd);
    34     procedure RunFunction(FunctionCall: TFunctionCall);
     34    function RunFunction(FunctionCall: TFunctionCall): TValue;
    3535    procedure RunForToDo(ForToDo: TForToDo);
    36     function Evaluate(Expression: TExpression): Boolean;
     36    function Evaluate(Expression: TExpression): TValue;
    3737  public
    3838    Variables: TListObject;
     
    5050resourcestring
    5151  SUnknownCommandType = 'Unknown command type';
     52  SUnknownOperator = 'Unknown operator "%s"';
    5253
    5354{ TExecutorInterpretter }
     
    9495procedure TExecutorInterpretter.RunAssignment(Assignment: TAssignment);
    9596begin
    96   //Assignment.Target := Assignment.Source;
     97  Assignment.Target.Value := Evaluate(Assignment.Source);
    9798end;
    9899
     
    111112end;
    112113
    113 procedure TExecutorInterpretter.RunFunction(FunctionCall: TFunctionCall);
     114function TExecutorInterpretter.RunFunction(FunctionCall: TFunctionCall): TValue;
    114115begin
    115116  RunBeginEnd(FunctionCall.FunctionRef.Code);
     
    126127end;
    127128
    128 function TExecutorInterpretter.Evaluate(Expression: TExpression): Boolean;
     129function TExecutorInterpretter.Evaluate(Expression: TExpression): TValue;
    129130begin
    130 //  case Expression.NodeType of
    131 //    ntConstant: ;
    132 //  end;
     131  with Expression do
     132  case NodeType of
     133    ntConstant: Result := Constant.Value;
     134    ntFunction: Result := RunFunction(FunctionCall);
     135    ntOperator: begin
     136      if OperatorName = '>' then
     137        Result := Evaluate(TExpression(SubItems.First)) > Evaluate(TExpression(SubItems.Last))
     138      else if OperatorName = '<' then
     139        Result := Evaluate(TExpression(SubItems.First)) < Evaluate(TExpression(SubItems.Last))
     140      else if OperatorName = '+' then
     141        Result := Evaluate(TExpression(SubItems.First)) + Evaluate(TExpression(SubItems.Last))
     142      else if OperatorName = '-' then
     143        Result := Evaluate(TExpression(SubItems.First)) - Evaluate(TExpression(SubItems.Last))
     144      else if OperatorName = '*' then
     145        Result := Evaluate(TExpression(SubItems.First)) * Evaluate(TExpression(SubItems.Last))
     146      else if OperatorName = 'div' then
     147        Result := Evaluate(TExpression(SubItems.First)) div Evaluate(TExpression(SubItems.Last))
     148        else raise Exception.CreateFmt(SUnknownOperator, [OperatorName]);
     149    end;
     150    ntVariable: Result := Variable.Value;
     151  end;
    133152end;
    134153
Note: See TracChangeset for help on using the changeset viewer.