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/UGeneratorPhp.pas

    r208 r212  
    1515  private
    1616    procedure GenerateProgram(Block: TBlock;  Prog:TProgram);
     17    procedure GenerateFunction(ParentBlock: TBlock; FunctionDef: TFunction);
    1718    procedure GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    1819    procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock);
     
    201202end;
    202203
     204procedure TGeneratorPhp.GenerateFunction(ParentBlock: TBlock;
     205  FunctionDef: TFunction);
     206var
     207  I: Integer;
     208begin
     209  AddText('function ' + FunctionDef.Name + '(');
     210  for I := 0 to FunctionDef.Params.Count - 1 do begin
     211    AddText('$' + TFunctionParameter(FunctionDef.Params[I]).Name);
     212    if I > 0 then AddText(', ');
     213  end;
     214  AddTextLine(')');
     215  if FunctionDef.InternalName <> '' then begin
     216    AddTextLine('{');
     217    Indent := Indent + 1;
     218    if FunctionDef.InternalName = 'WriteLn' then AddTextLine('echo($Text."\n");')
     219    else if FunctionDef.InternalName = 'Write' then AddTextLine('echo($Text);')
     220    else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return $Value;')
     221    else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return $Value;');
     222    Indent := Indent - 1;
     223    AddTextLine('}');
     224  end else begin
     225    GenerateBlock(ParentBlock, FunctionDef.Block);
     226    AddTextLine;
     227  end;
     228end;
     229
    203230procedure TGeneratorPhp.GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    204231begin
     
    228255var
    229256  I: Integer;
    230   J: Integer;
    231   FunctionDef: TFunction;
    232257begin
    233258  for I := 0 to Block.Functions.Count - 1 do begin
    234     FunctionDef := TFunction(Block.Functions[I]);
    235     AddText('function ' + FunctionDef.Name + '(');
    236     for J := 0 to FunctionDef.Params.Count - 1 do begin
    237       AddText('$' + TFunctionParameter(FunctionDef.Params[J]).Name);
    238       if J > 0 then AddText(', ');
    239     end;
    240     AddTextLine(')');
    241     if FunctionDef.InternalName <> '' then begin
    242       AddTextLine('{');
    243       Indent := Indent + 1;
    244       if FunctionDef.InternalName = 'WriteLn' then AddTextLine('echo($Text."\n");')
    245       else if FunctionDef.InternalName = 'Write' then AddTextLine('echo($Text);')
    246       else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return $Value;')
    247       else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return $Value;');
    248       Indent := Indent - 1;
    249       AddTextLine('}');
    250     end else begin
    251       GenerateBeginEnd(ParentBlock, FunctionDef.BeginEnd);
    252       AddTextLine;
    253     end;
     259    GenerateFunction(ParentBlock, TFunction(Block.Functions[I]));
    254260    AddTextLine;
    255261  end;
Note: See TracChangeset for help on using the changeset viewer.