Browse Source

passrc: initial program parsing. Fixed PasExpr constructor warnings. Added initial labels parsing

git-svn-id: trunk@15902 -
dmitry 15 years ago
parent
commit
1530872629
2 changed files with 79 additions and 11 deletions
  1. 38 10
      packages/fcl-passrc/src/pastree.pp
  2. 41 1
      packages/fcl-passrc/src/pparser.pp

+ 38 - 10
packages/fcl-passrc/src/pastree.pp

@@ -138,12 +138,12 @@ type
   TPasExpr = class(TPasElement)
   TPasExpr = class(TPasElement)
     Kind      : TPasExprKind;
     Kind      : TPasExprKind;
     OpCode    : TexprOpcode;
     OpCode    : TexprOpcode;
-    constructor Create(AParent : TPasElement; AKind: TPasExprKind; AOpCode: TexprOpcode);
+    constructor Create(AParent : TPasElement; AKind: TPasExprKind; AOpCode: TexprOpcode); virtual; overload;
   end;
   end;
 
 
   TUnaryExpr = class(TPasExpr)
   TUnaryExpr = class(TPasExpr)
     Operand   : TPasExpr;
     Operand   : TPasExpr;
-    constructor Create(AParent : TPasElement; AOperand: TPasExpr; AOpCode: TExprOpCode);
+    constructor Create(AParent : TPasElement; AOperand: TPasExpr; AOpCode: TExprOpCode); overload;
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
     destructor Destroy; override;
     destructor Destroy; override;
   end;
   end;
@@ -153,28 +153,28 @@ type
   TBinaryExpr = class(TPasExpr)
   TBinaryExpr = class(TPasExpr)
     left      : TPasExpr;
     left      : TPasExpr;
     right     : TPasExpr;
     right     : TPasExpr;
-    constructor Create(AParent : TPasElement; xleft, xright: TPasExpr; AOpCode: TExprOpCode);
-    constructor CreateRange(AParent : TPasElement; xleft, xright: TPasExpr);
+    constructor Create(AParent : TPasElement; xleft, xright: TPasExpr; AOpCode: TExprOpCode); overload;
+    constructor CreateRange(AParent : TPasElement; xleft, xright: TPasExpr); overload;
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
     destructor Destroy; override;
     destructor Destroy; override;
   end;
   end;
 
 
   TPrimitiveExpr = class(TPasExpr)
   TPrimitiveExpr = class(TPasExpr)
     Value     : AnsiString;
     Value     : AnsiString;
-    constructor Create(AParent : TPasElement; AKind: TPasExprKind; const AValue : Ansistring);
+    constructor Create(AParent : TPasElement; AKind: TPasExprKind; const AValue : Ansistring); overload;
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
   end;
   end;
   
   
   TBoolConstExpr = class(TPasExpr)
   TBoolConstExpr = class(TPasExpr)
     Value     : Boolean;
     Value     : Boolean;
-    constructor Create(AParent : TPasElement; AKind: TPasExprKind; const ABoolValue : Boolean);
+    constructor Create(AParent : TPasElement; AKind: TPasExprKind; const ABoolValue : Boolean); overload;
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
   end;
   end;
 
 
   { TNilExpr }
   { TNilExpr }
 
 
   TNilExpr = class(TPasExpr)
   TNilExpr = class(TPasExpr)
-    constructor Create(AParent : TPasElement);
+    constructor Create(AParent : TPasElement); overload;
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
   end;
   end;
 
 
@@ -184,7 +184,7 @@ type
     Value     : TPasExpr;
     Value     : TPasExpr;
     Params    : array of TPasExpr;
     Params    : array of TPasExpr;
     {pekArray, pekFuncCall, pekSet}
     {pekArray, pekFuncCall, pekSet}
-    constructor Create(AParent : TPasElement; AKind: TPasExprKind);
+    constructor Create(AParent : TPasElement; AKind: TPasExprKind); overload;
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
     destructor Destroy; override;
     destructor Destroy; override;
     procedure AddParam(xp: TPasExpr);
     procedure AddParam(xp: TPasExpr);
@@ -199,7 +199,7 @@ type
 
 
   TRecordValues = class(TPasExpr)
   TRecordValues = class(TPasExpr)
     Fields    : array of TRecordValuesItem;
     Fields    : array of TRecordValuesItem;
-    constructor Create(AParent : TPasElement);
+    constructor Create(AParent : TPasElement); overload;
     destructor Destroy; override;
     destructor Destroy; override;
     procedure AddField(const AName: AnsiString; Value: TPasExpr);
     procedure AddField(const AName: AnsiString; Value: TPasExpr);
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
@@ -209,7 +209,7 @@ type
 
 
   TArrayValues = class(TPasExpr)
   TArrayValues = class(TPasExpr)
     Values    : array of TPasExpr;
     Values    : array of TPasExpr;
-    constructor Create(AParent : TPasElement);
+    constructor Create(AParent : TPasElement); overload;
     destructor Destroy; override;
     destructor Destroy; override;
     procedure AddValues(AValue: TPasExpr);
     procedure AddValues(AValue: TPasExpr);
     function GetDeclaration(full : Boolean) : string; override;
     function GetDeclaration(full : Boolean) : string; override;
@@ -270,6 +270,10 @@ type
     Filename   : String;  // the IN filename, only written when not empty.
     Filename   : String;  // the IN filename, only written when not empty.
   end;
   end;
 
 
+  { TPasProgram }
+
+  TPasProgram = class(TPasModule);
+
   { TPasPackage }
   { TPasPackage }
 
 
   TPasPackage = class(TPasElement)
   TPasPackage = class(TPasElement)
