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/UGeneratorPascal.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 GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);
    1920    procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock);
     21    procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock);
    2022    procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd);
    2123    procedure GenerateCommand(Block: TBlock; Command: TCommand);
     
    188190begin
    189191  if Prog.Name <> '' then AddTextLine('program ' + Prog.Name + ';');
     192  AddTextLine('{$mode delphi}');
     193  AddTextLine('uses SysUtils;');
    190194  GenerateBlock(Block, Prog.Block);
    191195  AddTextLine('.');
    192196end;
    193197
     198procedure TGeneratorPascal.GenerateFunction(ParentBlock: TBlock;
     199  FunctionDef: TFunction);
     200var
     201  I: Integer;
     202begin
     203  AddText('function ' + FunctionDef.Name);
     204  if FunctionDef.Params.Count > 0 then begin
     205    AddText('(');
     206    for I := 0 to FunctionDef.Params.Count - 1 do begin
     207      AddText(TFunctionParameter(FunctionDef.Params[I]).Name);
     208      AddText(': ');
     209      AddText(TFunctionParameter(FunctionDef.Params[I]).TypeRef.Name);
     210      if I > 0 then AddText(', ');
     211    end;
     212    AddText(')');
     213  end;
     214  if Assigned(FunctionDef.ResultType) then begin
     215    AddText(': ');
     216    AddText(FunctionDef.ResultType.Name);
     217  end;
     218  AddTextLine(';');
     219  if FunctionDef.InternalName <> '' then begin
     220    AddTextLine('begin');
     221    Indent := Indent + 1;
     222    if FunctionDef.InternalName = 'WriteLn' then AddTextLine('System.WriteLn(Text);')
     223    else if FunctionDef.InternalName = 'Write' then AddTextLine('System.Write(Text);')
     224    else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return SysUtils.IntToStr(Value);')
     225    else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return SysUtils.StrToInt(Value);');
     226    Indent := Indent - 1;
     227    AddTextLine('end;');
     228  end else begin
     229    GenerateBlock(ParentBlock, FunctionDef.Block);
     230    AddTextLine(';');
     231  end;
     232end;
     233
    194234procedure TGeneratorPascal.GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    195235begin
    196236  GenerateBlockConst(ParentBlock, Block);
    197237  GenerateBlockVar(ParentBlock, Block);
     238  GenerateBlockFunctions(ParentBlock, Block);
    198239  GenerateBeginEnd(ParentBlock, Block.BeginEnd);
    199240end;
     
    203244  I: Integer;
    204245  Variable: TVariable;
    205 begin
    206   if Block.Variables.Count > 0 then begin
     246  VarCount: Integer;
     247begin
     248  VarCount := 0;
     249  for I := 0 to Block.Variables.Count - 1 do
     250    if not TVariable(Block.Variables[I]).Internal then Inc(VarCount);
     251
     252  if VarCount > 0 then begin
    207253    AddTextLine('var');
    208254    Indent := Indent + 1;
    209     for I := 0 to Block.Variables.Count - 1 do begin
     255    for I := 0 to Block.Variables.Count - 1 do
     256    if not TVariable(Block.Variables[I]).Internal then begin
    210257      Variable := TVariable(Block.Variables[I]);
    211258      AddTextLine(Variable.Name + ': ' + Variable.TypeRef.Name + ';');
     
    249296end;
    250297
     298procedure TGeneratorPascal.GenerateBlockFunctions(ParentBlock: TBlock;
     299  Block: TBlock);
     300var
     301  I: Integer;
     302begin
     303  for I := 0 to Block.Functions.Count - 1 do begin
     304    GenerateFunction(ParentBlock, TFunction(Block.Functions[I]));
     305    AddTextLine;
     306  end;
     307end;
     308
    251309procedure TGeneratorPascal.Generate;
    252310begin
Note: See TracChangeset for help on using the changeset viewer.