Ignore:
Timestamp:
Apr 22, 2020, 10:23:31 PM (5 years ago)
Author:
chronos
Message:
  • Added: Transformation of Result variable assignment into Return statement.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2/UGeneratorCSharp.pas

    r212 r213  
    2020    procedure GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);
    2121    procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock);
    22     procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd);
     22    procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd; Enclosed: Boolean = True);
    2323    procedure GenerateCommand(Block: TBlock; Command: TCommand);
    2424    procedure GenerateIfThenElse(Block: TBlock; IfThenElse: TIfThenElse);
     
    3333    procedure GenerateBreak(Block: TBlock; BreakCmd: TBreak);
    3434    procedure GenerateContinue(Block: TBlock; ContinueCmd: TContinue);
     35    procedure GenerateReturn(Block: TBlock; Return: TReturn);
    3536    procedure GenerateTypeRef(TypeRef: TType);
    3637    procedure GenerateValue(Value: TValue);
     
    5556  else if Command is TBreak then GenerateBreak(Block, TBreak(Command))
    5657  else if Command is TContinue then GenerateContinue(Block, TContinue(Command))
     58  else if Command is TReturn then GenerateReturn(Block, TReturn(Command))
    5759  else if Command is TEmptyCommand then
    5860  else raise Exception.Create('Unsupported command type');
     
    184186end;
    185187
     188procedure TGeneratorCSharp.GenerateReturn(Block: TBlock; Return: TReturn);
     189begin
     190  AddText('return ');
     191  GenerateExpression(Block, Return.Expression);
     192end;
     193
    186194procedure TGeneratorCSharp.GenerateTypeRef(TypeRef: TType);
    187195begin
     
    209217  AddTextLine('{');
    210218  Indent := Indent + 1;
     219  GenerateBlockFunctions(nil, Prog.SystemBlock);
    211220  GenerateBlock(nil, Prog.SystemBlock);
    212221  AddTextLine('public static void Main()');
     
    216225  AddTextLine('}');
    217226  AddTextLine();
     227  GenerateBlockFunctions(Prog.Block, Prog.Block);
    218228  AddTextLine('public void Entry()');
    219229  GenerateBlock(Block, Prog.Block);
     
    223233
    224234procedure TGeneratorCSharp.GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    225 begin
    226   GenerateBlockVar(Block, Block);
    227   GenerateBlockConst(Block, Block);
    228   GenerateBlockFunctions(Block, Block);
     235var
     236  I: Integer;
     237begin
    229238  if Block.BeginEnd.Commands.Count > 0 then begin
    230     GenerateBeginEnd(ParentBlock, Block.BeginEnd);
    231     AddTextLine;
     239    AddTextLine('{');
     240    Indent := Indent + 1;
     241    GenerateBlockVar(Block, Block);
     242    GenerateBlockConst(Block, Block);
     243    GenerateBeginEnd(ParentBlock, Block.BeginEnd, False);
     244    Indent := Indent - 1;
     245    AddTextLine('}');
    232246  end;
    233247end;
     
    245259    AddTextLine(';');
    246260  end;
     261  if Block.Constants.Count > 0 then AddTextLine;
    247262end;
    248263
     
    251266  I: Integer;
    252267  Variable: TVariable;
    253 begin
    254   if Block.Variables.Count > 0 then begin
     268  VarCount: Integer;
     269begin
     270  VarCount := 0;
     271  for I := 0 to Block.Variables.Count - 1 do
     272    if not TVariable(Block.Variables[I]).Internal then Inc(VarCount);
     273  if VarCount > 0 then begin
    255274    for I := 0 to Block.Variables.Count - 1 do
    256275    if not TVariable(Block.Variables[I]).Internal then begin
     
    259278      AddTextLine(' ' + Variable.Name + ';');
    260279    end;
    261     AddTextLine;
    262   end;
     280  end;
     281  if VarCount > 0 then AddTextLine;
    263282end;
    264283
     
    310329end;
    311330
    312 procedure TGeneratorCSharp.GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd);
    313 var
    314   I: Integer;
    315 begin
    316   AddTextLine('{');
    317   Indent := Indent + 1;
     331procedure TGeneratorCSharp.GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd; Enclosed: Boolean = True);
     332var
     333  I: Integer;
     334begin
     335  if Enclosed then begin
     336    AddTextLine('{');
     337    Indent := Indent + 1;
     338  end;
    318339  for I := 0 to BeginEnd.Commands.Count - 1 do begin
    319340    GenerateCommand(Block, TCommand(BeginEnd.Commands[I]));
    320341    AddTextLine(';');
    321342  end;
    322   Indent := Indent - 1;
    323   AddText('}');
     343  if Enclosed then begin
     344    Indent := Indent - 1;
     345    AddText('}');
     346  end;
    324347end;
    325348
Note: See TracChangeset for help on using the changeset viewer.