Changeset 234 for branches/xpascal/Generators/GeneratorPhp.pas
- Timestamp:
- Jun 27, 2023, 12:50:09 AM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/xpascal/Generators/GeneratorPhp.pas
r232 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 GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock); 20 procedure GenerateBlockProcedures(ParentBlock: TBlock; Block: TBlock); 19 21 procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd); 20 22 procedure GenerateCommand(Block: TBlock; Command: TCommand); … … 24 26 procedure GenerateForToDo(Block: TBlock; ForToDo: TForToDo); 25 27 procedure GenerateFunctionCall(Block: TBlock; FunctionCall: TFunctionCall); 28 procedure GenerateProcedureCall(Block: TBlock; ProcedureCall: TProcedureCall); 26 29 procedure GenerateAssignment(Block: TBlock; Assignment: TAssignment); 27 30 procedure GenerateExpression(Block: TBlock; Expression: TExpression); … … 52 55 if Command is TBeginEnd then GenerateBeginEnd(Block, TBeginEnd(Command)) 53 56 else if Command is TFunctionCall then GenerateFunctionCall(Block, TFunctionCall(Command)) 57 else if Command is TProcedureCall then GenerateProcedureCall(Block, TProcedureCall(Command)) 54 58 else if Command is TAssignment then GenerateAssignment(Block, TAssignment(Command)) 55 59 else if Command is TIfThenElse then GenerateIfThenElse(Block, TIfThenElse(Command)) … … 133 137 end; 134 138 139 procedure TGeneratorPhp.GenerateProcedureCall(Block: TBlock; 140 ProcedureCall: TProcedureCall); 141 var 142 I: Integer; 143 begin 144 AddText(ProcedureCall.ProcedureDef.Name); 145 if ProcedureCall.Params.Count > 0 then begin 146 AddText('('); 147 for I := 0 to ProcedureCall.Params.Count - 1 do 148 GenerateExpression(Block, TExpression(ProcedureCall.Params[I])); 149 AddText(')'); 150 end; 151 end; 152 135 153 procedure TGeneratorPhp.GenerateAssignment(Block: TBlock; Assignment: TAssignment); 136 154 begin … … 240 258 AddTextLine('{'); 241 259 Indent := Indent + 1; 242 if FunctionDef.InternalName = 'WriteLn' then AddTextLine('echo($Text."\n");') 243 else if FunctionDef.InternalName = 'Write' then AddTextLine('echo($Text);') 244 else if FunctionDef.InternalName = 'ReadLn' then AddTextLine('$Text = readline();') 245 else if FunctionDef.InternalName = 'Read' then AddTextLine('$Text = readline();') 246 else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return $Value;') 260 if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return $Value;') 247 261 else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return $Value;') 248 262 else if FunctionDef.InternalName = 'BoolToStr' then AddTextLine('return $Value;') … … 256 270 end; 257 271 272 procedure TGeneratorPhp.GenerateProcedure(ParentBlock: TBlock; 273 ProcedureDef: TProcedure); 274 var 275 I: Integer; 276 begin 277 AddText('function ' + ProcedureDef.Name + '('); 278 for I := 0 to ProcedureDef.Params.Count - 1 do begin 279 if ProcedureDef.Params[I].Kind = pkVar then 280 AddText('&'); 281 AddText('$' + TFunctionParameter(ProcedureDef.Params[I]).Name); 282 if I > 0 then AddText(', '); 283 end; 284 AddTextLine(')'); 285 if ProcedureDef.InternalName <> '' then begin 286 AddTextLine('{'); 287 Indent := Indent + 1; 288 if ProcedureDef.InternalName = 'WriteLn' then AddTextLine('echo($Text."\n");') 289 else if ProcedureDef.InternalName = 'Write' then AddTextLine('echo($Text);') 290 else if ProcedureDef.InternalName = 'ReadLn' then AddTextLine('$Text = readline();') 291 else if ProcedureDef.InternalName = 'Read' then AddTextLine('$Text = readline();'); 292 Indent := Indent - 1; 293 AddTextLine('}'); 294 end else begin 295 GenerateBlock(ParentBlock, ProcedureDef.Block); 296 AddTextLine; 297 end; 298 end; 299 258 300 procedure TGeneratorPhp.GenerateBlock(ParentBlock: TBlock; Block: TBlock); 259 301 begin 260 302 GenerateBlockConst(ParentBlock, Block); 303 GenerateBlockProcedures(ParentBlock, Block); 261 304 GenerateBlockFunctions(ParentBlock, Block); 262 305 if Block.BeginEnd.Commands.Count > 0 then begin … … 290 333 end; 291 334 335 procedure TGeneratorPhp.GenerateBlockProcedures(ParentBlock: TBlock; 336 Block: TBlock); 337 var 338 I: Integer; 339 begin 340 for I := 0 to Block.Procedures.Count - 1 do begin 341 GenerateProcedure(ParentBlock, TProcedure(Block.Procedures[I])); 342 AddTextLine; 343 end; 344 end; 345 292 346 procedure TGeneratorPhp.GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd); 293 347 var
Note:
See TracChangeset
for help on using the changeset viewer.