Changeset 212 for branches/interpreter2/UGeneratorCSharp.pas
- Timestamp:
- Apr 22, 2020, 12:04:22 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2/UGeneratorCSharp.pas
r208 r212 15 15 private 16 16 procedure GenerateProgram(Block: TBlock; Prog:TProgram); 17 procedure GenerateFunction(ParentBlock: TBlock; FunctionDef: TFunction); 17 18 procedure GenerateBlock(ParentBlock: TBlock; Block: TBlock); 18 19 procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock); … … 209 210 Indent := Indent + 1; 210 211 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()'); 211 219 GenerateBlock(Block, Prog.Block); 212 220 Indent := Indent - 1; … … 216 224 procedure TGeneratorCSharp.GenerateBlock(ParentBlock: TBlock; Block: TBlock); 217 225 begin 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); 223 229 if Block.BeginEnd.Commands.Count > 0 then begin 224 230 GenerateBeginEnd(ParentBlock, Block.BeginEnd); … … 234 240 for I := 0 to Block.Constants.Count - 1 do begin 235 241 Constant := TConstant(Block.Constants[I]); 236 AddText('static ');237 242 GenerateTypeRef(Constant.TypeRef); 238 243 AddText(' ' + Constant.Name + ' = '); … … 248 253 begin 249 254 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 251 257 Variable := TVariable(Block.Variables[I]); 252 AddText('static ');253 258 GenerateTypeRef(Variable.TypeRef); 254 259 AddTextLine(' ' + Variable.Name + ';'); … … 258 263 end; 259 264 265 procedure TGeneratorCSharp.GenerateFunction(ParentBlock: TBlock; 266 FunctionDef: TFunction); 267 var 268 I: Integer; 269 begin 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; 299 end; 300 260 301 procedure TGeneratorCSharp.GenerateBlockFunctions(ParentBlock: TBlock; 261 302 Block: TBlock); 262 303 var 263 304 I: Integer; 264 J: Integer;265 FunctionDef: TFunction;266 305 begin 267 306 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])); 299 308 AddTextLine; 300 309 end;
Note:
See TracChangeset
for help on using the changeset viewer.