Changeset 5 for trunk/Compiler/Analyze


Ignore:
Timestamp:
Nov 5, 2010, 9:36:20 AM (14 years ago)
Author:
george
Message:
  • Fixed: Variable and constant expression.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/Analyze/UPascalParser.pas

    r4 r5  
    137137function TPascalParser.ParseExpression(SourceCode: TExpression): Boolean;
    138138var
    139   NewVariable: TVariable;
    140   NewExpression: TExpression;
    141   NewMethod: TFunction;
    142   Constant: TConstant;
    143   UseType: TType;
    144   //  Brackets: Integer;
    145139  Expressions: TListExpression;
    146   I: integer;
    147   II: integer;
     140  I: Integer;
     141  II: Integer;
    148142begin
    149143  try
    150144    Expressions := TListExpression.Create;
     145    Expressions.OwnsObjects := False;
    151146    Expressions.Add(TExpression.Create);
    152147    with SourceCode do begin
     
    155150        if not ParseExpressionParenthases(SourceCode, Expressions) then
    156151        if not ParseExpressionOperator(SourceCode, Expressions) then
    157         if not ParseExpressionRightValue(SourceCode, Expressions) then begin
     152        if not ParseExpressionRightValue(SourceCode, Expressions) then
     153        begin
    158154          ErrorMessage(SInvalidAssignmentValue, [NextToken]);
    159155          ReadToken;
     
    171167            Expressions[I - 1].SubItems.Last := Expressions[I];
    172168            Expressions[I + 1].SubItems.First := Expressions[I];
    173             //Expressions.Delete(I);
     169            Expressions.Delete(I);
    174170          end else Inc(I);
    175171        end;
     
    196192    // Subexpression
    197193    NewExpression := TExpression.Create;
     194    NewExpression.Braces := True;
    198195    NewExpression.CommonBlock := SourceCode.CommonBlock;
    199196    ParseExpression(NewExpression);
     
    230227  NewExpression: TExpression;
    231228  Identifier: string;
    232   O: TObject;
    233 begin
    234   O := nil;
    235   with SourceCode do
    236   if IsIdentificator(NextToken) then begin
     229  IntConst: Integer;
     230begin
     231  NewExpression := nil;
     232  with SourceCode do begin
     233//  if IsIdentificator(NextToken) then begin
    237234    // Start with type
    238235    UseType := CommonBlock.Types.Search(NextToken);
     
    240237      ReadToken;
    241238      if (UseType is TTypeRecord) or (UseType is TTypeClass) then begin
     239        // type.variable or type.function
    242240        Expect('.');
    243241        Identifier := ReadToken;
    244242        UseVariable := TTypeRecord(UseType).CommonBlock.Variables.Search(Identifier);
    245243        if Assigned(UseVariable) then begin
    246           O := UseVariable;
    247         end;
    248         if not Assigned(O) then begin
     244          NewExpression := TExpression.Create;
     245          NewExpression.CommonBlock := SourceCode.CommonBlock;
     246          NewExpression.NodeType := ntVariable;
     247          NewExpression.Variable := UseVariable;
     248        end;
     249        if not Assigned(NewExpression) then begin
    249250          UseFunction := TTypeRecord(UseType).CommonBlock.Functions.Search(Identifier);
    250251          if Assigned(UseFunction) then begin
    251             O := UseFunction;
     252            NewExpression := TExpression.Create;
     253            NewExpression.CommonBlock := SourceCode.CommonBlock;
     254            NewExpression.NodeType := ntFunction;
     255            NewExpression.FunctionCall := UseFunction;
    252256          end;
    253257        end;
    254         if not Assigned(O) then
     258        if not Assigned(NewExpression) then
    255259          ErrorMessage(SUndefinedVariable, [Identifier]);
    256260      end else ErrorMessage(SIllegalExpression, [Identifier]);
    257261    end;
    258     if not Assigned(O) then begin
    259       UseVariable := CommonBlock.Variables.Search(Identifier);
     262    if not Assigned(NewExpression) then begin
     263      // Referenced variable
     264      UseVariable := CommonBlock.Variables.Search(NextToken);
    260265      if Assigned(UseVariable) then begin
    261         // Referenced variable
    262266        ReadToken;
    263267        NewExpression := TExpression.Create;
    264268        NewExpression.CommonBlock := SourceCode.CommonBlock;
    265269        NewExpression.NodeType := ntVariable;
    266         NewExpression.Variable := TVariable(UseVariable);
    267         SubItems.Last := NewExpression;
    268       end;
    269     end;
    270     if not Assigned(O) then begin
    271       ParseExpressionFunctionCall(SourceCode, Expressions, TFunction(O));
    272       NewExpression := TExpression.Create;
    273       NewExpression.CommonBlock := SourceCode.CommonBlock;
    274       NewExpression.NodeType := ntFunction;
    275       NewExpression.FunctionCall := TFunction(O);
    276       SubItems.Last := NewExpression;
    277     end;
    278     if not Assigned(O) then begin
     270        NewExpression.Variable := UseVariable;
     271      end;
     272    end;
     273    if not Assigned(NewExpression) then begin
     274      // Function call
     275      ParseExpressionFunctionCall(SourceCode, Expressions, UseFunction);
     276      if Assigned(UseFunction) then begin
     277        NewExpression := TExpression.Create;
     278        NewExpression.CommonBlock := SourceCode.CommonBlock;
     279        NewExpression.NodeType := ntFunction;
     280        NewExpression.FunctionCall := UseFunction;
     281      end;
     282    end;
     283    if not Assigned(NewExpression) then begin
     284      // Referenced constant
    279285      UseConstant := CommonBlock.Constants.Search(NextToken);
    280286      if Assigned(UseConstant) then begin
    281287        ReadToken;
    282         O := UseConstant;
    283288        NewExpression := TExpression.Create;
    284289        NewExpression.CommonBlock := SourceCode.CommonBlock;
    285290        NewExpression.NodeType := ntConstant;
    286         NewExpression.Constant := TConstant(O);
    287         SubItems.Last := NewExpression;
    288       end;
    289     end;
    290     if not Assigned(O) then begin
     291        NewExpression.Constant := UseConstant;
     292      end;
     293    end;
     294    if not Assigned(NewExpression) then begin
    291295      // Constant value
    292       O := TConstant.Create;
    293       TConstant(O).Value := ReadToken;
    294296      NewExpression := TExpression.Create;
    295297      NewExpression.CommonBlock := SourceCode.CommonBlock;
    296       NewExpression.NodeType := ntConstant;
    297       NewExpression.Constant := TConstant(O);
    298       SubItems.Last := NewExpression;
    299     end;
    300     if not Assigned(O) then begin
     298      NewExpression.NodeType := ntValue;
     299      NewExpression.Constant := TConstant.Create;
     300      NewExpression.Value := ReadToken;
     301      if TryStrToInt(NewExpression.Value, IntConst) then
     302        NewExpression.Value := IntConst;
     303    end;
     304    if Assigned(NewExpression) then begin
     305      Expressions.Last.SubItems.Last := NewExpression;
     306      with Expressions.Items[Expressions.Add(TExpression.Create)] do
     307      begin
     308        CommonBlock := SourceCode.CommonBlock;
     309        SubItems.First := NewExpression;
     310      end;
     311      Result := True;
     312    end else begin
     313      Result := False;
    301314      ErrorMessage(SUnknownIdentifier, [ReadToken]);
    302315    end;
    303 
    304     with Expressions.Items[Expressions.Add(TExpression.Create)] do
    305     begin
    306       CommonBlock := SourceCode.CommonBlock;
    307       SubItems.First := NewExpression;
    308     end;
    309     Result := True;
    310   end else Result := False;
     316//  end else Result := False;
     317  end;
    311318end;
    312319
Note: See TracChangeset for help on using the changeset viewer.