Ignore:
Timestamp:
Apr 9, 2009, 2:08:56 PM (15 years ago)
Author:
george
Message:
  • Přidáno: Nástřel parsování funkcí.
  • Přidáno: Zobrazení stromu struktury programu.
  • Opraveno: Zobrazení chybových hlášení.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC/UPascalParser.pas

    r14 r19  
    2828    function IsOperator(Text: string): Boolean;
    2929    procedure ParseProgram(AProgram: TProgram);
    30     procedure ParseFunctionList(FunctionList: TFunctionList);
    3130    procedure ParseModule(Module: TModule);
    3231    procedure ParseModuleUnit(Module: TModule);
    3332    procedure ParseModuleProgram(Module: TModule);
    34     procedure ParseFunction(AFunction: TFunction);
     33    procedure ParseFunction(FunctionList: TFunctionList);
    3534    procedure ParseVariableList(VariableList: TVariableList);
    3635    procedure ParseVariable(Variable: TVariable);
     
    3938    procedure ParseTypeList(TypeList: TTypeList);
    4039    procedure ParseType(AType: TType);
    41     procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock);
     40    procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';');
    4241    function ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression;
    4342    procedure ParseCommonBlockProgramCode(CommonBlock: TCommonBlock);
    4443    procedure ParseCommonBlockOperation(CommonBlock: TCommonBlock);
    45     procedure Parse;
     44    procedure Log(Text: string);
    4645    property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage;
    4746  end;
     
    5857procedure TPascalParser.Expect(Code: string);
    5958begin
     59  Log('Expected: ' + Code + '  Readed: ' + NextCode);
    6060  if NextCode <> Code then begin
    6161    ErrorMessage('Expected ' + Code + ' but ' + NextCode + ' found.');
     
    112112begin
    113113  Result := (Character = ' ') or (Character = #13) or (Character = #10);
     114end;
     115
     116procedure TPascalParser.Log(Text: string);
     117const
     118  LogFileName = 'ParseLog.txt';
     119var
     120  LogFile: TextFile;
     121begin
     122  AssignFile(LogFile, LogFileName);
     123  if FileExists(LogFileName) then Append(LogFile)
     124    else Rewrite(LogFile);
     125  WriteLn(LogFile, Text);
     126  CloseFile(LogFile);
    114127end;
    115128
     
    189202begin
    190203  Result := NextCode(True);
    191 end;
    192 
    193 procedure TPascalParser.ParseFunction(AFunction: TFunction);
    194 begin
    195   with AFunction do begin
    196     if NextCode = 'var' then ParseVariableList(TVariableList(Variables))
    197     else if NextCode = 'const' then ParseConstantList(TConstantList(Constants))
    198     else if NextCode = 'type' then ParseTypeList(TTypeList(Types))
    199     else ParseProgram(ProgramCode);
     204  Log('Read: ' + Result);
     205end;
     206
     207procedure TPascalParser.ParseFunction(FunctionList: TFunctionList);
     208begin
     209  with FunctionList do begin
     210    with TFunction(Items[Add(TFunction.Create)]) do begin
     211      Expect('procedure');
     212      Name := ReadCode;
     213      Expect(';');
     214      ParseCommonBlockDefinitions(Items[Count - 1]);
     215    end;
    200216  end;
    201217end;
     
    205221  I: Integer;
    206222begin
     223  Log('==== Parse start ====');
    207224  with AProgram do begin
    208225    for I := 0 to Modules.Count - 1 do
     
    227244
    228245procedure TPascalParser.ParseConstantList(ConstantList: TConstantList);
    229 begin
    230 //  Compiler.Expect('const');
    231 //  while Compiler.IsIdentificator(Compiler.NextCode) do
    232 //    TConstant(Items[Add(TConstant.Create)]).Parse(Compiler);
     246var
     247  Identifiers: TStringList;
     248  NewValueType: TType;
     249  TypeName: string;
     250  ConstantName: string;
     251  Constant: TConstant;
     252  I: Integer;
     253  ConstantValue: string;
     254begin
     255  Identifiers := TStringList.Create;
     256  with ConstantList do begin
     257    Expect('const');
     258    while IsIdentificator(NextCode) do begin
     259      ConstantName := ReadCode;
     260      Constant := Search(ConstantName);
     261      if not Assigned(Constant) then begin
     262        Identifiers.Add(ConstantName);
     263        while NextCode = ',' do begin
     264          Expect(',');
     265          Identifiers.Add(ReadCode);
     266        end;
     267      end else ErrorMessage('Pøedefinování existující konstanty.');
     268      Expect(':');
     269      TypeName := ReadCode;
     270      NewValueType := Parent.Types.Search(TypeName);
     271      Expect('=');
     272      ConstantValue := ReadCode;
     273      Expect(';');
     274
     275      if NewValueType = nil then ErrorMessage('Typ ' + TypeName + ' nebyl definován.')
     276        else for I := 0 to Identifiers.Count - 1 do
     277          with TConstant(Items[Add(TConstant.Create)]) do begin
     278            Name := Identifiers[I];
     279            ValueType := NewValueType;
     280            Value := ConstantValue;
     281          end;
     282    end;
     283  end;
     284  Identifiers.Destroy;
    233285end;
    234286
     
    253305      end;
    254306    end;
    255     ParseCommonBlockDefinitions(Module);
     307    ParseCommonBlockDefinitions(Module, '.');
    256308  end;
    257309end;
     
    266318end;
    267319
    268 procedure TPascalParser.Parse;
    269 begin
    270 
    271 end;
    272 
    273 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock);
     320procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';');
    274321begin
    275322  with CommonBlock do begin
    276     while NextCode <> '.' do begin
     323    while NextCode <> EndSymbol do begin
    277324      if NextCode = 'var' then ParseVariableList(TVariableList(Variables))
    278325      else if NextCode = 'const' then ParseConstantList(TConstantList(Constants))
    279326      else if NextCode = 'type' then ParseTypeList(TTypeList(Types))
     327      else if NextCode = 'procedure' then ParseFunction(Methods)
    280328      else begin
    281329        ParseCommonBlockProgramCode(CommonBlock);
     
    283331      end;
    284332    end;
     333    Expect(EndSymbol);
    285334  end;
    286335end;
     
    371420
    372421            if Identifier[1] = '''' then begin
    373               SetLength(TExpression(SubItems[1]).Value, Length(Identifier));
    374               for I := 1 to Length(Identifier) do TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]);
     422              TExpression(SubItems[1]).Value := Identifier;
     423              //SetLength(TExpression(SubItems[1]).Value, Length(Identifier));
     424              //for I := 1 to Length(Identifier) do
     425              //  TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]);
    375426            end else begin
    376               SetLength(TExpression(SubItems[1]).Value, 1);
    377               TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);
     427              //SetLength(TExpression(SubItems[1]).Value, 1);
     428              //TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);
    378429            end;
    379430          end;
     
    518569            with TExpression(SubItems[1]) do begin
    519570              NodeType := ntConstant;
    520               SetLength(Value, 1);
    521               Value[0] := 1;
     571              //SetLength(Value, 1);
     572              //Value[0] := 1;
     573              Value := 1;
    522574            end;
    523575          end;
     
    617669end;
    618670
    619 procedure TPascalParser.ParseFunctionList(FunctionList: TFunctionList);
    620 begin
    621 
    622 end;
    623 
    624671end.
Note: See TracChangeset for help on using the changeset viewer.