Changeset 5 for branches/Void
- Timestamp:
- Nov 9, 2009, 8:34:09 AM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 5 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/Example.void
r4 r5 1 Define Text 1 2 Write Leave me 3 Write Heal the world 2 4 Assign Text Hell 3 5 Pause -
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; -
branches/Void/project1.lpi
r4 r5 37 37 <IsPartOfProject Value="True"/> 38 38 <UnitName Value="project1"/> 39 <CursorPos X=" 1" Y="65"/>40 <TopLine Value=" 36"/>39 <CursorPos X="3" Y="45"/> 40 <TopLine Value="26"/> 41 41 <EditorIndex Value="0"/> 42 <UsageCount Value="2 1"/>42 <UsageCount Value="25"/> 43 43 <Loaded Value="True"/> 44 44 </Unit0> … … 47 47 <IsPartOfProject Value="True"/> 48 48 <UnitName Value="UCompilator"/> 49 <CursorPos X=" 6" Y="59"/>50 <TopLine Value=" 47"/>49 <CursorPos X="58" Y="124"/> 50 <TopLine Value="103"/> 51 51 <EditorIndex Value="1"/> 52 <UsageCount Value="2 1"/>52 <UsageCount Value="25"/> 53 53 <Loaded Value="True"/> 54 54 </Unit1> … … 56 56 <Filename Value="Example.void"/> 57 57 <IsPartOfProject Value="True"/> 58 <CursorPos X=" 17" Y="2"/>58 <CursorPos X="24" Y="4"/> 59 59 <TopLine Value="1"/> 60 60 <EditorIndex Value="2"/> 61 <UsageCount Value="2 1"/>61 <UsageCount Value="25"/> 62 62 <Loaded Value="True"/> 63 63 <SyntaxHighlighter Value="None"/> … … 69 69 <TopLine Value="1"/> 70 70 <EditorIndex Value="3"/> 71 <UsageCount Value="2 0"/>71 <UsageCount Value="24"/> 72 72 <Loaded Value="True"/> 73 73 <SyntaxHighlighter Value="None"/> 74 74 </Unit3> 75 75 </Units> 76 <JumpHistory Count=" 15" HistoryIndex="14">76 <JumpHistory Count="30" HistoryIndex="29"> 77 77 <Position1> 78 78 <Filename Value="UCompilator.pas"/> 79 <Caret Line=" 15" Column="41" TopLine="1"/>79 <Caret Line="51" Column="68" TopLine="36"/> 80 80 </Position1> 81 81 <Position2> 82 82 <Filename Value="UCompilator.pas"/> 83 <Caret Line=" 14" Column="1" TopLine="1"/>83 <Caret Line="52" Column="52" TopLine="37"/> 84 84 </Position2> 85 85 <Position3> 86 86 <Filename Value="UCompilator.pas"/> 87 <Caret Line="2 4" Column="1" TopLine="1"/>87 <Caret Line="20" Column="24" TopLine="9"/> 88 88 </Position3> 89 89 <Position4> 90 90 <Filename Value="UCompilator.pas"/> 91 <Caret Line=" 28" Column="20" TopLine="2"/>91 <Caret Line="57" Column="41" TopLine="42"/> 92 92 </Position4> 93 93 <Position5> 94 94 <Filename Value="UCompilator.pas"/> 95 <Caret Line=" 32" Column="1" TopLine="2"/>95 <Caret Line="85" Column="1" TopLine="55"/> 96 96 </Position5> 97 97 <Position6> 98 <Filename Value=" UCompilator.pas"/>99 <Caret Line=" 16" Column="15" TopLine="1"/>98 <Filename Value="project1.lpr"/> 99 <Caret Line="38" Column="5" TopLine="36"/> 100 100 </Position6> 101 101 <Position7> 102 102 <Filename Value="UCompilator.pas"/> 103 <Caret Line=" 18" Column="1" TopLine="2"/>103 <Caret Line="28" Column="34" TopLine="10"/> 104 104 </Position7> 105 105 <Position8> 106 106 <Filename Value="UCompilator.pas"/> 107 <Caret Line=" 55" Column="5" TopLine="26"/>107 <Caret Line="86" Column="21" TopLine="70"/> 108 108 </Position8> 109 109 <Position9> 110 110 <Filename Value="UCompilator.pas"/> 111 <Caret Line=" 37" Column="1" TopLine="22"/>111 <Caret Line="11" Column="30" TopLine="3"/> 112 112 </Position9> 113 113 <Position10> 114 114 <Filename Value="UCompilator.pas"/> 115 <Caret Line=" 51" Column="68" TopLine="36"/>115 <Caret Line="24" Column="59" TopLine="10"/> 116 116 </Position10> 117 117 <Position11> 118 118 <Filename Value="UCompilator.pas"/> 119 <Caret Line=" 52" Column="52" TopLine="37"/>119 <Caret Line="41" Column="35" TopLine="39"/> 120 120 </Position11> 121 121 <Position12> 122 122 <Filename Value="UCompilator.pas"/> 123 <Caret Line=" 20" Column="24" TopLine="9"/>123 <Caret Line="81" Column="9" TopLine="52"/> 124 124 </Position12> 125 125 <Position13> 126 126 <Filename Value="UCompilator.pas"/> 127 <Caret Line=" 57" Column="41" TopLine="42"/>127 <Caret Line="24" Column="35" TopLine="9"/> 128 128 </Position13> 129 129 <Position14> 130 130 <Filename Value="UCompilator.pas"/> 131 <Caret Line=" 85" Column="1" TopLine="55"/>131 <Caret Line="44" Column="57" TopLine="60"/> 132 132 </Position14> 133 133 <Position15> 134 <Filename Value=" project1.lpr"/>135 <Caret Line=" 38" Column="5" TopLine="36"/>134 <Filename Value="UCompilator.pas"/> 135 <Caret Line="25" Column="1" TopLine="13"/> 136 136 </Position15> 137 <Position16> 138 <Filename Value="UCompilator.pas"/> 139 <Caret Line="48" Column="55" TopLine="48"/> 140 </Position16> 141 <Position17> 142 <Filename Value="UCompilator.pas"/> 143 <Caret Line="89" Column="22" TopLine="70"/> 144 </Position17> 145 <Position18> 146 <Filename Value="UCompilator.pas"/> 147 <Caret Line="19" Column="9" TopLine="1"/> 148 </Position18> 149 <Position19> 150 <Filename Value="UCompilator.pas"/> 151 <Caret Line="89" Column="22" TopLine="74"/> 152 </Position19> 153 <Position20> 154 <Filename Value="UCompilator.pas"/> 155 <Caret Line="100" Column="24" TopLine="88"/> 156 </Position20> 157 <Position21> 158 <Filename Value="UCompilator.pas"/> 159 <Caret Line="114" Column="23" TopLine="99"/> 160 </Position21> 161 <Position22> 162 <Filename Value="project1.lpr"/> 163 <Caret Line="18" Column="15" TopLine="1"/> 164 </Position22> 165 <Position23> 166 <Filename Value="project1.lpr"/> 167 <Caret Line="48" Column="52" TopLine="45"/> 168 </Position23> 169 <Position24> 170 <Filename Value="project1.lpr"/> 171 <Caret Line="44" Column="7" TopLine="30"/> 172 </Position24> 173 <Position25> 174 <Filename Value="project1.lpr"/> 175 <Caret Line="1" Column="5" TopLine="1"/> 176 </Position25> 177 <Position26> 178 <Filename Value="project1.lpr"/> 179 <Caret Line="40" Column="27" TopLine="26"/> 180 </Position26> 181 <Position27> 182 <Filename Value="project1.lpr"/> 183 <Caret Line="18" Column="49" TopLine="1"/> 184 </Position27> 185 <Position28> 186 <Filename Value="project1.lpr"/> 187 <Caret Line="51" Column="58" TopLine="50"/> 188 </Position28> 189 <Position29> 190 <Filename Value="project1.lpr"/> 191 <Caret Line="41" Column="16" TopLine="26"/> 192 </Position29> 193 <Position30> 194 <Filename Value="UCompilator.pas"/> 195 <Caret Line="11" Column="77" TopLine="1"/> 196 </Position30> 137 197 </JumpHistory> 138 198 </ProjectOptions> -
branches/Void/project1.lpr
r3 r5 10 10 11 11 type 12 13 { TApplication } 14 12 15 TApplication = class(TCustomApplication) 13 16 protected 14 17 procedure DoRun; override; 18 procedure CompilatorError(Text: string; var Terminate: Boolean); 15 19 public 16 20 Compilator: TCompilator; … … 34 38 35 39 Compilator := TCompilator.Create; 36 Compilator.Compile('Example.void'); 37 Compilator.Destroy; 38 //ReadLn; 40 with Compilator do begin 41 OnError := CompilatorError; 42 Compile('Example.void'); 43 Destroy; 44 end; 45 ReadLn; 39 46 40 47 // stop program loop 41 48 Terminate; 49 end; 50 51 procedure TApplication.CompilatorError(Text: string; var Terminate: Boolean); 52 begin 53 Terminate := False; 54 WriteLn('Error: ' + Text); 42 55 end; 43 56 … … 46 59 inherited Create(TheOwner); 47 60 StopOnException := True; 48 end; 61 62 end; 49 63 50 64 destructor TApplication.Destroy;
Note:
See TracChangeset
for help on using the changeset viewer.