Changeset 141 for branches/easy compiler/USourceCode.pas
- Timestamp:
- Jan 16, 2018, 2:44:19 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/easy compiler/USourceCode.pas
r140 r141 18 18 end; 19 19 20 { TSourceValue } 21 22 TSourceValue = class 23 procedure Assign(Source: TSourceValue); virtual; 24 end; 25 26 TSourceValueClass = class of TSourceValue; 27 28 { TSourceType } 29 30 TSourceType = class 31 Name: string; 32 ValueClass: TSourceValueClass; 33 end; 34 35 { TSourceTypes } 36 37 TSourceTypes = class(TObjectList) 38 function AddNew(Name: string; ClassType: TSourceValueClass): TSourceType; 39 function Search(Name: string): TSourceType; 40 end; 41 42 { TSourceVariable } 43 20 44 TSourceVariable = class 21 45 Name: string; 46 ValueType: TSourceType; 22 47 end; 23 48 … … 25 50 26 51 TSourceVariables = class(TObjectList) 27 function AddNew(Name: string = ''): TSourceVariable;52 function AddNew(Name: string; ValueType: TSourceType): TSourceVariable; 28 53 function Search(Name: string): TSourceVariable; 29 54 end; … … 33 58 end; 34 59 60 { TSourceValueString } 61 62 TSourceValueString = class(TSourceValue) 63 Value: string; 64 procedure Assign(Source: TSourceValue); override; 65 end; 66 67 { TSourceValueInteger } 68 69 TSourceValueInteger = class(TSourceValue) 70 Value: Integer; 71 procedure Assign(Source: TSourceValue); override; 72 end; 73 35 74 TSourceConstant = class 36 75 Name: string; 37 Value: string;38 end; 39 40 TSourceParameterKind = (pkString, pkVariable );76 Value: TSourceValue; 77 end; 78 79 TSourceParameterKind = (pkString, pkVariable, pkType); 41 80 42 81 TSourceFunctionParameter = class … … 68 107 69 108 TSourceConstants = class(TObjectList) 70 function AddNew(Value: string; Name: string = ''): TSourceConstant; 109 function AddNewString(Value: string; Name: string = ''): TSourceConstant; 110 function AddNewInteger(Value: Integer; Name: string = ''): TSourceConstant; 111 function Search(Name: string): TSourceConstant; 71 112 end; 72 113 … … 89 130 procedure InitFunctions; 90 131 public 132 Types: TSourceTypes; 91 133 Variables: TSourceVariables; 92 134 Constants: TSourceConstants; … … 100 142 implementation 101 143 102 { TSource Functions }103 104 function TSource Functions.AddNew(Name: string): TSourceFunction;105 begin 106 Result := TSource Function.Create;144 { TSourceTypes } 145 146 function TSourceTypes.AddNew(Name: string; ClassType: TSourceValueClass): TSourceType; 147 begin 148 Result := TSourceType.Create; 107 149 Result.Name := Name; 108 Add(Result); 109 end; 110 111 function TSourceFunctions.Search(Name: string): TSourceFunction; 112 var 113 Item: TSourceFunction; 150 Result.ValueClass := ClassType; 151 Add(Result); 152 end; 153 154 function TSourceTypes.Search(Name: string): TSourceType; 155 var 156 Item: TSourceType; 114 157 begin 115 158 Result := nil; … … 121 164 end; 122 165 166 { TSourceValue } 167 168 procedure TSourceValue.Assign(Source: TSourceValue); 169 begin 170 raise Exception.Create('Value assign not implemented'); 171 end; 172 173 { TSourceValueInteger } 174 175 procedure TSourceValueInteger.Assign(Source: TSourceValue); 176 begin 177 if Source is TSourceValueInteger then 178 Value := TSourceValueInteger(Source).Value 179 else raise Exception.Create('Type for assignment not matches'); 180 end; 181 182 { TSourceValueString } 183 184 procedure TSourceValueString.Assign(Source: TSourceValue); 185 begin 186 if Source is TSourceValueString then 187 Value := TSourceValueString(Source).Value 188 else raise Exception.Create('Type for assignment not matches'); 189 end; 190 191 { TSourceFunctions } 192 193 function TSourceFunctions.AddNew(Name: string): TSourceFunction; 194 begin 195 Result := TSourceFunction.Create; 196 Result.Name := Name; 197 Add(Result); 198 end; 199 200 function TSourceFunctions.Search(Name: string): TSourceFunction; 201 var 202 Item: TSourceFunction; 203 begin 204 Result := nil; 205 for Item in Self do 206 if Item.Name = Name then begin 207 Result := Item; 208 Break; 209 end; 210 end; 211 123 212 { TSourceFunction } 124 213 … … 147 236 { TSourceVariables } 148 237 149 function TSourceVariables.AddNew(Name: string ): TSourceVariable;238 function TSourceVariables.AddNew(Name: string;ValueType: TSourceType): TSourceVariable; 150 239 begin 151 240 Result := TSourceVariable.Create; 152 241 Result.Name := Name; 242 Result.ValueType := ValueType; 153 243 Add(Result); 154 244 end; … … 156 246 function TSourceVariables.Search(Name: string): TSourceVariable; 157 247 var 158 Variable: TSourceVariable;248 Item: TSourceVariable; 159 249 begin 160 250 Result := nil; 161 for Variablein Self do162 if Variable.Name = Name then begin163 Result := Variable;251 for Item in Self do 252 if Item.Name = Name then begin 253 Result := Item; 164 254 Break; 165 255 end; … … 168 258 { TSourceConstants } 169 259 170 function TSourceConstants.AddNew(Value: string; Name: string): TSourceConstant; 260 function TSourceConstants.AddNewString(Value: string; Name: string 261 ): TSourceConstant; 171 262 begin 172 263 Result := TSourceConstant.Create; 173 Result.Value := Value; 264 Result.Value := TSourceValueString.Create; 265 TSourceValueString(Result.Value).Value := Value; 174 266 Result.Name := ''; 175 267 Add(Result); 268 end; 269 270 function TSourceConstants.AddNewInteger(Value: Integer; Name: string 271 ): TSourceConstant; 272 begin 273 Result := TSourceConstant.Create; 274 Result.Value := TSourceValueInteger.Create; 275 TSourceValueInteger(Result.Value).Value := Value; 276 Result.Name := ''; 277 Add(Result); 278 end; 279 280 function TSourceConstants.Search(Name: string): TSourceConstant; 281 var 282 Item: TSourceConstant; 283 begin 284 Result := nil; 285 for Item in Self do 286 if Item.Name = Name then begin 287 Result := Item; 288 Break; 289 end; 176 290 end; 177 291 … … 191 305 begin 192 306 Functions.Clear; 307 308 // Init types 309 Types.AddNew('Integer', TSourceValueInteger); 310 Types.AddNew('String', TSourceValueString); 311 312 // Init functions 193 313 Funct := Functions.AddNew('print'); 194 314 Funct.AddParameter('Text', pkString); 315 195 316 Funct := Functions.AddNew('println'); 196 317 Funct.AddParameter('Text', pkString); 318 319 Funct := Functions.AddNew('var'); 320 Funct.AddParameter('Variable', pkVariable); 321 Funct.AddParameter('Type', pkType); 322 197 323 Funct := Functions.AddNew('assign'); 198 324 Funct.AddParameter('Destination', pkVariable); 199 325 Funct.AddParameter('Source', pkString); 326 200 327 Funct := Functions.AddNew('inputln'); 201 328 Funct.AddParameter('Text', pkVariable); 329 330 Funct := Functions.AddNew('increment'); 331 Funct.AddParameter('Variable', pkVariable); 332 Funct.AddParameter('Addition', pkString); 202 333 end; 203 334 204 335 constructor TSourceCode.Create; 205 336 begin 337 Types := TSourceTypes.Create; 206 338 Variables := TSourceVariables.Create; 207 339 Constants := TSourceConstants.Create; 340 Functions := TSourceFunctions.Create; 208 341 Instructions := TSourceInstructions.Create; 209 Functions := TSourceFunctions.Create;210 342 InitFunctions; 211 343 end; … … 213 345 destructor TSourceCode.Destroy; 214 346 begin 347 Instructions.Free; 215 348 Functions.Free; 216 349 Variables.Free; 217 350 Constants.Free; 218 Instructions.Free;351 Types.Free; 219 352 inherited Destroy; 220 353 end;
Note:
See TracChangeset
for help on using the changeset viewer.