Changeset 211 for branches/interpreter2
- Timestamp:
- Apr 22, 2020, 9:00:02 AM (5 years ago)
- Location:
- branches/interpreter2
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2/Forms/UFormMain.pas
r210 r211 209 209 begin 210 210 ACompile.Execute; 211 //AOptimize.Execute;211 AOptimize.Execute; 212 212 FormOutput.SynEditOutput.Highlighter := nil; 213 213 FormOutput.SynEditOutput.Lines.Clear; -
branches/interpreter2/UExecutor.pas
r207 r211 57 57 end; 58 58 59 59 { TExecutorBlock } 60 60 61 61 TExecutorBlock = class … … 468 468 if not TValueBoolean(Value).Value then Break; 469 469 ExecuteCommand(Block, WhileDo.Command); 470 if WhileDo.DoContinue then begin 471 WhileDo.DoContinue := False; 472 Continue; 473 end; 474 if WhileDo.DoBreak then begin 475 WhileDo.DoBreak := False; 476 Break; 477 end; 470 478 end else raise Exception.Create('Expected boolean value.'); 471 479 Value.Free; … … 480 488 begin 481 489 while True do begin 482 for I := 0 to RepeatUntil.Commands.Count - 1 do 490 for I := 0 to RepeatUntil.Commands.Count - 1 do begin 483 491 ExecuteCommand(Block, TCommand(RepeatUntil.Commands[I])); 492 if RepeatUntil.DoContinue then begin 493 RepeatUntil.DoContinue := False; 494 Continue; 495 end; 496 if RepeatUntil.DoBreak then begin 497 RepeatUntil.DoBreak := False; 498 Break; 499 end; 500 end; 484 501 Value := ExecuteExpression(Block, RepeatUntil.Expression); 485 502 if Value is TValueBoolean then begin … … 501 518 while True do begin 502 519 ExecuteCommand(Block, ForToDo.Command); 520 if ForToDo.DoContinue then begin 521 ForToDo.DoContinue := False; 522 Continue; 523 end; 524 if ForToDo.DoBreak then begin 525 ForToDo.DoBreak := False; 526 Break; 527 end; 503 528 TValueInteger(Variable.Value).Value := TValueInteger(Variable.Value).Value + 1; 504 529 if TValueInteger(Variable.Value).Value > TValueInteger(Limit).Value then Break; … … 509 534 procedure TExecutor.ExecuteContinue(Block: TExecutorBlock; 510 535 ContinueCmd: TContinue); 511 begin 512 536 var 537 Node: TSourceNode; 538 begin 539 Node := ContinueCmd.Parent; 540 while Assigned(Node) and not (Node is TLoop) and Assigned(Node.Parent) do 541 Node := Node.Parent; 542 543 if Node is TLoop then TLoop(Node).DoContinue := True 544 else raise Exception.Create('Break used outside of loop.'); 513 545 end; 514 546 515 547 procedure TExecutor.ExecuteBreak(Block: TExecutorBlock; BreakCmd: TBreak); 516 begin 517 548 var 549 Node: TSourceNode; 550 begin 551 Node := BreakCmd.Parent; 552 while Assigned(Node) and not (Node is TLoop) and Assigned(Node.Parent) do 553 Node := Node.Parent; 554 555 if Node is TLoop then TLoop(Node).DoBreak := True 556 else raise Exception.Create('Break used outside of loop.'); 518 557 end; 519 558 -
branches/interpreter2/UOptimizer.pas
r208 r211 58 58 WhileDo := TWhileDo.Create; 59 59 WhileDo.Command := TBeginEnd.Create; 60 WhileDo.Parent := TRepeatUntil(SourceNode).Parent; 60 61 TBeginEnd(WhileDo.Command).Commands := TRepeatUntil(SourceNode).Commands; 62 TBeginEnd(WhileDo.Command).Parent := WhileDo; 61 63 TRepeatUntil(SourceNode).Commands := TCommands.Create; 62 64 WhileDo.Expression := TExpressionOperand.Create; 65 WhileDo.Expression.Parent := WhileDo; 63 66 TExpressionOperand(WhileDo.Expression).OperandType := otConstantDirect; 64 67 TExpressionOperand(WhileDo.Expression).ConstantDirect := TConstant.Create; … … 68 71 // Add final if 69 72 Condition := TIfThenElse.Create; 73 Condition.Parent := WhileDo; 70 74 Condition.Expression := TRepeatUntil(SourceNode).Expression; 71 75 Condition.CommandThen := TBreak.Create; 76 Condition.CommandThen.Parent := Condition; 72 77 TRepeatUntil(SourceNode).Expression := TExpression.Create; 73 78 TBeginEnd(WhileDo.Command).Commands.Add(Condition); -
branches/interpreter2/UParser.pas
r207 r211 63 63 while not Tokenizer.CheckNext('end', tkKeyword) do begin 64 64 if ParseCommand(Block, Command) then begin 65 Command.Parent := BeginEnd; 65 66 BeginEnd.Commands.Add(Command); 66 67 Tokenizer.Expect(';', tkSpecialSymbol); … … 185 186 Prog.Block.Free; 186 187 Prog.Block := Block; 188 Block.Parent := Prog; 187 189 Tokenizer.Expect('.', tkSpecialSymbol); 188 190 end else begin … … 198 200 Result := False; 199 201 Block := TBlock.Create; 200 Block.Parent := ParentBlock;202 Block.ParentBlock := ParentBlock; 201 203 ParseBlockVar(Block); 202 204 ParseBlockConst(Block); … … 205 207 Block.BeginEnd.Free; 206 208 Block.BeginEnd := BeginEnd; 209 BeginEnd.Parent := Block; 207 210 end else Block.Free; 208 211 end; … … 293 296 Variable: TVariable; 294 297 Expression: TExpression; 295 begin 298 LastPos: TTokenizerPos; 299 begin 300 LastPos := Tokenizer.Pos; 296 301 Result := False; 297 302 Token := Tokenizer.GetNext; 298 303 if Token.Kind = tkIdentifier then begin 299 Result := True;300 304 Variable := Block.GetVariable(Token.Text); 301 305 if Assigned(Variable) then begin … … 308 312 Assignment.Expression.Free; 309 313 Assignment.Expression := Expression; 314 Expression.Parent := Assignment; 310 315 end else begin 311 316 Result := False; … … 316 321 end else Error('Variable ' + Token.Text + ' not defined.'); 317 322 end; 323 if not Result then Tokenizer.Pos := LastPos; 318 324 end; 319 325 … … 481 487 IfThenElse.CommandThen.Free; 482 488 IfThenElse.CommandThen := Command; 489 Command.Parent := IfThenElse; 483 490 if Tokenizer.CheckNext('else', tkKeyword) then begin 484 491 Tokenizer.Expect('else', tkKeyword); … … 486 493 IfThenElse.CommandElse.Free; 487 494 IfThenElse.CommandElse := Command; 495 Command.Parent := IfThenElse; 488 496 end else Error('Expected command'); 489 497 end; … … 510 518 WhileDo.Command.Free; 511 519 WhileDo.Command := Command; 520 Command.Parent := WhileDo; 512 521 end else Error('Expected command'); 513 522 end else Error('Expected expression'); … … 529 538 if ParseCommand(Block, Command) then begin 530 539 RepeatUntil.Commands.Add(Command); 540 Command.Parent := RepeatUntil; 531 541 Tokenizer.Expect(';', tkSpecialSymbol); 532 542 end else begin … … 569 579 ForToDo.Command.Free; 570 580 ForToDo.Command := Command; 581 Command.Parent := ForToDo; 571 582 end else Error('Expected command.'); 572 583 end else Error('Expected expression.'); -
branches/interpreter2/USource.pas
r208 r211 34 34 function GetFieldsCount: Integer; virtual; 35 35 public 36 Parent: TSourceNode; 36 37 function GetField(Index: Integer): TField; virtual; 37 38 procedure GetValue(Index: Integer; out Value); virtual; … … 54 55 TSourceNodes = class(TSourceNode) 55 56 private 57 Parent: TSourceNode; 56 58 function GetCount: Integer; 57 59 function GetItem(Index: Integer): TObject; … … 316 318 end; 317 319 320 TLoop = class(TCommand) 321 DoBreak: Boolean; 322 DoContinue: Boolean; 323 end; 324 318 325 { TWhileDo } 319 326 320 TWhileDo = class(T Command)327 TWhileDo = class(TLoop) 321 328 private 322 329 function GetFieldsCount: Integer; override; … … 333 340 { TRepeatUntil } 334 341 335 TRepeatUntil = class(T Command)342 TRepeatUntil = class(TLoop) 336 343 private 337 344 function GetFieldsCount: Integer; override; … … 354 361 { TForToDo } 355 362 356 TForToDo = class(T Command)363 TForToDo = class(TLoop) 357 364 private 358 365 function GetFieldsCount: Integer; override; … … 375 382 function GetFieldsCount: Integer; override; 376 383 public 377 Parent : TBlock;384 ParentBlock: TBlock; 378 385 Variables: TVariables; 379 386 Constants: TConstants; … … 1234 1241 begin 1235 1242 Result := Types.SearchByName(Name); 1236 if not Assigned(Result) and Assigned(Parent ) then1237 Result := Parent .Types.SearchByName(Name);1243 if not Assigned(Result) and Assigned(ParentBlock) then 1244 Result := ParentBlock.Types.SearchByName(Name); 1238 1245 end; 1239 1246 … … 1241 1248 begin 1242 1249 Result := Constants.SearchByName(Name); 1243 if not Assigned(Result) and Assigned(Parent ) then1244 Result := Parent .Constants.SearchByName(Name);1250 if not Assigned(Result) and Assigned(ParentBlock) then 1251 Result := ParentBlock.Constants.SearchByName(Name); 1245 1252 end; 1246 1253 … … 1248 1255 begin 1249 1256 Result := Variables.SearchByName(Name); 1250 if not Assigned(Result) and Assigned(Parent ) then1251 Result := Parent .Variables.SearchByName(Name);1257 if not Assigned(Result) and Assigned(ParentBlock) then 1258 Result := ParentBlock.Variables.SearchByName(Name); 1252 1259 end; 1253 1260 … … 1255 1262 begin 1256 1263 Result := Functions.SearchByName(Name); 1257 if not Assigned(Result) and Assigned(Parent ) then1258 Result := Parent .Functions.SearchByName(Name);1264 if not Assigned(Result) and Assigned(ParentBlock) then 1265 Result := ParentBlock.Functions.SearchByName(Name); 1259 1266 end; 1260 1267 -
branches/interpreter2/interpreter.lpi
r210 r211 189 189 </Options> 190 190 </Linking> 191 <Other> 192 <CompilerMessages> 193 <IgnoredMessages idx5024="True"/> 194 </CompilerMessages> 195 </Other> 191 196 </CompilerOptions> 192 197 <Debugging>
Note:
See TracChangeset
for help on using the changeset viewer.