@@ -736,6 +740,15 @@ type
     Commands: TStrings;
     Commands: TStrings;
   end;
   end;
 
 
+  { TPasLabels }
+
+  TPasLabels = class(TPasImplElement)
+  public
+    Labels  : TStrings;
+    constructor Create(const AName: string; AParent: TPasElement); override;
+    destructor Destroy; override;
+  end;
+
   TPasImplBeginBlock = class;
   TPasImplBeginBlock = class;
   TPasImplRepeatUntil = class;
   TPasImplRepeatUntil = class;
   TPasImplIfElse = class;
   TPasImplIfElse = class;
@@ -2422,6 +2435,7 @@ end;
 
 
 constructor TPasExpr.Create(AParent : TPasElement; AKind: TPasExprKind; AOpCode: TexprOpcode);
 constructor TPasExpr.Create(AParent : TPasElement; AKind: TPasExprKind; AOpCode: TexprOpcode);
 begin
 begin
+  Create(ClassName, AParent);
   Kind:=AKind;
   Kind:=AKind;
   OpCode:=AOpCode;
   OpCode:=AOpCode;
 end;
 end;
@@ -2645,4 +2659,18 @@ begin
   inherited Create(AParent,pekNil, eopNone);
   inherited Create(AParent,pekNil, eopNone);
 end;
 end;
 
 
+{ TPasLabels }
+
+constructor TPasLabels.Create(const AName:string;AParent:TPasElement);
+begin
+  inherited Create(AName,AParent);
+  Labels := TStringList.Create;
+end;
+
+destructor TPasLabels.Destroy;
+begin
+  Labels.Free;
+  inherited Destroy;
+end;
+
 end.
 end.

+ 41 - 1
packages/fcl-passrc/src/pparser.pp

@@ -160,6 +160,7 @@ type
 
 
     procedure ParseMain(var Module: TPasModule);
     procedure ParseMain(var Module: TPasModule);
     procedure ParseUnit(var Module: TPasModule);
     procedure ParseUnit(var Module: TPasModule);
+    procedure ParseProgram(var Module: TPasModule);
     procedure ParseInterface;
     procedure ParseInterface;
     procedure ParseImplementation;
     procedure ParseImplementation;
     procedure ParseInitialization;
     procedure ParseInitialization;
@@ -186,6 +187,7 @@ type
     procedure ParseProcBeginBlock(Parent: TProcedureBody);
     procedure ParseProcBeginBlock(Parent: TProcedureBody);
     procedure ParseStatement(Parent: TPasImplBlock;
     procedure ParseStatement(Parent: TPasImplBlock;
                              out NewImplElement: TPasImplElement);
                              out NewImplElement: TPasImplElement);
+    procedure ParseLabels(AParent: TPasElement);
 
 
     property FileResolver: TFileResolver read FFileResolver;
     property FileResolver: TFileResolver read FFileResolver;
     property Scanner: TPascalScanner read FScanner;
     property Scanner: TPascalScanner read FScanner;
@@ -1226,6 +1228,7 @@ begin
   NextToken;
   NextToken;
   case CurToken of
   case CurToken of
     tkUnit: ParseUnit(Module);
     tkUnit: ParseUnit(Module);
+    tkProgram: ParseProgram(Module);
     else
     else
       ParseExc(Format(SParserExpectTokenError, ['unit']));
       ParseExc(Format(SParserExpectTokenError, ['unit']));
   end;
   end;
@@ -1253,6 +1256,26 @@ begin
   end;
   end;
 end;
 end;
 
 
+// Starts after the "program" token
+procedure TPasParser.ParseProgram(var Module: TPasModule);
+begin
+  Module := nil;
+  Module := TPasModule(CreateElement(TPasProgram, ExpectIdentifier,
+    Engine.Package));
+  CurModule:=Module;
+  try
+    if Assigned(Engine.Package) then
+    begin
+      Module.PackageName := Engine.Package.Name;
+      Engine.Package.Modules.Add(Module);
+    end;
+    NextToken;
+    ParseImplementation;
+  finally
+    CurModule:=nil;
+  end;
+end;
+
 // Starts after the "interface" token
 // Starts after the "interface" token
 procedure TPasParser.ParseInterface;
 procedure TPasParser.ParseInterface;
 var
 var
@@ -1537,7 +1560,12 @@ begin
           end
           end
         else
         else
           ParseExc(SParserSyntaxError);
           ParseExc(SParserSyntaxError);
-        end
+        end;
+      tklabel:
+        begin
+          if not (Declarations is TInterfaceSection) then
+            ParseLabels(Declarations);
+        end;
     else
     else
       ParseExc(SParserSyntaxError);
       ParseExc(SParserSyntaxError);
     end;
     end;
@@ -2895,6 +2923,18 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure TPasParser.ParseLabels(AParent: TPasElement);
+var
+  Labels: TPasLabels;
+begin
+  Labels:=TPasLabels(CreateElement(TPasLabels, '', AParent));
+  repeat
+    Labels.Labels.Add(ExpectIdentifier);
+    NextToken;
+    if not (CurToken in [tkSemicolon, tkComma]) then
+      ParseExc(Format(SParserExpectTokenError, [TokenInfos[tkSemicolon]]));
+  until CurToken=tkSemicolon;
+end;
 
 
 // Starts after the "procedure" or "function" token
 // Starts after the "procedure" or "function" token
 function TPasParser.ParseProcedureOrFunctionDecl(Parent: TPasElement;
 function TPasParser.ParseProcedureOrFunctionDecl(Parent: TPasElement;