Ignore:
Timestamp:
Apr 22, 2020, 12:04:22 PM (5 years ago)
Author:
chronos
Message:
  • Added: Support for custom functions.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2/USource.pas

    r211 r212  
    1212  TFunctions = class;
    1313  TBeginEnd = class;
     14  TBlock = class;
    1415
    1516  TDataType = (dtNone, dtString, dtBoolean, dtInteger, dtFloat, dtColor,
     
    129130    Name: string;
    130131    TypeRef: TType;
     132    Internal: Boolean;
    131133    procedure GetValue(Index: Integer; out Value); override;
    132134    function GetField(Index: Integer): TField; override;
     
    183185    Params: TFunctionParameters;
    184186    ResultType: TType;
    185     BeginEnd: TBeginEnd;
     187    Block: TBlock;
     188    ParentType: TType;
    186189    procedure GetValue(Index: Integer; out Value); override;
    187190    function GetField(Index: Integer): TField; override;
     
    194197
    195198  TFunctions = class(TSourceNodes)
     199    ParentType: TType;
    196200    function SearchByName(Name: string): TFunction;
    197201    function AddNew(Name: string): TFunction;
     
    253257  public
    254258    TypeRef: TType;
     259    FunctionRef: TFunction;
    255260    Operation: TExpressionOperator;
    256261    Items: TExpressions;
     262    function GetFunctionName: string;
    257263    procedure GetValue(Index: Integer; out Value); override;
    258264    function GetField(Index: Integer): TField; override;
     
    839845procedure TFunction.GetValue(Index: Integer; out Value);
    840846begin
    841   if Index = 0 then TBeginEnd(Value) := BeginEnd
     847  if Index = 0 then TBlock(Value) := Block
    842848  else if Index = 1 then TFunctionParameters(Value) := Params
    843849  else if Index = 2 then TType(Value) := ResultType
     
    862868procedure TFunction.SetValue(Index: Integer; var Value);
    863869begin
    864   if Index = 0 then BeginEnd := TBeginEnd(Value)
     870  if Index = 0 then Block := TBlock(Value)
    865871  else if Index = 1 then Params := TFunctionParameters(Value)
    866872  else if Index = 2 then ResultType := TType(Value)
     
    872878begin
    873879  Params := TFunctionParameters.Create;
    874   BeginEnd := TBeginEnd.Create;
     880  Block := TBlock.Create;
    875881end;
    876882
    877883destructor TFunction.Destroy;
    878884begin
    879   BeginEnd.Free;
     885  Block.Free;
    880886  Params.Free;
    881887  inherited Destroy;
     
    910916begin
    911917  Functions := TFunctions.Create;
     918  Functions.ParentType := Self;
    912919end;
    913920
     
    950957end;
    951958
     959function TExpressionOperation.GetFunctionName: string;
     960begin
     961  if Operation = eoAdd then Result := '_Add'
     962  else if Operation = eoSub then Result := '_Sub'
     963  else if Operation = eoEqual then Result := '_Equal'
     964  else if Operation = eoNotEqual then Result := '_NotEqual'
     965  else raise Exception.Create('Unsupported operation type.');
     966end;
     967
    952968function TExpressionOperation.GetFieldsCount: Integer;
    953969begin
     
    11591175  Result := TFunction.Create;
    11601176  Result.Name := Name;
     1177  Result.ParentType := ParentType;
    11611178  Add(Result);
    11621179end;
     
    12421259  Result := Types.SearchByName(Name);
    12431260  if not Assigned(Result) and Assigned(ParentBlock) then
    1244     Result := ParentBlock.Types.SearchByName(Name);
     1261    Result := ParentBlock.GetType(Name);
    12451262end;
    12461263
     
    12491266  Result := Constants.SearchByName(Name);
    12501267  if not Assigned(Result) and Assigned(ParentBlock) then
    1251     Result := ParentBlock.Constants.SearchByName(Name);
     1268    Result := ParentBlock.GetConstant(Name);
    12521269end;
    12531270
     
    12561273  Result := Variables.SearchByName(Name);
    12571274  if not Assigned(Result) and Assigned(ParentBlock) then
    1258     Result := ParentBlock.Variables.SearchByName(Name);
     1275    Result := ParentBlock.GetVariable(Name);
    12591276end;
    12601277
     
    12631280  Result := Functions.SearchByName(Name);
    12641281  if not Assigned(Result) and Assigned(ParentBlock) then
    1265     Result := ParentBlock.Functions.SearchByName(Name);
     1282    Result := ParentBlock.GetFunction(Name);
    12661283end;
    12671284
     
    12691286begin
    12701287  Constants := TConstants.Create;
     1288  Constants.Parent := Self;
    12711289  Variables := TVariables.Create;
     1290  Variables.Parent := Self;
    12721291  Functions := TFunctions.Create;
     1292  Functions.Parent := Self;
    12731293  Types := TTypes.Create;
     1294  Types.Parent := Self;
    12741295  BeginEnd := TBeginEnd.Create;
     1296  BeginEnd.Parent := Self;
    12751297end;
    12761298
Note: See TracChangeset for help on using the changeset viewer.