Changeset 212 for branches/interpreter2/UGeneratorPascal.pas
- Timestamp:
- Apr 22, 2020, 12:04:22 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2/UGeneratorPascal.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 GenerateBlockVar(ParentBlock: TBlock; Block: TBlock); 19 20 procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock); 21 procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock); 20 22 procedure GenerateBeginEnd(Block: TBlock; BeginEnd: TBeginEnd); 21 23 procedure GenerateCommand(Block: TBlock; Command: TCommand); … … 188 190 begin 189 191 if Prog.Name <> '' then AddTextLine('program ' + Prog.Name + ';'); 192 AddTextLine('{$mode delphi}'); 193 AddTextLine('uses SysUtils;'); 190 194 GenerateBlock(Block, Prog.Block); 191 195 AddTextLine('.'); 192 196 end; 193 197 198 procedure TGeneratorPascal.GenerateFunction(ParentBlock: TBlock; 199 FunctionDef: TFunction); 200 var 201 I: Integer; 202 begin 203 AddText('function ' + FunctionDef.Name); 204 if FunctionDef.Params.Count > 0 then begin 205 AddText('('); 206 for I := 0 to FunctionDef.Params.Count - 1 do begin 207 AddText(TFunctionParameter(FunctionDef.Params[I]).Name); 208 AddText(': '); 209 AddText(TFunctionParameter(FunctionDef.Params[I]).TypeRef.Name); 210 if I > 0 then AddText(', '); 211 end; 212 AddText(')'); 213 end; 214 if Assigned(FunctionDef.ResultType) then begin 215 AddText(': '); 216 AddText(FunctionDef.ResultType.Name); 217 end; 218 AddTextLine(';'); 219 if FunctionDef.InternalName <> '' then begin 220 AddTextLine('begin'); 221 Indent := Indent + 1; 222 if FunctionDef.InternalName = 'WriteLn' then AddTextLine('System.WriteLn(Text);') 223 else if FunctionDef.InternalName = 'Write' then AddTextLine('System.Write(Text);') 224 else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return SysUtils.IntToStr(Value);') 225 else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return SysUtils.StrToInt(Value);'); 226 Indent := Indent - 1; 227 AddTextLine('end;'); 228 end else begin 229 GenerateBlock(ParentBlock, FunctionDef.Block); 230 AddTextLine(';'); 231 end; 232 end; 233 194 234 procedure TGeneratorPascal.GenerateBlock(ParentBlock: TBlock; Block: TBlock); 195 235 begin 196 236 GenerateBlockConst(ParentBlock, Block); 197 237 GenerateBlockVar(ParentBlock, Block); 238 GenerateBlockFunctions(ParentBlock, Block); 198 239 GenerateBeginEnd(ParentBlock, Block.BeginEnd); 199 240 end; … … 203 244 I: Integer; 204 245 Variable: TVariable; 205 begin 206 if Block.Variables.Count > 0 then begin 246 VarCount: Integer; 247 begin 248 VarCount := 0; 249 for I := 0 to Block.Variables.Count - 1 do 250 if not TVariable(Block.Variables[I]).Internal then Inc(VarCount); 251 252 if VarCount > 0 then begin 207 253 AddTextLine('var'); 208 254 Indent := Indent + 1; 209 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 210 257 Variable := TVariable(Block.Variables[I]); 211 258 AddTextLine(Variable.Name + ': ' + Variable.TypeRef.Name + ';'); … … 249 296 end; 250 297 298 procedure TGeneratorPascal.GenerateBlockFunctions(ParentBlock: TBlock; 299 Block: TBlock); 300 var 301 I: Integer; 302 begin 303 for I := 0 to Block.Functions.Count - 1 do begin 304 GenerateFunction(ParentBlock, TFunction(Block.Functions[I])); 305 AddTextLine; 306 end; 307 end; 308 251 309 procedure TGeneratorPascal.Generate; 252 310 begin
Note:
See TracChangeset
for help on using the changeset viewer.