Ignore:
Timestamp:
Jun 27, 2023, 12:50:09 AM (17 months ago)
Author:
chronos
Message:
  • Fixed: Procedures generation.
  • Fixed: Splitters between panels.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/xpascal/Generators/GeneratorCSharp.pas

    r233 r234  
    1414    procedure GenerateProgram(Block: TBlock;  Prog:TProgram);
    1515    procedure GenerateFunction(ParentBlock: TBlock; FunctionDef: TFunction);
     16    procedure GenerateProcedure(ParentBlock: TBlock; ProcedureDef: TProcedure);
    1617    procedure GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    1718    procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock);
    1819    procedure GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);
    1920    procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock);
     21    procedure GenerateBlockProcedures(ParentBlock: TBlock; Block: TBlock);
    2022    procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd; Enclosed: Boolean = True);
    2123    procedure GenerateCommand(Block: TBlock; Command: TCommand);
     
    2527    procedure GenerateForToDo(Block: TBlock; ForToDo: TForToDo);
    2628    procedure GenerateFunctionCall(Block: TBlock; FunctionCall: TFunctionCall);
     29    procedure GenerateProcedureCall(Block: TBlock; ProcedureCall: TProcedureCall);
    2730    procedure GenerateAssignment(Block: TBlock; Assignment: TAssignment);
    2831    procedure GenerateExpression(Block: TBlock; Expression: TExpression);
     
    5457  if Command is TBeginEnd then GenerateBeginEnd(Block, TBeginEnd(Command))
    5558  else if Command is TFunctionCall then GenerateFunctionCall(Block, TFunctionCall(Command))
     59  else if Command is TProcedureCall then GenerateProcedureCall(Block, TProcedureCall(Command))
    5660  else if Command is TAssignment then GenerateAssignment(Block, TAssignment(Command))
    5761  else if Command is TIfThenElse then GenerateIfThenElse(Block, TIfThenElse(Command))
     
    138142end;
    139143
     144procedure TGeneratorCSharp.GenerateProcedureCall(Block: TBlock;
     145  ProcedureCall: TProcedureCall);
     146var
     147  I: Integer;
     148begin
     149  AddText(ProcedureCall.ProcedureDef.Name);
     150  if ProcedureCall.Params.Count > 0 then begin
     151    AddText('(');
     152    for I := 0 to ProcedureCall.Params.Count - 1 do begin
     153      if ProcedureCall.ProcedureDef.Params[I].Kind = pkVar then
     154        AddText('ref ');
     155      GenerateExpression(Block, TExpression(ProcedureCall.Params[I]));
     156    end;
     157    AddText(')');
     158  end;
     159end;
     160
    140161procedure TGeneratorCSharp.GenerateAssignment(Block: TBlock; Assignment: TAssignment);
    141162begin
     
    238259  Indent := Indent + 1;
    239260  GenerateBlockFunctions(nil, Prog.SystemBlock);
     261  GenerateBlockProcedures(nil, Prog.SystemBlock);
    240262  GenerateBlock(nil, Prog.SystemBlock);
    241263  AddTextLine('public static void Main()');
     
    246268  AddTextLine();
    247269  GenerateBlockFunctions(Prog.Block, Prog.Block);
     270  GenerateBlockProcedures(Prog.Block, Prog.Block);
    248271  AddTextLine('public void Entry()');
    249272  GenerateBlock(Block, Prog.Block);
     
    320343    AddTextLine('{');
    321344    Indent := Indent + 1;
    322     if FunctionDef.InternalName = 'WriteLn' then AddTextLine('Console.Write(Text + "\n");')
    323     else if FunctionDef.InternalName = 'Write' then AddTextLine('Console.Write(Text);')
    324     else if FunctionDef.InternalName = 'ReadLn' then AddTextLine('Text = Console.ReadLine();')
    325     else if FunctionDef.InternalName = 'Read' then AddTextLine('Text = Console.ReadLine();')
    326     else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return Value.ToString();')
     345    if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return Value.ToString();')
    327346    else if FunctionDef.InternalName = 'StrToInt' then begin
    328347      AddTextLine('int x = 0;');
     
    349368end;
    350369
     370procedure TGeneratorCSharp.GenerateProcedure(ParentBlock: TBlock;
     371  ProcedureDef: TProcedure);
     372var
     373  I: Integer;
     374  Param: TFunctionParameter;
     375begin
     376  AddText('void ' + ProcedureDef.Name + '(');
     377  for I := 0 to ProcedureDef.Params.Count - 1 do begin
     378    Param := TFunctionParameter(ProcedureDef.Params[I]);
     379    if Param.Kind = pkVar then AddText('ref ');
     380    GenerateTypeRef(Param.TypeRef);
     381    AddText(' ');
     382    AddText(Param.Name);
     383    if I > 0 then AddText(', ');
     384  end;
     385  AddTextLine(')');
     386  if ProcedureDef.InternalName <> '' then begin
     387    AddTextLine('{');
     388    Indent := Indent + 1;
     389    if ProcedureDef.InternalName = 'WriteLn' then AddTextLine('Console.Write(Text + "\n");')
     390    else if ProcedureDef.InternalName = 'Write' then AddTextLine('Console.Write(Text);')
     391    else if ProcedureDef.InternalName = 'ReadLn' then AddTextLine('Text = Console.ReadLine();')
     392    else if ProcedureDef.InternalName = 'Read' then AddTextLine('Text = Console.ReadLine();');
     393
     394    Indent := Indent - 1;
     395    AddTextLine('}');
     396  end else begin
     397    GenerateBlock(ParentBlock, ProcedureDef.Block);
     398    AddTextLine;
     399  end;
     400end;
     401
    351402procedure TGeneratorCSharp.GenerateBlockFunctions(ParentBlock: TBlock;
    352403  Block: TBlock);
     
    360411end;
    361412
     413procedure TGeneratorCSharp.GenerateBlockProcedures(ParentBlock: TBlock;
     414  Block: TBlock);
     415var
     416  I: Integer;
     417begin
     418  for I := 0 to Block.Procedures.Count - 1 do begin
     419    GenerateProcedure(ParentBlock, TProcedure(Block.Procedures[I]));
     420    AddTextLine;
     421  end;
     422end;
     423
    362424procedure TGeneratorCSharp.GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd; Enclosed: Boolean = True);
    363425var
Note: See TracChangeset for help on using the changeset viewer.