Changeset 24 for trunk/Compiler/Analyze
- Timestamp:
- Nov 10, 2010, 9:42:27 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Compiler/Analyze/UAnalyzerPascal.pas
r21 r24 50 50 function ParseType(TypeList: TTypeList; var NewType: TType; ExpectName: Boolean = True; 51 51 AssignSymbol: string = '='; ForwardDeclaration: Boolean = False): Boolean; 52 function ParseTypeSubType(TypeList: TTypeList; var NewType: TType; Name: string; 52 function ParseTypeParameters(var NewType: TType): Boolean; 53 function ParseTypeSubType(var NewType: TType; 53 54 ExpectName: Boolean; ForwardDeclaration: Boolean): Boolean; 54 function ParseTypeBase( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;55 function ParseTypePointer( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;56 function ParseTypeEnumeration( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;57 function ParseTypeRecord( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;58 function ParseTypeClass( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;59 function ParseTypeArray( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;60 function ParseTypeSubRange( TypeList: TTypeList; var NewType: TType; Name: string): Boolean;55 function ParseTypeBase(var NewType: TType): Boolean; 56 function ParseTypePointer(var NewType: TType): Boolean; 57 function ParseTypeEnumeration(var NewType: TType): Boolean; 58 function ParseTypeRecord(var NewType: TType): Boolean; 59 function ParseTypeClass(var NewType: TType): Boolean; 60 function ParseTypeArray(var NewType: TType): Boolean; 61 function ParseTypeSubRange(var NewType: TType): Boolean; 61 62 constructor Create; 62 63 destructor Destroy; override; … … 1024 1025 function TAnalyzerPascal.ParseType(TypeList: TTypeList; var NewType: TType; ExpectName: Boolean = True; 1025 1026 AssignSymbol: string = '='; ForwardDeclaration: Boolean = False): Boolean; 1026 var 1027 Name: string; 1028 TypeName: string; 1029 begin 1030 NewType := nil; 1027 begin 1028 NewType := TType.Create; 1029 NewType.Parent := TypeList; 1031 1030 //with SourceCode do 1032 1031 begin 1033 1032 if ExpectName then begin 1034 Name := ReadToken; 1033 NewType.Name := ReadToken; 1034 if ParseTypeParameters(NewType) then ; 1035 1035 Expect(AssignSymbol); 1036 1036 end; 1037 1037 1038 1038 Result := True; 1039 if not ParseTypeEnumeration( TypeList, NewType, Name) then1040 if not ParseTypeRecord( TypeList, NewType, Name) then1041 if not ParseTypeClass( TypeList, NewType, Name) then1042 if not ParseTypeArray( TypeList, NewType, Name) then1043 if not ParseTypePointer( TypeList, NewType, Name) then1044 if not ParseTypeBase( TypeList, NewType, Name) then1045 if not ParseTypeSubType( TypeList, NewType, Name, ExpectName, ForwardDeclaration) then1046 if not ParseTypeSubRange( TypeList, NewType, Name) then begin1039 if not ParseTypeEnumeration(NewType) then 1040 if not ParseTypeRecord(NewType) then 1041 if not ParseTypeClass(NewType) then 1042 if not ParseTypeArray(NewType) then 1043 if not ParseTypePointer(NewType) then 1044 if not ParseTypeBase(NewType) then 1045 if not ParseTypeSubType(NewType, ExpectName, ForwardDeclaration) then 1046 if not ParseTypeSubRange(NewType) then begin 1047 1047 ErrorMessage(SInvalidConstruction, [NextToken]); 1048 NewType.Free; 1048 1049 Result := False; 1049 1050 end; … … 1051 1052 end; 1052 1053 1053 function TAnalyzerPascal.ParseTypeSubType(TypeList: TTypeList; var NewType: TType; Name: string; 1054 function TAnalyzerPascal.ParseTypeParameters(var NewType: TType): Boolean; 1055 var 1056 NewType2: TType; 1057 begin 1058 if NextToken = '<' then begin 1059 Expect('<'); 1060 while ((NextToken = ',') or (NewType.Parameters.Count = 0)) and (NextTokenType <> ttEndOfFile) do begin 1061 if NewType.Parameters.Count > 0 then Expect(','); 1062 NewType2 := TType.Create; 1063 NewType2.Name := ReadToken; 1064 NewType2.Parent := NewType.Parent; 1065 NewType.Parameters.Add(NewType2); 1066 end; 1067 Expect('>'); 1068 Result := True; 1069 end else Result := False; 1070 end; 1071 1072 function TAnalyzerPascal.ParseTypeSubType(var NewType: TType; 1054 1073 ExpectName: Boolean; ForwardDeclaration: Boolean): Boolean; 1055 1074 var 1056 1075 TypeName: string; 1076 TempType: TType; 1057 1077 begin 1058 1078 Result := False; 1059 NewType := nil;1060 1079 // Use existed type 1061 1080 if NextTokenType = ttIdentifier then begin 1062 1081 TypeName := ReadToken; 1063 1082 if ExpectName then begin 1064 NewType := TType.Create; 1065 NewType.Parent := TypeList; 1066 NewType.Name := Name; 1067 NewType.UsedType := TypeList.Search(TypeName); 1083 NewType.UsedType := NewType.Parent.Search(TypeName); 1068 1084 Result := True; 1069 1085 if not Assigned(NewType.UsedType) then 1070 1086 ErrorMessage(SUndefinedType, [TypeName], -1); 1071 1087 end else begin 1072 NewType := TypeList.Search(TypeName);1088 NewType := NewType.Parent.Search(TypeName); 1073 1089 if not Assigned(TType(NewType)) then begin 1074 1090 if ForwardDeclaration then begin 1075 1091 // ForwardDeclaration 1076 NewType := TType.Create;1077 NewType.Parent := TypeList;1078 1092 NewType.Name := TypeName; 1079 1093 NewType.UsedType := nil; … … 1086 1100 end; 1087 1101 1088 function TAnalyzerPascal.ParseTypeBase(TypeList: TTypeList; var NewType: TType; Name: string): Boolean; 1102 function TAnalyzerPascal.ParseTypeBase(var NewType: TType): Boolean; 1103 var 1104 TempType: TType; 1089 1105 begin 1090 1106 // Buildin base type construction 1091 1107 if NextToken = 'type' then begin 1092 1108 Expect('type'); 1109 TempType := NewType; 1093 1110 NewType := TTypeInherited.Create; 1094 NewType. Parent := TypeList;1095 NewType.Name := Name;1111 NewType.Assign(TempType); 1112 TempType.Free; 1096 1113 if NextToken = '(' then begin 1097 1114 Expect('('); 1098 if ParseType( TypeList, NewType.UsedType, False) then ;1115 if ParseType(NewType.Parent, NewType.UsedType, False) then ; 1099 1116 Expect(')'); 1100 1117 end else NewType.UsedType := nil; … … 1103 1120 end; 1104 1121 1105 function TAnalyzerPascal.ParseTypePointer(TypeList: TTypeList; var NewType: TType; Name: string 1106 ): Boolean; 1122 function TAnalyzerPascal.ParseTypePointer(var NewType: TType): Boolean; 1123 var 1124 TempType: TType; 1107 1125 begin 1108 1126 if NextToken = '^' then begin 1109 1127 Expect('^'); 1128 TempType := NewType; 1110 1129 NewType := TTypePointer.Create; 1111 NewType.Parent := TypeList; 1112 NewType.Name := Name; 1113 Result := ParseType(TypeList, NewType.UsedType, False, '=', True); 1114 end else Result := False; 1115 end; 1116 1117 function TAnalyzerPascal.ParseTypeEnumeration(TypeList: TTypeList; var NewType: TType; 1118 Name: string): Boolean; 1130 NewType.Assign(TempType); 1131 TempType.Free; 1132 Result := ParseType(NewType.Parent, NewType.UsedType, False, '=', True); 1133 end else Result := False; 1134 end; 1135 1136 function TAnalyzerPascal.ParseTypeEnumeration(var NewType: TType 1137 ): Boolean; 1138 var 1139 TempType: TType; 1119 1140 begin 1120 1141 if NextToken = '(' then begin 1121 1142 Expect('('); 1143 TempType := NewType; 1122 1144 NewType := TTypeEnumeration.Create; 1123 NewType. Parent := TypeList;1124 NewType.Name := Name;1145 NewType.Assign(TempType); 1146 TempType.Free; 1125 1147 with TTypeEnumeration(NewType) do 1126 1148 with TEnumItem(Items[Items.Add(TEnumItem.Create)]) do begin … … 1148 1170 end; 1149 1171 1150 function TAnalyzerPascal.ParseTypeRecord(TypeList: TTypeList; var NewType: TType; Name: string 1151 ): Boolean; 1172 function TAnalyzerPascal.ParseTypeRecord(var NewType: TType): Boolean; 1152 1173 var 1153 1174 Visibility: TTypeVisibility; 1154 1175 SectionType: TCommonBlockSection; 1155 1176 NewType2: TType; 1177 TempType: TType; 1156 1178 begin 1157 1179 if NextToken = 'record' then begin … … 1159 1181 SectionType := cbsVariable; 1160 1182 Visibility := tvPublic; 1183 TempType := NewType; 1161 1184 NewType := TTypeRecord.Create; 1162 NewType. Parent := TypeList;1163 T TypeRecord(NewType).CommonBlock.Parent := TypeList.Parent;1164 NewType.Name := Name;1185 NewType.Assign(TempType); 1186 TempType.Free; 1187 TTypeRecord(NewType).CommonBlock.Parent := NewType.Parent.Parent; 1165 1188 while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do begin 1166 1189 // Visibility sections … … 1220 1243 end; 1221 1244 1222 function TAnalyzerPascal.ParseTypeClass( TypeList: TTypeList; var NewType: TType; Name: string1245 function TAnalyzerPascal.ParseTypeClass(var NewType: TType 1223 1246 ): Boolean; 1224 1247 var 1225 1248 NewType2: TType; 1249 TempType: TType; 1226 1250 begin 1227 1251 if NextToken = 'class' then begin 1228 1252 Expect('class'); 1253 TempType := NewType; 1229 1254 NewType := TTypeClass.Create; 1230 NewType. Parent := TypeList;1231 NewType.Name := Name;1255 NewType.Assign(TempType); 1256 TempType.Free; 1232 1257 if NextToken <> ';' then begin 1233 1258 while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do 1234 1259 begin 1235 if ParseType( TypeList, NewType2, True, ':') then begin1236 NewType2.Parent := TypeList;1260 if ParseType(NewType.Parent, NewType2, True, ':') then begin 1261 NewType2.Parent := NewType.Parent; 1237 1262 TTypeClass(NewType).Items.Add(NewType2); 1238 1263 end; … … 1245 1270 end; 1246 1271 1247 function TAnalyzerPascal.ParseTypeArray(TypeList: TTypeList; var NewType: TType; Name: string 1248 ): Boolean; 1272 function TAnalyzerPascal.ParseTypeArray(var NewType: TType): Boolean; 1249 1273 var 1250 1274 UseName: string; 1251 1275 UseType: TType; 1276 TempType: TType; 1252 1277 begin 1253 1278 if NextToken = 'array' then begin 1254 1279 Expect('array'); 1280 TempType := NewType; 1255 1281 NewType := TTypeArray.Create; 1256 NewType. Parent := TypeList;1257 NewType.Name := Name;1282 NewType.Assign(TempType); 1283 TempType.Free; 1258 1284 if NextToken = '[' then begin 1259 1285 Expect('['); 1260 1286 UseName := NextToken; 1261 1287 if NextTokenType = ttIdentifier then begin 1262 TTypeArray(NewType).IndexType := TypeList.Parent.Types.Search(UseName);1288 TTypeArray(NewType).IndexType := NewType.Parent.Parent.Types.Search(UseName); 1263 1289 if not Assigned(TTypeArray(NewType).IndexType) then 1264 1290 ErrorMessage(SUndefinedType, [UseName], -1); 1265 1291 end else 1266 1292 if NextTokenType = ttConstantNumber then begin 1267 ParseTypeSubRange(T ypeList, TTypeArray(NewType).IndexType, Name);1293 ParseTypeSubRange(TTypeArray(NewType).IndexType); 1268 1294 if not Assigned(TTypeArray(NewType).IndexType) then begin 1269 1295 ErrorMessage(SInvalidConstruction, [NextToken], -1); … … 1274 1300 Expect('of'); 1275 1301 UseName := NextToken; 1276 Result := ParseType( TypeList, TTypeArray(NewType).ItemType, False);1302 Result := ParseType(NewType.Parent, TTypeArray(NewType).ItemType, False); 1277 1303 if not Assigned(TTypeArray(NewType).ItemType) then 1278 1304 ErrorMessage(SUndefinedType, [UseName], -1); … … 1280 1306 end; 1281 1307 1282 function TAnalyzerPascal.ParseTypeSubRange(TypeList: TTypeList; var NewType: TType; 1283 Name: string): Boolean; 1308 function TAnalyzerPascal.ParseTypeSubRange(var NewType: TType): Boolean; 1284 1309 var 1285 1310 UseName: string; 1311 TempType: TType; 1286 1312 begin 1287 1313 if NextTokenType = ttConstantString then begin 1314 TempType := NewType; 1288 1315 NewType := TTypeSubRange.Create; 1316 NewType.Assign(TempType); 1317 TempType.Free; 1289 1318 TTypeSubRange(NewType).Bottom := ReadToken; 1290 1319 Expect('..'); … … 1293 1322 end else 1294 1323 if NextTokenType = ttConstantNumber then begin 1324 TempType := NewType; 1295 1325 NewType := TTypeSubRange.Create; 1326 NewType.Assign(TempType); 1327 TempType.Free; 1296 1328 TTypeSubRange(NewType).Bottom := ReadToken; 1297 1329 Expect('..');
Note:
See TracChangeset
for help on using the changeset viewer.