Ignore:
Timestamp:
Jan 17, 2018, 5:27:23 PM (7 years ago)
Author:
chronos
Message:
  • Added: Unfinished support for variable arrays.
File:
1 edited

Legend:

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

    r147 r148  
    2828  end;
    2929
     30  TSourceValues = class(TObjectList)
     31  end;
     32
    3033  TSourceValueClass = class of TSourceValue;
    3134
     
    3437  TSourceType = class
    3538    Name: string;
    36     ValueClass: TSourceValueClass;
    37   end;
     39    function GetValueType: TSourceValueClass; virtual;
     40  end;
     41
     42  TSourceTypeClass = class of TSourceType;
    3843
    3944  { TSourceTypes }
     
    4146  TSourceTypes = class(TObjectList)
    4247    Parent: TSourceTypes;
    43     function AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
     48    function AddNew(Name: string; ClassType: TSourceTypeClass): TSourceType;
    4449    function Search(Name: string): TSourceType;
    4550  end;
     51
     52  { TSourceTypeInteger }
     53
     54  TSourceTypeInteger = class(TSourceType)
     55    function GetValueType: TSourceValueClass; override;
     56  end;
     57
     58  { TSourceTypeString }
     59
     60  TSourceTypeString = class(TSourceType)
     61    function GetValueType: TSourceValueClass; override;
     62  end;
     63
     64  { TSourceTypeArray }
     65
     66  TSourceTypeArray = class(TSourceType)
     67    ItemType: TSourceType;
     68    function GetValueType: TSourceValueClass; override;
     69  end;
     70
    4671
    4772  { TSourceVariable }
     
    6489  end;
    6590
     91  TSourceReferenceArray = class(TSourceReference)
     92    ArrayRef: TSourceVariable;
     93    Index: TSourceReference;
     94  end;
     95
    6696  { TSourceValueString }
    6797
     
    76106    Value: Integer;
    77107    procedure Assign(Source: TSourceValue); override;
     108  end;
     109
     110  { TSourceValueArray }
     111
     112  TSourceValueArray = class(TSourceValue)
     113    Items: TSourceValues;
     114    procedure Assign(Source: TSourceValue); override;
     115    constructor Create;
     116    destructor Destroy; override;
    78117  end;
    79118
     
    146185  end;
    147186
    148   { TCommandIfZero }
    149 
    150   TCommandIfZero = class(TSourceCommand)
    151     Variable: TSourceReferenceVariable;
     187  { TCommandIfEqual }
     188
     189  TCommandIfEqual = class(TSourceCommand)
     190    Reference1: TSourceReference;
     191    Reference2: TSourceReference;
    152192    destructor Destroy; override;
    153193  end;
     
    184224implementation
    185225
    186 { TCommandIfZero }
    187 
    188 destructor TCommandIfZero.Destroy;
    189 begin
    190   Variable.Free;
     226{ TSourceType }
     227
     228function TSourceType.GetValueType: TSourceValueClass;
     229begin
     230  Result := nil;
     231end;
     232
     233{ TSourceTypeInteger }
     234
     235function TSourceTypeInteger.GetValueType: TSourceValueClass;
     236begin
     237  Result := TSourceValueInteger;
     238end;
     239
     240{ TSourceTypeString }
     241
     242function TSourceTypeString.GetValueType: TSourceValueClass;
     243begin
     244  Result := TSourceValueString;
     245end;
     246
     247{ TSourceTypeArray }
     248
     249function TSourceTypeArray.GetValueType: TSourceValueClass;
     250begin
     251  Result := TSourceValueArray;
     252end;
     253
     254{ TSourceValueArray }
     255
     256procedure TSourceValueArray.Assign(Source: TSourceValue);
     257var
     258  I: Integer;
     259  Value: TSourceValue;
     260begin
     261  if Source is TSourceValueInteger then begin
     262    while Items.Count < TSourceValueArray(Source).Items.Count do begin
     263      Value := TSourceValue(TSourceValue(TSourceValueArray(Source).Items[Items.Count]).ClassType.Create);
     264      Items.Add(Value);
     265    end;
     266    while Items.Count > TSourceValueArray(Source).Items.Count do begin
     267      Items.Delete(Items.Count - 1);
     268    end;
     269    for I := 0 to Items.Count - 1 do
     270      TSourceValue(Items[I]).Assign(TSourceValue(TSourceValueArray(Source).Items[I]));
     271  end else raise Exception.Create('Type for assignment not matches');
     272end;
     273
     274constructor TSourceValueArray.Create;
     275begin
     276  Items := TSourceValues.Create;
     277end;
     278
     279destructor TSourceValueArray.Destroy;
     280begin
     281  Items.Free;
     282  inherited Destroy;
     283end;
     284
     285{ TCommandIfEqual }
     286
     287destructor TCommandIfEqual.Destroy;
     288begin
     289  Reference1.Free;
     290  Reference2.Free;
    191291  inherited Destroy;
    192292end;
     
    236336{ TSourceTypes }
    237337
    238 function TSourceTypes.AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
    239 begin
    240   Result := TSourceType.Create;
     338function TSourceTypes.AddNew(Name: string; ClassType: TSourceTypeClass): TSourceType;
     339begin
     340  Result := ClassType.Create;
    241341  Result.Name := Name;
    242   Result.ValueClass := ClassType;
    243342  Add(Result);
    244343end;
     
    395494var
    396495  Funct: TSourceFunction;
     496  Typ: TSourceType;
    397497begin
    398498  Functions.Clear;
    399499
    400500  // Init types
    401   Types.AddNew('Integer', TSourceValueInteger);
    402   Types.AddNew('String', TSourceValueString);
     501  Types.AddNew('Integer', TSourceTypeInteger);
     502  Types.AddNew('String', TSourceTypeString);
     503  Typ := Types.AddNew('StringArray', TSourceTypeArray);
     504  TSourceTypeArray(Typ).ItemType := TSourceTypeString.Create;
     505  Typ := Types.AddNew('IntegerArray', TSourceTypeArray);
     506  TSourceTypeArray(Typ).ItemType := TSourceTypeInteger.Create;
    403507
    404508  // Init functions
Note: See TracChangeset for help on using the changeset viewer.