Ignore:
Timestamp:
Jan 17, 2018, 3:34:13 PM (7 years ago)
Author:
chronos
Message:
  • Added: Support for Repeat blocks and Break command.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/easy compiler/USourceGenerator.pas

    r146 r147  
    1515    Indent: Integer;
    1616    function GenerateBeginEnd(BeginEnd: TCommandBeginEnd): string;
     17    function GenerateCommand(Command: TSourceCommand): string;
     18    function GenerateRef(Reference: TSourceReference): string;
    1719    function IndentStr: string;
    1820  public
     
    4951end;
    5052
    51 function TSourceGenerator.GenerateBeginEnd(BeginEnd: TCommandBeginEnd): string;
     53function TSourceGenerator.GenerateRef(Reference: TSourceReference): string;
    5254var
    53   Instruction: TSourceCommand;
    54   I: Integer;
    5555  Value: TSourceValue;
    56 
    57 function GenerateRef(Reference: TSourceReference): string;
    5856begin
    5957  Result := '';
     
    7169end;
    7270
     71function TSourceGenerator.GenerateCommand(Command: TSourceCommand): string;
     72begin
     73  Result := '';
     74  if Command is TCommandFunctionCall then
     75  with TCommandFunctionCall(Command) do begin
     76    if Name = 'print' then begin
     77      Result := Result + IndentStr + 'Write(' + GenerateRef(TSourceReference(Parameters[0])) + ');' +
     78        LineEnding;
     79    end else
     80    if Name = 'println' then begin
     81      Result := Result + IndentStr + 'WriteLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' +
     82        LineEnding;
     83    end else
     84    if Name = 'assign' then begin
     85      Result := Result + IndentStr + GenerateRef(TSourceReference(Parameters[0])) + ' := ' +
     86        GenerateRef(TSourceReference(Parameters[1])) + ';' + LineEnding;
     87    end else
     88    if Name = 'inputln' then begin
     89      Result := Result + IndentStr + 'ReadLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + LineEnding;
     90    end else
     91    if Name = 'increment' then begin
     92      Result := Result + IndentStr + 'Inc(' + GenerateRef(TSourceReference(Parameters[0])) + ', ' +
     93        GenerateRef(TSourceReference(Parameters[1])) + ');' + LineEnding;
     94    end else
     95    if Name = 'decrement' then begin
     96      Result := Result + IndentStr + 'Dec(' + GenerateRef(TSourceReference(Parameters[0])) + ', ' +
     97        GenerateRef(TSourceReference(Parameters[1])) + ');' + LineEnding;
     98    end else
     99    raise Exception.Create('Unsupported instruction name.');
     100  end else
     101  if Command is TCommandBeginEnd then begin
     102    Result := Result + GenerateBeginEnd(TCommandBeginEnd(Command)) +
     103      ';' + LineEnding;
     104  end else
     105  if Command is TCommandBreak then begin
     106    Result := Result + IndentStr + 'Break;' + LineEnding;
     107  end else
     108  if Command is TCommandContinue then begin
     109    Result := Result + IndentStr + 'Continue;' + LineEnding;
     110  end else
     111  if Command is TCommandRepeat then begin
     112    Result := Result + IndentStr + 'repeat' + LineEnding;
     113    Inc(Indent);
     114    Result := Result + IndentStr +  GenerateCommand(TCommandRepeat(Command).Command) + LineEnding;
     115    Dec(Indent);
     116    Result := Result + IndentStr + 'until False;' + LineEnding;
     117  end else
     118  if Command is TCommandIfZero then begin
     119    Result := Result + IndentStr + 'if ' + TCommandIfZero(Command).Variable.Variable.Name + ' = 0 then ';
     120  end else
     121  raise Exception.Create('Unsupported instruction');
     122end;
     123
     124function TSourceGenerator.GenerateBeginEnd(BeginEnd: TCommandBeginEnd): string;
     125var
     126  I: Integer;
    73127begin
    74128  Result := '';
     
    76130  Inc(Indent);
    77131  with BeginEnd do
    78   for I := 0 to Instructions.Count - 1 do begin
    79     Instruction := TSourceCommand(Instructions[I]);
    80     if Instruction is TCommandFunctionCall then
    81     with TCommandFunctionCall(Instruction) do begin
    82       if Name = 'print' then begin
    83         Result := Result + IndentStr + 'Write(' + GenerateRef(TSourceReference(Parameters[0])) + ');' +
    84           LineEnding;
    85       end else
    86       if Name = 'println' then begin
    87         Result := Result + IndentStr + 'WriteLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' +
    88           LineEnding;
    89       end else
    90       if Name = 'assign' then begin
    91         Result := Result + IndentStr + GenerateRef(TSourceReference(Parameters[0])) + ' := ' +
    92           GenerateRef(TSourceReference(Parameters[1])) + ';' + LineEnding;
    93       end else
    94       if Name = 'inputln' then begin
    95         Result := Result + IndentStr + 'ReadLn(' + GenerateRef(TSourceReference(Parameters[0])) + ');' + LineEnding;
    96       end else
    97       if Name = 'increment' then begin
    98         Result := Result + IndentStr + 'Inc(' + GenerateRef(TSourceReference(Parameters[0])) + ', ' +
    99           GenerateRef(TSourceReference(Parameters[1])) + ');' + LineEnding;
    100       end else
    101       raise Exception.Create('Unsupported instruction name.');
    102     end else
    103     if Instruction is TCommandBeginEnd then begin
    104       Result := Result + GenerateBeginEnd(TCommandBeginEnd(Instruction)) +
    105         ';' + LineEnding;
    106     end else
    107     if Instruction is TCommandIfZero then begin
    108       Result := Result + IndentStr + 'if ' + TCommandIfZero(Instruction).Variable.Name + ' = 0 then ';
    109     end else
    110     raise Exception.Create('Unsupported instruction');
     132  for I := 0 to Commands.Count - 1 do begin
     133    Result := Result + GenerateCommand(TSourceCommand(Commands[I]));
    111134  end;
    112135  Dec(Indent);
Note: See TracChangeset for help on using the changeset viewer.