Changeset 5 for trunk/Compiler


Ignore:
Timestamp:
Nov 5, 2010, 9:36:20 AM (14 years ago)
Author:
george
Message:
  • Fixed: Variable and constant expression.
Location:
trunk/Compiler
Files:
4 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
  • trunk/Compiler/Produce/UProducerDynamicC.pas

    r2 r5  
    274274function TProducerDynamicC.GenerateExpression(Expression: TExpression): string;
    275275begin
    276   case Expression.NodeType of
    277     ntConstant: begin
    278       if VarType(Expression.Value) = varString then
    279         Result := '"' + Expression.Value + '"'
    280         else Result := Expression.Value;
    281     end;
    282     ntVariable: Result := Expression.Variable.Name;
    283     ntFunction: Result := Expression.FunctionCall.Name;
    284     ntOperator: begin
    285       Result := GenerateExpression(TExpression(Expression.SubItems.First))
    286         + ' ' + TranslateOperator(Expression.OperatorName) + ' ' +
    287         GenerateExpression(TExpression(Expression.SubItems.Last));
    288     end;
    289     ntNone: ;
     276  if Assigned(Expression) then begin
     277    case Expression.NodeType of
     278      ntConstant: begin
     279        Result := Expression.Constant.Name;
     280      end;
     281      ntValue: begin
     282       if VarType(Expression.Value) = varString then
     283          Result := '"' + Expression.Value + '"'
     284          else Result := Expression.Value;
     285      end;
     286      ntVariable: Result := Expression.Variable.Name;
     287      ntFunction: Result := Expression.FunctionCall.Name;
     288      ntOperator: begin
     289        Result := GenerateExpression(TExpression(Expression.SubItems.First))
     290          + ' ' + TranslateOperator(Expression.OperatorName) + ' ' +
     291          GenerateExpression(TExpression(Expression.SubItems.Last));
     292      end;
     293      ntNone: ;
     294    end;
     295    if Expression.Braces then Result := '(' + Result + ')';
    290296  end;
    291297end;
  • trunk/Compiler/Produce/UProducerPascal.pas

    r2 r5  
    309309function TProducerPascal.GenerateExpression(Expression: TExpression): string;
    310310begin
    311   case Expression.NodeType of
    312     ntConstant: Result := Expression.Value;
    313     ntVariable: Result := Expression.Variable.Name;
    314     ntFunction: Result := Expression.FunctionCall.Name;
    315     ntOperator: begin
    316       Result := GenerateExpression(TExpression(Expression.SubItems.First))
    317         + ' ' + Expression.OperatorName + ' ' +
    318         GenerateExpression(TExpression(Expression.SubItems.Last));
    319     end;
    320     ntNone: ;
     311  if Assigned(Expression) then begin
     312    case Expression.NodeType of
     313      ntConstant: begin
     314        Result := Expression.Constant.Name;
     315      end;
     316      ntValue: begin
     317       if VarType(Expression.Value) = varString then
     318          Result := '''' + Expression.Value + ''''
     319          else Result := Expression.Value;
     320      end;
     321      ntVariable: Result := Expression.Variable.Name;
     322      ntFunction: Result := Expression.FunctionCall.Name;
     323      ntOperator: begin
     324        Result := GenerateExpression(TExpression(Expression.SubItems.First))
     325          + ' ' + Expression.OperatorName + ' ' +
     326          GenerateExpression(TExpression(Expression.SubItems.Last));
     327      end;
     328      ntNone: ;
     329    end;
     330    if Expression.Braces then Result := '(' + Result + ')';
    321331  end;
    322332end;
  • trunk/Compiler/USourceCode.pas

    r4 r5  
    1414  TModuleType = (mdProgram, mdUnit, mdLibrary, mdPackage);
    1515
    16   TNodeType = (ntNone, ntVariable, ntFunction, ntConstant, ntOperator);
     16  TNodeType = (ntNone, ntVariable, ntFunction, ntConstant, ntOperator,
     17    ntValue);
    1718
    1819  TTypeVisibility = (tvPublic, tvPublished, tvPrivate, tvProtected);
     
    333334    SubItems: TListExpression;
    334335    Associated: Boolean;
     336    Braces: Boolean;
    335337    constructor Create;
    336338    destructor Destroy; override;
Note: See TracChangeset for help on using the changeset viewer.