Ignore:
Timestamp:
Feb 2, 2017, 11:35:09 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Improved parsin and prepared analyzed source execution.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/interpreter/Parser3.pas

    r96 r97  
    2424
    2525function ParseIfThen(IfThenElse: PIfThenElse): Boolean; forward;
    26 function ParseExecution: Boolean; forward;
     26function ParseWhileDo(WhileDo: PWhileDo): Boolean; forward;
     27function ParseExecution(Execution: PExecution): Boolean; forward;
     28function ParseBeginEnd(BeginEnd: PBeginEnd): Boolean; forward;
    2729
    2830
     
    3537begin
    3638  Result := (C = ';') or (C = '(') or (C = ')') or (C = ':') or (C = '=') or
    37     (C = '+') or (C = '-');
     39    (C = '+') or (C = '-') or (C = ';') or (C = '.');
    3840end;
    3941
     
    142144function IsOperator(Text: string): Boolean;
    143145begin
    144   Result := (Text = '=') or (Text = '<>') or (Text = '>') or (Text = '<') or
     146  Result := (Text = '+') or (Text = '-') or
     147    (Text = '=') or (Text = '<>') or (Text = '>') or (Text = '<') or
    145148    (Text = '<=') or (Text = '>=');
    146149end;
     
    169172  Variable: PVariable;
    170173  SubExpression: TExpression;
     174  Execution: TExecution;
    171175begin
    172176  Result := True;
     177  if CheckNext('not') then begin
     178    Expect('not');
     179  end;
    173180  if CheckNext('(') then begin
    174181    Expect('(');
     
    177184  end else
    178185  if ParseVariable(Variable) then begin
     186    OldPos := InputTextPos;
    179187    Next := ReadNext;
    180188    if IsOperator(Next) then begin
     
    183191
    184192      //end else ShowError('Expected variable');
    185     end else ShowError('Exprected operator but found ' + ReadNext);
     193    end else InputTextPos := OldPos;
    186194  end else
    187195  if LastTokenType = ttString then begin
    188 
    189   end else
    190   if ParseExecution then begin
     196    ReadNext
     197  end else
     198  if ParseExecution(@Execution) then begin
    191199  end else
    192200  ShowError('Expected variable but found ' + ReadNext);
     
    194202  OldPos := InputTextPos;
    195203  Next := ReadNext;
    196   if IsLogicOperator(Next) then begin
     204  if IsLogicOperator(Next) or IsOperator(Next) then begin
    197205    R := ParseExpression(@SubExpression);
    198206  end else InputTextPos := OldPos;
     
    214222end;
    215223
    216 function ParseExecution: Boolean;
     224function ParseExecution(Execution: PExecution): Boolean;
    217225var
    218226  OldPos: Integer;
     
    220228  Func: PFunction;
    221229  I: Integer;
     230  Expression: TExpression;
    222231begin
    223232  Result := True;
     
    230239      I := 0;
    231240      while I < Length(Func^.Parameters.Items) do begin
    232         Next := ReadNext;
     241        ParseExpression(@Expression);
    233242        if I < (Length(Func^.Parameters.Items) - 1) then Expect(',');
    234243        I := I + 1;
     
    245254var
    246255  IfThenElse: TIfThenElse;
     256  WhileDo: TWhileDo;
     257  BeginEnd: TBeginEnd;
     258  Execution: TExecution;
    247259begin
    248260  Result := True;
     261  if ParseBeginEnd(@BeginEnd) then begin
     262  end else
    249263  if ParseAssignment then begin
    250264  end else
    251   if ParseExecution then begin
    252 
     265  if ParseExecution(@Execution) then begin
    253266  end else
    254267  if ParseIfThen(@IfThenElse) then begin
    255 
     268  end else
     269  if ParseWhileDo(@WhileDo) then begin
    256270  end else Result := False;
    257271end;
     
    269283      ParseCommand;
    270284    end;
     285  end else Result := False;
     286end;
     287
     288function ParseWhileDo(WhileDo: PWhileDo): Boolean;
     289begin
     290  if CheckNext('while') then begin
     291    Result := True;
     292    Expect('while');
     293    ParseExpression(@WhileDo.Condition);
     294    Expect('do');
     295    ParseCommand;
    271296  end else Result := False;
    272297end;
     
    364389    ParseParams(@Func^.Parameters);
    365390    Expect(';');
     391    if ParseVariableSection then begin
     392    end;
    366393    ParseBeginEnd(@Func^.BeginEnd);
    367394    Expect(';');
     
    396423  ProgramCode^.Variables.Add(VariableCreate('Result', TypeString));
    397424  ProgramCode^.Variables.Add(VariableCreate('C', TypeChar));
     425  ProgramCode^.Variables.Add(VariableCreate('Text', TypeString));
    398426
    399427  SetLength(ProgramCode^.Functions.Items, 0);
Note: See TracChangeset for help on using the changeset viewer.