Ignore:
Timestamp:
Aug 10, 2010, 10:14:57 AM (14 years ago)
Author:
george
Message:

Project renamed to Transpascal.
Added support for generatin cycle For-to-do.
Added support for parsing and generating structured types as record and array.
Emit procedure in TProducerPascal and TProducerC reworked.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC/Analyze/UParser.pas

    r51 r53  
    6565    procedure ParseConstantList(SourceCode: TConstantList);
    6666    procedure ParseTypeList(SourceCode: TTypeList);
    67     function ParseType(TypeList: TTypeList): TType;
    68     function ParseRecordItem(TypeList: TTypeList): TType;
     67    function ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType;
    6968  private
    7069  end;
     
    999998  begin
    1000999    Expect('type');
    1001     while IsIdentificator(FNextToken) do
     1000    while IsIdentificator(FNextToken) do begin
    10021001      NewType := ParseType(SourceCode);
    10031002      if Assigned(NewType) then begin
     
    10051004        Add(NewType);
    10061005      end;
     1006      Expect(';');
     1007    end;
    10071008  end;
    10081009end;
     
    10101011{ TParserType }
    10111012
    1012 function TPascalParser.ParseType(TypeList: TTypeList): TType;
     1013function TPascalParser.ParseType(TypeList: TTypeList; ExpectName: Boolean = True; AssignSymbol: string = '='): TType;
    10131014var
    10141015  Name: string;
    1015   UsedTypeName: string;
     1016  TypeName: string;
    10161017begin
    10171018  //with SourceCode do
    10181019  begin
    1019     Name := ReadCode;
    1020     Expect('=');
     1020    if ExpectName then begin
     1021      Name := ReadCode;
     1022      Expect(AssignSymbol);
     1023    end;
    10211024    if FNextToken = 'record' then begin
    10221025      Expect('record');
     
    10261029      while (FNextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do
    10271030      begin
    1028         TTypeRecord(Result).Items.Add(ParseRecordItem(TypeList));
     1031        TTypeRecord(Result).Items.Add(ParseType(TypeList, True, ':'));
     1032        Expect(';');
    10291033      end;
    10301034      Expect('end');
     1035    end else
     1036    if FNextToken = 'array' then begin
     1037      Expect('array');
     1038      Result := TTypeArray.Create;
     1039      TTypeArray(Result).Parent := TypeList;
     1040      TType(Result).Name := Name;
     1041      if FNextToken = '[' then begin
     1042        Expect('[');
     1043        TypeName := FNextToken;
     1044        TTypeArray(Result).IndexType := ParseType(TypeList, False);
     1045        if not Assigned(TTypeArray(Result).IndexType) then
     1046          ErrorMessage(SUndefinedType, [TypeName]);
     1047        Expect(']');
     1048      end;
     1049      Expect('of');
     1050      TypeName := FNextToken;
     1051      TTypeArray(Result).ItemType := ParseType(TypeList, False);
     1052      if not Assigned(TTypeArray(Result).ItemType) then
     1053        ErrorMessage(SUndefinedType, [TypeName]);
    10311054    end else begin
    1032       UsedTypeName := ReadCode;
    1033       Result := TType.Create;
    1034       TType(Result).Parent := TypeList;
    1035       TType(Result).Name := Name;
    1036       TType(Result).UsedType := TypeList.Search(UsedTypeName);
    1037       if not Assigned(TType(Result).UsedType) then ErrorMessage(SUndefinedType, [UsedTypeName]);
    1038     end;
    1039     Expect(';');
    1040   end;
    1041 end;
    1042 
    1043 function TPascalParser.ParseRecordItem(TypeList: TTypeList): TType;
    1044 var
    1045   Name: string;
    1046   UsedTypeName: string;
    1047 begin
    1048   //with SourceCode do
    1049   begin
    1050     Name := ReadCode;
    1051     Expect(':');
    1052     if FNextToken = 'record' then begin
    1053       Expect('record');
    1054       Result := TTypeRecord.Create;
    1055       TTypeRecord(Result).Parent := TypeList;
    1056       TTypeRecord(Result).Name := Name;
    1057       while (FNextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do
    1058       begin
    1059         TTypeRecord(Result).Items.Add(ParseRecordItem(TypeList));
    1060       end;
    1061       Expect('end');
    1062     end else begin
    1063       UsedTypeName := ReadCode;
    1064       Result := TType.Create;
    1065       TType(Result).Parent := TypeList;
    1066       TType(Result).Name := Name;
    1067       TType(Result).UsedType := TypeList.Search(UsedTypeName);
    1068       if not Assigned(TType(Result).UsedType) then ErrorMessage(SUndefinedType, [UsedTypeName]);
    1069     end;
    1070     Expect(';');
     1055      TypeName := ReadCode;
     1056      if ExpectName then begin
     1057        Result := TType.Create;
     1058        TType(Result).Parent := TypeList;
     1059        TType(Result).Name := Name;
     1060        TType(Result).UsedType := TypeList.Search(TypeName);
     1061        if not Assigned(TType(Result).UsedType) then
     1062          ErrorMessage(SUndefinedType, [TypeName]);
     1063      end else begin
     1064        TType(Result) := TypeList.Search(TypeName);
     1065        if not Assigned(TType(Result)) then
     1066          ErrorMessage(SUndefinedType, [TypeName]);
     1067      end;
     1068    end;
    10711069  end;
    10721070end;
Note: See TracChangeset for help on using the changeset viewer.