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/UGeneratorCSharp.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);
     
    209210  Indent := Indent + 1;
    210211  GenerateBlock(nil, Prog.SystemBlock);
     212  AddTextLine('public static void Main()');
     213  AddTextLine('{');
     214  AddTextLine('  ' + Prog.Name + ' app = new ' + Prog.Name + '();');
     215  AddTextLine('  app.Entry();');
     216  AddTextLine('}');
     217  AddTextLine();
     218  AddTextLine('public void Entry()');
    211219  GenerateBlock(Block, Prog.Block);
    212220  Indent := Indent - 1;
     
    216224procedure TGeneratorCSharp.GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    217225begin
    218   GenerateBlockVar(ParentBlock, Block);
    219   GenerateBlockConst(ParentBlock, Block);
    220   GenerateBlockFunctions(ParentBlock, Block);
    221   if ParentBlock = Prog.SystemBlock then
    222     AddTextLine('public static void Main()');
     226  GenerateBlockVar(Block, Block);
     227  GenerateBlockConst(Block, Block);
     228  GenerateBlockFunctions(Block, Block);
    223229  if Block.BeginEnd.Commands.Count > 0 then begin
    224230    GenerateBeginEnd(ParentBlock, Block.BeginEnd);
     
    234240  for I := 0 to Block.Constants.Count - 1 do begin
    235241    Constant := TConstant(Block.Constants[I]);
    236     AddText('static ');
    237242    GenerateTypeRef(Constant.TypeRef);
    238243    AddText(' ' + Constant.Name + ' = ');
     
    248253begin
    249254  if Block.Variables.Count > 0 then begin
    250     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
    251257      Variable := TVariable(Block.Variables[I]);
    252       AddText('static ');
    253258      GenerateTypeRef(Variable.TypeRef);
    254259      AddTextLine(' ' + Variable.Name + ';');
     
    258263end;
    259264
     265procedure TGeneratorCSharp.GenerateFunction(ParentBlock: TBlock;
     266  FunctionDef: TFunction);
     267var
     268  I: Integer;
     269begin
     270  GenerateTypeRef(FunctionDef.ResultType);
     271  AddText(' ' + FunctionDef.Name + '(');
     272  for I := 0 to FunctionDef.Params.Count - 1 do begin
     273    GenerateTypeRef(TFunctionParameter(FunctionDef.Params[I]).TypeRef);
     274    AddText(' ');
     275    AddText(TFunctionParameter(FunctionDef.Params[I]).Name);
     276    if I > 0 then AddText(', ');
     277  end;
     278  AddTextLine(')');
     279  if FunctionDef.InternalName <> '' then begin
     280    AddTextLine('{');
     281    Indent := Indent + 1;
     282    if FunctionDef.InternalName = 'WriteLn' then AddTextLine('Console.Write(Text + "\n");')
     283    else if FunctionDef.InternalName = 'Write' then AddTextLine('Console.Write(Text);')
     284    else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return Value.ToString();')
     285    else if FunctionDef.InternalName = 'StrToInt' then begin
     286      AddTextLine('int x = 0;');
     287      AddTextLine('if (int.TryParse(Value, out x))');
     288      AddTextLine('{');
     289      AddTextLine('  return x;');
     290      AddTextLine('} else return 0;');
     291    end;
     292
     293    Indent := Indent - 1;
     294    AddTextLine('}');
     295  end else begin
     296    GenerateBlock(ParentBlock, FunctionDef.Block);
     297    AddTextLine;
     298  end;
     299end;
     300
    260301procedure TGeneratorCSharp.GenerateBlockFunctions(ParentBlock: TBlock;
    261302  Block: TBlock);
    262303var
    263304  I: Integer;
    264   J: Integer;
    265   FunctionDef: TFunction;
    266305begin
    267306  for I := 0 to Block.Functions.Count - 1 do begin
    268     FunctionDef := TFunction(Block.Functions[I]);
    269     AddText('static ');
    270     GenerateTypeRef(FunctionDef.ResultType);
    271     AddText(' ' + FunctionDef.Name + '(');
    272     for J := 0 to FunctionDef.Params.Count - 1 do begin
    273       GenerateTypeRef(TFunctionParameter(FunctionDef.Params[J]).TypeRef);
    274       AddText(' ');
    275       AddText(TFunctionParameter(FunctionDef.Params[J]).Name);
    276       if J > 0 then AddText(', ');
    277     end;
    278     AddTextLine(')');
    279     if FunctionDef.InternalName <> '' then begin
    280       AddTextLine('{');
    281       Indent := Indent + 1;
    282       if FunctionDef.InternalName = 'WriteLn' then AddTextLine('Console.Write(Text + "\n");')
    283       else if FunctionDef.InternalName = 'Write' then AddTextLine('Console.Write(Text);')
    284       else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return Value.ToString();')
    285       else if FunctionDef.InternalName = 'StrToInt' then begin
    286         AddTextLine('int x = 0;');
    287         AddTextLine('if (int.TryParse(Value, out x))');
    288         AddTextLine('{');
    289         AddTextLine('  return x;');
    290         AddTextLine('} else return 0;');
    291       end;
    292 
    293       Indent := Indent - 1;
    294       AddTextLine('}');
    295     end else begin
    296       GenerateBeginEnd(ParentBlock, FunctionDef.BeginEnd);
    297       AddTextLine;
    298     end;
     307    GenerateFunction(ParentBlock, TFunction(Block.Functions[I]));
    299308    AddTextLine;
    300309  end;
Note: See TracChangeset for help on using the changeset viewer.