Changeset 214
- Timestamp:
- Apr 23, 2020, 12:24:31 AM (5 years ago)
- Location:
- branches/interpreter2
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/interpreter2/Test.pas
r212 r214 25 25 WriteLn('DoThen'); 26 26 end else WriteLn('DoElse'); 27 28 if IsZero(1) then begin 29 WriteLn('Is really zero'); 30 end; 27 31 28 32 // While-Do -
branches/interpreter2/UExecutor.pas
r212 r214 10 10 type 11 11 TExecutorFunctions = class; 12 TExecutorBlock = class; 12 13 13 14 { TExecutorVariable } … … 45 46 TExecutorFunctionCallback = function(Params: array of TValue): TValue of object; 46 47 48 { TExecutorFunction } 49 47 50 TExecutorFunction = class 48 51 FunctionDef: TFunction; 52 Block: TExecutorBlock; 49 53 Callback: TExecutorFunctionCallback; 54 constructor Create; 55 destructor Destroy; override; 50 56 end; 51 57 … … 85 91 function ExecuteIntToStr(Params: array of TValue): TValue; 86 92 function ExecuteStrToInt(Params: array of TValue): TValue; 93 function ExecuteBooleanAssign(Params: array of TValue): TValue; 94 function ExecuteBooleanEqual(Params: array of TValue): TValue; 95 function ExecuteBooleanNotEqual(Params: array of TValue): TValue; 87 96 function ExecuteStringAssign(Params: array of TValue): TValue; 88 97 function ExecuteStringAdd(Params: array of TValue): TValue; … … 106 115 procedure ExecuteContinue(Block: TExecutorBlock; ContinueCmd: TContinue); 107 116 procedure ExecuteBreak(Block: TExecutorBlock; BreakCmd: TBreak); 108 procedure ExecuteBlock(ParentBlock: TExecutorBlock; Block: TBlock );117 procedure ExecuteBlock(ParentBlock: TExecutorBlock; Block: TBlock; ExistingBlock: TExecutorBlock = nil); 109 118 function ExecuteFunctionCall(Block: TExecutorBlock; FunctionCall: TFunctionCall): TValue; 110 119 procedure ExecuteAssignment(Block: TExecutorBlock; Assignment: TAssignment); … … 120 129 implementation 121 130 131 { TExecutorFunction } 132 133 constructor TExecutorFunction.Create; 134 begin 135 Block := TExecutorBlock.Create; 136 end; 137 138 destructor TExecutorFunction.Destroy; 139 begin 140 Block.Free; 141 inherited Destroy; 142 end; 143 122 144 { TExecutorVariable } 123 145 … … 295 317 end; 296 318 319 function TExecutor.ExecuteBooleanAssign(Params: array of TValue): TValue; 320 begin 321 Result := TValueBoolean.Create; 322 TValueBoolean(Result).Value := TValueBoolean(Params[0]).Value; 323 end; 324 325 function TExecutor.ExecuteBooleanEqual(Params: array of TValue): TValue; 326 begin 327 Result := TValueBoolean.Create; 328 TValueBoolean(Result).Value := TValueBoolean(Params[0]).Value = TValueBoolean(Params[1]).Value; 329 end; 330 331 function TExecutor.ExecuteBooleanNotEqual(Params: array of TValue): TValue; 332 begin 333 Result := TValueBoolean.Create; 334 TValueBoolean(Result).Value := TValueBoolean(Params[0]).Value <> TValueBoolean(Params[1]).Value; 335 end; 336 297 337 function TExecutor.ExecuteStringAssign(Params: array of TValue): TValue; 298 338 begin … … 360 400 for J := 0 to ExecutorType.TypeRef.Functions.Count - 1 do begin 361 401 ExecutorFunction := ExecutorType.Functions.AddNew(TFunction(ExecutorType.TypeRef.Functions[J])); 402 if ExecutorType.TypeRef.Name = 'Boolean' then begin 403 if ExecutorFunction.FunctionDef.Name = '_Assign' then begin 404 ExecutorFunction.Callback := ExecuteBooleanAssign; 405 end else 406 if ExecutorFunction.FunctionDef.Name = '_Equal' then begin 407 ExecutorFunction.Callback := ExecuteBooleanEqual; 408 end; 409 if ExecutorFunction.FunctionDef.Name = '_NotEqual' then begin 410 ExecutorFunction.Callback := ExecuteBooleanNotEqual; 411 end; 412 end else 362 413 if ExecutorType.TypeRef.Name = 'string' then begin 363 414 if ExecutorFunction.FunctionDef.Name = '_Assign' then begin … … 373 424 ExecutorFunction.Callback := ExecuteStringNotEqual; 374 425 end; 375 end ;426 end else 376 427 if ExecutorType.TypeRef.Name = 'Integer' then begin 377 428 if ExecutorFunction.FunctionDef.Name = '_Assign' then begin … … 557 608 end; 558 609 559 procedure TExecutor.ExecuteBlock(ParentBlock: TExecutorBlock; Block: TBlock );610 procedure TExecutor.ExecuteBlock(ParentBlock: TExecutorBlock; Block: TBlock; ExistingBlock: TExecutorBlock = nil); 560 611 var 561 612 ExecutorBlock: TExecutorBlock; 562 613 begin 563 ExecutorBlock := TExecutorBlock.Create; 614 if Assigned(ExistingBlock) then begin 615 ExecutorBlock := ExistingBlock 616 end else begin 617 ExecutorBlock := TExecutorBlock.Create; 618 InitExecutorBlock(ExecutorBlock, Block); 619 end; 564 620 ExecutorBlock.Parent := ParentBlock; 565 InitExecutorBlock(ExecutorBlock, Block);566 621 ExecuteBeginEnd(ExecutorBlock, Block.BeginEnd); 567 ExecutorBlock.Free;622 if not Assigned(ExistingBlock) then ExecutorBlock.Free; 568 623 end; 569 624 … … 574 629 Params: array of TValue; 575 630 I: Integer; 631 ExecutorVariable: TExecutorVariable; 632 Variable: TVariable; 576 633 begin 577 634 Result := nil; 578 635 ExecutorFunction := Block.GetFunction(FunctionCall.FunctionDef); 579 636 if Assigned(ExecutorFunction) then begin 580 SetLength(Params, FunctionCall.Params.Count); 581 for I := 0 to FunctionCall.Params.Count - 1 do begin 582 Params[I] := ExecuteExpression(Block, TExpression(FunctionCall.Params[0])); 583 end; 584 Result := ExecutorFunction.Callback(Params); 585 for I := 0 to FunctionCall.Params.Count - 1 do begin 586 Params[I].Free; 637 if FunctionCall.FunctionDef.InternalName <> '' then begin 638 SetLength(Params, FunctionCall.Params.Count); 639 for I := 0 to FunctionCall.Params.Count - 1 do begin 640 Params[I] := ExecuteExpression(Block, TExpression(FunctionCall.Params[0])); 641 end; 642 Result := ExecutorFunction.Callback(Params); 643 for I := 0 to FunctionCall.Params.Count - 1 do begin 644 Params[I].Free; 645 end; 646 end else begin 647 InitExecutorBlock(ExecutorFunction.Block, FunctionCall.FunctionDef.Block); 648 for I := 0 to FunctionCall.Params.Count - 1 do begin 649 Variable := FunctionCall.FunctionDef.Block.Variables.SearchByName(TFunctionParameter(FunctionCall.FunctionDef.Params[I]).Name); 650 ExecutorVariable := ExecutorFunction.Block.Variables.SearchByVariable(Variable); 651 ExecutorVariable.Value.Free; 652 ExecutorVariable.Value := ExecuteExpression(Block, TExpression(FunctionCall.Params[I])); 653 end; 654 ExecuteBlock(Block, FunctionCall.FunctionDef.Block, ExecutorFunction.Block); 655 ExecutorVariable := ExecutorFunction.Block.Variables.SearchByVariable(TVariable(FunctionCall.FunctionDef.Block.Variables.SearchByName('Result'))); 656 Result := ExecutorVariable.Value; 587 657 end; 588 658 end else raise Exception.Create('No executor for ' + FunctionCall.FunctionDef.Name + ' function.');
Note:
See TracChangeset
for help on using the changeset viewer.