| 1 | unit SourceCodePascal;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | SysUtils, Variants, Classes, Dialogs, Generics.Collections, SourceConvertor;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 | TMemoryType = (mtProgram, mtData, mtEEPROM);
|
|---|
| 10 |
|
|---|
| 11 | TModuleType = (mdProgram, mdUnit, mdLibrary, mdPackage);
|
|---|
| 12 |
|
|---|
| 13 | TNodeType = (ntNone, ntVariable, ntFunction, ntConstant, ntOperator,
|
|---|
| 14 | ntValue, ntTypecast, ntType);
|
|---|
| 15 |
|
|---|
| 16 | TTypeVisibility = (tvPublic, tvPublished, tvPrivate, tvProtected);
|
|---|
| 17 |
|
|---|
| 18 | TValue = Variant; //array of Byte;
|
|---|
| 19 |
|
|---|
| 20 | TCommonBlock = class;
|
|---|
| 21 | TTypes = class;
|
|---|
| 22 | TConstants = class;
|
|---|
| 23 | TVariables = class;
|
|---|
| 24 | TFunctions = class;
|
|---|
| 25 | TExpressions = class;
|
|---|
| 26 | TExpression = class;
|
|---|
| 27 | TFunction = class;
|
|---|
| 28 | TVariable = class;
|
|---|
| 29 | TConstant = class;
|
|---|
| 30 | TSourceModule = class;
|
|---|
| 31 | TProgram = class;
|
|---|
| 32 |
|
|---|
| 33 | TDevice = class
|
|---|
| 34 | Family: string;
|
|---|
| 35 | Memory: array[TMemoryType] of Integer;
|
|---|
| 36 | end;
|
|---|
| 37 |
|
|---|
| 38 | TContext = class
|
|---|
| 39 | end;
|
|---|
| 40 |
|
|---|
| 41 | TCommands = class;
|
|---|
| 42 |
|
|---|
| 43 | TCommand = class
|
|---|
| 44 | Parent: TObject;
|
|---|
| 45 | CommonBlock: TCommonBlock;
|
|---|
| 46 | end;
|
|---|
| 47 |
|
|---|
| 48 | TAssignment = class(TCommand)
|
|---|
| 49 | Target: TVariable;
|
|---|
| 50 | Source: TExpression;
|
|---|
| 51 | constructor Create;
|
|---|
| 52 | destructor Destroy; override;
|
|---|
| 53 | end;
|
|---|
| 54 |
|
|---|
| 55 | { TExpressions }
|
|---|
| 56 |
|
|---|
| 57 | TExpressions = class(TObjectList<TExpression>)
|
|---|
| 58 | procedure Assign(Source: TExpressions);
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | { TFunctionCall }
|
|---|
| 62 |
|
|---|
| 63 | TFunctionCall = class(TCommand)
|
|---|
| 64 | FunctionRef: TFunction;
|
|---|
| 65 | ParameterExpression: TExpressions;
|
|---|
| 66 | constructor Create;
|
|---|
| 67 | destructor Destroy; override;
|
|---|
| 68 | end;
|
|---|
| 69 |
|
|---|
| 70 | TBeginEnd = class(TCommand)
|
|---|
| 71 | Commands: TCommands;
|
|---|
| 72 | CommonBlock: TCommonBlock;
|
|---|
| 73 | procedure Clear;
|
|---|
| 74 | constructor Create;
|
|---|
| 75 | destructor Destroy; override;
|
|---|
| 76 | end;
|
|---|
| 77 |
|
|---|
| 78 | TWhileDo = class(TCommand)
|
|---|
| 79 | Condition: TExpression;
|
|---|
| 80 | Command: TCommand;
|
|---|
| 81 | constructor Create;
|
|---|
| 82 | destructor Destroy; override;
|
|---|
| 83 | end;
|
|---|
| 84 |
|
|---|
| 85 | TWithDo = class(TCommand)
|
|---|
| 86 | Context: TContext;
|
|---|
| 87 | Command: TCommand;
|
|---|
| 88 | end;
|
|---|
| 89 |
|
|---|
| 90 | TRepeatUntil = class(TCommand)
|
|---|
| 91 | Block: TCommands;
|
|---|
| 92 | Condition: TExpression;
|
|---|
| 93 | end;
|
|---|
| 94 |
|
|---|
| 95 | { TForToDo }
|
|---|
| 96 |
|
|---|
| 97 | TForToDo = class(TCommand)
|
|---|
| 98 | ControlVariable: TVariable;
|
|---|
| 99 | Start: TExpression;
|
|---|
| 100 | Stop: TExpression;
|
|---|
| 101 | Command: TCommand;
|
|---|
| 102 | constructor Create;
|
|---|
| 103 | destructor Destroy; override;
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | { TIfThenElse }
|
|---|
| 107 |
|
|---|
| 108 | TIfThenElse = class(TCommand)
|
|---|
| 109 | Condition: TExpression;
|
|---|
| 110 | Command: TCommand;
|
|---|
| 111 | ElseCommand: TCommand;
|
|---|
| 112 | constructor Create;
|
|---|
| 113 | destructor Destroy; override;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | TCaseOfEndBranche = class
|
|---|
| 117 | Constant: TConstant;
|
|---|
| 118 | Command: TCommand;
|
|---|
| 119 | end;
|
|---|
| 120 |
|
|---|
| 121 | TListCaseOfEndBranche = class(TObjectList<TCaseOfEndBranche>);
|
|---|
| 122 |
|
|---|
| 123 | TCaseOfEnd = class(TCommand)
|
|---|
| 124 | Expression: TExpression;
|
|---|
| 125 | Branches: TListCaseOfEndBranche;
|
|---|
| 126 | ElseCommand: TCommand;
|
|---|
| 127 | constructor Create;
|
|---|
| 128 | destructor Destroy; override;
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | TTryFinally = class(TCommand)
|
|---|
| 132 | Block: TCommands;
|
|---|
| 133 | FinallyBlock: TCommands;
|
|---|
| 134 | end;
|
|---|
| 135 |
|
|---|
| 136 | TTryExcept = class(TCommand)
|
|---|
| 137 | Block: TCommands;
|
|---|
| 138 | ExceptBlock: TCommands;
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | TCommands = class(TObjectList<TCommand>);
|
|---|
| 142 |
|
|---|
| 143 | TCommonBlockSection = (cbsVariable, cbsType, cbsConstant);
|
|---|
| 144 |
|
|---|
| 145 | TCommonBlock = class
|
|---|
| 146 | Name: string;
|
|---|
| 147 | Parent: TCommonBlock;
|
|---|
| 148 | ParentModule: TSourceModule;
|
|---|
| 149 | Constants: TConstants;
|
|---|
| 150 | Types: TTypes;
|
|---|
| 151 | Variables: TVariables;
|
|---|
| 152 | Functions: TFunctions;
|
|---|
| 153 | Order: TObjectList<TObject>;
|
|---|
| 154 | Code: TBeginEnd;
|
|---|
| 155 | constructor Create; virtual;
|
|---|
| 156 | destructor Destroy; override;
|
|---|
| 157 | // procedure CheckReferences;
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | { TType }
|
|---|
| 161 |
|
|---|
| 162 | TType = class
|
|---|
| 163 | ForwardDeclared: Boolean;
|
|---|
| 164 | Internal: Boolean;
|
|---|
| 165 | Parent: TTypes;
|
|---|
| 166 | Name: string;
|
|---|
| 167 | Size: Integer;
|
|---|
| 168 | UsedType: TType;
|
|---|
| 169 | Exported: Boolean;
|
|---|
| 170 | Visibility: TTypeVisibility;
|
|---|
| 171 | Parameters: TTypes;
|
|---|
| 172 | procedure Assign(Source: TType);
|
|---|
| 173 | constructor Create;
|
|---|
| 174 | destructor Destroy; override;
|
|---|
| 175 | end;
|
|---|
| 176 |
|
|---|
| 177 | { TTypes }
|
|---|
| 178 |
|
|---|
| 179 | TTypes = class(TObjectList<TType>)
|
|---|
| 180 | Parent: TCommonBlock;
|
|---|
| 181 | function Search(Name: string; Exported: Boolean = False): TType;
|
|---|
| 182 | destructor Destroy; override;
|
|---|
| 183 | function AddNew: TType;
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | TTypeInherited = class(TType)
|
|---|
| 187 | end;
|
|---|
| 188 |
|
|---|
| 189 | TTypeRecord = class(TType)
|
|---|
| 190 | CommonBlock: TCommonBlock;
|
|---|
| 191 | constructor Create;
|
|---|
| 192 | destructor Destroy; override;
|
|---|
| 193 | end;
|
|---|
| 194 |
|
|---|
| 195 | TTypeSubRange = class(TType)
|
|---|
| 196 | Bottom: Variant;
|
|---|
| 197 | Top: Variant;
|
|---|
| 198 | end;
|
|---|
| 199 |
|
|---|
| 200 | TTypeArray = class(TType)
|
|---|
| 201 | IndexType: TType;
|
|---|
| 202 | ItemType: TType;
|
|---|
| 203 | end;
|
|---|
| 204 |
|
|---|
| 205 | TTypePointer = class(TType)
|
|---|
| 206 | end;
|
|---|
| 207 |
|
|---|
| 208 | TEnumItem = class
|
|---|
| 209 | Name: string;
|
|---|
| 210 | Index: Integer;
|
|---|
| 211 | end;
|
|---|
| 212 |
|
|---|
| 213 | TEnumItems = class(TObjectList<TEnumItem>);
|
|---|
| 214 |
|
|---|
| 215 | TTypeEnumeration = class(TType)
|
|---|
| 216 | Items: TEnumItems;
|
|---|
| 217 | constructor Create;
|
|---|
| 218 | destructor Destroy; override;
|
|---|
| 219 | end;
|
|---|
| 220 |
|
|---|
| 221 | { TTypeClass }
|
|---|
| 222 |
|
|---|
| 223 | TTypeClass = class(TType)
|
|---|
| 224 | CommonBlock: TCommonBlock;
|
|---|
| 225 | constructor Create;
|
|---|
| 226 | destructor Destroy; override;
|
|---|
| 227 | end;
|
|---|
| 228 |
|
|---|
| 229 | TConstant = class
|
|---|
| 230 | System: Boolean;
|
|---|
| 231 | Name: string;
|
|---|
| 232 | ValueType: TType;
|
|---|
| 233 | Value: TValue;
|
|---|
| 234 | Exported: Boolean;
|
|---|
| 235 | end;
|
|---|
| 236 |
|
|---|
| 237 | TConstants = class(TObjectList<TConstant>)
|
|---|
| 238 | Parent: TCommonBlock;
|
|---|
| 239 | function Search(Name: string): TConstant;
|
|---|
| 240 | destructor Destroy; override;
|
|---|
| 241 | end;
|
|---|
| 242 |
|
|---|
| 243 | TVariable = class
|
|---|
| 244 | Name: string;
|
|---|
| 245 | ValueType: TType;
|
|---|
| 246 | DefaultValue: TValue;
|
|---|
| 247 | Exported: Boolean;
|
|---|
| 248 | Value: TValue; // runtime
|
|---|
| 249 | end;
|
|---|
| 250 |
|
|---|
| 251 | TVariables = class(TObjectList<TVariable>)
|
|---|
| 252 | Parent: TCommonBlock;
|
|---|
| 253 | function Search(Name: string; Exported: Boolean = False): TVariable;
|
|---|
| 254 | destructor Destroy; override;
|
|---|
| 255 | end;
|
|---|
| 256 |
|
|---|
| 257 | TParameter = class(TVariable)
|
|---|
| 258 | end;
|
|---|
| 259 |
|
|---|
| 260 | TParameters = class(TObjectList<TParameter>)
|
|---|
| 261 | Parent: TFunction;
|
|---|
| 262 | function Search(Name: string): TParameter;
|
|---|
| 263 | destructor Destroy; override;
|
|---|
| 264 | end;
|
|---|
| 265 |
|
|---|
| 266 | { TExpression }
|
|---|
| 267 |
|
|---|
| 268 | TExpression = class
|
|---|
| 269 | CommonBlock: TCommonBlock;
|
|---|
| 270 | NodeType: TNodeType;
|
|---|
| 271 | Variable: TVariable;
|
|---|
| 272 | Constant: TConstant;
|
|---|
| 273 | UseType: TType;
|
|---|
| 274 | FunctionCall: TFunctionCall;
|
|---|
| 275 | Value: TValue;
|
|---|
| 276 | OperatorName: string;
|
|---|
| 277 | SubItems: TExpressions;
|
|---|
| 278 | Associated: Boolean;
|
|---|
| 279 | Braces: Boolean;
|
|---|
| 280 | constructor Create;
|
|---|
| 281 | destructor Destroy; override;
|
|---|
| 282 | procedure Assign(Source: TExpression);
|
|---|
| 283 | end;
|
|---|
| 284 |
|
|---|
| 285 | TFunctionType = (ftFunction, ftProcedure, ftConstructor, ftDestructor);
|
|---|
| 286 |
|
|---|
| 287 | TFunction = class(TCommonBlock)
|
|---|
| 288 | public
|
|---|
| 289 | Internal: Boolean;
|
|---|
| 290 | FunctionType: TFunctionType;
|
|---|
| 291 | Parameters: TParameters;
|
|---|
| 292 | ResultType: TType;
|
|---|
| 293 | Exported: Boolean;
|
|---|
| 294 | BodyLoaded: Boolean;
|
|---|
| 295 | constructor Create; override;
|
|---|
| 296 | destructor Destroy; override;
|
|---|
| 297 | end;
|
|---|
| 298 |
|
|---|
| 299 | { TFunctions }
|
|---|
| 300 |
|
|---|
| 301 | TFunctions = class(TObjectList<TFunction>)
|
|---|
| 302 | Parent: TCommonBlock;
|
|---|
| 303 | function Search(Name: string; Exported: Boolean = False): TFunction;
|
|---|
| 304 | destructor Destroy; override;
|
|---|
| 305 | function AddNew: TFunction;
|
|---|
| 306 | end;
|
|---|
| 307 |
|
|---|
| 308 | TUsedModule = class
|
|---|
| 309 | Name: string;
|
|---|
| 310 | Location: string;
|
|---|
| 311 | Module: TSourceModule;
|
|---|
| 312 | Exported: Boolean;
|
|---|
| 313 | end;
|
|---|
| 314 |
|
|---|
| 315 | TUsedModules = class(TObjectList<TUsedModule>)
|
|---|
| 316 | ParentModule: TSourceModule;
|
|---|
| 317 | end;
|
|---|
| 318 |
|
|---|
| 319 | { TSourceModule }
|
|---|
| 320 |
|
|---|
| 321 | TSourceModule = class
|
|---|
| 322 | public
|
|---|
| 323 | ParentProgram: TProgram;
|
|---|
| 324 | Name: string;
|
|---|
| 325 | TargetFile: string;
|
|---|
| 326 | UsedModules: TUsedModules;
|
|---|
| 327 | Body: TCommonBlock;
|
|---|
| 328 | Internal: Boolean;
|
|---|
| 329 | function SearchVariable(Name: string; Outside: Boolean): TVariable; virtual;
|
|---|
| 330 | function SearchConstant(Name: string; Outside: Boolean): TConstant; virtual;
|
|---|
| 331 | function SearchType(AName: string; Outside: Boolean): TType; virtual;
|
|---|
| 332 | function SearchFunction(Name: string; Outside: Boolean): TFunction; virtual;
|
|---|
| 333 | constructor Create;
|
|---|
| 334 | destructor Destroy; override;
|
|---|
| 335 | end;
|
|---|
| 336 |
|
|---|
| 337 | { TModuleProgram }
|
|---|
| 338 |
|
|---|
| 339 | TModuleProgram = class(TSourceModule)
|
|---|
| 340 | constructor Create;
|
|---|
| 341 | destructor Destroy; override;
|
|---|
| 342 | end;
|
|---|
| 343 |
|
|---|
| 344 | { TModuleUnit }
|
|---|
| 345 |
|
|---|
| 346 | TModuleUnit = class(TSourceModule)
|
|---|
| 347 | InititializeSection: TCommonBlock;
|
|---|
| 348 | FinalalizeSection: TCommonBlock;
|
|---|
| 349 | constructor Create;
|
|---|
| 350 | destructor Destroy; override;
|
|---|
| 351 | end;
|
|---|
| 352 |
|
|---|
| 353 | TModulePackage = class(TSourceModule)
|
|---|
| 354 |
|
|---|
| 355 | end;
|
|---|
| 356 |
|
|---|
| 357 | TModuleLibrary = class(TSourceModule)
|
|---|
| 358 | end;
|
|---|
| 359 |
|
|---|
| 360 | { TModules }
|
|---|
| 361 |
|
|---|
| 362 | TModules = class(TObjectList<TSourceModule>)
|
|---|
| 363 | function Search(Name: string): TSourceModule;
|
|---|
| 364 | end;
|
|---|
| 365 |
|
|---|
| 366 | { TProgram }
|
|---|
| 367 |
|
|---|
| 368 | TProgram = class(TSource)
|
|---|
| 369 | Device: TDevice;
|
|---|
| 370 | Modules: TModules;
|
|---|
| 371 | MainModule: TSourceModule;
|
|---|
| 372 | procedure Clear;
|
|---|
| 373 | constructor Create;
|
|---|
| 374 | destructor Destroy; override;
|
|---|
| 375 | end;
|
|---|
| 376 |
|
|---|
| 377 | const
|
|---|
| 378 | KeyWords: array[0..37] of string = ('program', 'unit', 'uses', 'begin', 'end',
|
|---|
| 379 | 'type', 'const', 'var', 'array', 'record', 'absolute', 'virtual', 'class',
|
|---|
| 380 | 'set', 'private', 'public', 'interface', 'implementation', 'finalization',
|
|---|
| 381 | 'initialization', 'for', 'while', 'if', 'case', 'of', 'pointer',
|
|---|
| 382 | 'object', 'packed', 'procedure', 'function', 'to', 'do', 'downto', 'repeat',
|
|---|
| 383 | 'until', 'then', 'asm', 'else');
|
|---|
| 384 | Operators: array[0..22] of string = ('@', 'not', '*', 'and', '/', 'shl',
|
|---|
| 385 | 'shr', 'as', 'div', 'mod', 'or', 'xor', '-', '+', '=', '>', '<', '<>', '<=',
|
|---|
| 386 | '>=', 'is', 'in', ':=');
|
|---|
| 387 |
|
|---|
| 388 | resourcestring
|
|---|
| 389 | SAssignmentError = 'Assignment error';
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 | implementation
|
|---|
| 393 |
|
|---|
| 394 | { TFunction }
|
|---|
| 395 |
|
|---|
| 396 | constructor TFunction.Create;
|
|---|
| 397 | begin
|
|---|
| 398 | inherited;
|
|---|
| 399 | Parameters := TParameters.Create;
|
|---|
| 400 | Parameters.Parent := Self;
|
|---|
| 401 | //ResultType := TType.Create;
|
|---|
| 402 | end;
|
|---|
| 403 |
|
|---|
| 404 | destructor TFunction.Destroy;
|
|---|
| 405 | begin
|
|---|
| 406 | FreeAndNil(Parameters);
|
|---|
| 407 | // FreeAndNil(ResultType);
|
|---|
| 408 | inherited;
|
|---|
| 409 | end;
|
|---|
| 410 |
|
|---|
| 411 | { TProgram }
|
|---|
| 412 |
|
|---|
| 413 | procedure TProgram.Clear;
|
|---|
| 414 | begin
|
|---|
| 415 | Modules.Clear;
|
|---|
| 416 | MainModule := nil;
|
|---|
| 417 | end;
|
|---|
| 418 |
|
|---|
| 419 | constructor TProgram.Create;
|
|---|
| 420 | begin
|
|---|
| 421 | Device := TDevice.Create;
|
|---|
| 422 | Modules := TModules.Create;
|
|---|
| 423 | end;
|
|---|
| 424 |
|
|---|
| 425 | destructor TProgram.Destroy;
|
|---|
| 426 | begin
|
|---|
| 427 | FreeAndNil(Modules);
|
|---|
| 428 | FreeAndNil(Device);
|
|---|
| 429 | end;
|
|---|
| 430 |
|
|---|
| 431 | { TConstants }
|
|---|
| 432 |
|
|---|
| 433 | destructor TConstants.Destroy;
|
|---|
| 434 | begin
|
|---|
| 435 | inherited;
|
|---|
| 436 | end;
|
|---|
| 437 |
|
|---|
| 438 | function TConstants.Search(Name: string): TConstant;
|
|---|
| 439 | var
|
|---|
| 440 | I: Integer;
|
|---|
| 441 | begin
|
|---|
| 442 | I := 0;
|
|---|
| 443 | while (I < Count) and (LowerCase(TConstant(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
|
|---|
| 444 | if I < Count then Result := TConstant(Items[I]) else begin
|
|---|
| 445 | if Assigned(Parent.Parent) then Result := Parent.Parent.Constants.Search(Name)
|
|---|
| 446 | else begin
|
|---|
| 447 | Result := nil;
|
|---|
| 448 | end;
|
|---|
| 449 | end;
|
|---|
| 450 | end;
|
|---|
| 451 |
|
|---|
| 452 | { TSourceModule }
|
|---|
| 453 |
|
|---|
| 454 | constructor TSourceModule.Create;
|
|---|
| 455 | begin
|
|---|
| 456 | inherited;
|
|---|
| 457 | UsedModules := TUsedModules.Create;
|
|---|
| 458 | UsedModules.ParentModule := Self;
|
|---|
| 459 | Body := TCommonBlock.Create;
|
|---|
| 460 | Body.ParentModule := Self;
|
|---|
| 461 | end;
|
|---|
| 462 |
|
|---|
| 463 | destructor TSourceModule.Destroy;
|
|---|
| 464 | begin
|
|---|
| 465 | FreeAndNil(Body);
|
|---|
| 466 | FreeAndNil(UsedModules);
|
|---|
| 467 | inherited;
|
|---|
| 468 | end;
|
|---|
| 469 |
|
|---|
| 470 | (*
|
|---|
| 471 | procedure TCommonBlock.CheckReferences;
|
|---|
| 472 | var
|
|---|
| 473 | I: Integer;
|
|---|
| 474 | begin
|
|---|
| 475 | for I := 0 to Operations.Count - 1 do
|
|---|
| 476 | with TOperation(Operations[I]) do begin
|
|---|
| 477 | if (Instruction = inJump) or (Instruction = inConditionalJump) then
|
|---|
| 478 | TOperation(Operations[GotoAddress]).Referenced := True;
|
|---|
| 479 | end;
|
|---|
| 480 | end;
|
|---|
| 481 | *)
|
|---|
| 482 |
|
|---|
| 483 | constructor TCommonBlock.Create;
|
|---|
| 484 | begin
|
|---|
| 485 | Constants := TConstants.Create;
|
|---|
| 486 | Constants.Parent := Self;
|
|---|
| 487 | Types := TTypes.Create;
|
|---|
| 488 | Types.Parent := Self;
|
|---|
| 489 | Variables := TVariables.Create;
|
|---|
| 490 | Variables.Parent := Self;
|
|---|
| 491 | Functions := TFunctions.Create;
|
|---|
| 492 | Functions.Parent := Self;
|
|---|
| 493 | Code := TBeginEnd.Create;
|
|---|
| 494 | Code.Parent := Self;
|
|---|
| 495 | Code.CommonBlock := Self;
|
|---|
| 496 | Order := TObjectList<TObject>.Create;
|
|---|
| 497 | Order.OwnsObjects := False;
|
|---|
| 498 | end;
|
|---|
| 499 |
|
|---|
| 500 | destructor TCommonBlock.Destroy;
|
|---|
| 501 | begin
|
|---|
| 502 | FreeAndNil(Constants);
|
|---|
| 503 | FreeAndNil(Types);
|
|---|
| 504 | FreeAndNil(Variables);
|
|---|
| 505 | FreeAndNil(Functions);
|
|---|
| 506 | FreeAndNil(Code);
|
|---|
| 507 | FreeAndNil(Order);
|
|---|
| 508 | inherited;
|
|---|
| 509 | end;
|
|---|
| 510 |
|
|---|
| 511 | { TTypes }
|
|---|
| 512 |
|
|---|
| 513 | destructor TTypes.Destroy;
|
|---|
| 514 | begin
|
|---|
| 515 | inherited;
|
|---|
| 516 | end;
|
|---|
| 517 |
|
|---|
| 518 | function TTypes.AddNew: TType;
|
|---|
| 519 | begin
|
|---|
| 520 | Result := TType.Create;
|
|---|
| 521 | Add(Result);
|
|---|
| 522 | end;
|
|---|
| 523 |
|
|---|
| 524 | function TTypes.Search(Name: string; Exported: Boolean = False): TType;
|
|---|
| 525 | var
|
|---|
| 526 | I: Integer;
|
|---|
| 527 | begin
|
|---|
| 528 | // Search in own list
|
|---|
| 529 | I := 0;
|
|---|
| 530 | while (I < Count) and (LowerCase(TType(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
|
|---|
| 531 | if I < Count then Result := TType(Items[I])
|
|---|
| 532 | else Result := nil;
|
|---|
| 533 |
|
|---|
| 534 | // Search in parent
|
|---|
| 535 | if not Assigned(Result) and Assigned(Parent.Parent) then
|
|---|
| 536 | Result := Parent.Parent.Types.Search(Name);
|
|---|
| 537 |
|
|---|
| 538 | // Search global variables
|
|---|
| 539 | if not Assigned(Result) and Assigned(Parent.ParentModule) and (not Exported) then begin
|
|---|
| 540 | Result := Parent.ParentModule.SearchType(Name, True);
|
|---|
| 541 | end;
|
|---|
| 542 | end;
|
|---|
| 543 |
|
|---|
| 544 | { TVariables }
|
|---|
| 545 |
|
|---|
| 546 | destructor TVariables.Destroy;
|
|---|
| 547 | begin
|
|---|
| 548 | inherited;
|
|---|
| 549 | end;
|
|---|
| 550 |
|
|---|
| 551 | function TVariables.Search(Name: string; Exported: Boolean = False): TVariable;
|
|---|
| 552 | var
|
|---|
| 553 | I: Integer;
|
|---|
| 554 | begin
|
|---|
| 555 | // Search in own list
|
|---|
| 556 | I := 0;
|
|---|
| 557 | while (I < Count) and (LowerCase(TVariable(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
|
|---|
| 558 | if I < Count then Result := TVariable(Items[I]) else Result := nil;
|
|---|
| 559 |
|
|---|
| 560 | if not Exported then begin
|
|---|
| 561 | // Search parent block variables
|
|---|
| 562 | if not Assigned(Result) then begin
|
|---|
| 563 | if Assigned(Parent.Parent) then
|
|---|
| 564 | Result := Parent.Parent.Variables.Search(Name)
|
|---|
| 565 | else Result := nil;
|
|---|
| 566 | end;
|
|---|
| 567 |
|
|---|
| 568 | // Search parent function parameters
|
|---|
| 569 | if not Assigned(Result) then begin
|
|---|
| 570 | if Assigned(Parent) and (Parent is TFunction) then
|
|---|
| 571 | Result := TFunction(Parent).Parameters.Search(Name)
|
|---|
| 572 | else Result := nil;
|
|---|
| 573 | end;
|
|---|
| 574 |
|
|---|
| 575 | if not Assigned(Result) then begin
|
|---|
| 576 | if Assigned(Parent.Parent) and (Parent.Parent is TFunction) then
|
|---|
| 577 | Result := TFunction(Parent.Parent).Parameters.Search(Name)
|
|---|
| 578 | else Result := nil;
|
|---|
| 579 | end;
|
|---|
| 580 |
|
|---|
| 581 | // Search global variables
|
|---|
| 582 | //if not Assigned(Result) then begin
|
|---|
| 583 | // if Assigned(Parent.ParentModule) then
|
|---|
| 584 | // Result := Parent.ParentModule.SearchVariable(Name, True);
|
|---|
| 585 | //end;
|
|---|
| 586 | end;
|
|---|
| 587 | end;
|
|---|
| 588 |
|
|---|
| 589 | { TFunctions }
|
|---|
| 590 |
|
|---|
| 591 | destructor TFunctions.Destroy;
|
|---|
| 592 | begin
|
|---|
| 593 | inherited;
|
|---|
| 594 | end;
|
|---|
| 595 |
|
|---|
| 596 | function TFunctions.AddNew: TFunction;
|
|---|
| 597 | begin
|
|---|
| 598 | Result := TFunction.Create;
|
|---|
| 599 | Add(Result);
|
|---|
| 600 | end;
|
|---|
| 601 |
|
|---|
| 602 | function TFunctions.Search(Name: string; Exported: Boolean): TFunction;
|
|---|
| 603 | var
|
|---|
| 604 | I: Integer;
|
|---|
| 605 | begin
|
|---|
| 606 | // Search in own list
|
|---|
| 607 | I := 0;
|
|---|
| 608 | while (I < Count) and (LowerCase(TFunction(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
|
|---|
| 609 | if I < Count then Result := TFunction(Items[I]) else Result := nil;
|
|---|
| 610 |
|
|---|
| 611 | // Search parent block list
|
|---|
| 612 | if not Assigned(Result) and Assigned(Parent.Parent) then
|
|---|
| 613 | Result := Parent.Parent.Functions.Search(Name, Exported);
|
|---|
| 614 |
|
|---|
| 615 | // Search global variables
|
|---|
| 616 | if not Assigned(Result) and Assigned(Parent.ParentModule) and (not Exported) then
|
|---|
| 617 | Result := Parent.ParentModule.SearchFunction(Name, True);
|
|---|
| 618 | end;
|
|---|
| 619 |
|
|---|
| 620 | { TExpression }
|
|---|
| 621 |
|
|---|
| 622 | constructor TExpression.Create;
|
|---|
| 623 | begin
|
|---|
| 624 | SubItems := TExpressions.Create;
|
|---|
| 625 | SubItems.Count := 2;
|
|---|
| 626 | SubItems.OwnsObjects := False;
|
|---|
| 627 | end;
|
|---|
| 628 |
|
|---|
| 629 | destructor TExpression.Destroy;
|
|---|
| 630 | begin
|
|---|
| 631 | FreeAndNil(SubItems);
|
|---|
| 632 | inherited;
|
|---|
| 633 | end;
|
|---|
| 634 |
|
|---|
| 635 | procedure TExpression.Assign(Source: TExpression);
|
|---|
| 636 | begin
|
|---|
| 637 | CommonBlock := Source.CommonBlock;
|
|---|
| 638 | NodeType := Source.NodeType;
|
|---|
| 639 | FunctionCall := Source.FunctionCall;
|
|---|
| 640 | Value := Source.Value;
|
|---|
| 641 | Associated := Source.Associated;
|
|---|
| 642 | OperatorName := Source.OperatorName;
|
|---|
| 643 | Variable := Source.Variable;
|
|---|
| 644 | SubItems.Assign(Source.SubItems);
|
|---|
| 645 | end;
|
|---|
| 646 |
|
|---|
| 647 | { TParameters }
|
|---|
| 648 |
|
|---|
| 649 | destructor TParameters.Destroy;
|
|---|
| 650 | begin
|
|---|
| 651 | inherited;
|
|---|
| 652 | end;
|
|---|
| 653 |
|
|---|
| 654 | function TParameters.Search(Name: string): TParameter;
|
|---|
| 655 | var
|
|---|
| 656 | I: Integer;
|
|---|
| 657 | begin
|
|---|
| 658 | I := 0;
|
|---|
| 659 | while (I < Count) and (LowerCase(TParameter(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
|
|---|
| 660 | if I < Count then Result := TParameter(Items[I])
|
|---|
| 661 | else Result := nil;
|
|---|
| 662 | end;
|
|---|
| 663 |
|
|---|
| 664 | { TBeginEnd }
|
|---|
| 665 |
|
|---|
| 666 | procedure TBeginEnd.Clear;
|
|---|
| 667 | begin
|
|---|
| 668 | end;
|
|---|
| 669 |
|
|---|
| 670 | constructor TBeginEnd.Create;
|
|---|
| 671 | begin
|
|---|
| 672 | inherited;
|
|---|
| 673 | Commands := TCommands.Create;
|
|---|
| 674 | end;
|
|---|
| 675 |
|
|---|
| 676 | destructor TBeginEnd.Destroy;
|
|---|
| 677 | begin
|
|---|
| 678 | FreeAndNil(Commands);
|
|---|
| 679 | inherited;
|
|---|
| 680 | end;
|
|---|
| 681 |
|
|---|
| 682 | { TAssignment }
|
|---|
| 683 |
|
|---|
| 684 | constructor TAssignment.Create;
|
|---|
| 685 | begin
|
|---|
| 686 | // Source := TExpression.Create;
|
|---|
| 687 | end;
|
|---|
| 688 |
|
|---|
| 689 | destructor TAssignment.Destroy;
|
|---|
| 690 | begin
|
|---|
| 691 | FreeAndNil(Source);
|
|---|
| 692 | inherited;
|
|---|
| 693 | end;
|
|---|
| 694 |
|
|---|
| 695 | { TExpressions }
|
|---|
| 696 |
|
|---|
| 697 | procedure TExpressions.Assign(Source: TExpressions);
|
|---|
| 698 | var
|
|---|
| 699 | I: Integer;
|
|---|
| 700 | begin
|
|---|
| 701 | while Count > Source.Count do Delete(Count - 1);
|
|---|
| 702 | while Count < Source.Count do Add(TExpression.Create);
|
|---|
| 703 | for I := 0 to Count - 1 do
|
|---|
| 704 | Items[I].Assign(Source[I]);
|
|---|
| 705 | end;
|
|---|
| 706 |
|
|---|
| 707 | { TWhileDo }
|
|---|
| 708 |
|
|---|
| 709 | constructor TWhileDo.Create;
|
|---|
| 710 | begin
|
|---|
| 711 | Condition := TExpression.Create;
|
|---|
| 712 | Command := TCommand.Create;
|
|---|
| 713 | end;
|
|---|
| 714 |
|
|---|
| 715 | destructor TWhileDo.Destroy;
|
|---|
| 716 | begin
|
|---|
| 717 | FreeAndNil(Condition);
|
|---|
| 718 | FreeAndNil(Command);
|
|---|
| 719 | inherited;
|
|---|
| 720 | end;
|
|---|
| 721 |
|
|---|
| 722 | { TCaseOfEnd }
|
|---|
| 723 |
|
|---|
| 724 | constructor TCaseOfEnd.Create;
|
|---|
| 725 | begin
|
|---|
| 726 | inherited;
|
|---|
| 727 | Branches := TListCaseOfEndBranche.Create
|
|---|
| 728 | end;
|
|---|
| 729 |
|
|---|
| 730 | destructor TCaseOfEnd.Destroy;
|
|---|
| 731 | begin
|
|---|
| 732 | FreeAndNil(Branches);
|
|---|
| 733 | inherited;
|
|---|
| 734 | end;
|
|---|
| 735 |
|
|---|
| 736 | { TIfThenElse }
|
|---|
| 737 |
|
|---|
| 738 | constructor TIfThenElse.Create;
|
|---|
| 739 | begin
|
|---|
| 740 | Condition := TExpression.Create;
|
|---|
| 741 | end;
|
|---|
| 742 |
|
|---|
| 743 | destructor TIfThenElse.Destroy;
|
|---|
| 744 | begin
|
|---|
| 745 | FreeAndNil(Condition);
|
|---|
| 746 | inherited;
|
|---|
| 747 | end;
|
|---|
| 748 |
|
|---|
| 749 | { TFunctionCall }
|
|---|
| 750 |
|
|---|
| 751 | constructor TFunctionCall.Create;
|
|---|
| 752 | begin
|
|---|
| 753 | inherited;
|
|---|
| 754 | ParameterExpression := TExpressions.Create;
|
|---|
| 755 | end;
|
|---|
| 756 |
|
|---|
| 757 | destructor TFunctionCall.Destroy;
|
|---|
| 758 | begin
|
|---|
| 759 | FreeAndNil(ParameterExpression);
|
|---|
| 760 | inherited;
|
|---|
| 761 | end;
|
|---|
| 762 |
|
|---|
| 763 | { TForToDo }
|
|---|
| 764 |
|
|---|
| 765 | constructor TForToDo.Create;
|
|---|
| 766 | begin
|
|---|
| 767 | inherited;
|
|---|
| 768 | Start := TExpression.Create;
|
|---|
| 769 | Stop := TExpression.Create;
|
|---|
| 770 | end;
|
|---|
| 771 |
|
|---|
| 772 | destructor TForToDo.Destroy;
|
|---|
| 773 | begin
|
|---|
| 774 | FreeAndNil(Start);
|
|---|
| 775 | FreeAndNil(Stop);
|
|---|
| 776 | inherited;
|
|---|
| 777 | end;
|
|---|
| 778 |
|
|---|
| 779 | { TTypeRecord }
|
|---|
| 780 |
|
|---|
| 781 | constructor TTypeRecord.Create;
|
|---|
| 782 | begin
|
|---|
| 783 | inherited;
|
|---|
| 784 | CommonBlock := TCommonBlock.Create;
|
|---|
| 785 | end;
|
|---|
| 786 |
|
|---|
| 787 | destructor TTypeRecord.Destroy;
|
|---|
| 788 | begin
|
|---|
| 789 | FreeAndNil(CommonBlock);
|
|---|
| 790 | inherited;
|
|---|
| 791 | end;
|
|---|
| 792 |
|
|---|
| 793 | { TModules }
|
|---|
| 794 |
|
|---|
| 795 | function TModules.Search(Name: string): TSourceModule;
|
|---|
| 796 | var
|
|---|
| 797 | I: Integer;
|
|---|
| 798 | begin
|
|---|
| 799 | I := 0;
|
|---|
| 800 | while (I < Count) and (LowerCase(TSourceModule(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
|
|---|
| 801 | if I < Count then Result := TSourceModule(Items[I])
|
|---|
| 802 | else Result := nil;
|
|---|
| 803 | end;
|
|---|
| 804 |
|
|---|
| 805 | { TModuleProgram }
|
|---|
| 806 |
|
|---|
| 807 | function TSourceModule.SearchVariable(Name: string; Outside: Boolean): TVariable;
|
|---|
| 808 | var
|
|---|
| 809 | I: Integer;
|
|---|
| 810 | begin
|
|---|
| 811 | Result := Body.Variables.Search(Name, Outside);
|
|---|
| 812 |
|
|---|
| 813 | if not Assigned(Result) and Outside then begin
|
|---|
| 814 | I := 0;
|
|---|
| 815 | while (I < UsedModules.Count) and (not Assigned(Result)) do begin
|
|---|
| 816 | Result := TUsedModule(UsedModules[I]).Module.SearchVariable(Name, False);
|
|---|
| 817 | Inc(I);
|
|---|
| 818 | end;
|
|---|
| 819 | end;
|
|---|
| 820 | end;
|
|---|
| 821 |
|
|---|
| 822 | function TSourceModule.SearchConstant(Name: string; Outside: Boolean): TConstant;
|
|---|
| 823 | begin
|
|---|
| 824 | end;
|
|---|
| 825 |
|
|---|
| 826 | function TSourceModule.SearchType(AName: string; Outside: Boolean): TType;
|
|---|
| 827 | var
|
|---|
| 828 | I: Integer;
|
|---|
| 829 | begin
|
|---|
| 830 | Result := Body.Types.Search(AName, Outside);
|
|---|
| 831 |
|
|---|
| 832 | if not Assigned(Result) and Outside then begin
|
|---|
| 833 | I := 0;
|
|---|
| 834 | while (I < UsedModules.Count) and (not Assigned(Result)) do begin
|
|---|
| 835 | with TUsedModule(UsedModules[I]) do
|
|---|
| 836 | if Assigned(Module) then
|
|---|
| 837 | with Module do
|
|---|
| 838 | Result := SearchType(AName, False);
|
|---|
| 839 | Inc(I);
|
|---|
| 840 | end;
|
|---|
| 841 | end;
|
|---|
| 842 | end;
|
|---|
| 843 |
|
|---|
| 844 | function TSourceModule.SearchFunction(Name: string; Outside: Boolean): TFunction;
|
|---|
| 845 | var
|
|---|
| 846 | I: Integer;
|
|---|
| 847 | begin
|
|---|
| 848 | Result := Body.Functions.Search(Name, Outside);
|
|---|
| 849 |
|
|---|
| 850 | if not Assigned(Result) and Outside then begin
|
|---|
| 851 | I := 0;
|
|---|
| 852 | while (I < UsedModules.Count) and (not Assigned(Result)) do begin
|
|---|
| 853 | if Assigned(TUsedModule(UsedModules[I]).Module) then
|
|---|
| 854 | Result := TUsedModule(UsedModules[I]).Module.SearchFunction(Name, False);
|
|---|
| 855 | Inc(I);
|
|---|
| 856 | end;
|
|---|
| 857 | end;
|
|---|
| 858 | end;
|
|---|
| 859 |
|
|---|
| 860 | constructor TModuleProgram.Create;
|
|---|
| 861 | begin
|
|---|
| 862 | inherited;
|
|---|
| 863 | end;
|
|---|
| 864 |
|
|---|
| 865 | destructor TModuleProgram.Destroy;
|
|---|
| 866 | begin
|
|---|
| 867 | inherited;
|
|---|
| 868 | end;
|
|---|
| 869 |
|
|---|
| 870 | { TModuleUnit }
|
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 | constructor TModuleUnit.Create;
|
|---|
| 874 | begin
|
|---|
| 875 | inherited;
|
|---|
| 876 | InititializeSection := TCommonBlock.Create;
|
|---|
| 877 | InititializeSection.ParentModule := Self;
|
|---|
| 878 | FinalalizeSection := TCommonBlock.Create;
|
|---|
| 879 | FinalalizeSection.ParentModule := Self;
|
|---|
| 880 | end;
|
|---|
| 881 |
|
|---|
| 882 | destructor TModuleUnit.Destroy;
|
|---|
| 883 | begin
|
|---|
| 884 | FreeAndNil(InititializeSection);
|
|---|
| 885 | FreeAndNil(FinalalizeSection);
|
|---|
| 886 | inherited;
|
|---|
| 887 | end;
|
|---|
| 888 |
|
|---|
| 889 | { TTypeEnumeration }
|
|---|
| 890 |
|
|---|
| 891 | constructor TTypeEnumeration.Create;
|
|---|
| 892 | begin
|
|---|
| 893 | inherited;
|
|---|
| 894 | Items := TEnumItems.Create;
|
|---|
| 895 | end;
|
|---|
| 896 |
|
|---|
| 897 | destructor TTypeEnumeration.Destroy;
|
|---|
| 898 | begin
|
|---|
| 899 | FreeAndNil(Items);
|
|---|
| 900 | inherited;
|
|---|
| 901 | end;
|
|---|
| 902 |
|
|---|
| 903 | { TTypeClass }
|
|---|
| 904 |
|
|---|
| 905 | constructor TTypeClass.Create;
|
|---|
| 906 | begin
|
|---|
| 907 | inherited;
|
|---|
| 908 | CommonBlock := TCommonBlock.Create;
|
|---|
| 909 | end;
|
|---|
| 910 |
|
|---|
| 911 | destructor TTypeClass.Destroy;
|
|---|
| 912 | begin
|
|---|
| 913 | FreeAndNil(CommonBlock);
|
|---|
| 914 | inherited;
|
|---|
| 915 | end;
|
|---|
| 916 |
|
|---|
| 917 | { TType }
|
|---|
| 918 |
|
|---|
| 919 | procedure TType.Assign(Source: TType);
|
|---|
| 920 | var
|
|---|
| 921 | I: Integer;
|
|---|
| 922 | NewType: TType;
|
|---|
| 923 | begin
|
|---|
| 924 | if Assigned(Source) then begin
|
|---|
| 925 | Name := Source.Name;
|
|---|
| 926 | Parameters.Clear;
|
|---|
| 927 | for I := 0 to Source.Parameters.Count - 1 do begin
|
|---|
| 928 | NewType := TType.Create;
|
|---|
| 929 | //NewType := TType(Source.Parameters[I].ClassType.Create);
|
|---|
| 930 | NewType.Assign(TType(Source.Parameters[I]));
|
|---|
| 931 | Parameters.Add(NewType);
|
|---|
| 932 | end;
|
|---|
| 933 | Visibility := Source.Visibility;
|
|---|
| 934 | Parent := Source.Parent;
|
|---|
| 935 | Internal := Source.Internal;
|
|---|
| 936 | ForwardDeclared := Source.ForwardDeclared;
|
|---|
| 937 | Exported := Source.Exported;
|
|---|
| 938 | Size := Source.Size;
|
|---|
| 939 | UsedType := Source.UsedType;
|
|---|
| 940 | end else raise Exception.Create(SAssignmentError);
|
|---|
| 941 | end;
|
|---|
| 942 |
|
|---|
| 943 | constructor TType.Create;
|
|---|
| 944 | begin
|
|---|
| 945 | Parameters := TTypes.Create;
|
|---|
| 946 | //Parameters.Parent := Parent.Parent;
|
|---|
| 947 | end;
|
|---|
| 948 |
|
|---|
| 949 | destructor TType.Destroy;
|
|---|
| 950 | begin
|
|---|
| 951 | FreeAndNil(Parameters);
|
|---|
| 952 | inherited;
|
|---|
| 953 | end;
|
|---|
| 954 |
|
|---|
| 955 | end.
|
|---|
| 956 |
|
|---|