Changeset 143 for branches/easy compiler


Ignore:
Timestamp:
Jan 16, 2018, 3:40:08 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Better line indentation in generator.
Location:
branches/easy compiler
Files:
2 edited

Legend:

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

    r142 r143  
    3636
    3737  TSourceTypes = class(TObjectList)
     38    Parent: TSourceTypes;
    3839    function AddNew(Name: string; ClassType: TSourceValueClass): TSourceType;
    3940    function Search(Name: string): TSourceType;
     
    5051
    5152  TSourceVariables = class(TObjectList)
     53    Parent: TSourceVariables;
    5254    function AddNew(Name: string; ValueType: TSourceType): TSourceVariable;
    5355    function Search(Name: string): TSourceVariable;
     
    107109
    108110  TSourceConstants = class(TObjectList)
     111    Parent: TSourceConstants;
    109112    function AddNewString(Value: string; Name: string = ''): TSourceConstant;
    110113    function AddNewInteger(Value: Integer; Name: string = ''): TSourceConstant;
     
    141144    procedure InitFunctions;
    142145  public
     146    Functions: TSourceFunctions;
    143147    Types: TSourceTypes;
    144148    Variables: TSourceVariables;
    145149    Constants: TSourceConstants;
    146     Functions: TSourceFunctions;
    147150    Main: TSourceBeginEnd;
    148151    constructor Create;
     
    186189    Break;
    187190  end;
     191  if not Assigned(Result) and Assigned(Parent) then
     192    Result := Parent.Search(Name);
    188193end;
    189194
     
    278283    Break;
    279284  end;
     285  if not Assigned(Result) and Assigned(Parent) then
     286    Result := Parent.Search(Name);
    280287end;
    281288
     
    312319    Break;
    313320  end;
     321  if not Assigned(Result) and Assigned(Parent) then
     322    Result := Parent.Search(Name);
    314323end;
    315324
  • branches/easy compiler/USourceGenerator.pas

    r142 r143  
    1313  TSourceGenerator = class
    1414  private
     15    Indent: Integer;
    1516    function GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string;
     17    function IndentStr: string;
    1618  public
    1719    function Generate(SourceCode: TSourceCode): string;
     
    2729  I: Integer;
    2830begin
     31  Indent := 0;
    2932  Result := '';
    3033  if SourceCode.Variables.Count > 0 then
    31     Result := Result + 'var' + LineEnding;
     34    Result := Result + IndentStr + 'var' + LineEnding;
     35  Inc(Indent);
    3236  with SourceCode do
    3337  for I := 0 to Variables.Count - 1 do
    3438  with TSourceVariable(Variables[I]) do begin
    35     Result := Result + '  ' + Name + ': ';
     39    Result := Result + IndentStr + Name + ': ';
    3640    if ValueType.Name = 'String' then
    3741      Result := Result + 'string'
     
    4145    Result := Result + ';' + LineEnding;
    4246  end;
     47  Dec(Indent);
    4348  Result := Result + GenerateBeginEnd(SourceCode.Main) + '.' + LineEnding;
    4449end;
     
    4651function TSourceGenerator.GenerateBeginEnd(BeginEnd: TSourceBeginEnd): string;
    4752var
    48   Output: string;
    4953  Instruction: TSourceInstruction;
    5054  I: Integer;
     
    6872
    6973begin
    70   Output := '';
    71   Output := Output + 'begin' + LineEnding;
     74  Result := '';
     75  Result := Result + IndentStr + 'begin' + LineEnding;
     76  Inc(Indent);
    7277  with BeginEnd do
    7378  for I := 0 to Instructions.Count - 1 do begin
     
    7681    with TSourceFunctionCall(Instruction) do begin
    7782      if Name = 'print' then begin
    78         Output := Output + '  Write(' + GenerateRef(Parameters[0]) + ');' +
     83        Result := Result + IndentStr + 'Write(' + GenerateRef(Parameters[0]) + ');' +
    7984          LineEnding;
    8085      end else
    8186      if Name = 'println' then begin
    82         Output := Output + '  WriteLn(' + GenerateRef(Parameters[0]) + ');' +
     87        Result := Result + IndentStr + 'WriteLn(' + GenerateRef(Parameters[0]) + ');' +
    8388          LineEnding;
    8489      end else
    8590      if Name = 'assign' then begin
    86         Output := Output + '  ' + GenerateRef(Parameters[0]) + ' := ' +
     91        Result := Result + IndentStr + GenerateRef(Parameters[0]) + ' := ' +
    8792          GenerateRef(Parameters[1]) + ';' + LineEnding;
    8893      end else
    8994      if Name = 'inputln' then begin
    90         Output := Output + '  ReadLn(' + GenerateRef(Parameters[0]) + ');' + LineEnding;
     95        Result := Result + IndentStr + 'ReadLn(' + GenerateRef(Parameters[0]) + ');' + LineEnding;
    9196      end else
    9297      if Name = 'increment' then begin
    93         Output := Output + '  Inc(' + GenerateRef(Parameters[0]) + ', ' +
     98        Result := Result + IndentStr + 'Inc(' + GenerateRef(Parameters[0]) + ', ' +
    9499          GenerateRef(Parameters[1]) + ');' + LineEnding;
    95100      end else
     
    97102    end else
    98103    if Instruction is TSourceBeginEnd then begin
    99       Output := Output + GenerateBeginEnd(TSourceBeginEnd(Instruction)) +
     104      Result := Result + GenerateBeginEnd(TSourceBeginEnd(Instruction)) +
    100105        ';' + LineEnding;
    101106    end else
    102107    raise Exception.Create('Unsupported instruction');
    103108  end;
    104   Output := Output + 'end';
    105   Result := Output;
     109  Dec(Indent);
     110  Result := Result + IndentStr + 'end';
     111end;
     112
     113function TSourceGenerator.IndentStr: string;
     114begin
     115  SetLength(Result, Indent * 2);
     116  if Indent > 0 then
     117    FillChar(Result[1], Indent * 2, ' ');
    106118end;
    107119
Note: See TracChangeset for help on using the changeset viewer.