Changeset 19
- Timestamp:
- Nov 10, 2009, 4:15:47 PM (15 years ago)
- Location:
- branches/Void
- Files:
-
- 1 added
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Void/UCompilator.pas
r18 r19 6 6 7 7 uses 8 Classes, SysUtils, UOutputGenerator, UModel, U VoidParser, fgl;8 Classes, SysUtils, UOutputGenerator, UModel, UTokenizer, fgl, UGrammer; 9 9 10 10 type … … 23 23 private 24 24 FOnError: TOnErrorEvent; 25 Grammer: TGrammer; 25 26 procedure DoError(AText: string); 26 27 procedure ParseBeginEnd; … … 29 30 procedure ParseTypeDefinition; 30 31 procedure ParseExpression; 32 procedure InitGrammer; 31 33 public 32 34 ErrorMessages: TErrorMessageList; … … 49 51 Terminate: Boolean; 50 52 begin 51 with ErrorMessages .Adddo begin53 with ErrorMessages[ErrorMessages.Add(TErrorMessage.Create)] do begin 52 54 CodePosition := Parser.TokenStartPosition; 53 55 Text := AText; … … 147 149 end; 148 150 151 procedure TCompilator.InitGrammer; 152 var 153 RuleProgram, RuleBlock, RuleDeclaration, RuleStatement, RuleConstant, 154 RuleVariable, RuleFunction: TGrammerRule; 155 begin 156 Grammer := TGrammer.Create; 157 with Grammer do begin 158 RuleConstantDefinition := TGrammerRule.Create; 159 with RuleConstantDefinition do begin 160 Name := 'ConstantDefinition'; 161 RuleType := rtSequence; 162 end; 163 Rules.Add(RuleConstantDefinition); 164 165 RuleConstant := TGrammerRule.Create; 166 with RuleConstant do begin 167 Name := 'Constant'; 168 RuleType := rtSequence; 169 AddTerminal('const', ttIdentifier, False, False); 170 AddRule(RuleConstantDefinition, False, False); 171 end; 172 Rules.Add(RuleConstant); 173 174 RuleFunction := TGrammerRule.Create; 175 with RuleFunction do begin 176 Name := 'Function'; 177 RuleType := rtSequence; 178 end; 179 Rules.Add(RuleFunction); 180 181 RuleVariable := TGrammerRule.Create; 182 with RuleVariable do begin 183 Name := 'Variable'; 184 RuleType := rtSequence; 185 end; 186 Rules.Add(RuleVariable); 187 188 RuleStatement := TGrammerRule.Create; 189 with RuleStatement do begin 190 Name := 'Statement'; 191 RuleType := rtSequence; 192 end; 193 Rules.Add(RuleStatement); 194 195 RuleDeclaration := TGrammerRule.Create; 196 with RuleDeclaration do begin 197 Name := 'Declaration'; 198 RuleType := rtAlternative; 199 AddRule(RuleFunction, False, False); 200 AddRule(RuleConstant, False, False); 201 AddRule(RuleVariable, False, False); 202 end; 203 Rules.Add(RuleDeclaration); 204 205 RuleBlock := TGrammerRule.Create; 206 with RuleBlock do begin 207 Name := 'Block'; 208 RuleType := rtSequence; 209 AddRule(RuleDeclaration, False, True); 210 AddRule(RuleStatement, False, False); 211 end; 212 Rules.Add(RuleBlock); 213 214 RuleProgram := TGrammerRule.Create; 215 with RuleProgram do begin 216 Name := 'Program'; 217 RuleType := rtSequence; 218 AddRule(RuleBlock, False, False); 219 AddTerminal('.', ttSymbol, False, False); 220 end; 221 Rules.Add(RuleProgram); 222 223 TopRule := RuleProgram; 224 end; 225 end; 226 149 227 procedure TCompilator.Compile; 150 228 var … … 167 245 Variable: TVariable; 168 246 SourceVariable: TVariable; 169 VariableName: string;170 247 Value: string; 171 248 P: Integer; … … 282 359 constructor TCompilator.Create; 283 360 begin 361 Grammer := TGrammer.Create; 362 InitGrammer; 284 363 ErrorMessages := TErrorMessageList.Create; 285 364 SourceCode := TMemoryStream.Create; … … 290 369 destructor TCompilator.Destroy; 291 370 begin 371 Grammer.Destroy; 292 372 ErrorMessages.Destroy; 293 373 SourceCode.Destroy; -
branches/Void/UGrammer.pas
r18 r19 6 6 7 7 uses 8 Classes, SysUtils, fgl ;8 Classes, SysUtils, fgl, UTokenizer; 9 9 10 10 type … … 12 12 TRuleItemType = (itTerminal, itNonterminal); 13 13 14 TGrammerRule = class; 15 TGrammer = class; 16 17 THandleEvent = procedure (GrammerRule: TGrammerRule) of object; 18 14 19 TGrammerItem = class 20 private 21 Parent: TGrammerRule; 22 public 23 TokenType: TTokenType; 24 Rule: TGrammerRule; 15 25 ItemType: TRuleItemType; 16 26 Optional: Boolean; 17 27 Repetition: Boolean; 28 Text: string; 18 29 end; 19 30 20 31 TGrammerItemList = specialize TFPGObjectList<TGrammerItem>; 21 32 33 { TGrammerRule } 34 22 35 TGrammerRule = class 36 private 37 FOnHandle: THandleEvent; 38 Parent: TGrammer; 39 public 23 40 Name: string; 24 41 RuleType: TRuleType; 25 42 Items: TGrammerItemList; 43 procedure AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean); 44 procedure AddRule(ARule: TGrammerRule; AOptional, ARepetition: Boolean); 26 45 constructor Create; 27 46 destructor Destroy; override; 47 property OnHandle: THandleEvent read FOnHandle write FOnHandle; 28 48 end; 29 49 30 50 TGrammerRuleList = specialize TFPGObjectList<TGrammerRule>; 31 32 { TGrammer }33 51 34 52 TGrammer = class … … 40 58 41 59 implementation 60 61 procedure TGrammerRule.AddTerminal(AText: string; ATokenType: TTokenType; AOptional, ARepetition: Boolean 62 ); 63 begin 64 with Items[Items.Add(TGrammerItem.Create)] do begin 65 ItemType := itTerminal; 66 TokenType := ATokenType; 67 Text := AText; 68 Optional := AOptional; 69 Repetition := ARepetition; 70 Parent := Self; 71 end; 72 end; 73 74 procedure TGrammerRule.AddRule(ARule: TGrammerRule; AOptional, 75 ARepetition: Boolean); 76 begin 77 with Items[Items.Add(TGrammerItem.Create)] do begin 78 ItemType := itNonterminal; 79 Rule := ARule; 80 Optional := AOptional; 81 Repetition := ARepetition; 82 Parent := Self; 83 end; 84 end; 42 85 43 86 constructor TGrammerRule.Create; -
branches/Void/project1.lpi
r18 r19 9 9 <Icon Value="0"/> 10 10 <UseXPManifest Value="True"/> 11 <ActiveEditorIndexAtStart Value=" 10"/>11 <ActiveEditorIndexAtStart Value="2"/> 12 12 </General> 13 13 <VersionInfo> … … 37 37 </Item2> 38 38 </RequiredPackages> 39 <Units Count="2 4">39 <Units Count="25"> 40 40 <Unit0> 41 41 <Filename Value="project1.lpr"/> … … 44 44 <CursorPos X="51" Y="9"/> 45 45 <TopLine Value="1"/> 46 <UsageCount Value="5 6"/>46 <UsageCount Value="57"/> 47 47 </Unit0> 48 48 <Unit1> … … 55 55 <TopLine Value="114"/> 56 56 <EditorIndex Value="0"/> 57 <UsageCount Value="5 6"/>57 <UsageCount Value="57"/> 58 58 <Loaded Value="True"/> 59 59 </Unit1> … … 61 61 <Filename Value="UCompilator.pas"/> 62 62 <UnitName Value="UCompilator"/> 63 <CursorPos X=" 24" Y="290"/>64 <TopLine Value=" 51"/>63 <CursorPos X="3" Y="238"/> 64 <TopLine Value="223"/> 65 65 <EditorIndex Value="2"/> 66 <UsageCount Value="2 8"/>66 <UsageCount Value="29"/> 67 67 <Loaded Value="True"/> 68 68 </Unit2> … … 73 73 <CursorPos X="1" Y="13"/> 74 74 <TopLine Value="20"/> 75 <EditorIndex Value=" 6"/>76 <UsageCount Value="5 6"/>75 <EditorIndex Value="7"/> 76 <UsageCount Value="57"/> 77 77 <Loaded Value="True"/> 78 78 </Unit3> … … 82 82 <CursorPos X="8" Y="7"/> 83 83 <TopLine Value="1"/> 84 <UsageCount Value="5 6"/>84 <UsageCount Value="57"/> 85 85 <SyntaxHighlighter Value="None"/> 86 86 </Unit4> … … 91 91 <CursorPos X="16" Y="135"/> 92 92 <TopLine Value="65"/> 93 <EditorIndex Value="1 0"/>94 <UsageCount Value="5 6"/>93 <EditorIndex Value="11"/> 94 <UsageCount Value="57"/> 95 95 <Loaded Value="True"/> 96 96 </Unit5> … … 122 122 <Unit10> 123 123 <Filename Value="UVoidParser.pas"/> 124 <IsPartOfProject Value="True"/>125 124 <UnitName Value="UVoidParser"/> 126 125 <CursorPos X="30" Y="162"/> 127 126 <TopLine Value="140"/> 128 <EditorIndex Value=" 5"/>129 <UsageCount Value="5 6"/>127 <EditorIndex Value="6"/> 128 <UsageCount Value="57"/> 130 129 <Loaded Value="True"/> 131 130 </Unit10> … … 163 162 <TopLine Value="1"/> 164 163 <EditorIndex Value="1"/> 165 <UsageCount Value="3 2"/>164 <UsageCount Value="33"/> 166 165 <Loaded Value="True"/> 167 166 </Unit15> … … 178 177 <CursorPos X="24" Y="53"/> 179 178 <TopLine Value="2"/> 180 <EditorIndex Value=" 7"/>181 <UsageCount Value="3 1"/>179 <EditorIndex Value="8"/> 180 <UsageCount Value="32"/> 182 181 <Loaded Value="True"/> 183 182 </Unit17> … … 188 187 <CursorPos X="24" Y="80"/> 189 188 <TopLine Value="73"/> 190 <EditorIndex Value=" 9"/>191 <UsageCount Value="3 1"/>189 <EditorIndex Value="10"/> 190 <UsageCount Value="32"/> 192 191 <Loaded Value="True"/> 193 192 </Unit18> … … 198 197 <CursorPos X="24" Y="57"/> 199 198 <TopLine Value="1"/> 200 <EditorIndex Value=" 8"/>201 <UsageCount Value="3 1"/>199 <EditorIndex Value="9"/> 200 <UsageCount Value="32"/> 202 201 <Loaded Value="True"/> 203 202 </Unit19> … … 206 205 <IsPartOfProject Value="True"/> 207 206 <UnitName Value="UGrammer"/> 208 <CursorPos X=" 62" Y="20"/>209 <TopLine Value=" 1"/>207 <CursorPos X="15" Y="44"/> 208 <TopLine Value="29"/> 210 209 <EditorIndex Value="3"/> 211 <UsageCount Value="2 7"/>210 <UsageCount Value="28"/> 212 211 <Loaded Value="True"/> 213 212 </Unit20> … … 222 221 <Filename Value="..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/> 223 222 <UnitName Value="fgl"/> 224 <CursorPos X=" 36" Y="356"/>225 <TopLine Value=" 341"/>226 <EditorIndex Value=" 4"/>227 <UsageCount Value="1 1"/>223 <CursorPos X="15" Y="124"/> 224 <TopLine Value="111"/> 225 <EditorIndex Value="5"/> 226 <UsageCount Value="12"/> 228 227 <Loaded Value="True"/> 229 228 </Unit22> … … 234 233 <UsageCount Value="10"/> 235 234 </Unit23> 235 <Unit24> 236 <Filename Value="UTokenizer.pas"/> 237 <IsPartOfProject Value="True"/> 238 <UnitName Value="UTokenizer"/> 239 <CursorPos X="1" Y="1"/> 240 <TopLine Value="1"/> 241 <EditorIndex Value="4"/> 242 <UsageCount Value="21"/> 243 <Loaded Value="True"/> 244 </Unit24> 236 245 </Units> 237 246 <JumpHistory Count="30" HistoryIndex="29"> 238 247 <Position1> 239 <Filename Value="U Grammer.pas"/>240 <Caret Line=" 8" Column="25" TopLine="1"/>248 <Filename Value="UCompilator.pas"/> 249 <Caret Line="155" Column="24" TopLine="140"/> 241 250 </Position1> 242 251 <Position2> 243 252 <Filename Value="UGrammer.pas"/> 244 <Caret Line=" 25" Column="28" TopLine="10"/>253 <Caret Line="40" Column="39" TopLine="20"/> 245 254 </Position2> 246 255 <Position3> 247 <Filename Value="U Grammer.pas"/>248 <Caret Line=" 20" Column="33" TopLine="5"/>256 <Filename Value="UCompilator.pas"/> 257 <Caret Line="167" Column="16" TopLine="152"/> 249 258 </Position3> 250 259 <Position4> 251 260 <Filename Value="UGrammer.pas"/> 252 <Caret Line=" 37" Column="1" TopLine="9"/>261 <Caret Line="21" Column="27" TopLine="7"/> 253 262 </Position4> 254 263 <Position5> 255 264 <Filename Value="UGrammer.pas"/> 256 <Caret Line="62" Column=" 12" TopLine="36"/>265 <Caret Line="62" Column="29" TopLine="45"/> 257 266 </Position5> 258 267 <Position6> 259 268 <Filename Value="UGrammer.pas"/> 260 <Caret Line=" 30" Column="43" TopLine="34"/>269 <Caret Line="57" Column="35" TopLine="42"/> 261 270 </Position6> 262 271 <Position7> 263 272 <Filename Value="UGrammer.pas"/> 264 <Caret Line=" 63" Column="17" TopLine="37"/>273 <Caret Line="70" Column="1" TopLine="55"/> 265 274 </Position7> 266 275 <Position8> 267 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>268 <Caret Line=" 799" Column="9" TopLine="795"/>276 <Filename Value="UCompilator.pas"/> 277 <Caret Line="8" Column="58" TopLine="1"/> 269 278 </Position8> 270 279 <Position9> 271 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>272 <Caret Line=" 320" Column="50" TopLine="313"/>280 <Filename Value="UCompilator.pas"/> 281 <Caret Line="168" Column="25" TopLine="152"/> 273 282 </Position9> 274 283 <Position10> 275 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>276 <Caret Line=" 712" Column="39" TopLine="697"/>284 <Filename Value="UCompilator.pas"/> 285 <Caret Line="171" Column="34" TopLine="155"/> 277 286 </Position10> 278 287 <Position11> 279 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>280 <Caret Line=" 715" Column="23" TopLine="697"/>288 <Filename Value="UCompilator.pas"/> 289 <Caret Line="179" Column="34" TopLine="164"/> 281 290 </Position11> 282 291 <Position12> 283 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>284 <Caret Line=" 730" Column="11" TopLine="715"/>292 <Filename Value="UCompilator.pas"/> 293 <Caret Line="183" Column="7" TopLine="165"/> 285 294 </Position12> 286 295 <Position13> 287 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>288 <Caret Line=" 725" Column="31" TopLine="715"/>296 <Filename Value="UCompilator.pas"/> 297 <Caret Line="194" Column="1" TopLine="176"/> 289 298 </Position13> 290 299 <Position14> 291 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>292 <Caret Line=" 825" Column="41" TopLine="810"/>300 <Filename Value="UCompilator.pas"/> 301 <Caret Line="203" Column="18" TopLine="188"/> 293 302 </Position14> 294 303 <Position15> 295 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>296 <Caret Line=" 1" Column="24" TopLine="1"/>304 <Filename Value="UCompilator.pas"/> 305 <Caret Line="225" Column="1" TopLine="209"/> 297 306 </Position15> 298 307 <Position16> 299 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>300 <Caret Line=" 42" Column="20" TopLine="27"/>308 <Filename Value="UCompilator.pas"/> 309 <Caret Line="233" Column="1" TopLine="217"/> 301 310 </Position16> 302 311 <Position17> 303 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>304 <Caret Line=" 43" Column="20" TopLine="27"/>312 <Filename Value="UCompilator.pas"/> 313 <Caret Line="242" Column="34" TopLine="227"/> 305 314 </Position17> 306 315 <Position18> 307 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>308 <Caret Line=" 96" Column="20" TopLine="81"/>316 <Filename Value="UCompilator.pas"/> 317 <Caret Line="244" Column="1" TopLine="228"/> 309 318 </Position18> 310 319 <Position19> 311 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>312 <Caret Line=" 127" Column="20" TopLine="112"/>320 <Filename Value="UCompilator.pas"/> 321 <Caret Line="270" Column="1" TopLine="246"/> 313 322 </Position19> 314 323 <Position20> 315 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>316 <Caret Line=" 158" Column="20" TopLine="143"/>324 <Filename Value="UCompilator.pas"/> 325 <Caret Line="287" Column="34" TopLine="271"/> 317 326 </Position20> 318 327 <Position21> 319 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>320 <Caret Line=" 237" Column="20" TopLine="222"/>328 <Filename Value="UGrammer.pas"/> 329 <Caret Line="41" Column="25" TopLine="20"/> 321 330 </Position21> 322 331 <Position22> 323 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>324 <Caret Line=" 338" Column="25" TopLine="323"/>332 <Filename Value="UGrammer.pas"/> 333 <Caret Line="17" Column="65" TopLine="2"/> 325 334 </Position22> 326 335 <Position23> 327 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>328 <Caret Line=" 342" Column="25" TopLine="323"/>336 <Filename Value="UCompilator.pas"/> 337 <Caret Line="450" Column="1" TopLine="277"/> 329 338 </Position23> 330 339 <Position24> 331 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>332 <Caret Line=" 349" Column="10" TopLine="323"/>340 <Filename Value="UCompilator.pas"/> 341 <Caret Line="153" Column="15" TopLine="140"/> 333 342 </Position24> 334 343 <Position25> 335 <Filename Value=" ..\..\..\..\..\Programy\Lazarus_0.9.29\fpc\2.3.1\source\rtl\objpas\fgl.pp"/>336 <Caret Line=" 367" Column="12" TopLine="352"/>344 <Filename Value="UCompilator.pas"/> 345 <Caret Line="204" Column="27" TopLine="189"/> 337 346 </Position25> 338 347 <Position26> 339 <Filename Value="U Grammer.pas"/>340 <Caret Line=" 8" Column="20" TopLine="1"/>348 <Filename Value="UCompilator.pas"/> 349 <Caret Line="277" Column="14" TopLine="258"/> 341 350 </Position26> 342 351 <Position27> 343 <Filename Value="U Model.pas"/>344 <Caret Line=" 76" Column="25" TopLine="67"/>352 <Filename Value="UCompilator.pas"/> 353 <Caret Line="169" Column="12" TopLine="153"/> 345 354 </Position27> 346 355 <Position28> 347 <Filename Value="U Model.pas"/>348 <Caret Line="1 21" Column="23" TopLine="106"/>356 <Filename Value="UCompilator.pas"/> 357 <Caret Line="192" Column="27" TopLine="163"/> 349 358 </Position28> 350 359 <Position29> 351 <Filename Value="U Model.pas"/>352 <Caret Line=" 147" Column="31" TopLine="132"/>360 <Filename Value="UCompilator.pas"/> 361 <Caret Line="201" Column="19" TopLine="185"/> 353 362 </Position29> 354 363 <Position30> 355 <Filename Value="U Model.pas"/>356 <Caret Line=" 163" Column="26" TopLine="148"/>364 <Filename Value="UCompilator.pas"/> 365 <Caret Line="209" Column="28" TopLine="194"/> 357 366 </Position30> 358 367 </JumpHistory> -
branches/Void/project1.lpr
r18 r19 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, UOutputGenerator, LResources, UModel, U VoidParser,11 U ModelViewer, UZ80Generator, UCGenerator, UPascalGenerator, UGrammer;10 Forms, UMainForm, UOutputGenerator, LResources, UModel, UModelViewer, 11 UZ80Generator, UCGenerator, UPascalGenerator, UGrammer, UTokenizer; 12 12 13 13 {$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
Note:
See TracChangeset
for help on using the changeset viewer.