Ignore:
Timestamp:
Nov 9, 2009, 4:15:39 PM (15 years ago)
Author:
george
Message:
  • Přidáno: Parsování pascalovského bloku var a begin-end.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/UCompilator.pas

    r10 r11  
    1717    FOnError: TOnErrorEvent;
    1818    procedure DoError(Text: string);
     19    procedure ParseBeginEnd;
     20    procedure ParseProgram;
     21    procedure ParseVariableDefinition;
    1922  public
    2023    Model: TModel;
     
    2326    Parser: TVoidParser;
    2427    procedure Compile;
    25     procedure Process;
    2628    constructor Create;
    2729    destructor Destroy; override;
     
    4244end;
    4345
     46procedure TCompilator.ParseProgram;
     47begin
     48  while SourceCode.Position < SourceCode.Size do
     49  with Model, Module, BeginEnd do begin
     50    if Parser.TokenType = ttWhiteSpace then begin
     51    end else
     52    if Parser.TokenType = ttIdentifier then begin
     53      if Parser.TokenValue = 'program' then begin
     54        Parser.ParseNextToken;
     55        if Parser.TokenType <> ttWhiteSpace then DoError('Expect white space');
     56        Parser.ParseNextToken;
     57        if Parser.TokenType <> ttString then DoError('Expect string');
     58        Parser.ParseNextToken;
     59        if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue <> ';') then DoError('Expect ;');
     60        Parser.ParseNextToken;
     61      end else
     62      if Parser.TokenValue = 'var' then begin
     63        ParseVariableDefinition;
     64      end else
     65      if Parser.TokenValue = 'begin' then begin
     66        ParseBeginEnd;
     67      end;
     68    end;
     69  end;
     70end;
     71
     72procedure TCompilator.ParseVariableDefinition;
     73var
     74  VariableName: string;
     75  VariableType: string;
     76  Variable: TVariable;
     77begin
     78  repeat
     79    Parser.ParseNextToken;
     80    if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');
     81    Parser.ParseNextToken;
     82    if Parser.TokenType <> ttIdentifier then DoError('Expected identifier');
     83    VariableName := Parser.TokenValue;
     84    Parser.ParseNextToken;
     85    if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;
     86    if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ':') then
     87      DoError('Expected :');
     88    Parser.ParseNextToken;
     89    if Parser.TokenType = ttWhiteSpace then Parser.ParseNextToken;
     90    if Parser.TokenType <> ttIdentifier then DoError('Expected identifier');
     91    VariableType := Parser.TokenValue;
     92
     93    with Model.Module do begin
     94    Variable := FindVariableByName(VariableName);
     95    if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined')
     96    else begin
     97      Variable := TVariable.Create;
     98      with Variable do begin
     99        Name := VariableName;
     100        VarType := VariableType;
     101      end;
     102      Variables.Add(Variable);
     103    end;
     104    end;
     105
     106    Parser.ParseNextToken;
     107    if (Parser.TokenType <> ttSymbol) and (Parser.TokenValue = ';') then
     108      DoError('Expected ;');
     109    Parser.ParseNextToken;
     110    if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');
     111    Parser.ParseNextToken;
     112  until (Parser.TokenType = ttIdentifier) and ((Parser.TokenValue = 'begin') or
     113  (Parser.TokenValue = 'type'));
     114end;
     115
    44116procedure TCompilator.Compile;
    45117var
     
    49121  SourceCode.Position := 0;
    50122  Parser.Open(SourceCode);
    51 
    52   // Process source lines
    53   while SourceCode.Position < SourceCode.Size do begin;
    54     Process;
    55     Parser.ParseNextToken;
    56   end;
    57 
     123  ParseProgram;
    58124  Generator.Generate(Model);
    59125end;
    60126
    61 procedure TCompilator.Process;
     127procedure TCompilator.ParseBeginEnd;
    62128var
    63129  CommandName: string;
     
    67133  Value: string;
    68134begin
    69   with Model, BeginEnd do begin
     135  with Model, Module, BeginEnd do begin
     136    if Parser.TokenType = ttWhiteSpace then begin
     137    end else
    70138    if Parser.TokenType = ttIdentifier then begin
    71139      CommandName := Parser.TokenValue;
    72       if CommandName = 'Define' then begin
    73         Parser.ParseNextToken;
    74         if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');
    75         Parser.ParseNextToken;
    76         if Parser.TokenType <> ttIdentifier then DoError('Expected identifier');
    77         VariableName := Parser.TokenValue;
    78         Variable := FindVariableByName(VariableName);
    79         if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined')
    80         else begin
    81           Variable := TVariable.Create;
    82           with Variable do begin
    83             Name := VariableName;
    84             VarType := 'string';
    85           end;
    86           Variables.Add(Variable);
    87         end;
    88       end else begin
     140      begin
    89141        Command := FindProcedureByName(CommandName);
    90142        if Assigned(Command) then begin
     
    131183        end;
    132184      end;
     185      Parser.ParseNextToken;
     186      if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');
    133187    end else DoError('Expected identifier');
    134     Parser.ParseNextToken;
    135     if Parser.TokenType <> ttWhiteSpace then DoError('Expected white space');
    136188  end;
    137189end;
Note: See TracChangeset for help on using the changeset viewer.