Changeset 234 for branches/xpascal/Generators/GeneratorCSharp.pas
- Timestamp:
- Jun 27, 2023, 12:50:09 AM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/xpascal/Generators/GeneratorCSharp.pas
r233 r234 14 14 procedure GenerateProgram(Block: TBlock; Prog:TProgram); 15 15 procedure GenerateFunction(ParentBlock: TBlock; FunctionDef: TFunction); 16 procedure GenerateProcedure(ParentBlock: TBlock; ProcedureDef: TProcedure); 16 17 procedure GenerateBlock(ParentBlock: TBlock; Block: TBlock); 17 18 procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock); 18 19 procedure GenerateBlockVar(ParentBlock: TBlock; Block: TBlock); 19 20 procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock); 21 procedure GenerateBlockProcedures(ParentBlock: TBlock; Block: TBlock); 20 22 procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd; Enclosed: Boolean = True); 21 23 procedure GenerateCommand(Block: TBlock; Command: TCommand); … … 25 27 procedure GenerateForToDo(Block: TBlock; ForToDo: TForToDo); 26 28 procedure GenerateFunctionCall(Block: TBlock; FunctionCall: TFunctionCall); 29 procedure GenerateProcedureCall(Block: TBlock; ProcedureCall: TProcedureCall); 27 30 procedure GenerateAssignment(Block: TBlock; Assignment: TAssignment); 28 31 procedure GenerateExpression(Block: TBlock; Expression: TExpression); … … 54 57 if Command is TBeginEnd then GenerateBeginEnd(Block, TBeginEnd(Command)) 55 58 else if Command is TFunctionCall then GenerateFunctionCall(Block, TFunctionCall(Command)) 59 else if Command is TProcedureCall then GenerateProcedureCall(Block, TProcedureCall(Command)) 56 60 else if Command is TAssignment then GenerateAssignment(Block, TAssignment(Command)) 57 61 else if Command is TIfThenElse then GenerateIfThenElse(Block, TIfThenElse(Command)) … … 138 142 end; 139 143 144 procedure TGeneratorCSharp.GenerateProcedureCall(Block: TBlock; 145 ProcedureCall: TProcedureCall); 146 var 147 I: Integer; 148 begin 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; 159 end; 160 140 161 procedure TGeneratorCSharp.GenerateAssignment(Block: TBlock; Assignment: TAssignment); 141 162 begin … … 238 259 Indent := Indent + 1; 239 260 GenerateBlockFunctions(nil, Prog.SystemBlock); 261 GenerateBlockProcedures(nil, Prog.SystemBlock); 240 262 GenerateBlock(nil, Prog.SystemBlock); 241 263 AddTextLine('public static void Main()'); … … 246 268 AddTextLine(); 247 269 GenerateBlockFunctions(Prog.Block, Prog.Block); 270 GenerateBlockProcedures(Prog.Block, Prog.Block); 248 271 AddTextLine('public void Entry()'); 249 272 GenerateBlock(Block, Prog.Block); … … 320 343 AddTextLine('{'); 321 344 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();') 327 346 else if FunctionDef.InternalName = 'StrToInt' then begin 328 347 AddTextLine('int x = 0;'); … … 349 368 end; 350 369 370 procedure TGeneratorCSharp.GenerateProcedure(ParentBlock: TBlock; 371 ProcedureDef: TProcedure); 372 var 373 I: Integer; 374 Param: TFunctionParameter; 375 begin 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; 400 end; 401 351 402 procedure TGeneratorCSharp.GenerateBlockFunctions(ParentBlock: TBlock; 352 403 Block: TBlock); … … 360 411 end; 361 412 413 procedure TGeneratorCSharp.GenerateBlockProcedures(ParentBlock: TBlock; 414 Block: TBlock); 415 var 416 I: Integer; 417 begin 418 for I := 0 to Block.Procedures.Count - 1 do begin 419 GenerateProcedure(ParentBlock, TProcedure(Block.Procedures[I])); 420 AddTextLine; 421 end; 422 end; 423 362 424 procedure TGeneratorCSharp.GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd; Enclosed: Boolean = True); 363 425 var
Note:
See TracChangeset
for help on using the changeset viewer.