Changeset 27
- Timestamp:
- Oct 29, 2009, 11:07:49 AM (15 years ago)
- Location:
- branches/Syntetizer
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Syntetizer/Syntetizer.lpi
r26 r27 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 0"/>11 <ActiveEditorIndexAtStart Value="2"/> 12 12 </General> 13 13 <VersionInfo> … … 33 33 </Item1> 34 34 </RequiredPackages> 35 <Units Count=" 3">35 <Units Count="4"> 36 36 <Unit0> 37 37 <Filename Value="Syntetizer.lpr"/> … … 50 50 <ResourceBaseClass Value="Form"/> 51 51 <UnitName Value="UMainForm"/> 52 <CursorPos X=" 30" Y="42"/>53 <TopLine Value=" 28"/>52 <CursorPos X="18" Y="53"/> 53 <TopLine Value="40"/> 54 54 <EditorIndex Value="0"/> 55 55 <UsageCount Value="20"/> … … 60 60 <IsPartOfProject Value="True"/> 61 61 <UnitName Value="USyntetizer"/> 62 <CursorPos X=" 32" Y="80"/>63 <TopLine Value=" 60"/>62 <CursorPos X="50" Y="227"/> 63 <TopLine Value="211"/> 64 64 <EditorIndex Value="2"/> 65 65 <UsageCount Value="20"/> 66 66 <Loaded Value="True"/> 67 67 </Unit2> 68 <Unit3> 69 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.28\fpc\2.2.4\source\rtl\objpas\classes\classesh.inc"/> 70 <CursorPos X="3" Y="234"/> 71 <TopLine Value="218"/> 72 <UsageCount Value="10"/> 73 </Unit3> 68 74 </Units> 69 <JumpHistory Count=" 14" HistoryIndex="13">75 <JumpHistory Count="30" HistoryIndex="29"> 70 76 <Position1> 71 77 <Filename Value="USyntetizer.pas"/> 72 <Caret Line=" 15" Column="34" TopLine="1"/>78 <Caret Line="57" Column="65" TopLine="42"/> 73 79 </Position1> 74 80 <Position2> 75 81 <Filename Value="USyntetizer.pas"/> 76 <Caret Line=" 22" Column="15" TopLine="1"/>82 <Caret Line="63" Column="39" TopLine="54"/> 77 83 </Position2> 78 84 <Position3> 79 85 <Filename Value="USyntetizer.pas"/> 80 <Caret Line="1 3" Column="28" TopLine="1"/>86 <Caret Line="16" Column="1" TopLine="1"/> 81 87 </Position3> 82 88 <Position4> 83 89 <Filename Value="USyntetizer.pas"/> 84 <Caret Line=" 33" Column="34" TopLine="9"/>90 <Caret Line="100" Column="14" TopLine="84"/> 85 91 </Position4> 86 92 <Position5> 87 93 <Filename Value="USyntetizer.pas"/> 88 <Caret Line=" 43" Column="3" TopLine="28"/>94 <Caret Line="76" Column="19" TopLine="60"/> 89 95 </Position5> 90 96 <Position6> 91 <Filename Value="U MainForm.pas"/>92 <Caret Line=" 20" Column="29" TopLine="1"/>97 <Filename Value="USyntetizer.pas"/> 98 <Caret Line="74" Column="37" TopLine="52"/> 93 99 </Position6> 94 100 <Position7> 95 <Filename Value="U MainForm.pas"/>96 <Caret Line=" 33" Column="36" TopLine="14"/>101 <Filename Value="USyntetizer.pas"/> 102 <Caret Line="114" Column="1" TopLine="100"/> 97 103 </Position7> 98 104 <Position8> 99 <Filename Value="U MainForm.pas"/>100 <Caret Line=" 35" Column="81" TopLine="13"/>105 <Filename Value="USyntetizer.pas"/> 106 <Caret Line="154" Column="13" TopLine="138"/> 101 107 </Position8> 102 108 <Position9> 103 109 <Filename Value="USyntetizer.pas"/> 104 <Caret Line="2 9" Column="25" TopLine="1"/>110 <Caret Line="26" Column="43" TopLine="16"/> 105 111 </Position9> 106 112 <Position10> 107 113 <Filename Value="USyntetizer.pas"/> 108 <Caret Line="2 6" Column="34" TopLine="9"/>114 <Caret Line="29" Column="1" TopLine="11"/> 109 115 </Position10> 110 116 <Position11> 111 <Filename Value="U MainForm.pas"/>112 <Caret Line=" 49" Column="21" TopLine="28"/>117 <Filename Value="USyntetizer.pas"/> 118 <Caret Line="227" Column="6" TopLine="200"/> 113 119 </Position11> 114 120 <Position12> 115 <Filename Value="U MainForm.pas"/>116 <Caret Line=" 46" Column="35" TopLine="28"/>121 <Filename Value="USyntetizer.pas"/> 122 <Caret Line="218" Column="22" TopLine="200"/> 117 123 </Position12> 118 124 <Position13> 119 125 <Filename Value="USyntetizer.pas"/> 120 <Caret Line=" 36" Column="34" TopLine="16"/>126 <Caret Line="58" Column="3" TopLine="42"/> 121 127 </Position13> 122 128 <Position14> 123 129 <Filename Value="USyntetizer.pas"/> 124 <Caret Line=" 91" Column="16" TopLine="84"/>130 <Caret Line="220" Column="26" TopLine="200"/> 125 131 </Position14> 132 <Position15> 133 <Filename Value="USyntetizer.pas"/> 134 <Caret Line="221" Column="12" TopLine="200"/> 135 </Position15> 136 <Position16> 137 <Filename Value="USyntetizer.pas"/> 138 <Caret Line="65" Column="1" TopLine="39"/> 139 </Position16> 140 <Position17> 141 <Filename Value="USyntetizer.pas"/> 142 <Caret Line="239" Column="3" TopLine="222"/> 143 </Position17> 144 <Position18> 145 <Filename Value="USyntetizer.pas"/> 146 <Caret Line="62" Column="28" TopLine="46"/> 147 </Position18> 148 <Position19> 149 <Filename Value="USyntetizer.pas"/> 150 <Caret Line="245" Column="24" TopLine="222"/> 151 </Position19> 152 <Position20> 153 <Filename Value="USyntetizer.pas"/> 154 <Caret Line="225" Column="3" TopLine="223"/> 155 </Position20> 156 <Position21> 157 <Filename Value="UMainForm.pas"/> 158 <Caret Line="53" Column="18" TopLine="40"/> 159 </Position21> 160 <Position22> 161 <Filename Value="USyntetizer.pas"/> 162 <Caret Line="72" Column="28" TopLine="57"/> 163 </Position22> 164 <Position23> 165 <Filename Value="USyntetizer.pas"/> 166 <Caret Line="35" Column="20" TopLine="21"/> 167 </Position23> 168 <Position24> 169 <Filename Value="USyntetizer.pas"/> 170 <Caret Line="73" Column="16" TopLine="62"/> 171 </Position24> 172 <Position25> 173 <Filename Value="USyntetizer.pas"/> 174 <Caret Line="49" Column="32" TopLine="32"/> 175 </Position25> 176 <Position26> 177 <Filename Value="USyntetizer.pas"/> 178 <Caret Line="17" Column="34" TopLine="7"/> 179 </Position26> 180 <Position27> 181 <Filename Value="USyntetizer.pas"/> 182 <Caret Line="20" Column="57" TopLine="2"/> 183 </Position27> 184 <Position28> 185 <Filename Value="USyntetizer.pas"/> 186 <Caret Line="29" Column="1" TopLine="2"/> 187 </Position28> 188 <Position29> 189 <Filename Value="USyntetizer.pas"/> 190 <Caret Line="163" Column="23" TopLine="147"/> 191 </Position29> 192 <Position30> 193 <Filename Value="USyntetizer.pas"/> 194 <Caret Line="237" Column="34" TopLine="225"/> 195 </Position30> 126 196 </JumpHistory> 127 197 </ProjectOptions> -
branches/Syntetizer/UMainForm.lrs
r26 r27 1 { This is an automatically generated lazarus resource file }2 3 1 LazarusResources.Add('TMainForm','FORMDATA',[ 4 2 'TPF0'#9'TMainForm'#8'MainForm'#4'Left'#3'P'#2#6'Height'#3'm'#1#3'Top'#3#152#0 -
branches/Syntetizer/UMainForm.pas
r26 r27 47 47 with TProgram(BaseElement) do begin 48 48 Name := 'Test'; 49 with TCompilerDirective(CompilerDirectives[CompilerDirectives.Add(TCompilerDirective.Create)]) do begin 50 Variable := 'APPTYPE'; 51 Value := 'CONSOLE'; 52 end; 53 with Variables do begin 54 with TVariableDeclaration(Variables[Variables.Add(TVariableDeclaration.Create)]) do begin 55 Name := 'Text'; 56 VarType := 'string'; 57 end; 58 end; 59 with Code do begin 60 with TProcedureCall(Commands[Commands.Add(TProcedureCall.Create)]) do begin 61 Name := 'WriteLn'; 62 with TProcedureParameter(Parameters[Parameters.Add(TProcedureParameter.Create)]) do begin 63 Value := '''Hello hell'''; 64 end; 65 end; 66 with TProcedureCall(Commands[Commands.Add(TProcedureCall.Create)]) do begin 67 Name := 'ReadLn'; 68 end; 69 end; 49 70 end; 50 71 Syntetize; 51 72 Memo1.Lines.Assign(Output); 52 Output.SaveToFile('Output. pas');73 Output.SaveToFile('Output.dpr'); 53 74 end; 54 75 end; -
branches/Syntetizer/USyntetizer.pas
r26 r27 11 11 TSyntetizer = class; 12 12 13 TElement = class 13 ISyntetizable = interface 14 procedure Syntetize(Syntetizer: TSyntetizer); 15 end; 16 17 TElement = class(TInterfacedObject) 18 end; 19 20 TCompilerDirective = class(TElement) 21 Variable: string; 22 Value: string; 23 procedure Syntetize(Syntetizer: TSyntetizer); 24 end; 25 26 TCommand = class(TInterfacedObject, ISyntetizable) 14 27 procedure Syntetize(Syntetizer: TSyntetizer); virtual; 15 28 end; 16 29 17 TCommand = class(TElement) 18 end; 19 20 { TBeginEnd } 30 TProcedureParameter = class(TElement) 31 Value: string; 32 end; 33 34 { TProcedureCall } 35 36 TProcedureCall = class(TCommand) 37 Name: string; 38 Parameters: TList; // TList<TProcedureParameter> 39 constructor Create; 40 destructor Destroy; override; 41 procedure Syntetize(Syntetizer: TSyntetizer); override; 42 end; 21 43 22 44 TBeginEnd = class(TCommand) 23 Commands: TList; // TList<TCommand> 45 Commands: TList; // TList<ICommand> 46 LastEnd: Boolean; 24 47 procedure Syntetize(Syntetizer: TSyntetizer); override; 25 48 constructor Create; … … 27 50 end; 28 51 29 { TProgram } 30 31 TProgram = class(TElement) 52 TVariableDeclaration = class(TInterfacedObject, ISyntetizable) 53 Name: string; 54 VarType: string; 55 procedure Syntetize(Syntetizer: TSyntetizer); 56 end; 57 58 { TVariableSection } 59 60 TVariableSection = class(TInterfacedObject, ISyntetizable) 61 Variables: TList; // TList<TVariableDeclaration> 62 procedure Syntetize(Syntetizer: TSyntetizer); 63 constructor Create; 64 destructor Destroy; override; 65 end; 66 67 TProgram = class(TInterfacedObject, ISyntetizable) 32 68 Name: string; 33 69 Code: TBeginend; 34 procedure Syntetize(Syntetizer: TSyntetizer); override; 35 constructor Create; 36 destructor Destroy; override; 37 end; 70 CompilerDirectives: TList; // TList<TCompilerDirective> 71 Variables: TVariableSection; 72 procedure Syntetize(Syntetizer: TSyntetizer); 73 constructor Create; 74 destructor Destroy; override; 75 end; 76 77 { TSyntetizer } 38 78 39 79 TSyntetizer = class 40 BaseElement: TElement; 80 private 81 procedure AddLine(Line: string); 82 public 83 BaseElement: TProgram; 41 84 Output: TStringList; 85 Indent: Integer; 42 86 procedure Syntetize; 43 87 constructor Create; … … 48 92 49 93 { TSyntetizer } 94 95 procedure TSyntetizer.AddLine(Line: string); 96 var 97 I: Integer; 98 begin 99 for I := 1 to Indent * 2 do 100 Line := ' ' + Line; 101 Output.Add(Line); 102 end; 50 103 51 104 procedure TSyntetizer.Syntetize; … … 66 119 end; 67 120 68 { TElement }69 70 procedure TElement.Syntetize(Syntetizer: TSyntetizer);71 begin72 73 end;74 75 121 { TProgram } 76 122 77 123 procedure TProgram.Syntetize(Syntetizer: TSyntetizer); 78 begin 79 Syntetizer.Output.Add('program ' + Name + ';'); 124 var 125 I: Integer; 126 begin 127 Syntetizer.AddLine('program ' + Name + ';'); 128 for I := 0 to CompilerDirectives.Count - 1 do 129 TCompilerDirective(CompilerDirectives[I]).Syntetize(Syntetizer); 130 Variables.Syntetize(Syntetizer); 80 131 Code.Syntetize(Syntetizer); 81 132 inherited; … … 84 135 constructor TProgram.Create; 85 136 begin 137 CompilerDirectives := TList.Create; 138 Variables := TVariableSection.Create; 86 139 Code := TBeginEnd.Create; 140 Code.LastEnd := True; 87 141 end; 88 142 89 143 destructor TProgram.Destroy; 90 begin 144 var 145 I: Integer; 146 begin 147 Variables.Destroy; 148 for I := 0 to CompilerDirectives.Count - 1 do 149 TCompilerDirective(CompilerDirectives[I]).Destroy; 91 150 Code.Destroy; 92 151 inherited Destroy; … … 99 158 I: Integer; 100 159 begin 101 with Syntetizer.Output do begin 102 Add('begin'); 160 with Syntetizer do begin 161 AddLine('begin'); 162 Inc(Syntetizer.Indent); 103 163 for I := 0 to Commands.Count - 1 do 104 164 TCommand(Commands[I]).Syntetize(Syntetizer); 105 Add('end;'); 165 Dec(Syntetizer.Indent); 166 if LastEnd then AddLine('end.') else AddLine('end;'); 106 167 end; 107 168 inherited Syntetize(Syntetizer); … … 123 184 end; 124 185 186 { TProcedureCall } 187 188 constructor TProcedureCall.Create; 189 begin 190 Parameters := TList.Create; 191 end; 192 193 destructor TProcedureCall.Destroy; 194 var 195 I: Integer; 196 begin 197 for I := 0 to Parameters.Count - 1 do 198 TProcedureParameter(Parameters[I]).Destroy; 199 Parameters.Destroy; 200 inherited Destroy; 201 end; 202 203 procedure TProcedureCall.Syntetize(Syntetizer: TSyntetizer); 204 var 205 ParameterString: string; 206 I: Integer; 207 begin 208 for I := 0 to Parameters.Count - 1 do 209 ParameterString := ParameterString + TProcedureParameter(Parameters[I]).Value; 210 if Parameters.Count > 0 then 211 ParameterString := '(' + ParameterString + ')'; 212 Syntetizer.AddLine(Name + ParameterString + ';'); 213 inherited Syntetize(Syntetizer); 214 end; 215 216 { TCompilerDirective } 217 218 procedure TCompilerDirective.Syntetize(Syntetizer: TSyntetizer); 219 begin 220 Syntetizer.AddLine('{$' + Variable + ' ' + Value + '}'); 221 end; 222 223 { TVariableDeclaration } 224 225 procedure TVariableDeclaration.Syntetize(Syntetizer: TSyntetizer); 226 begin 227 Syntetizer.AddLine(Name + ': ' + VarType + ';'); 228 inherited; 229 end; 230 231 { TCommand } 232 233 procedure TCommand.Syntetize(Syntetizer: TSyntetizer); 234 begin 235 end; 236 237 { TVariableSection } 238 239 procedure TVariableSection.Syntetize(Syntetizer: TSyntetizer); 240 var 241 I: Integer; 242 SectionString: string; 243 begin 244 Syntetizer.AddLine('var'); 245 Inc(Syntetizer.Indent); 246 for I := 0 to Variables.Count - 1 do 247 TVariableDeclaration(Variables[I]).Syntetize(Syntetizer); 248 Dec(Syntetizer.Indent); 249 end; 250 251 constructor TVariableSection.Create; 252 begin 253 Variables := TList.Create; 254 end; 255 256 destructor TVariableSection.Destroy; 257 var 258 I: Integer; 259 begin 260 for I := 0 to Variables.Count - 1 do 261 TVariableDeclaration(Variables[I]).Destroy; 262 Variables.Destroy; 263 inherited Destroy; 264 end; 265 125 266 end. 126 267
Note:
See TracChangeset
for help on using the changeset viewer.