Changeset 205 for branches/interpreter2/UGeneratorPhp.pas
- Timestamp:
- Apr 20, 2020, 1:10:44 AM (5 years ago)
- Location:
- branches/interpreter2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2
- Property svn:ignore
-
old new 4 4 interpreter.res 5 5 heaptrclog.trc 6 Generated
-
- Property svn:ignore
-
branches/interpreter2/UGeneratorPhp.pas
r204 r205 6 6 7 7 uses 8 Classes, SysUtils, strutils, USource ;8 Classes, SysUtils, strutils, USource, UGenerator; 9 9 10 10 type … … 12 12 { TGeneratorPhp } 13 13 14 TGeneratorPhp = class 14 TGeneratorPhp = class(TGenerator) 15 15 private 16 Indent: Integer;17 procedure AddText(Text: string);18 procedure AddTextLine(Text: string = '');19 16 procedure GenerateProgram(Block: TBlock; Prog:TProgram); 20 17 procedure GenerateBlock(ParentBlock: TBlock; Block: TBlock); 21 procedure GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);22 18 procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock); 23 19 procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock); … … 26 22 procedure GenerateIfThenElse(Block: TBlock; IfThenElse: TIfThenElse); 27 23 procedure GenerateWhileDo(Block: TBlock; WhileDo: TWhileDo); 24 procedure GenerateRepeatUntil(Block: TBlock; RepeatUntil: TRepeatUntil); 28 25 procedure GenerateForToDo(Block: TBlock; ForToDo: TForToDo); 29 26 procedure GenerateFunctionCall(Block: TBlock; FunctionCall: TFunctionCall); … … 35 32 public 36 33 Prog: TProgram; 37 Output: string;38 34 procedure Generate; 39 35 end; … … 50 46 else if Command is TIfThenElse then GenerateIfThenElse(Block, TIfThenElse(Command)) 51 47 else if Command is TWhileDo then GenerateWhileDo(Block, TWhileDo(Command)) 48 else if Command is TRepeatUntil then GenerateRepeatUntil(Block, TRepeatUntil(Command)) 52 49 else if Command is TForToDo then GenerateForToDo(Block, TForToDo(Command)) 53 50 else raise Exception.Create('Unsupported command type'); … … 73 70 AddText(' ) '); 74 71 GenerateCommand(Block, WhileDo.Command); 72 end; 73 74 procedure TGeneratorPhp.GenerateRepeatUntil(Block: TBlock; 75 RepeatUntil: TRepeatUntil); 76 var 77 I: Integer; 78 begin 79 AddTextLine('while (true)'); 80 AddTextLine('{'); 81 Indent := Indent + 1; 82 for I := 0 to RepeatUntil.Commands.Count - 1 do begin 83 GenerateCommand(Block, TCommand(RepeatUntil.Commands[I])); 84 AddTextLine(';'); 85 end; 86 AddText('if ('); 87 GenerateExpression(Block, RepeatUntil.Expression); 88 AddTextLine(') break;'); 89 Indent := Indent - 1; 90 AddTextLine('}'); 75 91 end; 76 92 … … 128 144 for I := 0 to Expression.Items.Count - 1 do begin 129 145 if I > 0 then begin 130 AddText(' ');131 146 if Expression.Operation = eoAdd then begin 132 147 if Expression.TypeRef.Name = 'string' then AddText('.') 133 else AddText(' +');148 else AddText(' + '); 134 149 end 135 else if Expression.Operation = eoSub then AddText('-') 136 else if Expression.Operation = eoEqual then AddText('==') 137 else if Expression.Operation = eoNotEqual then AddText('!='); 138 AddText(' '); 150 else if Expression.Operation = eoSub then AddText(' - ') 151 else if Expression.Operation = eoEqual then AddText(' == ') 152 else if Expression.Operation = eoNotEqual then AddText(' != '); 139 153 end; 140 154 GenerateExpression(Block, TExpression(Expression.Items[I])); … … 163 177 end; 164 178 165 procedure TGeneratorPhp.AddText(Text: string);166 begin167 Output := Output + Text;168 end;169 170 procedure TGeneratorPhp.AddTextLine(Text: string);171 begin172 AddText(Text + LineEnding + DupeString(' ', Indent));173 end;174 175 179 procedure TGeneratorPhp.GenerateProgram(Block: TBlock; Prog: TProgram); 176 180 begin … … 185 189 begin 186 190 GenerateBlockConst(ParentBlock, Block); 187 //GenerateBlockVar(ParentBlock, Block);188 191 GenerateBlockFunctions(ParentBlock, Block); 189 192 if Block.BeginEnd.Commands.Count > 0 then 190 193 GenerateBeginEnd(ParentBlock, Block.BeginEnd); 191 end;192 193 procedure TGeneratorPhp.GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);194 var195 I: Integer;196 Variable: TVariable;197 begin198 if Block.Variables.Count > 0 then begin199 AddText('var');200 Inc(Indent);201 AddTextLine;202 for I := 0 to Block.Variables.Count - 1 do begin203 Variable := TVariable(Block.Variables[I]);204 AddTextLine(Variable.Name + ': ' + Variable.TypeRef.Name + ';');205 end;206 Dec(Indent);207 AddTextLine;208 end;209 194 end; 210 195 … … 239 224 if FunctionDef.InternalName <> '' then begin 240 225 AddTextLine('{'); 226 Indent := Indent + 1; 241 227 if FunctionDef.InternalName = 'WriteLn' then AddTextLine('echo($Text."\n");') 242 228 else if FunctionDef.InternalName = 'Write' then AddTextLine('echo($Text);') 243 229 else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return $Value;') 244 230 else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return $Value;'); 245 Dec(Indent);231 Indent := Indent - 1; 246 232 AddTextLine('}'); 247 233 end else begin … … 257 243 I: Integer; 258 244 begin 259 AddText('{'); 260 Inc(Indent); 261 AddTextLine(''); 245 AddTextLine('{'); 246 Indent := Indent + 1; 262 247 for I := 0 to BeginEnd.Commands.Count - 1 do begin 263 248 GenerateCommand(Block, TCommand(BeginEnd.Commands[I])); 264 AddText(';'); 265 if I < BeginEnd.Commands.Count - 1 then AddTextLine(''); 266 end; 267 Dec(Indent); 268 AddTextLine(''); 249 AddTextLine(';'); 250 end; 251 Indent := Indent - 1; 269 252 AddText('}'); 270 253 end;
Note:
See TracChangeset
for help on using the changeset viewer.