Ignore:
Timestamp:
Oct 29, 2009, 11:07:49 AM (15 years ago)
Author:
george
Message:
  • Přidáno: Deklarace proměnných a volání procedur.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Syntetizer/USyntetizer.pas

    r26 r27  
    1111  TSyntetizer = class;
    1212
    13   TElement = class
     13  ISyntetizable = interface
     14    procedure Syntetize(Syntetizer: TSyntetizer);
     15  end;
     16
     17  TElement = class(TInterfacedObject)
     18  end;
     19
     20  TCompilerDirective = class(TElement)
     21    Variable: string;
     22    Value: string;
     23    procedure Syntetize(Syntetizer: TSyntetizer);
     24  end;
     25
     26  TCommand = class(TInterfacedObject, ISyntetizable)
    1427    procedure Syntetize(Syntetizer: TSyntetizer); virtual;
    1528  end;
    1629
    17   TCommand = class(TElement)
    18   end;
    19 
    20   { TBeginEnd }
     30  TProcedureParameter = class(TElement)
     31    Value: string;
     32  end;
     33
     34  { TProcedureCall }
     35
     36  TProcedureCall = class(TCommand)
     37    Name: string;
     38    Parameters: TList; // TList<TProcedureParameter>
     39    constructor Create;
     40    destructor Destroy; override;
     41    procedure Syntetize(Syntetizer: TSyntetizer); override;
     42  end;
    2143
    2244  TBeginEnd = class(TCommand)
    23     Commands: TList; // TList<TCommand>
     45    Commands: TList; // TList<ICommand>
     46    LastEnd: Boolean;
    2447    procedure Syntetize(Syntetizer: TSyntetizer); override;
    2548    constructor Create;
     
    2750  end;
    2851
    29   { TProgram }
    30 
    31   TProgram = class(TElement)
     52  TVariableDeclaration = class(TInterfacedObject, ISyntetizable)
     53    Name: string;
     54    VarType: string;
     55    procedure Syntetize(Syntetizer: TSyntetizer);
     56  end;
     57
     58  { TVariableSection }
     59
     60  TVariableSection = class(TInterfacedObject, ISyntetizable)
     61    Variables: TList; // TList<TVariableDeclaration>
     62    procedure Syntetize(Syntetizer: TSyntetizer);
     63    constructor Create;
     64    destructor Destroy; override;
     65  end;
     66
     67  TProgram = class(TInterfacedObject, ISyntetizable)
    3268    Name: string;
    3369    Code: TBeginend;
    34     procedure Syntetize(Syntetizer: TSyntetizer); override;
    35     constructor Create;
    36     destructor Destroy; override;
    37   end;
     70    CompilerDirectives: TList; // TList<TCompilerDirective>
     71    Variables: TVariableSection;
     72    procedure Syntetize(Syntetizer: TSyntetizer);
     73    constructor Create;
     74    destructor Destroy; override;
     75  end;
     76
     77  { TSyntetizer }
    3878
    3979  TSyntetizer = class
    40     BaseElement: TElement;
     80  private
     81    procedure AddLine(Line: string);
     82  public
     83    BaseElement: TProgram;
    4184    Output: TStringList;
     85    Indent: Integer;
    4286    procedure Syntetize;
    4387    constructor Create;
     
    4892
    4993{ TSyntetizer }
     94
     95procedure TSyntetizer.AddLine(Line: string);
     96var
     97  I: Integer;
     98begin
     99  for I := 1 to Indent * 2 do
     100    Line := ' ' + Line;
     101  Output.Add(Line);
     102end;
    50103
    51104procedure TSyntetizer.Syntetize;
     
    66119end;
    67120
    68 { TElement }
    69 
    70 procedure TElement.Syntetize(Syntetizer: TSyntetizer);
    71 begin
    72 
    73 end;
    74 
    75121{ TProgram }
    76122
    77123procedure TProgram.Syntetize(Syntetizer: TSyntetizer);
    78 begin
    79   Syntetizer.Output.Add('program ' + Name + ';');
     124var
     125  I: Integer;
     126begin
     127  Syntetizer.AddLine('program ' + Name + ';');
     128  for I := 0 to CompilerDirectives.Count - 1 do
     129    TCompilerDirective(CompilerDirectives[I]).Syntetize(Syntetizer);
     130  Variables.Syntetize(Syntetizer);
    80131  Code.Syntetize(Syntetizer);
    81132  inherited;
     
    84135constructor TProgram.Create;
    85136begin
     137  CompilerDirectives := TList.Create;
     138  Variables := TVariableSection.Create;
    86139  Code := TBeginEnd.Create;
     140  Code.LastEnd := True;
    87141end;
    88142
    89143destructor TProgram.Destroy;
    90 begin
     144var
     145  I: Integer;
     146begin
     147  Variables.Destroy;
     148  for I := 0 to CompilerDirectives.Count - 1 do
     149    TCompilerDirective(CompilerDirectives[I]).Destroy;
    91150  Code.Destroy;
    92151  inherited Destroy;
     
    99158  I: Integer;
    100159begin
    101   with Syntetizer.Output do begin
    102     Add('begin');
     160  with Syntetizer do begin
     161    AddLine('begin');
     162    Inc(Syntetizer.Indent);
    103163    for I := 0 to Commands.Count - 1 do
    104164      TCommand(Commands[I]).Syntetize(Syntetizer);
    105     Add('end;');
     165    Dec(Syntetizer.Indent);
     166    if LastEnd then AddLine('end.') else AddLine('end;');
    106167  end;
    107168  inherited Syntetize(Syntetizer);
     
    123184end;
    124185
     186{ TProcedureCall }
     187
     188constructor TProcedureCall.Create;
     189begin
     190  Parameters := TList.Create;
     191end;
     192
     193destructor TProcedureCall.Destroy;
     194var
     195  I: Integer;
     196begin
     197  for I := 0 to Parameters.Count - 1 do
     198    TProcedureParameter(Parameters[I]).Destroy;
     199  Parameters.Destroy;
     200  inherited Destroy;
     201end;
     202
     203procedure TProcedureCall.Syntetize(Syntetizer: TSyntetizer);
     204var
     205  ParameterString: string;
     206  I: Integer;
     207begin
     208  for I := 0 to Parameters.Count - 1 do
     209    ParameterString := ParameterString + TProcedureParameter(Parameters[I]).Value;
     210  if Parameters.Count > 0 then
     211    ParameterString := '(' + ParameterString + ')';
     212  Syntetizer.AddLine(Name + ParameterString + ';');
     213  inherited Syntetize(Syntetizer);
     214end;
     215
     216{ TCompilerDirective }
     217
     218procedure TCompilerDirective.Syntetize(Syntetizer: TSyntetizer);
     219begin
     220  Syntetizer.AddLine('{$' + Variable + ' ' + Value + '}');
     221end;
     222
     223{ TVariableDeclaration }
     224
     225procedure TVariableDeclaration.Syntetize(Syntetizer: TSyntetizer);
     226begin
     227  Syntetizer.AddLine(Name + ': ' + VarType + ';');
     228  inherited;
     229end;
     230
     231{ TCommand }
     232
     233procedure TCommand.Syntetize(Syntetizer: TSyntetizer);
     234begin
     235end;
     236
     237{ TVariableSection }
     238
     239procedure TVariableSection.Syntetize(Syntetizer: TSyntetizer);
     240var
     241  I: Integer;
     242  SectionString: string;
     243begin
     244  Syntetizer.AddLine('var');
     245  Inc(Syntetizer.Indent);
     246  for I := 0 to Variables.Count - 1 do
     247    TVariableDeclaration(Variables[I]).Syntetize(Syntetizer);
     248  Dec(Syntetizer.Indent);
     249end;
     250
     251constructor TVariableSection.Create;
     252begin
     253  Variables := TList.Create;
     254end;
     255
     256destructor TVariableSection.Destroy;
     257var
     258  I: Integer;
     259begin
     260  for I := 0 to Variables.Count - 1 do
     261    TVariableDeclaration(Variables[I]).Destroy;
     262  Variables.Destroy;
     263  inherited Destroy;
     264end;
     265
    125266end.
    126267
Note: See TracChangeset for help on using the changeset viewer.