Changeset 5 for branches/Void/UCompilator.pas
- Timestamp:
- Nov 9, 2009, 8:34:09 AM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void
- Property svn:ignore
-
old new 5 5 Example.void.bdsproj 6 6 project1.exe 7 *.dsk
-
- Property svn:ignore
-
branches/Void/UCompilator.pas
r4 r5 9 9 10 10 type 11 TOnErrorEvent = procedure (Text: string; var Terminate: Boolean) of object; 11 12 12 { TCompilator } 13 TVariable = class 14 Name: string; 15 VarType: string; 16 end; 17 18 TCommand = class 19 Text: string; 20 end; 13 21 14 22 TCompilator = class 23 private 24 FOnError: TOnErrorEvent; 25 Variables: TList; 26 Commands: TList; 27 procedure DoError(Text: string); 28 function FindVariableByName(AName: string): TVariable; 29 public 15 30 Output: TStringList; 16 31 procedure Compile(FileName: string); … … 19 34 destructor Destroy; override; 20 35 function Parse(var Text: string; Separator: string = ' '): string; 21 end; 36 property OnError: TOnErrorEvent read FOnError write FOnError; 37 end; 22 38 23 39 implementation 24 40 25 41 { TCompilator } 42 43 procedure TCompilator.DoError(Text: string); 44 var 45 Terminate: Boolean; 46 begin 47 Terminate := False; 48 if Assigned(FOnError) then FOnError(Text, Terminate); 49 if Terminate then raise Exception.Create('Compilation terminated'); 50 end; 51 52 function TCompilator.FindVariableByName(AName: string): TVariable; 53 var 54 I: Integer; 55 begin 56 I := 0; 57 while (I < Variables.Count) and (TVariable(Variables[I]).Name <> AName) do 58 Inc(I); 59 if I < Variables.Count then Result := Variables[I] else Result := nil; 60 end; 26 61 27 62 procedure TCompilator.Compile(FileName: string); … … 33 68 SourceCode.LoadFromFile(FileName); 34 69 70 // Process source lines 71 for I := 0 to SourceCode.Count - 1 do begin 72 ProcessLine(SourceCode[I]); 73 end; 74 35 75 // Prepare output 36 76 Output.Clear; 37 77 Output.Add('program ' + ExtractFileName(FileName) + ';'); 38 78 Output.Add('{$APPTYPE CONSOLE}'); 79 80 // var section 81 if Variables.Count > 0 then Output.Add('var'); 82 for I := 0 to Variables.Count - 1 do 83 with TVariable(Variables[I]) do 84 Output.Add(' ' + Name + ': ' + VarType + ';'); 85 86 // Code block 39 87 Output.Add('begin'); 40 41 for I := 0 to SourceCode.Count - 1 do begin 42 ProcessLine(SourceCode[I]); 43 end; 44 SourceCode.Destroy; 45 46 // Finalize output code 88 for I := 0 to Commands.Count - 1 do 89 with TCommand(Commands[I]) do 90 Output.Add(Text); 47 91 Output.Add('end.'); 48 92 Output.SaveToFile(ExtractFileName(FileName) + '.dpr'); 93 SourceCode.Destroy; 49 94 end; 50 95 … … 52 97 var 53 98 Command: string; 54 begin 99 Variable: TVariable; 100 VariableName: string; 101 begin 55 102 Command := Parse(Line); 56 103 WriteLn(Command); 57 if Command = 'Write' then Output.Add('WriteLn(''' + Line + ''');') 58 else if Command = 'Assign' then begin 59 Output.Add('// Define' + Line) 60 end else if Command = 'Pause' then Output.Add('ReadLn;') 61 else if Command = 'Exit' then Output.Add('Exit;') 62 else begin 63 Output.Add('// Unknown command: ' + Command); 64 end; 104 if Command = 'Write' then 105 with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 106 Text := 'WriteLn(''' + Line + ''');'; 107 end else if Command = 'Define' then begin 108 VariableName := Parse(Line); 109 Variable := FindVariableByName(VariableName); 110 if Assigned(Variable) then DoError('Variable ' + VariableName + ' redefined') 111 else begin 112 Variable := TVariable.Create; 113 with Variable do begin 114 Name := VariableName; 115 VarType := 'string'; 116 end; 117 Variables.Add(Variable); 118 end; 119 end else if Command = 'Assign' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 120 VariableName := Parse(Line); 121 Variable := FindVariableByName(VariableName); 122 if not Assigned(Variable) then DoError('Undefined variable ' + VariableName) 123 else begin 124 Text := VariableName + ' := ''' + Parse(Line) + ''';'; 125 end; 126 end else if Command = 'Pause' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 127 Text := 'ReadLn;'; 128 end else if Command = 'Exit' then with TCommand(Commands[Commands.Add(TCommand.Create)]) do begin 129 Text := 'Exit;'; 130 end else DoError('Unknown command: ' + Command); 65 131 end; 66 132 … … 68 134 begin 69 135 Output := TStringList.Create; 136 Variables := TList.Create; 137 Commands := TList.Create; 70 138 end; 71 139 72 140 destructor TCompilator.Destroy; 141 var 142 I: Integer; 73 143 begin 74 144 Output.Destroy; 145 for I := 0 to Variables.Count - 1 do 146 TVariable(Variables[I]).Destroy; 147 Variables.Destroy; 148 for I := 0 to Commands.Count - 1 do 149 TCommand(Commands[I]).Destroy; 150 Commands.Destroy; 75 151 inherited Destroy; 76 152 end;
Note:
See TracChangeset
for help on using the changeset viewer.