Changeset 118 for branches/generator/Forms/UFormMain.pas
- Timestamp:
- Nov 27, 2017, 5:10:04 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/generator/Forms/UFormMain.pas
r117 r118 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 Menus, ActnList, ComCtrls, UPersistentForm ;9 Menus, ActnList, ComCtrls, UPersistentForm, URules; 10 10 11 11 type … … 14 14 15 15 TFormMain = class(TForm) 16 ABuildCompiler: TAction; 16 17 AExit: TAction; 17 18 AOpen: TAction; … … 29 30 MenuItem6: TMenuItem; 30 31 MenuItem7: TMenuItem; 32 MenuItem8: TMenuItem; 33 MenuItem9: TMenuItem; 31 34 OpenDialog1: TOpenDialog; 32 35 SaveDialog1: TSaveDialog; 33 36 ToolBar1: TToolBar; 34 37 ToolButton1: TToolButton; 38 ToolButton2: TToolButton; 39 procedure ABuildCompilerExecute(Sender: TObject); 35 40 procedure AExitExecute(Sender: TObject); 36 41 procedure AOpenExecute(Sender: TObject); … … 93 98 end; 94 99 100 procedure TFormMain.ABuildCompilerExecute(Sender: TObject); 101 var 102 ParserFile: TStringList; 103 SourceFile: TStringList; 104 Rule: TRule; 105 Item: TRuleItem; 106 OutputDir: string; 107 Line: string; 108 I: Integer; 109 begin 110 OutputDir := 'Generated'; 111 ForceDirectories(OutputDir); 112 113 ParserFile := TStringList.Create; 114 with ParserFile do begin 115 Add('program Compiler;'); 116 Add(''); 117 Add('{$MODE Delphi}'); 118 Add(''); 119 Add('uses'); 120 Add(' Source;'); 121 Add(''); 122 Add('procedure Compile;'); 123 Add('begin'); 124 Add('end;'); 125 Add(''); 126 Add('begin'); 127 Add(' Compile;'); 128 Add('end.'); 129 SaveToFile(OutputDir + DirectorySeparator + 'Compiler.pas'); 130 end; 131 FreeAndNil(ParserFile); 132 133 SourceFile := TStringList.Create; 134 with SourceFile do begin 135 Add('unit Source;'); 136 Add(''); 137 Add('{$MODE Delphi}'); 138 Add(''); 139 Add('interface'); 140 Add(''); 141 Add('uses'); 142 Add(' fgl;'); 143 Add(''); 144 Add('type'); 145 for Rule in Core.Grammer.Rules do begin 146 Add(' T' + Rule.Name + ' = class;'); 147 end; 148 Add(''); 149 for Rule in Core.Grammer.Rules do begin 150 Add(' T' + Rule.Name + ' = class'); 151 for Item in Rule.Items do begin 152 if Item.RuleItemType = ritNonTerminal then 153 if Item.Repetitive then 154 Add(' ' + Item.NonTerminal.Name + ': TFPGObjectList<T' + Item.NonTerminal.Name + '>;') 155 else Add(' ' + Item.NonTerminal.Name + ': T' + Item.NonTerminal.Name + ';'); 156 end; 157 Add(' end;' + LineEnding); 158 end; 159 Add(''); 160 Add('implementation'); 161 Add(''); 162 for Rule in Core.Grammer.Rules do begin 163 Add('function Parse' + Rule.Name + ': Boolean;'); 164 Add('begin'); 165 I := 0; 166 for Item in Rule.Items do begin 167 Line := ' '; 168 if Rule.Items.RuleType = rtOr then begin 169 if I > 0 then Line := Line + 'else '; 170 Line := Line + 'if '; 171 end else 172 if Rule.Items.RuleType = rtAnd then begin 173 Line := Line + ''; 174 end; 175 if Item.RuleItemType = ritTerminal then 176 Line := Line + 'Expect(''' + Item.Terminal + ''')' 177 else if Item.RuleItemType = ritNonTerminal then 178 Line := Line + 'Parse' + Item.NonTerminal.Name; 179 if Rule.Items.RuleType = rtOr then begin 180 Line := Line + ' then '; 181 end else 182 if Rule.Items.RuleType = rtAnd then begin 183 Line := Line + ';'; 184 end; 185 Add(Line); 186 Inc(I); 187 end; 188 if Rule.Items.RuleType = rtOr then begin 189 Add(' else ShowError(''Unexpected token'');'); 190 end; 191 Add('end;'); 192 Add(''); 193 end; 194 195 Add('end.'); 196 197 SaveToFile(OutputDir + DirectorySeparator +'Source.pas'); 198 end; 199 FreeAndNil(SourceFile); 200 end; 201 95 202 procedure TFormMain.AOpenExecute(Sender: TObject); 96 203 begin
Note:
See TracChangeset
for help on using the changeset viewer.