Ignore:
Timestamp:
Jan 16, 2018, 3:19:23 PM (7 years ago)
Author:
chronos
Message:
  • Added: Support for begin end block.
File:
1 edited

Legend:

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

    r141 r142  
    1212
    1313  TSourceGenerator = class
     14  private
     15    function GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string;
     16  public
    1417    function Generate(SourceCode: TSourceCode): string;
    1518  end;
     
    2124
    2225function TSourceGenerator.Generate(SourceCode: TSourceCode): string;
     26var
     27  I: Integer;
     28begin
     29  Result := '';
     30  if SourceCode.Variables.Count > 0 then
     31    Result := Result + 'var' + LineEnding;
     32  with SourceCode do
     33  for I := 0 to Variables.Count - 1 do
     34  with TSourceVariable(Variables[I]) do begin
     35    Result := Result + '  ' + Name + ': ';
     36    if ValueType.Name = 'String' then
     37      Result := Result + 'string'
     38    else if ValueType.Name = 'Integer' then
     39      Result := Result + 'Integer'
     40    else raise Exception.Create('Unsupported type');
     41    Result := Result + ';' + LineEnding;
     42  end;
     43  Result := Result + GenerateBeginEnd(SourceCode.Main) + '.' + LineEnding;
     44end;
     45
     46function TSourceGenerator.GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string;
    2347var
    2448  Output: string;
     
    4569begin
    4670  Output := '';
    47   if SourceCode.Variables.Count > 0 then
    48     Output := Output + 'var' + LineEnding;
    49   with SourceCode do
    50   for I := 0 to Variables.Count - 1 do
    51   with TSourceVariable(Variables[I]) do begin
    52     Output := Output + '  ' + Name + ': ';
    53     if ValueType.Name = 'String' then
    54       Output := Output + 'string'
    55     else if ValueType.Name = 'Integer' then
    56       Output := Output + 'Integer'
    57     else raise Exception.Create('Unsupported type');
    58     Output := Output + ';' + LineEnding;
    59   end;
    6071  Output := Output + 'begin' + LineEnding;
    61   with SourceCode do
     72  with BeginEnd do
    6273  for I := 0 to Instructions.Count - 1 do begin
    6374    Instruction := TSourceInstruction(Instructions[I]);
    64     if Instruction is TSourceInstructionFunction then
    65     with TSourceInstructionFunction(Instruction) do begin
     75    if Instruction is TSourceFunctionCall then
     76    with TSourceFunctionCall(Instruction) do begin
    6677      if Name = 'print' then begin
    6778        Output := Output + '  Write(' + GenerateRef(Parameters[0]) + ');' +
     
    8495      end else
    8596      raise Exception.Create('Unsupported instruction name.');
    86     end else raise Exception.Create('Unsupported instruction');
     97    end else
     98    if Instruction is TSourceBeginEnd then begin
     99      Output := Output + GenerateBeginEnd(TSourceBeginEnd(Instruction)) +
     100        ';' + LineEnding;
     101    end else
     102    raise Exception.Create('Unsupported instruction');
    87103  end;
    88   Output := Output + 'end.' + LineEnding;
     104  Output := Output + 'end';
    89105  Result := Output;
    90106end;
Note: See TracChangeset for help on using the changeset viewer.