Ignore:
Timestamp:
Apr 20, 2020, 1:10:44 AM (5 years ago)
Author:
chronos
Message:
  • Added: Support for repeat-until.
Location:
branches/interpreter2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter2

    • Property svn:ignore
      •  

        old new  
        44interpreter.res
        55heaptrclog.trc
         6Generated
  • branches/interpreter2/UGeneratorPhp.pas

    r204 r205  
    66
    77uses
    8   Classes, SysUtils, strutils, USource;
     8  Classes, SysUtils, strutils, USource, UGenerator;
    99
    1010type
     
    1212  { TGeneratorPhp }
    1313
    14   TGeneratorPhp = class
     14  TGeneratorPhp = class(TGenerator)
    1515  private
    16     Indent: Integer;
    17     procedure AddText(Text: string);
    18     procedure AddTextLine(Text: string = '');
    1916    procedure GenerateProgram(Block: TBlock;  Prog:TProgram);
    2017    procedure GenerateBlock(ParentBlock: TBlock; Block: TBlock);
    21     procedure GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);
    2218    procedure GenerateBlockConst(ParentBlock: TBlock; Block: TBlock);
    2319    procedure GenerateBlockFunctions(ParentBlock: TBlock; Block: TBlock);
     
    2622    procedure GenerateIfThenElse(Block: TBlock; IfThenElse: TIfThenElse);
    2723    procedure GenerateWhileDo(Block: TBlock; WhileDo: TWhileDo);
     24    procedure GenerateRepeatUntil(Block: TBlock; RepeatUntil: TRepeatUntil);
    2825    procedure GenerateForToDo(Block: TBlock; ForToDo: TForToDo);
    2926    procedure GenerateFunctionCall(Block: TBlock; FunctionCall: TFunctionCall);
     
    3532  public
    3633    Prog: TProgram;
    37     Output: string;
    3834    procedure Generate;
    3935  end;
     
    5046  else if Command is TIfThenElse then GenerateIfThenElse(Block, TIfThenElse(Command))
    5147  else if Command is TWhileDo then GenerateWhileDo(Block, TWhileDo(Command))
     48  else if Command is TRepeatUntil then GenerateRepeatUntil(Block, TRepeatUntil(Command))
    5249  else if Command is TForToDo then GenerateForToDo(Block, TForToDo(Command))
    5350  else raise Exception.Create('Unsupported command type');
     
    7370  AddText(' ) ');
    7471  GenerateCommand(Block, WhileDo.Command);
     72end;
     73
     74procedure TGeneratorPhp.GenerateRepeatUntil(Block: TBlock;
     75  RepeatUntil: TRepeatUntil);
     76var
     77  I: Integer;
     78begin
     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('}');
    7591end;
    7692
     
    128144  for I := 0 to Expression.Items.Count - 1 do begin
    129145    if I > 0 then begin
    130       AddText(' ');
    131146      if Expression.Operation = eoAdd then begin
    132147        if Expression.TypeRef.Name = 'string' then AddText('.')
    133         else AddText('+');
     148        else AddText(' + ');
    134149      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(' != ');
    139153    end;
    140154    GenerateExpression(Block, TExpression(Expression.Items[I]));
     
    163177end;
    164178
    165 procedure TGeneratorPhp.AddText(Text: string);
    166 begin
    167   Output := Output + Text;
    168 end;
    169 
    170 procedure TGeneratorPhp.AddTextLine(Text: string);
    171 begin
    172   AddText(Text + LineEnding + DupeString('  ', Indent));
    173 end;
    174 
    175179procedure TGeneratorPhp.GenerateProgram(Block: TBlock; Prog: TProgram);
    176180begin
     
    185189begin
    186190  GenerateBlockConst(ParentBlock, Block);
    187   //GenerateBlockVar(ParentBlock, Block);
    188191  GenerateBlockFunctions(ParentBlock, Block);
    189192  if Block.BeginEnd.Commands.Count > 0 then
    190193    GenerateBeginEnd(ParentBlock, Block.BeginEnd);
    191 end;
    192 
    193 procedure TGeneratorPhp.GenerateBlockVar(ParentBlock: TBlock; Block: TBlock);
    194 var
    195   I: Integer;
    196   Variable: TVariable;
    197 begin
    198   if Block.Variables.Count > 0 then begin
    199     AddText('var');
    200     Inc(Indent);
    201     AddTextLine;
    202     for I := 0 to Block.Variables.Count - 1 do begin
    203       Variable := TVariable(Block.Variables[I]);
    204       AddTextLine(Variable.Name + ': ' + Variable.TypeRef.Name + ';');
    205     end;
    206     Dec(Indent);
    207     AddTextLine;
    208   end;
    209194end;
    210195
     
    239224    if FunctionDef.InternalName <> '' then begin
    240225      AddTextLine('{');
     226      Indent := Indent + 1;
    241227      if FunctionDef.InternalName = 'WriteLn' then AddTextLine('echo($Text."\n");')
    242228      else if FunctionDef.InternalName = 'Write' then AddTextLine('echo($Text);')
    243229      else if FunctionDef.InternalName = 'IntToStr' then AddTextLine('return $Value;')
    244230      else if FunctionDef.InternalName = 'StrToInt' then AddTextLine('return $Value;');
    245       Dec(Indent);
     231      Indent := Indent - 1;
    246232      AddTextLine('}');
    247233    end else begin
     
    257243  I: Integer;
    258244begin
    259   AddText('{');
    260   Inc(Indent);
    261   AddTextLine('');
     245  AddTextLine('{');
     246  Indent := Indent + 1;
    262247  for I := 0 to BeginEnd.Commands.Count - 1 do begin
    263248    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;
    269252  AddText('}');
    270253end;
Note: See TracChangeset for help on using the changeset viewer.