Ignore:
Timestamp:
Nov 10, 2009, 4:15:47 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Vylepšeny třídy pro analýzu gramatiky.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/UCompilator.pas

    r18 r19  
    66
    77uses
    8   Classes, SysUtils, UOutputGenerator, UModel, UVoidParser, fgl;
     8  Classes, SysUtils, UOutputGenerator, UModel, UTokenizer, fgl, UGrammer;
    99
    1010type
     
    2323  private
    2424    FOnError: TOnErrorEvent;
     25    Grammer: TGrammer;
    2526    procedure DoError(AText: string);
    2627    procedure ParseBeginEnd;
     
    2930    procedure ParseTypeDefinition;
    3031    procedure ParseExpression;
     32    procedure InitGrammer;
    3133  public
    3234    ErrorMessages: TErrorMessageList;
     
    4951  Terminate: Boolean;
    5052begin
    51   with ErrorMessages.Add do begin
     53  with ErrorMessages[ErrorMessages.Add(TErrorMessage.Create)] do begin
    5254    CodePosition := Parser.TokenStartPosition;
    5355    Text := AText;
     
    147149end;
    148150
     151procedure TCompilator.InitGrammer;
     152var
     153  RuleProgram, RuleBlock, RuleDeclaration, RuleStatement, RuleConstant,
     154  RuleVariable, RuleFunction: TGrammerRule;
     155begin
     156  Grammer := TGrammer.Create;
     157  with Grammer do begin
     158    RuleConstantDefinition := TGrammerRule.Create;
     159    with RuleConstantDefinition do begin
     160      Name := 'ConstantDefinition';
     161      RuleType := rtSequence;
     162    end;
     163    Rules.Add(RuleConstantDefinition);
     164
     165    RuleConstant := TGrammerRule.Create;
     166    with RuleConstant do begin
     167      Name := 'Constant';
     168      RuleType := rtSequence;
     169      AddTerminal('const', ttIdentifier, False, False);
     170      AddRule(RuleConstantDefinition, False, False);
     171    end;
     172    Rules.Add(RuleConstant);
     173
     174    RuleFunction := TGrammerRule.Create;
     175    with RuleFunction do begin
     176      Name := 'Function';
     177      RuleType := rtSequence;
     178    end;
     179    Rules.Add(RuleFunction);
     180
     181    RuleVariable := TGrammerRule.Create;
     182    with RuleVariable do begin
     183      Name := 'Variable';
     184      RuleType := rtSequence;
     185    end;
     186    Rules.Add(RuleVariable);
     187
     188    RuleStatement := TGrammerRule.Create;
     189    with RuleStatement do begin
     190      Name := 'Statement';
     191      RuleType := rtSequence;
     192    end;
     193    Rules.Add(RuleStatement);
     194
     195    RuleDeclaration := TGrammerRule.Create;
     196    with RuleDeclaration do begin
     197      Name := 'Declaration';
     198      RuleType := rtAlternative;
     199      AddRule(RuleFunction, False, False);
     200      AddRule(RuleConstant, False, False);
     201      AddRule(RuleVariable, False, False);
     202    end;
     203    Rules.Add(RuleDeclaration);
     204
     205    RuleBlock := TGrammerRule.Create;
     206    with RuleBlock do begin
     207      Name := 'Block';
     208      RuleType := rtSequence;
     209      AddRule(RuleDeclaration, False, True);
     210      AddRule(RuleStatement, False, False);
     211    end;
     212    Rules.Add(RuleBlock);
     213
     214    RuleProgram := TGrammerRule.Create;
     215    with RuleProgram do begin
     216      Name := 'Program';
     217      RuleType := rtSequence;
     218      AddRule(RuleBlock, False, False);
     219      AddTerminal('.', ttSymbol, False, False);
     220    end;
     221    Rules.Add(RuleProgram);
     222
     223    TopRule := RuleProgram;
     224  end;
     225end;
     226
    149227procedure TCompilator.Compile;
    150228var
     
    167245  Variable: TVariable;
    168246  SourceVariable: TVariable;
    169   VariableName: string;
    170247  Value: string;
    171248  P: Integer;
     
    282359constructor TCompilator.Create;
    283360begin
     361  Grammer := TGrammer.Create;
     362  InitGrammer;
    284363  ErrorMessages := TErrorMessageList.Create;
    285364  SourceCode := TMemoryStream.Create;
     
    290369destructor TCompilator.Destroy;
    291370begin
     371  Grammer.Destroy;
    292372  ErrorMessages.Destroy;
    293373  SourceCode.Destroy;
Note: See TracChangeset for help on using the changeset viewer.