Ignore:
Timestamp:
Jan 18, 2018, 1:30:58 PM (7 years ago)
Author:
chronos
Message:
  • Fixed: Now arrays of string and integer are supported and executed correctly by executor.
  • Added: IfNotEqual command.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/easy compiler/USourceExecutor.pas

    r148 r149  
    1717    Variable: TSourceVariable;
    1818    Value: TSourceValue;
     19
    1920    destructor Destroy; override;
    2021  end;
     
    4950    procedure ExecuteBreak(CommandBreak: TCommandBreak);
    5051    procedure ExecuteIfEqual(IfEqual: TCommandIfEqual);
     52    procedure ExecuteIfNotEqual(IfNotEqual: TCommandIfNotEqual);
    5153    procedure ExecuteRepeat(CommandRepeat: TCommandRepeat);
    5254    function ReadValueReference(Reference: TSourceReference): TSourceValue;
    53     function ReadVarReference(Reference: TSourceReference): TSourceVariable;
     55    function ReadVarReference(Reference: TSourceReference): TSourceValue;
    5456  public
    5557    constructor Create;
     
    141143end;
    142144
    143 function TSourceExecutor.ReadVarReference(Reference: TSourceReference): TSourceVariable;
     145function TSourceExecutor.ReadVarReference(Reference: TSourceReference): TSourceValue;
    144146var
    145147  ArrayIndex: TSourceValue;
     148  Variable: TSourceVariable;
     149  ExecutorVar: TExecutorVariable;
     150  I: Integer;
    146151begin
    147152  Result := nil;
    148153  if Reference is TSourceReferenceVariable then begin
    149     Result := TSourceReferenceVariable(Reference).Variable;
     154    Variable := TSourceReferenceVariable(Reference).Variable;
     155  end else
     156  if Reference is TSourceReferenceArray then begin
     157    Variable := TSourceReferenceArray(Reference).ArrayRef;
     158  end else
     159  raise Exception.Create('Unsupported reference');
     160
     161  ExecutorVar := Variables.Search(Variable);
     162  if not Assigned(ExecutorVar) then begin
     163    ExecutorVar := TExecutorVariable.Create;
     164    ExecutorVar.Variable := Variable;
     165    Variables.Add(ExecutorVar);
     166    ExecutorVar.Value := Variable.ValueType.GetValueType.Create;
     167  end;
     168  if Reference is TSourceReferenceVariable then begin
     169    Result := ExecutorVar.Value;
    150170  end else
    151171  if Reference is TSourceReferenceArray then begin
     
    153173    if not (ArrayIndex is TSourceValueInteger) then
    154174      raise Exception.Create('Only integer array index supported');
    155 //    Result := TSourceValue(TSourceValueArray(Variables.Search(TSourceReferenceArray(Reference).ArrayRef).Value).Items[TSourceValueInteger(ArrayIndex)]);
    156 //TODO:    Result := Variables.Search(TSourceReferenceArray(Reference).ArrayRef);
    157   end else raise Exception.Create('Unsupported reference');
     175    I := TSourceValueInteger(ArrayIndex).Value;
     176    while TSourceValueArray(ExecutorVar.Value).Items.Count < (I + 1) do begin
     177      TSourceValueArray(ExecutorVar.Value).Items.Add(TSourceTypeArray(TSourceReferenceArray(Reference).ArrayRef.ValueType).ItemType.GetValueType.Create);
     178    end;
     179    Result := TSourceValue(TSourceValueArray(ExecutorVar.Value).Items[I]);
     180  end else
     181  raise Exception.Create('Unsupported reference');
    158182end;
    159183
    160184procedure TSourceExecutor.ExecuteAssign(CommandAssign: TCommandFunctionCall);
    161185var
    162   Variable: TSourceVariable;
    163   Value: TSourceValue;
    164   ExecutorVar: TExecutorVariable;
     186  Dest: TSourceValue;
     187  Source: TSourceValue;
    165188begin
    166189  with TCommandFunctionCall(CommandAssign) do begin
    167     Variable := ReadVarReference(TSourceReference(Parameters[0]));
    168     Value := ReadValueReference(TSourceReference(Parameters[1]));
    169     ExecutorVar := Variables.Search(Variable);
    170     if not Assigned(ExecutorVar) then begin
    171       ExecutorVar := TExecutorVariable.Create;
    172       ExecutorVar.Variable := Variable;
    173       Variables.Add(ExecutorVar);
    174       ExecutorVar.Value := Variable.ValueType.GetValueType.Create;
    175     end;
    176     ExecutorVar.Value.Assign(Value);
     190    Dest := ReadVarReference(TSourceReference(Parameters[0]));
     191    Source := ReadValueReference(TSourceReference(Parameters[1]));
     192    Dest.Assign(Source);
    177193  end;
    178194end;
     
    230246end;
    231247
     248procedure TSourceExecutor.ExecuteIfNotEqual(IfNotEqual: TCommandIfNotEqual);
     249var
     250  Value1: TSourceValue;
     251  Value2: TSourceValue;
     252begin
     253  Value1 := ReadValueReference(IfNotEqual.Reference1);
     254  Value2 := ReadValueReference(IfNotEqual.Reference2);
     255  if (Value1 is TSourceValueInteger) and (Value2 is TSourceValueInteger) then begin
     256    if TSourceValueInteger(Value1).Value = TSourceValueInteger(Value2).Value then
     257      SkipNext := True;
     258  end else
     259  if (Value1 is TSourceValueString) and (Value2 is TSourceValueString) then begin
     260    if TSourceValueString(Value1).Value = TSourceValueString(Value2).Value then
     261      SkipNext := True;
     262  end else
     263  raise Exception.Create('Unsupported types for comparison.');
     264end;
     265
    232266procedure TSourceExecutor.ExecuteRepeat(CommandRepeat: TCommandRepeat);
    233267var
     
    251285  Text: string;
    252286  IntValue: Integer;
     287  Dest: TSourceValue;
    253288begin
    254289  if Command is TCommandFunctionCall then
     
    276311    if Name = 'inputln' then begin
    277312      if Assigned(FOnInput) then begin
    278         Variable := ReadVarReference(TSourceReference(Parameters[0]));
    279         ExecutorVar := Variables.Search(Variable);
    280         if ExecutorVar.Value is TSourceValueString then begin
    281           TSourceValueString(ExecutorVar.Value).Value := FOnInput;
    282           FOnOutput(TSourceValueString(ExecutorVar.Value).Value + LineEnding);
     313        Value := ReadVarReference(TSourceReference(Parameters[0]));
     314        if Value is TSourceValueString then begin
     315          TSourceValueString(Value).Value := FOnInput;
     316          FOnOutput(TSourceValueString(Value).Value + LineEnding);
    283317        end else
    284         if ExecutorVar.Value is TSourceValueInteger then begin
     318        if Value is TSourceValueInteger then begin
    285319          Text := FOnInput;
    286320          if TryStrToInt(Text, IntValue) then
    287             TSourceValueInteger(ExecutorVar.Value).Value := IntValue
    288             else TSourceValueInteger(ExecutorVar.Value).Value := 0;
    289           FOnOutput(IntToStr(TSourceValueInteger(ExecutorVar.Value).Value) + LineEnding);
     321            TSourceValueInteger(Value).Value := IntValue
     322            else TSourceValueInteger(Value).Value := 0;
     323          FOnOutput(IntToStr(TSourceValueInteger(Value).Value) + LineEnding);
    290324        end else
    291325        raise Exception.Create('Unsupported value type');
     
    296330    end else
    297331    if Name = 'increment' then begin
    298       Variable := ReadVarReference(TSourceReference(Parameters[0]));
     332      Dest := ReadVarReference(TSourceReference(Parameters[0]));
    299333      Value := ReadValueReference(TSourceReference(Parameters[1]));
    300       ExecutorVar := Variables.Search(Variable);
    301       if not Assigned(ExecutorVar) then raise Exception.Create('Variable not found');
    302       if (ExecutorVar.Value is TSourceValueInteger) and (Value is TSourceValueInteger) then
    303         Inc(TSourceValueInteger(ExecutorVar.Value).Value, TSourceValueInteger(Value).Value)
     334      if (Dest is TSourceValueInteger) and (Value is TSourceValueInteger) then
     335        Inc(TSourceValueInteger(Dest).Value, TSourceValueInteger(Value).Value)
    304336      else raise Exception.Create('Wrong type for increment');
    305337    end else
    306338    if Name = 'decrement' then begin
    307       Variable := ReadVarReference(TSourceReference(Parameters[0]));
     339      Dest := ReadVarReference(TSourceReference(Parameters[0]));
    308340      Value := ReadValueReference(TSourceReference(Parameters[1]));
    309       ExecutorVar := Variables.Search(Variable);
    310       if not Assigned(ExecutorVar) then raise Exception.Create('Variable not found');
    311       if (ExecutorVar.Value is TSourceValueInteger) and (Value is TSourceValueInteger) then
    312         Dec(TSourceValueInteger(ExecutorVar.Value).Value, TSourceValueInteger(Value).Value)
     341      if (Dest is TSourceValueInteger) and (Value is TSourceValueInteger) then
     342        Dec(TSourceValueInteger(Dest).Value, TSourceValueInteger(Value).Value)
    313343      else raise Exception.Create('Wrong type for increment');
    314344    end else
     
    324354    ExecuteIfEqual(TCommandIfEqual(Command));
    325355  end else
     356  if Command is TCommandIfNotEqual then begin
     357    ExecuteIfNotEqual(TCommandIfNotEqual(Command));
     358  end else
    326359  if Command is TCommandRepeat then begin
    327360    ExecuteRepeat(Command as TCommandRepeat);
Note: See TracChangeset for help on using the changeset viewer.